From de7d31799b420caf16b23fc9dd87efb088c09049 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Sat, 29 May 2021 13:42:26 +0200 Subject: Starting a fairly extensive rework of the parser. --- src/core/src/tonkadur/fate/v1/Utils.java | 38 - .../tonkadur/fate/v1/lang/computation/Access.java | 2 +- .../v1/lang/computation/AccessAsReference.java | 152 - .../fate/v1/lang/computation/AccessPointer.java | 14 +- .../fate/v1/lang/computation/AtReference.java | 2 +- .../fate/v1/lang/computation/FieldAccess.java | 2 +- .../fate/v1/lang/computation/FieldReference.java | 152 - .../fate/v1/lang/computation/LambdaEvaluation.java | 19 +- .../src/tonkadur/fate/v1/lang/meta/Reference.java | 48 - .../tonkadur/fate/v1/lang/type/PointerType.java | 2 +- src/core/src/tonkadur/fate/v1/lang/type/Type.java | 8 +- src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 | 513 +- src/core/src/tonkadur/fate/v1/parser/FateParser.g4 | 6758 +++++--------------- src/core/src/tonkadur/fate/v1/parser/Parser.java | 229 + .../v1/compiler/fate/v1/ComputationCompiler.java | 31 - 15 files changed, 2090 insertions(+), 5880 deletions(-) delete mode 100644 src/core/src/tonkadur/fate/v1/lang/computation/AccessAsReference.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/computation/FieldReference.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/meta/Reference.java create mode 100644 src/core/src/tonkadur/fate/v1/parser/Parser.java diff --git a/src/core/src/tonkadur/fate/v1/Utils.java b/src/core/src/tonkadur/fate/v1/Utils.java index 9c7466d..4ab56e4 100644 --- a/src/core/src/tonkadur/fate/v1/Utils.java +++ b/src/core/src/tonkadur/fate/v1/Utils.java @@ -20,42 +20,4 @@ public class Utils { /* Utility class. */ private Utils () {} - - public static void add_file_content - ( - final String filename, - final Context context, - final World world - ) - throws IOException - { - add_file_content(filename, context, null, world); - } - - public static void add_file_content - ( - final String filename, - final Context context, - final Deque> local_variables, - final World world - ) - throws IOException - { - final CommonTokenStream tokens; - final FateLexer lexer; - final FateParser parser; - - lexer = new FateLexer(CharStreams.fromFileName(filename)); - tokens = new CommonTokenStream(lexer); - parser = new FateParser(tokens); - - parser.fate_file(context, local_variables, world); - - world.add_loaded_file(filename); - - if (parser.getNumberOfSyntaxErrors() > 0) - { - throw new IOException("There were syntaxic errors in " + filename); - } - } } diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/Access.java b/src/core/src/tonkadur/fate/v1/lang/computation/Access.java index f914b1c..843874c 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/Access.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/Access.java @@ -58,7 +58,7 @@ public class Access extends Computation current_type = parent.get_type(); - while (current_type.get_act_as_type().equals(Type.REF)) + while (current_type.get_act_as_type().equals(Type.PTR)) { parent = AtReference.build(origin, parent); current_type = parent.get_type(); diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/AccessAsReference.java b/src/core/src/tonkadur/fate/v1/lang/computation/AccessAsReference.java deleted file mode 100644 index 1fe94a0..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/computation/AccessAsReference.java +++ /dev/null @@ -1,152 +0,0 @@ -package tonkadur.fate.v1.lang.computation; - -import java.util.Collections; -import java.util.List; - -import tonkadur.parser.Origin; - -import tonkadur.error.ErrorManager; - -import tonkadur.fate.v1.error.InvalidTypeException; - -import tonkadur.fate.v1.lang.meta.ComputationVisitor; -import tonkadur.fate.v1.lang.meta.Computation; -import tonkadur.fate.v1.lang.meta.Reference; - -import tonkadur.fate.v1.lang.type.CollectionType; -import tonkadur.fate.v1.lang.type.Type; -import tonkadur.fate.v1.lang.type.PointerType; - -public class AccessAsReference extends Reference -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final Reference parent; - protected final Computation index; - - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - protected AccessAsReference - ( - final Origin origin, - final Reference parent, - final Type type, - final Computation index - ) - { - super - ( - origin, - type, - (parent.get_name() + "." + index.toString()) - ); - - this.parent = parent; - this.index = index; - } - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - public static AccessAsReference build - ( - final Origin origin, - Reference parent, - final Computation index - ) - throws InvalidTypeException - { - Type current_type; - - current_type = parent.get_type(); - - while (current_type.get_act_as_type().equals(Type.REF)) - { - parent = AtReference.build(origin, parent); - current_type = parent.get_type(); - } - - if (!index.get_type().can_be_used_as(Type.INT)) - { - ErrorManager.handle - ( - new InvalidTypeException - ( - index.get_origin(), - current_type, - Collections.singleton(Type.INT), - parent.get_name() - ) - ); - } - - if (!(current_type instanceof CollectionType)) - { - ErrorManager.handle - ( - new InvalidTypeException - ( - origin, - current_type, - Collections.singleton(Type.LIST), - parent.get_name() - ) - ); - - current_type = Type.ANY; - } - else - { - current_type = ((CollectionType) current_type).get_content_type(); - } - - return - new AccessAsReference - ( - origin, - parent, - current_type, - index - ); - } - - - /**** AccessAsReferenceors ************************************************************/ - @Override - public void get_visited_by (final ComputationVisitor cv) - throws Throwable - { - cv.visit_access_as_reference(this); - } - - public Computation get_index () - { - return index; - } - - public Reference get_parent () - { - return parent; - } - - /**** Misc. ****************************************************************/ - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append("(AccessAsReference ("); - sb.append(type.get_name()); - sb.append(") "); - sb.append(name); - sb.append("."); - sb.append(index.toString()); - sb.append(")"); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/AccessPointer.java b/src/core/src/tonkadur/fate/v1/lang/computation/AccessPointer.java index 22eabd6..fbc2ab7 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/AccessPointer.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/AccessPointer.java @@ -11,7 +11,7 @@ import tonkadur.fate.v1.error.InvalidTypeException; import tonkadur.fate.v1.lang.meta.ComputationVisitor; import tonkadur.fate.v1.lang.meta.Computation; -import tonkadur.fate.v1.lang.meta.Reference; +import tonkadur.fate.v1.lang.meta.Computation; import tonkadur.fate.v1.lang.type.CollectionType; import tonkadur.fate.v1.lang.type.PointerType; @@ -22,7 +22,7 @@ public class AccessPointer extends Computation /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final Reference parent; + protected final Computation parent; protected final Computation index; /***************************************************************************/ @@ -32,7 +32,7 @@ public class AccessPointer extends Computation protected AccessPointer ( final Origin origin, - final Reference parent, + final Computation parent, final Type type, final Computation index ) @@ -54,7 +54,7 @@ public class AccessPointer extends Computation public static AccessPointer build ( final Origin origin, - Reference parent, + Computation parent, final Computation index ) throws InvalidTypeException @@ -63,9 +63,9 @@ public class AccessPointer extends Computation current_type = parent.get_type(); - while (current_type.get_act_as_type().equals(Type.REF)) + while (current_type.get_act_as_type().equals(Type.PTR)) { - parent = AtReference.build(origin, parent); + parent = AtComputation.build(origin, parent); current_type = parent.get_type(); } @@ -120,7 +120,7 @@ public class AccessPointer extends Computation return index; } - public Reference get_parent () + public Computation get_parent () { return parent; } diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/AtReference.java b/src/core/src/tonkadur/fate/v1/lang/computation/AtReference.java index 7e68f11..7ae1e1f 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/AtReference.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/AtReference.java @@ -65,7 +65,7 @@ public class AtReference extends Reference ( origin, current_type, - Collections.singleton(Type.REF) + Collections.singleton(Type.PTR) ) ); diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java b/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java index 3c4279d..56708ff 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java @@ -61,7 +61,7 @@ public class FieldAccess extends Computation current_type = parent.get_type(); - while (current_type.get_act_as_type().equals(Type.REF)) + while (current_type.get_act_as_type().equals(Type.PTR)) { parent = AtReference.build(origin, (Reference) parent); current_type = parent.get_type(); diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/FieldReference.java b/src/core/src/tonkadur/fate/v1/lang/computation/FieldReference.java deleted file mode 100644 index 1c9a592..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/computation/FieldReference.java +++ /dev/null @@ -1,152 +0,0 @@ -package tonkadur.fate.v1.lang.computation; - -import java.util.Collections; -import java.util.List; - -import tonkadur.parser.Origin; - -import tonkadur.error.ErrorManager; - -import tonkadur.fate.v1.error.InvalidTypeException; -import tonkadur.fate.v1.error.UnknownStructureFieldException; - -import tonkadur.fate.v1.lang.meta.ComputationVisitor; -import tonkadur.fate.v1.lang.meta.Reference; -import tonkadur.fate.v1.lang.meta.Computation; - -import tonkadur.fate.v1.lang.type.StructType; -import tonkadur.fate.v1.lang.type.Type; - -public class FieldReference extends Reference -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final Computation parent; - protected final String field_name; - - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - protected FieldReference - ( - final Origin origin, - final Computation parent, - final Type type, - final String field_name - ) - { - super(origin, type, (parent.toString() + "." + field_name)); - - this.parent = parent; - this.field_name = field_name; - } - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - public static FieldReference build - ( - final Origin origin, - Computation parent, - final String field - ) - throws - InvalidTypeException, - UnknownStructureFieldException - { - Type current_type; - - current_type = parent.get_type(); - - while (current_type.get_act_as_type().equals(Type.REF)) - { - parent = AtReference.build(origin, (Reference) parent); - current_type = parent.get_type(); - } - - if (!(current_type instanceof StructType)) - { - ErrorManager.handle - ( - new InvalidTypeException - ( - origin, - current_type, - Collections.singleton(Type.DICT), - parent.toString() - ) - ); - - current_type = Type.ANY; - } - else - { - current_type = ((StructType) current_type).get_field_type(origin, field); - } - - return new FieldReference(origin, parent, current_type, field); - } - - public static FieldReference build - ( - final Origin origin, - Computation parent, - final List field_sequence - ) - throws - InvalidTypeException, - UnknownStructureFieldException - { - for (final String field: field_sequence) - { - parent = build(origin, parent, field); - } - - if (parent instanceof FieldReference) - { - return (FieldReference) parent; - } - else - { - return null; - } - } - - /**** Accessors ************************************************************/ - @Override - public void get_visited_by (final ComputationVisitor cv) - throws Throwable - { - cv.visit_field_reference(this); - } - - public String get_field_name () - { - return field_name; - } - - public Computation get_parent () - { - return parent; - } - - /**** Misc. ****************************************************************/ - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append("(FieldReference ("); - sb.append(type.get_name()); - sb.append(") "); - sb.append(parent.toString()); - sb.append(" "); - sb.append(field_name); - sb.append(")"); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java b/src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java index e29ac6b..fc66a49 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java @@ -10,7 +10,6 @@ import tonkadur.fate.v1.lang.type.LambdaType; import tonkadur.fate.v1.lang.meta.ComputationVisitor; import tonkadur.fate.v1.lang.meta.Computation; -import tonkadur.fate.v1.lang.meta.Reference; import tonkadur.fate.v1.lang.meta.RecurrentChecks; public class LambdaEvaluation extends Computation @@ -18,7 +17,7 @@ public class LambdaEvaluation extends Computation /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final Reference lambda_function; + protected final Computation lambda_function; protected final List parameters; /***************************************************************************/ @@ -28,7 +27,7 @@ public class LambdaEvaluation extends Computation protected LambdaEvaluation ( final Origin origin, - final Reference lambda_function, + final Computation lambda_function, final List parameters, final Type act_as ) @@ -46,20 +45,24 @@ public class LambdaEvaluation extends Computation public static LambdaEvaluation build ( final Origin origin, - final Reference reference, + final Computation lambda_function, final List parameters ) throws ParsingError { - RecurrentChecks.assert_lambda_matches_computations(reference, parameters); + RecurrentChecks.assert_lambda_matches_computations + ( + lambda_function, + parameters + ); return new LambdaEvaluation ( origin, - reference, + lambda_function, parameters, - (((LambdaType) reference.get_type()).get_return_type()) + (((LambdaType) lambda_function.get_type()).get_return_type()) ); } @@ -71,7 +74,7 @@ public class LambdaEvaluation extends Computation cv.visit_lambda_evaluation(this); } - public Reference get_lambda_function_reference () + public Computation get_lambda_function () { return lambda_function; } diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/Reference.java b/src/core/src/tonkadur/fate/v1/lang/meta/Reference.java deleted file mode 100644 index 52f9057..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/meta/Reference.java +++ /dev/null @@ -1,48 +0,0 @@ -package tonkadur.fate.v1.lang.meta; - -import tonkadur.parser.Origin; - -import tonkadur.fate.v1.lang.type.Type; - -public abstract class Reference extends Computation -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final String name; - - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - protected Reference (final Origin origin, final Type type, final String name) - { - super(origin, type); - - this.name = name; - } - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - /**** Accessors ************************************************************/ - public String get_name () - { - return name; - } - - /**** Misc. ****************************************************************/ - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append("("); - sb.append(type.get_name()); - sb.append(" Reference "); - sb.append(name); - sb.append(")"); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java b/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java index 57e3450..0269cf7 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java @@ -77,7 +77,7 @@ public class PointerType extends Type @Override public Type get_act_as_type () { - return Type.REF; + return Type.PTR; } /**** Misc. ****************************************************************/ diff --git a/src/core/src/tonkadur/fate/v1/lang/type/Type.java b/src/core/src/tonkadur/fate/v1/lang/type/Type.java index 850afeb..4385ed0 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/Type.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/Type.java @@ -27,7 +27,7 @@ public class Type extends DeclaredEntity public static final Type INT; public static final Type LAMBDA; public static final Type LIST; - public static final Type REF; + public static final Type PTR; public static final Type TEXT; public static final Type SEQUENCE; public static final Type SET; @@ -56,7 +56,7 @@ public class Type extends DeclaredEntity INT = new Type(base, null, "int"); LAMBDA = new Type(base, null, "lambda"); LIST = new Type(base, null, "list"); - REF = new Type(base, null, "ref"); + PTR = new Type(base, null, "ptr"); TEXT = new Type(base, null, "text"); SEQUENCE = new Type(base, null, "sequence"); SET = new Type(base, null, "set"); @@ -71,7 +71,7 @@ public class Type extends DeclaredEntity ALL_TYPES.add(INT); ALL_TYPES.add(LAMBDA); ALL_TYPES.add(LIST); - ALL_TYPES.add(REF); + ALL_TYPES.add(PTR); ALL_TYPES.add(TEXT); ALL_TYPES.add(SEQUENCE); ALL_TYPES.add(SET); @@ -90,7 +90,7 @@ public class Type extends DeclaredEntity COMPARABLE_TYPES.add(LAMBDA); COMPARABLE_TYPES.add(STRING); COMPARABLE_TYPES.add(BOOL); - COMPARABLE_TYPES.add(REF); + COMPARABLE_TYPES.add(PTR); COLLECTION_TYPES = new HashSet(); diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index 9d822a7..92ca943 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -8,6 +8,10 @@ lexer grammar FateLexer; fragment SEP: [ \t\r\n]+; fragment US: '_'?; +fragment DICT_NS: 'dict:'; +fragment LIST_NS: 'list:'; +fragment SET_NS: 'set:'; +fragment STRUCT_NS: 'struct:'; WS: SEP; @@ -15,198 +19,359 @@ L_PAREN: '('; R_PAREN: ')'; -ABS_KW: L_PAREN 'abs'('olute'?) SEP+; -DICT_KW: L_PAREN 'dict'('ionary'?) SEP+; -ACCESS_KW: L_PAREN 'access' SEP+; -ACCESS_POINTER_KW: L_PAREN 'access'US('ptr'|'pointer') SEP+; -ADD_KW: L_PAREN 'add'(US'element')? SEP+; -IMP_ADD_KW: L_PAREN 'add'(US'element')?'!' SEP+; -ADD_AT_KW: L_PAREN 'add'(US'element')?US'at' SEP+; -IMP_ADD_AT_KW: L_PAREN 'add'(US'element')?US'at!' SEP+; -ADD_ALL_KW: L_PAREN 'add'US'all'(US'elements')? SEP+; -IMP_ADD_ALL_KW: L_PAREN 'add'US'all'(US'elements')?'!' SEP+; -AND_KW: L_PAREN ('and'|'/\\') SEP+; -IMP_ASSERT_KW: L_PAREN 'assert!' SEP+; -AT_KW: L_PAREN 'at' SEP+; -IMP_BREAK_KW: L_PAREN 'break'('!'?) SEP* R_PAREN; -CAR_KW: L_PAREN 'car' SEP+; -CAST_KW: L_PAREN 'cast' SEP+; -CDR_KW: L_PAREN 'cdr' SEP+; -IMP_CLEAR_KW: L_PAREN 'clear!' SEP+; -COND_KW: L_PAREN 'cond' SEP+; -CONS_KW: L_PAREN 'cons' SEP+; -COUNT_KW: L_PAREN 'count' SEP+; + + +FATE_VERSION_KW: L_PAREN 'fate'US'version' SEP+; + + + + DECLARE_ALIAS_TYPE_KW: - L_PAREN ((('declare'|'define'|'def')US(('sub'|'alias')US)?'type')|'typedef') SEP+; -DECLARE_STRUCT_TYPE_KW: L_PAREN - ('declare'|'define'|'def')US('struct''ure'?)(US'type')? SEP+; -DECLARE_EXTRA_INSTRUCTION_KW: L_PAREN ('declare'|'define'|'def')US'extra'US'instruction' SEP+; -DECLARE_EXTRA_COMPUTATION_KW: L_PAREN ('declare'|'define'|'def')US'extra'US'computation' SEP+; -DECLARE_EXTRA_TYPE_KW: L_PAREN ('declare'|'define'|'def')US'extra'US'type' SEP+; -DECLARE_EVENT_TYPE_KW: L_PAREN ('declare'|'define'|'def')(US'input')?US'event'(US'type')? SEP+; -DECLARE_TEXT_EFFECT_KW: L_PAREN ('declare'|'define'|'def')US'text'US'effect' SEP+; -DECLARE_VARIABLE_KW: L_PAREN 'global' SEP+; -LOCAL_KW: L_PAREN 'local' SEP+; -EXTERNAL_KW: L_PAREN 'extern'('al'?) SEP+; -DEFAULT_KW: L_PAREN 'default' SEP+; -DEFINE_SEQUENCE_KW: L_PAREN ('declare'|'define'|'def')US(('seq'('uence')?)|('proc'('edure'?))) SEP+; -DIVIDE_KW: L_PAREN ('divide'|'/'|'div') SEP+; -DICT_TO_LIST_KW: - L_PAREN - (('dict'('ionary'?)US'to'US'list')|('list'US'from'US'dict'('ionary'?))) - SEP+; -DICT_FROM_LIST_KW: L_PAREN - (('dict'('ionary'?)US'from'US'list')|('list'US'to'US'dict'('ionary'?))) + ((('declare'|('def''ine'?))US(('sub'|'alias')US)?'type')|'typedef') SEP+; -DICT_KEYS_KW: L_PAREN (('get'US)?'dict'('ionary'?)US'keys') SEP+; -DICT_VALUES_KW: L_PAREN (('get'US)?'dict'('ionary'?)US'values') SEP+; -DICT_MERGE_KW: L_PAREN ('dict'('ionary'?)US'merge') SEP+; -DICT_MAP_KW: L_PAREN ('dict'('ionary'?)US'map') SEP+; -DICT_FILTER_KW: L_PAREN ('dict'('ionary'?)US'filter') SEP+; -DICT_SET_KW: L_PAREN ('dict'('ionary'?)US'set') SEP+; -DICT_REMOVE_KW: L_PAREN ('dict'('ionary'?)US'remove') SEP+; -DICT_HAS_KW: L_PAREN ('dict'('ionary'?)US'has') SEP+; -DICT_GET_KW: L_PAREN ('dict'('ionary'?)US'get') SEP+; -DICT_GET_POINTER_KW: L_PAREN ('dict'('ionary'?)US'get'US('ptr'|'pointer')) SEP+; +DECLARE_STRUCT_TYPE_KW: + L_PAREN ('declare'|('def''ine'?))US('struct''ure'?)(US'type')? SEP+; +DECLARE_EXTRA_INSTRUCTION_KW: + L_PAREN ('declare'|('def''ine'?))US'extra'US'instruction' SEP+; +DECLARE_EXTRA_COMPUTATION_KW: + L_PAREN ('declare'|('def''ine'?))US'extra'US'computation' SEP+; +DECLARE_EXTRA_TYPE_KW: + L_PAREN ('declare'|('def''ine'?))US'extra'US'type' SEP+; +DECLARE_EVENT_TYPE_KW: + L_PAREN ('declare'|('def''ine'?))(US'input')?US'event'(US'type')? SEP+; +DECLARE_TEXT_EFFECT_KW: + L_PAREN ('declare'|('def''ine'?))US'text'US'effect' SEP+; +DECLARE_GLOBAL_VARIABLE_KW: L_PAREN 'global' SEP+; +DECLARE_EXTERNAL_VARIABLE_KW: L_PAREN 'extern'('al'?) SEP+; -IMP_DICT_TO_LIST_KW: - L_PAREN - (('dict'('ionary'?)US'to'US'list')|('list'US'from'US'dict'('ionary'?)))'!' - SEP+; -IMP_DICT_FROM_LIST_KW: +DEFINE_SEQUENCE_KW: L_PAREN - (('dict'('ionary'?)US'from'US'list')|('list'US'to'US'dict'('ionary'?)))'!' + ('declare'|('def''ine'?))US(('seq'('uence'?))|('proc'('edure'?))) SEP+; -IMP_DICT_KEYS_KW: L_PAREN (('get'US)?'dict'('ionary'?)US'keys')'!' SEP+; -IMP_DICT_VALUES_KW: L_PAREN (('get'US)?'dict'('ionary'?)US'values')'!' SEP+; -IMP_DICT_MERGE_KW: L_PAREN ('dict'('ionary'?)US'merge')'!' SEP+; -IMP_DICT_MAP_KW: L_PAREN ('dict'('ionary'?)US'map')'!' SEP+; -IMP_DICT_FILTER_KW: L_PAREN ('dict'('ionary'?)US'filter')'!' SEP+; -IMP_DICT_SET_KW: L_PAREN ('dict'('ionary'?)US'set')'!' SEP+; -IMP_DICT_REMOVE_KW: L_PAREN ('dict'('ionary'?)US'remove')'!' SEP+; -IMP_DICT_HAS_KW: L_PAREN ('dict'('ionary'?)US'has')'!' SEP+; -IMP_DICT_GET_KW: L_PAREN ('dict'('ionary'?)US'get')'!' SEP+; -IMP_DICT_GET_POINTER_KW: - L_PAREN ('dict'('ionary'?)US'get'US('ptr'|'pointer'))'!' SEP+; -DO_WHILE_KW: L_PAREN ('do'US'while') SEP+; -ENABLE_TEXT_EFFECT_KW: L_PAREN 'text'US'effect' SEP+; -END_KW: L_PAREN 'end'('!'?) SEP* R_PAREN; -EQUALS_KW: L_PAREN ('equals'|'='|'=='|'eq') SEP+; -EXTENSION_FIRST_LEVEL_KW: L_PAREN '@'; -EXTRA_INSTRUCTION_KW: L_PAREN '#'; -JOIN_KW: L_PAREN ('text'US)? 'join' SEP+; -EXTRA_COMPUTATION_KW: L_PAREN '$'; -FALSE_KW: L_PAREN 'false' SEP* R_PAREN; -IGNORE_ERROR_KW: L_PAREN 'ignore'US('error'|'warning') SEP+; + IMP_IGNORE_ERROR_KW: L_PAREN 'ignore'US('error'|'warning')'!' SEP+; -FATE_VERSION_KW: L_PAREN 'fate'US'version' SEP+; -FIELD_KW: L_PAREN 'field' SEP+; -FIELD_ACCESS_KW: L_PAREN (('get'US'field')|('field'US'access')) SEP+; -FILTER_KW: L_PAREN 'filter' SEP+; -INDEXED_FILTER_KW: L_PAREN 'indexed'US'filter' SEP+; -IMP_FILTER_KW: L_PAREN 'filter!' SEP+; -IMP_INDEXED_FILTER_KW: L_PAREN 'indexed'US'filter!' SEP+; -FOR_EACH_KW: L_PAREN ('for'US'each') SEP+; -FOR_KW: L_PAREN 'for' SEP+; -FOLDR_KW: L_PAREN 'foldr' SEP+; -FOLDL_KW: L_PAREN 'foldl' SEP+; -FREE_KW: L_PAREN ('free!'|'release!'|'destroy!') SEP+; -GREATER_EQUAL_THAN_KW: L_PAREN ('greater'US'equal'US'than'|'>='|'ge') SEP+; -GREATER_THAN_KW: L_PAREN ('greater'US'than'|'>'|'gt') SEP+; -IF_ELSE_KW: L_PAREN ('if'US'else') SEP+; -IF_KW: L_PAREN 'if' SEP+; -IMPLIES_KW: L_PAREN ('implies'|'=>'|'->') SEP+; +REQUIRE_EXTENSION_KW: L_PAREN 'require'US'extension' SEP+; +REQUIRE_KW: L_PAREN 'require' SEP+; INCLUDE_KW: L_PAREN 'include' SEP+; -INDEX_OF_KW: L_PAREN ('index'US'of') SEP+; -INDEXED_MAP_KW: L_PAREN 'indexed'US'map' SEP+; -IMP_INDEXED_MAP_KW: L_PAREN 'indexed'US'map!' SEP+; -IS_MEMBER_KW: L_PAREN ('is'US'member'|'contains'|'has') SEP+; -IS_EMPTY_KW: L_PAREN 'is'US'empty' SEP+; -LOWER_EQUAL_THAN_KW: L_PAREN ('lower'US'equal'US'than'|'=<'|'<='|'le') SEP+; -LOWER_THAN_KW: L_PAREN ('lower'US'than'|'<'|'lt') SEP+; -LET_KW: L_PAREN 'let' SEP+; + + + + +DECLARE_LOCAL_VARIABLE_KW: L_PAREN 'local' SEP+; + + + + +ABS_KW: L_PAREN 'abs'('olute'?) SEP+; +CLAMP_KW: L_PAREN ('clamp') SEP+; +DIVIDE_KW: L_PAREN (('div''ide'?)|'/') SEP+; +MAX_KW: L_PAREN ('max'('imum'?)) SEP+; MINUS_KW: L_PAREN ('minus'|'-') SEP+; MIN_KW: L_PAREN ('min'('imum'?)) SEP+; -MAP_KW: L_PAREN 'map' SEP+; -STRING_KW: L_PAREN 'string' SEP+; -IMP_MAP_KW: L_PAREN 'map!' SEP+; -MAX_KW: L_PAREN ('max'('imum'?)) SEP+; -LAMBDA_KW: L_PAREN 'lambda' SEP+; -EVAL_KW: L_PAREN 'eval'('uate'?) SEP+; -CLAMP_KW: L_PAREN ('clamp') SEP+; -MODULO_KW: L_PAREN ('modulo'|'%'|'mod') SEP+; -MERGE_TO_LIST_KW : L_PAREN 'merge'US'to'US'list' SEP+; -MERGE_TO_SET_KW : L_PAREN 'merge'US'to'US'set' SEP+; -INDEXED_MERGE_TO_LIST_KW : L_PAREN 'indexed'US'merge'US'to'US'list' SEP+; -INDEXED_MERGE_TO_SET_KW : L_PAREN 'indexed'US'merge'US'to'US'set' SEP+; -IMP_MERGE_KW : L_PAREN 'merge!' SEP+; -IMP_INDEXED_MERGE_KW : L_PAREN 'indexed'US'merge!' SEP+; -SAFE_MERGE_TO_LIST_KW : L_PAREN 'safe'US'merge'US'to'US'list' SEP+; -SAFE_MERGE_TO_SET_KW : L_PAREN 'safe'US'merge'US'to'US'set' SEP+; -SAFE_INDEXED_MERGE_TO_LIST_KW : L_PAREN (('safe'US'indexed')|('indexed'US'safe'))US'merge'US'to'US'list' SEP+; -SAFE_INDEXED_MERGE_TO_SET_KW : L_PAREN (('safe'US'indexed')|('indexed'US'safe'))US'merge'US'to'US'set' SEP+; -SAFE_IMP_MERGE_KW : L_PAREN 'safe'US'merge!' SEP+; -SAFE_IMP_INDEXED_MERGE_KW : L_PAREN (('indexed'US'safe')|('safe'US'indexed'))US'merge!' SEP+; -NEWLINE_KW: L_PAREN 'newline' SEP* R_PAREN; -ALLOCATE_KW: L_PAREN (('alloc''ate'?)|'malloc'|'new')'!' SEP+; +MODULO_KW: L_PAREN (('mod''ulo'?)|'%') SEP+; +PLUS_KW: L_PAREN ('plus'|'+') SEP+; +POWER_KW: L_PAREN (('pow''er'?)|'^'|'**'|) SEP+; +RANDOM_KW: L_PAREN (('rand''om'?)|'rnd') SEP+; +TIMES_KW: L_PAREN ('times'|'*') SEP+; + + + + +DICT_KW: L_PAREN 'dict'('ionary'?) SEP+; +DICT_FILTER_KW: L_PAREN DICT_NS 'filter' SEP+; +DICT_FOLD_KW: L_PAREN DICT_NS 'fold' SEP+; +DICT_FROM_LIST_KW: + L_PAREN ((DICT_NS'from'US'list')|(LIST_NS'to'US'dict'('ionary'?))) SEP+; +DICT_GET_KW: L_PAREN DICT_NS 'get' SEP+; +DICT_HAS_KW: L_PAREN DICT_NS 'has' SEP+; +DICT_KEYS_KW: L_PAREN DICT_NS ('get'US)?'keys' SEP+; +DICT_MAP_KW: L_PAREN DICT_NS 'map' SEP+; +DICT_MERGE_KW: L_PAREN DICT_NS 'merge' SEP+; +DICT_REMOVE_KW: L_PAREN DICT_NS ('rm'|'remove'|'del'|'delete') SEP+; +DICT_SET_KW: L_PAREN DICT_NS 'set' SEP+; +DICT_SIZE_KW: L_PAREN DICT_NS 'size' SEP+; +DICT_TO_LIST_KW: + L_PAREN ((DICT_NS'to'US'list')|(LIST_NS'from'US'dict'('ionary'?))) SEP+; +DICT_VALUES_KW: L_PAREN DICT_NS ('get'US)?'values' SEP+; +IMP_DICT_FILTER_KW: L_PAREN DICT_NS 'filter''!' SEP+; +IMP_DICT_FOLD_KW: L_PAREN DICT_NS 'fold''!' SEP+; +IMP_DICT_FROM_LIST_KW: + L_PAREN ((DICT_NS'from'US'list')|(LIST_NS'to'US'dict'('ionary'?)))'!' SEP+; +IMP_DICT_GET_KW: L_PAREN DICT_NS 'get''!' SEP+; +IMP_DICT_HAS_KW: L_PAREN DICT_NS 'has''!' SEP+; +IMP_DICT_KEYS_KW: L_PAREN DICT_NS ('get'US)?'keys''!' SEP+; +IMP_DICT_MAP_KW: L_PAREN DICT_NS 'map''!' SEP+; +IMP_DICT_MERGE_KW: L_PAREN DICT_NS 'merge''!' SEP+; +IMP_DICT_REMOVE_KW: L_PAREN DICT_NS ('rm'|'remove'|'del'|'delete')'!' SEP+; +IMP_DICT_SET_KW: L_PAREN DICT_NS 'set''!' SEP+; +IMP_DICT_TO_LIST_KW: + L_PAREN ((DICT_NS'to'US'list')|(LIST_NS'from'US'dict'('ionary'?)))'!' SEP+; +IMP_DICT_VALUES_KW: L_PAREN DICT_NS ('get'US)?'values''!' SEP+; + + + + +LIST_KW: L_PAREN 'list' SEP+; +LIST_ADD_ALL_KW: L_PAREN LIST_NS 'add'US'all'(US'elements')? SEP+; +LIST_ADD_ALL_KW: L_PAREN LIST_NS 'add'US'all'(US'elements')? SEP+; +LIST_ADD_AT_KW: L_PAREN LIST_NS 'add'(US'element')?US'at' SEP+; +LIST_ADD_KW: L_PAREN LIST_NS 'add'(US'element')? SEP+; +LIST_COUNT_KW: L_PAREN LIST_NS 'count' SEP+; +LIST_FILTER_KW: L_PAREN LIST_NS 'filter' SEP+; +LIST_FOLDL_KW: L_PAREN LIST_NS 'foldl' SEP+; +LIST_FOLDR_KW: L_PAREN LIST_NS 'foldr' SEP+; +LIST_GET_KW: L_PAREN LIST_NS 'get' SEP+; +LIST_INDEXED_FILTER_KW: L_PAREN LIST_NS 'indexed'US'filter' SEP+; +LIST_INDEXED_MAP_KW: L_PAREN LIST_NS 'indexed'US'map' SEP+; +LIST_INDEXED_MERGE_KW: L_PAREN LIST_NS 'indexed'US'merge' SEP+; +LIST_INDEXED_PARTITION_KW: L_PAREN LIST_NS 'indexed'US'partition' SEP+; +LIST_INDEX_OF_KW: L_PAREN LIST_NS 'index'US'of' SEP+; +LIST_IS_EMPTY_KW: L_PAREN LIST_NS 'is'US'empty' SEP+; +LIST_IS_MEMBER_KW: L_PAREN LIST_NS (('is'US'member')|'contains'|'has') SEP+; +LIST_MAP_KW: L_PAREN LIST_NS 'map' SEP+; +LIST_MERGE_KW: L_PAREN LIST_NS 'merge' SEP+; +LIST_PARTITION_KW: L_PAREN LIST_NS 'partition' SEP+; +LIST_POP_LEFT_KW: L_PAREN LIST_NS 'pop'US'left' SEP+; +LIST_POP_RIGHT_KW: L_PAREN LIST_NS 'pop'US'right' SEP+; +LIST_PUSH_LEFT_KW: L_PAREN LIST_NS 'push'US'left' SEP+; +LIST_PUSH_RIGHT_KW: L_PAREN LIST_NS 'push'US'right' SEP+; +LIST_RANGE_KW: L_PAREN LIST_NS 'range' SEP+; +LIST_REMOVE_ALL_KW: L_PAREN LIST_NS 'remove'US'all' SEP+; +LIST_REMOVE_AT_KW: L_PAREN LIST_NS 'remove'US('element'?US)?'at' SEP+; +LIST_REMOVE_ONE_KW: L_PAREN LIST_NS 'remove'US'one' SEP+; +LIST_REVERSE_KW: L_PAREN LIST_NS 'reverse'(US'list')? SEP+; +LIST_SAFE_INDEXED_MERGE_KW: + L_PAREN LIST_NS (('safe'US'indexed')|('indexed'US'safe'))US'merge' SEP+; +LIST_SAFE_MERGE_KW: L_PAREN LIST_NS 'safe'US'merge' SEP+; +LIST_SET_KW: L_PAREN LIST_NS 'set' SEP+; +LIST_SHUFFLE_KW: L_PAREN LIST_NS 'shuffle' SEP+; +LIST_SIZE_KW: L_PAREN LIST_NS 'size' SEP+; +LIST_SORT_KW: L_PAREN LIST_NS 'sort' SEP+; +LIST_SUB_LIST_KW: L_PAREN LIST_NS 'sub'US'list' SEP+; +IMP_LIST_ADD_ALL_KW: L_PAREN LIST_NS 'add'US'all'(US'elements')?'!' SEP+; +IMP_LIST_ADD_AT_KW: L_PAREN LIST_NS 'add'(US'element')?US'at!' SEP+; +IMP_LIST_ADD_KW: L_PAREN LIST_NS 'add'(US'element')?'!' SEP+; +IMP_LIST_CLEAR_KW: L_PAREN LIST_NS 'clear!' SEP+; +IMP_LIST_FILTER_KW: L_PAREN LIST_NS 'filter!' SEP+; +IMP_LIST_INDEXED_FILTER_KW: L_PAREN LIST_NS 'indexed'US'filter!' SEP+; +IMP_LIST_INDEXED_MAP_KW: L_PAREN LIST_NS 'indexed'US'map!' SEP+; +IMP_LIST_INDEXED_MERGE_KW: L_PAREN LIST_NS 'indexed'US'merge!' SEP+; +IMP_LIST_INDEXED_PARTITION_KW: L_PAREN LIST_NS 'indexed'US'partition!' SEP+; +IMP_LIST_MAP_KW: L_PAREN LIST_NS 'map!' SEP+; +IMP_LIST_MERGE_KW: L_PAREN LIST_NS 'merge!' SEP+; +IMP_LIST_PARTITION_KW: L_PAREN LIST_NS 'partition!' SEP+; +IMP_LIST_POP_LEFT_KW: L_PAREN LIST_NS 'pop'US'left!' SEP+; +IMP_LIST_POP_RIGHT_KW: L_PAREN LIST_NS 'pop'US'right!' SEP+; +IMP_LIST_PUSH_LEFT_KW: L_PAREN LIST_NS 'push'US'left!' SEP+; +IMP_LIST_PUSH_RIGHT_KW: L_PAREN LIST_NS 'push'US'right!' SEP+; +IMP_LIST_REMOVE_ALL_KW: L_PAREN LIST_NS 'remove'US'all!' SEP+; +IMP_LIST_REMOVE_AT_KW: L_PAREN LIST_NS ('remove'US('elem''ent'?US)?'at!') SEP+; +IMP_LIST_REMOVE_ONE_KW: L_PAREN LIST_NS 'remove'US'one!' SEP+; +IMP_LIST_REVERSE_KW: L_PAREN LIST_NS 'reverse'(US'list')?'!' SEP+; +IMP_LIST_SAFE_INDEXED_MERGE_KW: + L_PAREN LIST_NS (('indexed'US'safe')|('safe'US'indexed'))US'merge!' SEP+; +IMP_LIST_SAFE_MERGE_KW: L_PAREN LIST_NS 'safe'US'merge!' SEP+; +IMP_LIST_SET_KW: L_PAREN LIST_NS 'set!' SEP+; +IMP_LIST_SHUFFLE_KW: L_PAREN LIST_NS 'shuffle!' SEP+; +IMP_LIST_SORT_KW: L_PAREN LIST_NS 'sort!' SEP+; +IMP_LIST_SUB_LIST_KW: L_PAREN LIST_NS 'sub'US'list!' SEP+; + + + + +SET_KW: L_PAREN 'set' SEP+; +SET_ADD_ALL_KW: L_PAREN SET_NS 'add'US'all'(US'elements')? SEP+; +SET_ADD_KW: L_PAREN SET_NS 'add'(US'element')? SEP+; +SET_COUNT_KW: L_PAREN SET_NS 'count' SEP+; +SET_FILTER_KW: L_PAREN SET_NS 'filter' SEP+; +SET_FOLDL_KW: L_PAREN SET_NS foldl' SEP+; +SET_FOLDR_KW: L_PAREN SET_NS 'foldr' SEP+; +SET_GET_KW: L_PAREN SET_NS 'get' SEP+; +SET_INDEXED_FILTER_KW: L_PAREN SET_NS 'indexed'US'filter' SEP+; +SET_INDEXED_MAP_KW: L_PAREN SET_NS 'indexed'US'map' SEP+; +SET_INDEXED_MERGE_KW: L_PAREN SET_NS 'indexed'US'merge' SEP+; +SET_INDEXED_PARTITION_KW: L_PAREN SET_NS 'indexed'US'partition' SEP+; +SET_INDEX_OF_KW: L_PAREN SET_NS 'index'US'of' SEP+; +SET_IS_EMPTY_KW: L_PAREN SET_NS 'is'US'empty' SEP+; +SET_IS_MEMBER_KW: L_PAREN SET_NS ('is'US'member'|'contains'|'has') SEP+; +SET_MAP_KW: L_PAREN SET_NS 'map' SEP+; +SET_MERGE_KW: L_PAREN SET_NS 'merge' SEP+; +SET_PARTITION_KW: L_PAREN SET_NS 'partition' SEP+; +SET_POP_LEFT_KW: L_PAREN SET_NS 'pop'US'left' SEP+; +SET_POP_RIGHT_KW: L_PAREN SET_NS 'pop'US'right' SEP+; +SET_RANGE_KW: L_PAREN SET_NS 'range' SEP+; +SET_REMOVE_AT_KW: L_PAREN SET_NS ('remove'US('elem'('ent')?US)?'at') SEP+; +SET_REMOVE_ONE_KW: L_PAREN SET_NS 'remove'US'one' SEP+; +SET_SAFE_INDEXED_MERGE_KW: + L_PAREN SET_NS (('safe'US'indexed')|('indexed'US'safe'))US'merge' SEP+; +SET_SAFE_MERGE_KW: L_PAREN SET_NS 'safe'US'merge' SEP+; +SET_SET_KW: L_PAREN SET_NS 'set' SEP+; +SET_SIZE_KW: L_PAREN SET_NS 'size' SEP+; +IMP_SET_ADD_ALL_KW: L_PAREN SET_NS 'add'US'all'(US'elements')?'!' SEP+; +IMP_SET_ADD_KW: L_PAREN SET_NS 'add'(US'element')?'!' SEP+; +IMP_SET_CLEAR_KW: L_PAREN SET_NS 'clear!' SEP+; +IMP_SET_FILTER_KW: L_PAREN SET_NS 'filter!' SEP+; +IMP_SET_INDEXED_FILTER_KW: L_PAREN SET_NS 'indexed'US'filter!' SEP+; +IMP_SET_INDEXED_MAP_KW: L_PAREN SET_NS 'indexed'US'map!' SEP+; +IMP_SET_INDEXED_MERGE_KW: L_PAREN SET_NS 'indexed'US'merge!' SEP+; +IMP_SET_INDEXED_PARTITION_KW: L_PAREN SET_NS 'indexed'US'partition!' SEP+; +IMP_SET_MAP_KW: L_PAREN SET_NS 'map!' SEP+; +IMP_SET_MERGE_KW: L_PAREN SET_NS 'merge!' SEP+; +IMP_SET_PARTITION_KW: L_PAREN SET_NS 'partition!' SEP+; +IMP_SET_POP_LEFT_KW: L_PAREN SET_NS 'pop'US'left!' SEP+; +IMP_SET_POP_RIGHT_KW: L_PAREN SET_NS 'pop'US'right!' SEP+; +IMP_SET_REMOVE_AT_KW: L_PAREN SET_NS ('remove'US('elem'('ent')?US)?'at!') SEP+; +IMP_SET_REMOVE_ONE_KW: L_PAREN SET_NS 'remove'US'one!' SEP+; +IMP_SET_SAFE_INDEXED_MERGE_KW: + L_PAREN SET_NS (('indexed'US'safe')|('safe'US'indexed'))US'merge!' SEP+; +IMP_SET_SAFE_MERGE_KW: L_PAREN SET_NS 'safe'US'merge!' SEP+; + + + + +AND_KW: L_PAREN ('and'|'/\\') SEP+; +FALSE_KW: L_PAREN 'false' SEP* R_PAREN; +IMPLIES_KW: L_PAREN ('implies'|'=>'|'->') SEP+; NOT_KW: L_PAREN ('not'|'~'|'!') SEP+; ONE_IN_KW: L_PAREN ('exactly'US)?'one'(US'in')? SEP+; OR_KW: L_PAREN ('or'|'\\/') SEP+; +TRUE_KW: L_PAREN 'true' SEP* R_PAREN; + + + + +ENABLE_TEXT_EFFECT_KW: L_PAREN 'text'US'effect' SEP+; +NEWLINE_KW: L_PAREN 'newline' SEP* R_PAREN; +TEXT_JOIN_KW: L_PAREN TEXT_NS 'join' SEP+; TEXT_KW: L_PAREN 'text' SEP+; -PARTITION_KW: L_PAREN 'partition' SEP+; -IMP_PARTITION_KW: L_PAREN 'partition!' SEP+; -INDEXED_PARTITION_KW: L_PAREN 'indexed'US'partition' SEP+; -IMP_INDEXED_PARTITION_KW: L_PAREN 'indexed'US'partition!' SEP+; -POP_LEFT_KW: L_PAREN 'pop'US'left' SEP+; -IMP_POP_LEFT_KW: L_PAREN 'pop'US'left!' SEP+; -POP_RIGHT_KW: L_PAREN 'pop'US'right' SEP+; -IMP_POP_RIGHT_KW: L_PAREN 'pop'US'right!' SEP+; -PUSH_LEFT_KW: L_PAREN 'push'US'left' SEP+; -IMP_PUSH_LEFT_KW: L_PAREN 'push'US'left!' SEP+; -PUSH_RIGHT_KW: L_PAREN 'push'US'right' SEP+; -IMP_PUSH_RIGHT_KW: L_PAREN 'push'US'right!' SEP+; -PLAYER_CHOICE_KW: L_PAREN ('choice'|'user'US'choice'|'player'US'choice')'!' SEP+; + + + + + + + +CONS_KW: L_PAREN 'cons' SEP+; +CAR_KW: L_PAREN 'car' SEP+; +CDR_KW: L_PAREN 'cdr' SEP+; + + + + +COND_KW: L_PAREN 'cond' SEP+; +DO_WHILE_KW: L_PAREN 'do'US'while' SEP+; +FOR_KW: L_PAREN 'for' SEP+; +FOR_EACH_KW: L_PAREN 'for'US'each' SEP+; +WHILE_KW: L_PAREN 'while' SEP+; +SWITCH_KW: L_PAREN 'switch' SEP+; +IMP_BREAK_KW: L_PAREN 'break'('!'?) SEP* R_PAREN; +IMP_CONTINUE_KW: L_PAREN 'continue'('!'?) SEP* R_PAREN; + + + + +IF_ELSE_KW: L_PAREN 'if'US'else' SEP+; +IF_KW: L_PAREN 'if' SEP+; + + + + +STRING_KW: L_PAREN 'string' SEP+; + + + + +DEFAULT_KW: L_PAREN 'default' SEP+; +CAST_KW: L_PAREN 'cast' SEP+; + + + + +EQUALS_KW: L_PAREN ('equals'|'='|'=='|'eq') SEP+; +GREATER_EQUAL_THAN_KW: L_PAREN (('greater'US'equal'US'than')|'>='|'ge') SEP+; +GREATER_THAN_KW: L_PAREN (('greater'US'than')|'>'|'gt') SEP+; +LOWER_EQUAL_THAN_KW: L_PAREN (('lower'US'equal'US'than')|'=<'|'<='|'le') SEP+; +LOWER_THAN_KW: L_PAREN (('lower'US'than')|'<'|'lt') SEP+; + + + + +IMP_ASSERT_KW: L_PAREN 'assert!' SEP+; +IGNORE_ERROR_KW: L_PAREN 'ignore'US('error'|'warning') SEP+; + + + + +EXTENSION_FIRST_LEVEL_KW: L_PAREN '@'; +EXTRA_INSTRUCTION_KW: L_PAREN '#'; +EXTRA_COMPUTATION_KW: L_PAREN '$'; + + + + +FIELD_KW: L_PAREN STRUCT_NS ('get'US)?'field' SEP+; +SET_FIELDS_KW: L_PAREN STRUCT_NS 'set'US'fields' SEP+; +IMP_SET_FIELDS_KW: L_PAREN STRUCT_NS 'set'US'fields!' SEP+; + + + + +PLAYER_CHOICE_KW: + L_PAREN ('choice'|'user'US'choice'|'player'US'choice')'!' SEP+; TEXT_OPTION_KW: L_PAREN ('option'|'user'US'option'|'player'US'option') SEP+; EVENT_OPTION_KW: L_PAREN ('event'|'user'US'event'|'player'US'event') SEP+; -PLUS_KW: L_PAREN ('plus'|'+') SEP+; -POWER_KW: L_PAREN ('power'|'^'|'**'|'pow') SEP+; -RANGE_KW: L_PAREN 'range' SEP+; -RANDOM_KW: L_PAREN ('random'|'rand'|'rnd') SEP+; -REF_KW: L_PAREN (((('ref'('erence'?))|'ptr'|'pointer')(US'to')?)|('addr'('ess'?)(US'of')?)) SEP+; -REMOVE_ALL_KW: L_PAREN 'remove'US'all' SEP+; -IMP_REMOVE_ALL_KW: L_PAREN 'remove'US'all!' SEP+; -REVERSE_KW: L_PAREN 'reverse'(US'list')? SEP+; -IMP_REVERSE_KW: L_PAREN 'reverse'(US'list')?'!' SEP+; -REMOVE_ONE_KW: L_PAREN 'remove'US'one' SEP+; -IMP_REMOVE_ONE_KW: L_PAREN 'remove'US'one!' SEP+; -REMOVE_AT_KW: L_PAREN ('remove'US('elem'('ent')?US)?'at') SEP+; -IMP_REMOVE_AT_KW: L_PAREN ('remove'US('elem'('ent')?US)?'at!') SEP+; -REQUIRE_EXTENSION_KW: L_PAREN 'require'US'extension' SEP+; -REQUIRE_KW: L_PAREN 'require' SEP+; -PROMPT_STRING_KW: L_PAREN 'prompt_string!' SEP+; -PROMPT_INTEGER_KW: L_PAREN 'prompt_int'('eger'?)'!' SEP+; -SHUFFLE_KW: L_PAREN 'shuffle' SEP+; -IMP_SHUFFLE_KW: L_PAREN 'shuffle!' SEP+; -SORT_KW: L_PAREN 'sort' SEP+; -IMP_SORT_KW: L_PAREN 'sort!' SEP+; -SET_FIELDS_KW: L_PAREN 'set'US'fields' SEP+; -IMP_SET_FIELDS_KW: L_PAREN 'set'US'fields!' SEP+; +PROMPT_STRING_KW: L_PAREN 'prompt'US'str''ing'?'!' SEP+; +PROMPT_INTEGER_KW: L_PAREN 'prompt'US'int''eger'?'!' SEP+; + + + + +LET_KW: L_PAREN 'let' SEP+; +AT_KW: L_PAREN 'at' SEP+; +REF_KW: + L_PAREN + ( + ((('ref''erence'?)|'ptr'|'pointer')(US'to')?) + |('addr''ess'?(US'of')?) + ) + SEP+; IMP_SET_KW: L_PAREN 'set'(US(('val''ue'?)|('var''iable'?)))?'!' SEP+; -SUB_LIST_KW: L_PAREN 'sub'US'list' SEP+; -IMP_SUB_LIST_KW: L_PAREN 'sub'US'list!' SEP+; -LIST_KW: L_PAREN 'list' SEP+; -SIZE_KW: L_PAREN 'size' SEP+; -SEQUENCE_KW: L_PAREN ('seq'|'sequence') SEP+; -SWITCH_KW: L_PAREN 'switch' SEP+; -TIMES_KW: L_PAREN ('times'|'*') SEP+; -TRUE_KW: L_PAREN 'true' SEP* R_PAREN; +VARIABLE_KW: L_PAREN 'var''iable'? SEP+; + + + + +ALLOCATE_KW: L_PAREN (('alloc''ate'?)|'malloc'|'new')'!' SEP+; +FREE_KW: L_PAREN ('free!'|'release!'|'destroy!') SEP+; + + + + +LAMBDA_KW: L_PAREN 'lambda' SEP+; +EVAL_KW: L_PAREN 'eval''uate'? SEP+; + + + + + + + + +SEQUENCE_KW: L_PAREN 'seq''uence'? SEP+; DONE_KW: L_PAREN 'done''!'? SEP* R_PAREN; -SET_KW: L_PAREN 'set' SEP+; -VARIABLE_KW: L_PAREN ('variable'|'var') SEP+; -VISIT_KW: L_PAREN ('call'|'visit')(US(('seq'('uence'?))|('proc'('edure'?))))?'!' SEP+; -CONTINUE_AS_KW: L_PAREN (('continue'US('as'|'to'|'with'))|('jump'(US'to')?)|('go'US'to')|'exec')(US(('seq'('uence'?))|('proc'('edure'?))))?'!' SEP+; -WHILE_KW: L_PAREN 'while' SEP+; +VISIT_KW: + L_PAREN ('call'|'visit')(US(('seq''uence'?)|('proc''edure'?)))?'!' SEP+; +CONTINUE_AS_KW: + L_PAREN + (('continue'US('as'|'to'|'with'))|('jump'(US'to')?)|('go'US'to')|'exec') + (US(('seq''uence'?)|('proc''edure'?)))? + '!' + SEP+; +END_KW: L_PAREN 'end'('!'?) SEP* R_PAREN; + + + WORD: ((~([ \t\r\n()]))|'(lp)'|'(rp)'|'(sp)')+ { diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index a467b2d..ff7ea40 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -1,8 +1,8 @@ -parser grammar FateParser; +parser grammar TinyFateParser; options { - tokenVocab = FateLexer; + tokenVocab = TinyFateLexer; } @header @@ -46,54 +46,29 @@ options @members { - Context CONTEXT; - World WORLD; - Deque> LOCAL_VARIABLES; - Deque> HIERARCHICAL_VARIABLES; - int BREAKABLE_LEVELS; - Deque> CHOICE_LIMITED_VARIABLES; + Parser PARSER; } /******************************************************************************/ /******************************************************************************/ /******************************************************************************/ -fate_file -[ - Context context, - Deque> local_variables, - World world -] +fate_file [Parser PARSER] @init { - CONTEXT = context; - WORLD = world; + PARSER = parser; - if (local_variables == null) - { - LOCAL_VARIABLES = new ArrayDeque>(); - LOCAL_VARIABLES.push(new HashMap()); - } - else - { - LOCAL_VARIABLES = local_variables; - } - - HIERARCHICAL_VARIABLES = new ArrayDeque>(); - BREAKABLE_LEVELS = 0; - - HIERARCHICAL_VARIABLES.push(new ArrayList()); - CHOICE_LIMITED_VARIABLES = new ArrayDeque>(); + PARSER.increase_local_variables_hierarchy(); } : WS* FATE_VERSION_KW WORD WS* R_PAREN WS* ( ( - first_level_fate_instr + first_level_instruction | ( - general_fate_instr + instruction { - WORLD.add_global_instruction(($general_fate_instr.result)); + PARSER.get_world().add_global_instruction(($instruction.result)); } ) ) @@ -104,7 +79,7 @@ fate_file } ; -general_fate_sequence +maybe_instruction_list returns [List result] @init { @@ -112,201 +87,28 @@ returns [List result] } : (WS* - general_fate_instr + instruction { - $result.add(($general_fate_instr.result)); + $result.add(($instruction.result)); } WS*)* { } ; -first_level_fate_instr: - DEFINE_SEQUENCE_KW - new_reference_name - WS* - ( - L_PAREN WS* variable_list WS* R_PAREN - { - final Map variable_map; - - variable_map = new HashMap(); - - variable_map.putAll(($variable_list.result).as_map()); - - LOCAL_VARIABLES.push(variable_map); - } - ) - pre_sequence_point=WS+ - general_fate_sequence - WS* - R_PAREN - { - final Origin start_origin, sequence_origin; - final Sequence new_sequence; - - start_origin = - CONTEXT.get_origin_at - ( - ($DEFINE_SEQUENCE_KW.getLine()), - ($DEFINE_SEQUENCE_KW.getCharPositionInLine()) - ); - - sequence_origin = - CONTEXT.get_origin_at - ( - ($pre_sequence_point.getLine()), - ($pre_sequence_point.getCharPositionInLine()) - ); - - new_sequence = - new Sequence - ( - start_origin, - new InstructionList - ( - sequence_origin, - ($general_fate_sequence.result) - ), - ($new_reference_name.result), - ($variable_list.result).get_entries() - ); - - WORLD.sequences().add(new_sequence); - LOCAL_VARIABLES.pop(); - } - - | DECLARE_VARIABLE_KW - type - WS+ - name=new_reference_name - WS* - R_PAREN - { - final Origin start_origin, type_origin; - final Variable new_variable; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_VARIABLE_KW.getLine()), - ($DECLARE_VARIABLE_KW.getCharPositionInLine()) - ); - - new_variable = - new Variable - ( - start_origin, - ($type.result), - ($name.result), - false - ); - - WORLD.variables().add(new_variable); - } - - | EXTERNAL_KW - type - WS+ - name=new_reference_name - WS* - R_PAREN - { - final Origin start_origin, type_origin; - final Variable new_variable; - - start_origin = - CONTEXT.get_origin_at - ( - ($EXTERNAL_KW.getLine()), - ($EXTERNAL_KW.getCharPositionInLine()) - ); - - new_variable = - new Variable - ( - start_origin, - ($type.result), - ($name.result), - true - ); - - WORLD.variables().add(new_variable); - } - - | IMP_IGNORE_ERROR_KW WORD WS+ first_level_fate_instr WS* R_PAREN - { - /* TODO: temporarily disable an compiler error category */ - } - - | DECLARE_TEXT_EFFECT_KW - new_reference_name - WS+ - params=type_list - WS* - R_PAREN - { - final Origin start_origin; - final TextEffect new_text_effect; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_TEXT_EFFECT_KW.getLine()), - ($DECLARE_TEXT_EFFECT_KW.getCharPositionInLine()) - ); - - new_text_effect = - new TextEffect - ( - start_origin, - ($type_list.result), - ($new_reference_name.result) - ); - - WORLD.text_effects().add(new_text_effect); - } - - | DECLARE_TEXT_EFFECT_KW - new_reference_name - WS* - R_PAREN - { - final Origin start_origin; - final TextEffect new_text_effect; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_TEXT_EFFECT_KW.getLine()), - ($DECLARE_TEXT_EFFECT_KW.getCharPositionInLine()) - ); - - new_text_effect = - new TextEffect - ( - start_origin, - new ArrayList(), - ($new_reference_name.result) - ); - - WORLD.text_effects().add(new_text_effect); - } - - | REQUIRE_EXTENSION_KW WORD WS* R_PAREN - { - WORLD.add_required_extension(($WORD.text)); - - /* TODO: error report if extension not explicitly enabled. */ - } - - | DECLARE_ALIAS_TYPE_KW parent=type WS+ new_reference_name WS* R_PAREN +first_level_instruction +@init +{ + Parser.LocalVariables previous_local_variables_stack; +} +: + DECLARE_ALIAS_TYPE_KW parent=type WS+ identifier WS* R_PAREN { final Origin start_origin; final Type new_type; start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($DECLARE_ALIAS_TYPE_KW.getLine()), ($DECLARE_ALIAS_TYPE_KW.getCharPositionInLine()) @@ -316,18 +118,13 @@ first_level_fate_instr: ($parent.result).generate_alias ( start_origin, - ($new_reference_name.result) + ($identifier.result) ); - WORLD.types().add(new_type); + PARSER.get_world().types().add(new_type); } - | DECLARE_STRUCT_TYPE_KW - new_reference_name - WS* - variable_list - WS* - R_PAREN + | DECLARE_STRUCT_TYPE_KW identifier WS* variable_list WS* R_PAREN { final Origin start_origin; final Type new_type; @@ -335,17 +132,13 @@ first_level_fate_instr: field_types = new HashMap(); - for - ( - final Variable te: - ($variable_list.result).get_entries() - ) + for (final Variable te: ($variable_list.result).get_entries()) { field_types.put(te.get_name(), te.get_type()); } start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($DECLARE_STRUCT_TYPE_KW.getLine()), ($DECLARE_STRUCT_TYPE_KW.getCharPositionInLine()) @@ -356,63 +149,19 @@ first_level_fate_instr: ( start_origin, field_types, - ($new_reference_name.result) - ); - - WORLD.types().add(new_type); - } - - | DECLARE_EXTRA_TYPE_KW new_reference_name WS* R_PAREN - { - final Origin start_origin; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_EXTRA_TYPE_KW.getLine()), - ($DECLARE_EXTRA_TYPE_KW.getCharPositionInLine()) - ); - - WORLD.types().add - ( - new ExtraType - ( - start_origin, - ($new_reference_name.result) - ) - ); - } - - | DECLARE_EXTRA_INSTRUCTION_KW new_reference_name WS* R_PAREN - { - final Origin start_origin; - final ExtraInstruction extra_instruction; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_EXTRA_INSTRUCTION_KW.getLine()), - ($DECLARE_EXTRA_INSTRUCTION_KW.getCharPositionInLine()) - ); - - extra_instruction = - new ExtraInstruction - ( - start_origin, - ($new_reference_name.result), - new ArrayList() + ($identifier.result) ); - WORLD.extra_instructions().add(extra_instruction); + PARSER.get_world().types().add(new_type); } - | DECLARE_EXTRA_INSTRUCTION_KW new_reference_name WS+ type_list WS* R_PAREN + | DECLARE_EXTRA_INSTRUCTION_KW identifier maybe_type_list WS* R_PAREN { final Origin start_origin; final ExtraInstruction extra_instruction; start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($DECLARE_EXTRA_INSTRUCTION_KW.getLine()), ($DECLARE_EXTRA_INSTRUCTION_KW.getCharPositionInLine()) @@ -422,20 +171,24 @@ first_level_fate_instr: new ExtraInstruction ( start_origin, - ($new_reference_name.result), + ($identifier.result), ($type_list.result) ); - WORLD.extra_instructions().add(extra_instruction); + PARSER.get_world().extra_instructions().add(extra_instruction); } - | DECLARE_EXTRA_COMPUTATION_KW type WS+ new_reference_name WS+ type_list WS* R_PAREN + | DECLARE_EXTRA_COMPUTATION_KW + type WS+ + identifier + maybe_type_list WS* + R_PAREN { final Origin start_origin; final ExtraComputation extra_computation; start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($DECLARE_EXTRA_COMPUTATION_KW.getLine()), ($DECLARE_EXTRA_COMPUTATION_KW.getCharPositionInLine()) @@ -446,44 +199,42 @@ first_level_fate_instr: ( start_origin, ($type.result), - ($new_reference_name.result), + ($identifier.result), ($type_list.result) ); - WORLD.extra_computations().add(extra_computation); + PARSER.get_world().extra_computations().add(extra_computation); } - | DECLARE_EXTRA_COMPUTATION_KW type WS+ new_reference_name WS* R_PAREN + | DECLARE_EXTRA_TYPE_KW identifier maybe_type_list WS* R_PAREN { final Origin start_origin; - final ExtraComputation extra_computation; start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($DECLARE_EXTRA_COMPUTATION_KW.getLine()), - ($DECLARE_EXTRA_COMPUTATION_KW.getCharPositionInLine()) + ($DECLARE_EXTRA_TYPE_KW.getLine()), + ($DECLARE_EXTRA_TYPE_KW.getCharPositionInLine()) ); - extra_computation = - new ExtraComputation + PARSER.get_world().types().add + ( + new ExtraType ( start_origin, - ($type.result), - ($new_reference_name.result), - new ArrayList() - ); - - WORLD.extra_computations().add(extra_computation); + ($identifier.result), + ($maybe_type_list.result) + ) + ); } - | DECLARE_EVENT_TYPE_KW new_reference_name WS* R_PAREN + | DECLARE_EVENT_TYPE_KW identifier maybe_type_list WS* R_PAREN { final Origin start_origin; final Event new_event; start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($DECLARE_EVENT_TYPE_KW.getLine()), ($DECLARE_EVENT_TYPE_KW.getCharPositionInLine()) @@ -493,96 +244,191 @@ first_level_fate_instr: new Event ( start_origin, - new ArrayList(), - ($new_reference_name.result) + ($type_list.result), + ($identifier.result) ); - WORLD.events().add(new_event); + PARSER.get_world().events().add(new_event); } - | DECLARE_EVENT_TYPE_KW new_reference_name WS+ type_list WS* R_PAREN + | DECLARE_TEXT_EFFECT_KW identifier maybe_type_list WS* R_PAREN { final Origin start_origin; - final Event new_event; + final TextEffect new_text_effect; start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($DECLARE_EVENT_TYPE_KW.getLine()), - ($DECLARE_EVENT_TYPE_KW.getCharPositionInLine()) + ($DECLARE_TEXT_EFFECT_KW.getLine()), + ($DECLARE_TEXT_EFFECT_KW.getCharPositionInLine()) ); - new_event = - new Event + new_text_effect = + new TextEffect ( start_origin, ($type_list.result), - ($new_reference_name.result) + ($identifier.result) ); - WORLD.events().add(new_event); + PARSER.get_world().text_effects().add(new_text_effect); } - - | REQUIRE_KW WORD WS* R_PAREN + | DECLARE_GLOBAL_VARIABLE_KW type WS+ name=identifier WS* R_PAREN { - final String filename; - - filename = Files.resolve_filename(CONTEXT, ($WORD.text)); + final Origin start_origin, type_origin; + final Variable new_variable; - if (!WORLD.has_loaded_file(filename)) - { - CONTEXT.push + start_origin = + PARSER.get_origin_at ( - CONTEXT.get_location_at - ( - ($REQUIRE_KW.getLine()), - ($REQUIRE_KW.getCharPositionInLine()) - ), - filename + ($DECLARE_GLOBAL_VARIABLE_KW.getLine()), + ($DECLARE_GLOBAl_VARIABLE_KW.getCharPositionInLine()) ); - tonkadur.fate.v1.Utils.add_file_content + new_variable = + new Variable ( - filename, - CONTEXT, - LOCAL_VARIABLES, - WORLD + start_origin, + ($type.result), + ($name.result), + false ); - CONTEXT.pop(); - } + PARSER.get_world().variables().add(new_variable); } - | INCLUDE_KW WORD WS* R_PAREN + | DECLARE_EXTERNAL_VARIABLE_KW type WS+ name=identifier WS* R_PAREN { - final String filename; - - filename = Files.resolve_filename(CONTEXT, ($WORD.text)); + final Origin start_origin, type_origin; + final Variable new_variable; - CONTEXT.push - ( - CONTEXT.get_location_at + start_origin = + PARSER.get_origin_at ( - ($INCLUDE_KW.getLine()), + ($DECLARE_EXTERNAL_VARIABLE_KW.getLine()), + ($DECLARE_EXTERNAL_VARIABLE_KW.getCharPositionInLine()) + ); + + new_variable = + new Variable + ( + start_origin, + ($type.result), + ($name.result), + true + ); + + PARSER.get_world().variables().add(new_variable); + } + + | DEFINE_SEQUENCE_KW + { + previous_local_variables_stack = PARSER.get_local_variables_stack(); + PARSER.discard_local_variables_stack(); + PARSER.increase_local_variables_herarchy(); + } + identifier + WS* + ( + L_PAREN WS* variable_list WS* R_PAREN + { + PARSER.add_local_variables(($variable_list.result).as_map()); + PARSER.increase_local_variables_herarchy(); + } + ) + pre_sequence_point=WS+ + maybe_instruction_list + WS* + R_PAREN + { + final Origin start_origin, sequence_origin; + final Sequence new_sequence; + + start_origin = + PARSER.get_origin_at + ( + ($DEFINE_SEQUENCE_KW.getLine()), + ($DEFINE_SEQUENCE_KW.getCharPositionInLine()) + ); + + sequence_origin = + PARSER.get_origin_at + ( + ($pre_sequence_point.getLine()), + ($pre_sequence_point.getCharPositionInLine()) + ); + + new_sequence = + new Sequence + ( + start_origin, + new InstructionList + ( + sequence_origin, + ($maybe_instruction_list.result) + ), + ($identifier.result), + ($variable_list.result).get_entries() + ); + + PARSER.get_world().sequences().add(new_sequence); + PARSER.restore_local_variables_stack(previous_local_variables_stack); + } + + + | IMP_IGNORE_ERROR_KW WORD WS+ first_level_instruction WS* R_PAREN + { + /* TODO: temporarily disable an compiler error category */ + } + + + | REQUIRE_EXTENSION_KW WORD WS* R_PAREN + { + WORLD.add_required_extension(($WORD.text)); + + /* TODO: error report if extension not explicitly enabled. */ + } + + | REQUIRE_KW WORD WS* R_PAREN + { + final String filename; + + filename = Files.resolve_filename(PARSER.get_context(), ($WORD.text)); + + if (!PARSER.get_world().has_loaded_file(filename)) + { + PARSER.add_file_content + ( + PARSER.get_location_at + ( + ($REQUIRE_KW.getLine()), + ($REQUIRE_KW.getCharPositionInLine()) + ), + filename + ); + } + } + + | INCLUDE_KW WORD WS* R_PAREN + { + final String filename; + + filename = Files.resolve_filename(PARSER.get_context(), ($WORD.text)); + + PARSER.add_file_content + ( + PARSER.get_location_at + ( + ($INCLUDE_KW.getLine()), ($INCLUDE_KW.getCharPositionInLine()) ), filename ); - - tonkadur.fate.v1.Utils.add_file_content - ( - filename, - CONTEXT, - LOCAL_VARIABLES, - WORLD - ); - - CONTEXT.pop(); } /* - | EXTENSION_FIRST_LEVEL_KW WORD WS+ general_fate_sequence WS* R_PAREN + | EXTENSION_FIRST_LEVEL_KW WORD WS+ instruction_list WS* R_PAREN { final Origin origin; final ExtensionInstruction instr; @@ -605,57 +451,59 @@ first_level_fate_instr: } else { - instr.build(WORLD, CONTEXT, origin, ($general_fate_sequence.result)); + instr.build(WORLD, CONTEXT, origin, ($instruction_list.result)); } } */ ; catch [final Throwable e] { - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } + PARSER.handle_error(e); } /* Trying to get rule priorities right */ -general_fate_instr +instruction returns [Instruction result] : - L_PAREN WS+ general_fate_sequence WS+ R_PAREN + L_PAREN WS* maybe_instruction_list WS* R_PAREN { + /* + * Don't define a local variable hierachy just for that group, as it would + * prevent things like + * (for + * ( + * (local int i) + * (set i 0) + * ) + * (< i 3) + * (set i (+ i 1)) + * stuff + * ) + * + */ $result = new InstructionList ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($L_PAREN.getLine()), ($L_PAREN.getCharPositionInLine()) ), - ($general_fate_sequence.result) + ($maybe_instruction_list.result) ); } - | LOCAL_KW - type - WS+ - name=new_reference_name - WS* - R_PAREN + | DECLARE_LOCAL_VARIABLE_KW type WS+ name=identifier WS* R_PAREN { final Origin start_origin, type_origin; final Variable new_variable; final Map variable_map; start_origin = - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($LOCAL_KW.getLine()), - ($LOCAL_KW.getCharPositionInLine()) + ($DECLARE_LOCAL_VARIABLE_KW.getLine()), + ($DECLARE_LOCAL_VARIABLE_KW.getCharPositionInLine()) ); new_variable = @@ -667,5155 +515,1599 @@ returns [Instruction result] false ); - variable_map = LOCAL_VARIABLES.peekFirst(); - - if (variable_map.containsKey(($name.result))) - { - ErrorManager.handle - ( - new DuplicateLocalVariableException - ( - variable_map.get(($name.result)), - new_variable - ) - ); - } - else - { - variable_map.put(($name.result), new_variable); - } + PARSER.add_local_variable(new_variable); $result = new LocalVariable(new_variable); - - if (!HIERARCHICAL_VARIABLES.isEmpty()) - { - HIERARCHICAL_VARIABLES.peekFirst().add(new_variable.get_name()); - } } - | PROMPT_STRING_KW - targetv=non_text_value WS+ - min_size=non_text_value WS+ - max_size=non_text_value WS+ - paragraph WS* - R_PAREN +/******************************************************************************/ +/**** LOOPS *******************************************************************/ +/******************************************************************************/ + | COND_KW instruction_cond_list WS* R_PAREN { $result = - PromptString.build + CondInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($PROMPT_STRING_KW.getLine()), - ($PROMPT_STRING_KW.getCharPositionInLine()) + ($COND_KW.getLine()), + ($COND_KW.getCharPositionInLine()) ), - ($targetv.result), - ($min_size.result), - ($max_size.result), - ($paragraph.result) + ($instruction_cond_list.result) ); } - | PROMPT_INTEGER_KW - targetv=non_text_value WS+ - min_size=non_text_value WS+ - max_size=non_text_value WS+ - paragraph WS* + | DO_WHILE_KW computation WS* + { + PARSER.increment_breakable_levels(); + PARSER.increment_continue_levels(); + PARSER.increase_local_variables_hierarchy(); + } + maybe_instruction_list WS* R_PAREN { + PARSER.decrease_local_variables_hierarchy(); + PARSER.decrement_continue_levels(); + PARSER.decrement_breakable_levels(); + $result = - PromptInteger.build + DoWhile.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($PROMPT_INTEGER_KW.getLine()), - ($PROMPT_INTEGER_KW.getCharPositionInLine()) + ($DO_WHILE_KW.getLine()), + ($DO_WHILE_KW.getCharPositionInLine()) ), - ($targetv.result), - ($min_size.result), - ($max_size.result), - ($paragraph.result) + ($computation.result), + ($maybe_instruction_list.result) ); } - | IMP_DICT_SET_KW key=value WS+ val=value WS+ value_reference WS* R_PAREN + | FOR_KW + { + PARSER.increase_local_variables_hierarchy(); + } + pre=instruction WS* computation WS* post=instruction WS* + { + PARSER.increment_breakable_levels(); + PARSER.increment_continue_levels(); + } + maybe_instruction_list + WS* + R_PAREN { - // TODO - $result = null; - /* - SetEntry.build + PARSER.decrement_continue_levels(); + PARSER.decrement_breakable_levels(); + PARSER.decrease_local_variables_hierarchy(); + + $result = + For.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_DICT_SET_KW.getLine()), - ($IMP_DICT_SET_KW.getCharPositionInLine()) + ($FOR_KW.getLine()), + ($FOR_KW.getCharPositionInLine()) ), - ($key.result), - ($val.result) + ($computation.result), + ($pre.result), + ($maybe_instruction_list.result), + ($post.result) ); - */ } - | IMP_ADD_KW value_list WS+ value_reference WS* R_PAREN - { - final List add_instrs; + | FOR_EACH_KW coll=computation WS+ identifier + { + final Variable new_variable; + final Type collection_type; + Type elem_type; - add_instrs = new ArrayList(); + elem_type = Type.ANY; - for (final Computation value: ($value_list.result)) - { - add_instrs.add - ( - AddElement.build + collection_type = ($coll.result).get_type(); + + /* FIXME: This doesn't let you use it with a Dict. */ + if (collection_type instanceof CollectionType) + { + elem_type = ((CollectionType) collection_type).get_content_type(); + } + else + { + ErrorManager.handle + ( + new InvalidTypeException + ( + PARSER.get_origin_at + ( + ($FOR_EACH_KW.getLine()), + ($FOR_EACH_KW.getCharPositionInLine()) + ), + elem_type, + Type.COLLECTION_TYPES + ) + ); + + elem_type = Type.ANY; + } + + new_variable = + new Variable ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_ADD_KW.getLine()), - ($IMP_ADD_KW.getCharPositionInLine()) + ($FOR_EACH_KW.getLine()), + ($FOR_EACH_KW.getCharPositionInLine()) ), - value, - ($value_reference.result) - ) - ); + elem_type, + ($identifier.result), + false + ); + + PARSER.increase_local_variables_hierarchy(); + PARSER.increment_breakable_levels(); + PARSER.increment_continue_levels(); + + PARSER.add_local_variable(new_variable); } + WS+ maybe_instruction_list WS* R_PAREN + { + PARSER.decrement_continue_levels(); + PARSER.decrement_breakable_levels(); + PARSER.decrease_local_variables_hierarchy(); $result = - new InstructionList + new ForEach ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_ADD_KW.getLine()), - ($IMP_ADD_KW.getCharPositionInLine()) + ($FOR_EACH_KW.getLine()), + ($FOR_EACH_KW.getCharPositionInLine()) ), - add_instrs + ($coll.result), + ($identifier.result), + ($maybe_instruction_list.result) ); } - | IMP_ADD_AT_KW - index=non_text_value WS+ - element=value WS+ - value_reference WS* - R_PAREN + | WHILE_KW computation WS* + { + PARSER.increase_local_variables_hierarchy(); + PARSER.increment_breakable_levels(); + PARSER.increment_continue_levels(); + } + maybe_instruction_list WS* R_PAREN { + PARSER.decrement_continue_levels(); + PARSER.decrement_breakable_levels(); + PARSER.decrease_local_variables_hierarchy(); + $result = - AddElementAt.build + While.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_ADD_AT_KW.getLine()), - ($IMP_ADD_AT_KW.getCharPositionInLine()) + ($WHILE_KW.getLine()), + ($WHILE_KW.getCharPositionInLine()) ), - ($index.result), - ($element.result), - ($value_reference.result) + ($computation.result), + ($maybe_instruction_list.result) ); } - | IMP_ADD_ALL_KW - sourcev=non_text_value WS+ - target=value_reference WS* + | SWITCH_KW computation WS* + { + PARSER.increment_breakable_levels(); + } + instr_cond_list WS* + { + PARSER.increase_local_variables_hierarchy(); + } + instruction WS* R_PAREN { + PARSER.decrease_local_variables_hierarchy(); + PARSER.decrement_breakable_levels(); + $result = - AddElementsOf.build + SwitchInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_ADD_ALL_KW.getLine()), - ($IMP_ADD_ALL_KW.getCharPositionInLine()) + ($SWITCH_KW.getLine()), + ($SWITCH_KW.getCharPositionInLine()) ), - ($sourcev.result), - ($target.result) + ($computation.result), + ($instr_cond_list.result), + ($instruction.result) ); } - | END_KW +/******************************************************************************/ +/**** IF ELSE *****************************************************************/ +/******************************************************************************/ + | IF_KW computation WS* + { + PARSER.increase_local_variables_hierarchy(); + } + maybe_instruction_list WS* + R_PAREN { + PARSER.decrease_local_variables_hierarchy(); + $result = - new End + IfInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($END_KW.getLine()), - ($END_KW.getCharPositionInLine()) - ) + ($IF_KW.getLine()), + ($IF_KW.getCharPositionInLine()) + ), + ($computation.result), + ($maybe_instruction_list.result) ); } - | DONE_KW + | IF_ELSE_KW computation + { + PARSER.increase_local_variables_hierarchy(); + } + WS* if_true=instruction + { + PARSER.decrease_local_variables_hierarchy(); + PARSER.increase_local_variables_hierarchy(); + } + WS* if_false=instruction WS* + R_PAREN { + PARSER.decrease_local_variables_hierarchy(); + $result = - new Done + IfElseInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($DONE_KW.getLine()), - ($DONE_KW.getCharPositionInLine()) - ) + ($IF_ELSE_KW.getLine()), + ($IF_ELSE_KW.getCharPositionInLine()) + ), + ($computation.result), + ($if_true.result), + ($if_false.result) ); } - | IMP_IGNORE_ERROR_KW WORD WS+ general_fate_instr WS* R_PAREN - { - /* TODO: temporarily disable an compiler error category */ - $result = ($general_fate_instr.result); +/******************************************************************************/ +/**** ERRORS ******************************************************************/ +/******************************************************************************/ + + | IMP_IGNORE_ERROR_KW WORD WS+ instruction WS* R_PAREN + { + /* TODO: temporarily disable an compiler error category */ + $result = ($instruction.result); } - | IMP_DICT_REMOVE_KW - value WS+ - value_reference WS* - R_PAREN + | IMP_ASSERT_KW computation WS+ paragraph WS* R_PAREN { - // TODO - $result = null; - /* - RemoveEntry.build + $result = + Assert.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_DICT_REMOVE_KW.getLine()), - ($IMP_DICT_REMOVE_KW.getCharPositionInLine()) + ($IMP_ASSERT_KW.getLine()), + ($IMP_ASSERT_KW.getCharPositionInLine()) ), - ($value.result), - ($value_reference.result) + ($computation.result), + ($paragraph.result) ); - */ } - | IMP_REMOVE_ONE_KW - value WS+ - value_reference WS* - R_PAREN +/******************************************************************************/ +/**** STRUCTURES **************************************************************/ +/******************************************************************************/ + + | IMP_SET_FIELDS_KW computation WS* field_value_list WS* R_PAREN { + /* + * A bit of a lazy solution: build field references, then extract the data + */ + final List> assignments; + + assignments = new ArrayList>(); + + for + ( + final Cons> entry: + ($field_value_list.result) + ) + { + final FieldReference fr; + final Computation cp; + + fr = + FieldReference.build + ( + entry.get_car(), + ($computation.result), + entry.get_cdr().get_car() + ); + + cp = entry.get_cdr().get_cdr(); + + RecurrentChecks.assert_can_be_used_as(cp, fr.get_type()); + + assignments.add(new Cons(fr.get_field_name(), cp)); + } + $result = - RemoveElement.build + new SetFields ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_REMOVE_ONE_KW.getLine()), - ($IMP_REMOVE_ONE_KW.getCharPositionInLine()) + ($IMP_SET_FIELDS_KW.getLine()), + ($IMP_SET_FIELDS_KW.getCharPositionInLine()) ), - ($value.result), - ($value_reference.result) + ($computation.result), + assignments ); } - | IMP_REMOVE_AT_KW - non_text_value WS+ - value_reference WS* +/******************************************************************************/ +/**** PLAYER INPUTS ***********************************************************/ +/******************************************************************************/ + + | PLAYER_CHOICE_KW + { + // FIXME: handle player_choice limited local variables. + PARSER.push_choice_limited_variables_level(); + } + player_choice_list WS* R_PAREN { $result = - RemoveElementAt.build + new PlayerChoice ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_REMOVE_AT_KW.getLine()), - ($IMP_REMOVE_AT_KW.getCharPositionInLine()) + ($PLAYER_CHOICE_KW.getLine()), + ($PLAYER_CHOICE_KW.getCharPositionInLine()) ), - ($non_text_value.result), - ($value_reference.result) + ($player_choice_list.result) ); + + PARSER.pop_choice_limited_variables_level(); } - | IMP_REMOVE_ALL_KW - value WS+ - value_reference WS* + | PROMPT_STRING_KW + targetv=computation WS+ + min_size=computation WS+ + max_size=computation WS+ + paragraph WS* R_PAREN { $result = - RemoveAllOfElement.build + PromptString.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_REMOVE_ALL_KW.getLine()), - ($IMP_REMOVE_ALL_KW.getCharPositionInLine()) + ($PROMPT_STRING_KW.getLine()), + ($PROMPT_STRING_KW.getCharPositionInLine()) ), - ($value.result), - ($value_reference.result) + ($targetv.result), + ($min_size.result), + ($max_size.result), + ($paragraph.result) ); } - | IMP_CLEAR_KW value_reference WS* R_PAREN + | PROMPT_INTEGER_KW + targetv=computation WS+ + min_size=computation WS+ + max_size=computation WS+ + paragraph WS* + R_PAREN { $result = - Clear.build + PromptInteger.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_CLEAR_KW.getLine()), - ($IMP_CLEAR_KW.getCharPositionInLine()) + ($PROMPT_INTEGER_KW.getLine()), + ($PROMPT_INTEGER_KW.getCharPositionInLine()) ), - ($value_reference.result) + ($targetv.result), + ($min_size.result), + ($max_size.result), + ($paragraph.result) ); } - | IMP_REVERSE_KW value_reference WS* R_PAREN +/******************************************************************************/ +/**** GENERIC INSTRUCTIONS ****************************************************/ +/******************************************************************************/ + | L_PAREN identifier IMP_MARKER maybe_computation_list WS* R_PAREN { $result = - ReverseList.build + GenericInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_REVERSE_KW.getLine()), - ($IMP_REVERSE_KW.getCharPositionInLine()) + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) ), - ($value_reference.result) + ($identifier.result), + ($maybe_computation_list.result) ); } - | IMP_PUSH_LEFT_KW value WS+ value_reference WS* R_PAREN +/******************************************************************************/ +/**** DISPLAYED COMPUTATIONS **************************************************/ +/******************************************************************************/ + | paragraph { $result = - PushElement.build + new Display ( - CONTEXT.get_origin_at - ( - ($IMP_PUSH_LEFT_KW.getLine()), - ($IMP_PUSH_LEFT_KW.getCharPositionInLine()) - ), - ($value.result), - ($value_reference.result), - true + ($paragraph.result.get_origin()), + ($paragraph.result) ); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} + +instruction_cond_list +returns [List> result] +@init +{ + Computation condition; + + condition = null; + + $result = new ArrayList>(); +} +: + ( + ( + ( + (L_PAREN WS* computation WS+) + { + condition = ($computation.result); + } + ) + | + ( + something_else=. + { + condition = + VariableFromWord.generate + ( + PARSER, + PARSER.get_origin_at + ( + ($something_else.getLine()), + ($something_else.getCharPositionInLine()) + ), + ($something_else.text).substring(1).trim() + ); + } + ) + ) + { + PARSER.increase_local_variables_hierarchy(); + } + instruction WS* R_PAREN + { + PARSER.decrease_local_variables_hierarchy(); - | IMP_PUSH_RIGHT_KW value WS+ value_reference WS* R_PAREN + $result.add(new Cons(condition, ($instruction.result))); + } + WS* + )+ { - $result = - PushElement.build + } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} + +instruction_switch_list +returns [List> result] +@init +{ + $result = new ArrayList>(); +} +: + ( + L_PAREN WS* computation WS+ + { + PARSER.increase_local_variables_hierarchy(); + } + instruction WS* R_PAREN + { + PARSER.decrease_local_variables_hierarchy(); + + $result.add ( - CONTEXT.get_origin_at - ( - ($IMP_PUSH_RIGHT_KW.getLine()), - ($IMP_PUSH_RIGHT_KW.getCharPositionInLine()) - ), - ($value.result), - ($value_reference.result), - false + new Cons(($value.result), ($instruction.result)) ); + } + WS* + )+ + { + } +; + +player_choice_list returns [List result] +@init +{ + $result = new ArrayList(); +} +: + ( + WS* player_choice + { + $result.add($player_choice.result); + } + )+ +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} + +maybe_player_choice_list +returns [List result] +@init +{ + $result = new ArrayList(); +} +: + (WS* + player_choice + { + $result.add(($player_choice.result)); + } + WS*)* + { } +; - | IMP_POP_RIGHT_KW value_reference WS+ non_text_value WS* R_PAREN +player_choice +returns [Instruction result] +/* + * Do not use a separate Local Variable stack for the player choice + * instructions. + */ +: + TEXT_OPTION_KW + L_PAREN WS* paragraph WS* R_PAREN WS* + { + PARSER.increase_local_variables_hierarchy(); + } + maybe_instruction_list WS* + R_PAREN { + PARSER.decrease_local_variables_hierarchy(); + $result = - PopElement.build + new TextOption ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_POP_RIGHT_KW.getLine()), - ($IMP_POP_RIGHT_KW.getCharPositionInLine()) + ($TEXT_OPTION_KW.getLine()), + ($TEXT_OPTION_KW.getCharPositionInLine()) ), - ($non_text_value.result), - ($value_reference.result), - false + ($paragraph.result), + ($maybe_instruction_list.result) ); } - | IMP_POP_LEFT_KW value_reference WS+ non_text_value WS* R_PAREN + | EVENT_OPTION_KW + L_PAREN WS* WORD maybe_value_list WS* R_PAREN WS* + { + PARSER.increase_local_variables_hierarchy(); + } + maybe_instruction_list WS* + R_PAREN { + final Origin origin; + final Event event; + + PARSER.decrease_local_variables_hierarchy(); + + origin = + PARSER.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) + ); + + event = PARSER.get_world().events().get(origin, ($WORD.text)); + $result = - PopElement.build + EventOption.build ( - CONTEXT.get_origin_at - ( - ($IMP_POP_LEFT_KW.getLine()), - ($IMP_POP_LEFT_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($value_reference.result), - true + origin, + event, + ($maybe_value_list.result), + ($maybe_instruction_list.result) ); } - | IMP_MAP_KW non_text_value WS+ value_reference WS* R_PAREN + | L_PAREN maybe_player_choice_list WS* R_PAREN { $result = - tonkadur.fate.v1.lang.instruction.Map.build + new InstructionList ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_MAP_KW.getLine()), - ($IMP_MAP_KW.getCharPositionInLine()) + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) ), - ($non_text_value.result), - ($value_reference.result), - new ArrayList() + ($maybe_player_choice_list.result) ); } - | IMP_DICT_MAP_KW non_text_value WS+ value_reference WS* R_PAREN + | IF_KW computation WS* player_choice_list WS* R_PAREN { - // TODO - $result = null; - /* - DictMap.build + $result = + IfInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_DICT_MAP_KW.getLine()), - ($IMP_DICT_MAP_KW.getCharPositionInLine()) + ($IF_KW.getLine()), + ($IF_KW.getCharPositionInLine()) ), - ($non_text_value.result), - ($value_reference.result), - new ArrayList() + ($computation.result), + ($player_choice_list.result) ); - */ } - - | IMP_MAP_KW non_text_value WS+ value_reference WS+ value_list WS* R_PAREN + | IF_ELSE_KW + computation WS* + if_true=player_choice WS* + if_false=player_choice WS* + R_PAREN { $result = - tonkadur.fate.v1.lang.instruction.Map.build + IfElseInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_MAP_KW.getLine()), - ($IMP_MAP_KW.getCharPositionInLine()) + ($IF_ELSE_KW.getLine()), + ($IF_ELSE_KW.getCharPositionInLine()) ), ($non_text_value.result), - ($value_reference.result), - ($value_list.result) + ($if_true.result), + ($if_false.result) ); } - | IMP_INDEXED_MAP_KW non_text_value WS+ value_reference WS* R_PAREN + | COND_KW player_choice_cond_list WS* R_PAREN { $result = - IndexedMap.build + CondInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_INDEXED_MAP_KW.getLine()), - ($IMP_INDEXED_MAP_KW.getCharPositionInLine()) + ($COND_KW.getLine()), + ($COND_KW.getCharPositionInLine()) ), - ($non_text_value.result), - ($value_reference.result), - new ArrayList() + ($player_choice_cond_list.result) ); } - | IMP_INDEXED_MAP_KW - non_text_value WS+ - value_reference WS+ - value_list WS* + | SWITCH_KW + computation WS* + player_choice_switch_list WS+ + player_choice WS* R_PAREN { $result = - IndexedMap.build + SwitchInstruction.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_INDEXED_MAP_KW.getLine()), - ($IMP_INDEXED_MAP_KW.getCharPositionInLine()) + ($SWITCH_KW.getLine()), + ($SWITCH_KW.getCharPositionInLine()) ), - ($non_text_value.result), - ($value_reference.result), - ($value_list.result) + ($value.result), + ($player_choice_switch_list.result), + ($player_choice.result) ); } - | IMP_DICT_MERGE_KW - fun=non_text_value WS+ - inv1=non_text_value WS+ - value_reference WS* + | FOR_KW + l0=L_PAREN + choice_for_variable_list WS* + R_PAREN WS* + computation WS* + l1=L_PAREN + choice_for_update_variable_list WS* + R_PAREN WS* + player_choice_list WS* R_PAREN { - // TODO - $result = null; - /* - DictMerge.build + $result = + For.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_DICT_MERGE_KW.getLine()), - ($IMP_DICT_MERGE_KW.getCharPositionInLine()) + ($FOR_KW.getLine()), + ($FOR_KW.getCharPositionInLine()) ), - ($fun.result), - ($inv1.result), - ($value_reference.result), - new ArrayList() - ); - */ - } - - | IMP_DICT_MERGE_KW - fun=non_text_value WS+ - inv1=non_text_value WS+ - value_reference WS+ - value_list WS* - R_PAREN - { - // TODO - $result = null; - /* - DictMerge.build - ( - CONTEXT.get_origin_at + ($computation.result), + new InstructionList ( - ($IMP_DICT_MERGE_KW.getLine()), - ($IMP_DICT_MERGE_KW.getCharPositionInLine()) + PARSER.get_origin_at + ( + ($l0.getLine()), + ($l0.getCharPositionInLine()) + ), + ($choice_for_variable_list.result) ), - ($fun.result), - ($inv1.result), - ($value_reference.result), - ($value_list.result) + ($player_choice_list.result), + new InstructionList + ( + PARSER.get_origin_at + ( + ($l1.getLine()), + ($l1.getCharPositionInLine()) + ), + ($choice_for_update_variable_list.result) + ) ); - */ } - | IMP_MERGE_KW - fun=non_text_value WS+ - inv1=non_text_value WS+ - value_reference WS* - R_PAREN - { - $result = - Merge.build - ( - CONTEXT.get_origin_at + | FOR_EACH_KW + computation WS+ identifier + { + final Map variable_map; + final Variable new_variable; + final Type collection_type; + Type elem_type; + + elem_type = Type.ANY; + + collection_type = ($non_text_value.result).get_type(); + + if (collection_type instanceof CollectionType) + { + elem_type = ((CollectionType) collection_type).get_content_type(); + } + else + { + ErrorManager.handle ( - ($IMP_MERGE_KW.getLine()), - ($IMP_MERGE_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv1.result), - null, - ($value_reference.result), - null, - new ArrayList() - ); - } + new InvalidTypeException + ( + PARSER.get_origin_at + ( + ($FOR_EACH_KW.getLine()), + ($FOR_EACH_KW.getCharPositionInLine()) + ), + elem_type, + Type.COLLECTION_TYPES + ) + ); - | IMP_MERGE_KW - fun=non_text_value WS+ - inv1=non_text_value WS+ - value_reference WS+ - value_list WS* - R_PAREN - { - $result = - Merge.build - ( - CONTEXT.get_origin_at + elem_type = Type.ANY; + } + + new_variable = + new Variable ( - ($IMP_MERGE_KW.getLine()), - ($IMP_MERGE_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv1.result), - null, - ($value_reference.result), - null, - ($value_list.result) - ); - } + PARSER.get_origin_at + ( + ($FOR_EACH_KW.getLine()), + ($FOR_EACH_KW.getCharPositionInLine()) + ), + elem_type, + ($identifier.result), + false + ); - | IMP_INDEXED_MERGE_KW - fun=non_text_value WS+ - inv1=non_text_value WS+ - value_reference WS* - R_PAREN + PARSER.add_context_variable(new_variable); + } + WS+ + { + PARSER.get_hierarchical_variables().push(new ArrayList()); + } + player_choice_list + { + for (final String s: PARSER.get_hierarchical_variables().pop()) + { + PARSER.get_local_variables().peekFirst().remove(s); + } + } + WS* + R_PAREN { $result = - IndexedMerge.build + new ForEach ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_INDEXED_MERGE_KW.getLine()), - ($IMP_INDEXED_MERGE_KW.getCharPositionInLine()) + ($FOR_EACH_KW.getLine()), + ($FOR_EACH_KW.getCharPositionInLine()) ), - ($fun.result), - ($inv1.result), - null, - ($value_reference.result), - null, - new ArrayList() + ($non_text_value.result), + ($identifier.result), + ($player_choice_list.result) ); + + variable_map.remove(($identifier.result)); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | IMP_INDEXED_MERGE_KW - fun=non_text_value WS+ - inv1=non_text_value WS+ - value_reference WS+ - value_list WS* - R_PAREN - { - $result = - IndexedMerge.build +player_choice_cond_list +returns [List> result] +@init +{ + Computation condition; + + condition = null; + + $result = new ArrayList>(); +} +: + ( + ( ( - CONTEXT.get_origin_at - ( - ($IMP_INDEXED_MERGE_KW.getLine()), - ($IMP_INDEXED_MERGE_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv1.result), - null, - ($value_reference.result), - null, - ($value_list.result) - ); + (L_PAREN WS* computation WS+) + { + condition = ($computation.result); + } + ) + | + ( + something_else=. + { + condition = + VariableFromWord.generate + ( + PARSER, + PARSER.get_origin_at + ( + ($something_else.getLine()), + ($something_else.getCharPositionInLine()) + ), + ($something_else.text).substring(1).trim() + ); + } + ) + ) + player_choice WS* R_PAREN + { + $result.add(new Cons(condition, ($player_choice.result))); + } + WS* + )+ + { } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | SAFE_IMP_MERGE_KW - fun=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - def0=value WS+ - value_reference WS* - R_PAREN +player_choice_switch_list +returns [List> result] +@init +{ + $result = new ArrayList>(); +} +: + ( + L_PAREN WS* computation WS* player_choice WS* R_PAREN + { + $result.add(new Cons(($computation.result), ($player_choice.result))); + } + WS* + )+ { - $result = - Merge.build - ( - CONTEXT.get_origin_at - ( - ($SAFE_IMP_MERGE_KW.getLine()), - ($SAFE_IMP_MERGE_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv1.result), - ($def1.result), - ($value_reference.result), - ($def0.result), - new ArrayList() - ); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | SAFE_IMP_MERGE_KW - fun=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - def0=value WS+ - value_reference WS+ - value_list WS* - R_PAREN +paragraph +returns [TextNode result] +@init +{ + final List content = new ArrayList(); +} +: + computation_list { - $result = - Merge.build - ( - CONTEXT.get_origin_at - ( - ($SAFE_IMP_MERGE_KW.getLine()), - ($SAFE_IMP_MERGE_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv1.result), - ($def1.result), - ($value_reference.result), - ($def0.result), - ($value_list.result) - ); + // convert all computations to text. + // return text node. + return new Paragraph(computation_list.result); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | SAFE_IMP_INDEXED_MERGE_KW - fun=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - def0=value WS+ - value_reference WS* - R_PAREN +sentence +returns [Computation result] +@init +{ + final StringBuilder string_builder = new StringBuilder(); +} +: + first_word=WORD { - $result = - IndexedMerge.build - ( - CONTEXT.get_origin_at - ( - ($SAFE_IMP_INDEXED_MERGE_KW.getLine()), - ($SAFE_IMP_INDEXED_MERGE_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv1.result), - ($def1.result), - ($value_reference.result), - ($def0.result), - new ArrayList() - ); + string_builder.append(($first_word.text)); } - - | SAFE_IMP_INDEXED_MERGE_KW - fun=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - def0=value WS+ - value_reference WS+ - value_list WS* - R_PAREN + ( + WS+ next_word=WORD + { + string_builder.append(" "); + string_builder.append(($next_word.text)); + } + )* { $result = - IndexedMerge.build + Constant.build_string ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($SAFE_IMP_INDEXED_MERGE_KW.getLine()), - ($SAFE_IMP_INDEXED_MERGE_KW.getCharPositionInLine()) + ($first_word.getLine()), + ($first_word.getCharPositionInLine()) ), - ($fun.result), - ($inv1.result), - ($def1.result), - ($value_reference.result), - ($def0.result), - ($value_list.result) + string_builder.toString() ); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | IMP_SUB_LIST_KW - vstart=non_text_value WS+ - vend=non_text_value WS+ - value_reference WS* - R_PAREN +type +returns [Type result] +: + WORD { $result = - SubList.build + PARSER.get_world().types().get ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_SUB_LIST_KW.getLine()), - ($IMP_SUB_LIST_KW.getCharPositionInLine()) + ($WORD.getLine()), + ($WORD.getCharPositionInLine()) ), - ($vstart.result), - ($vend.result), - ($value_reference.result) + ($WORD.text) ); } - | IMP_FILTER_KW non_text_value WS+ value_reference WS* R_PAREN + | L_PAREN identifier WS+ type_list WS* R_PAREN { - $result = - Filter.build + final Type t; + + t = + PARSER.get_world().types().get ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($IMP_FILTER_KW.getLine()), - ($IMP_FILTER_KW.getCharPositionInLine()) + ($WORD.getLine()), + ($WORD.getCharPositionInLine()) ), - ($non_text_value.result), - ($value_reference.result), - new ArrayList() + ($WORD.text) ); - } - - | IMP_FILTER_KW non_text_value WS+ value_reference WS+ value_list WS* R_PAREN - { - $result = - Filter.build - ( - CONTEXT.get_origin_at - ( - ($IMP_FILTER_KW.getLine()), - ($IMP_FILTER_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($value_reference.result), - ($value_list.result) - ); - } - - | IMP_INDEXED_FILTER_KW non_text_value WS+ value_reference WS* R_PAREN - { - $result = - IndexedFilter.build - ( - CONTEXT.get_origin_at - ( - ($IMP_INDEXED_FILTER_KW.getLine()), - ($IMP_INDEXED_FILTER_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($value_reference.result), - new ArrayList() - ); - } - - | IMP_INDEXED_FILTER_KW - non_text_value WS+ - value_reference WS+ - value_list WS* - R_PAREN - { - $result = - IndexedFilter.build - ( - CONTEXT.get_origin_at - ( - ($IMP_INDEXED_FILTER_KW.getLine()), - ($IMP_INDEXED_FILTER_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($value_reference.result), - ($value_list.result) - ); - } - - | IMP_PARTITION_KW - non_text_value WS+ - iftrue=value_reference WS+ - iffalse=value_reference WS* - R_PAREN - { - $result = - Partition.build - ( - CONTEXT.get_origin_at - ( - ($IMP_PARTITION_KW.getLine()), - ($IMP_PARTITION_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($iftrue.result), - ($iffalse.result), - new ArrayList() - ); - } - - | IMP_PARTITION_KW - non_text_value WS+ - iftrue=value_reference WS+ - iffalse=value_reference WS+ - value_list WS* - R_PAREN - { - $result = - Partition.build - ( - CONTEXT.get_origin_at - ( - ($IMP_PARTITION_KW.getLine()), - ($IMP_PARTITION_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($iftrue.result), - ($iffalse.result), - ($value_list.result) - ); - } - - | IMP_INDEXED_PARTITION_KW - non_text_value WS+ - iftrue=value_reference WS+ - iffalse=value_reference WS* - R_PAREN - { - $result = - IndexedPartition.build - ( - CONTEXT.get_origin_at - ( - ($IMP_INDEXED_PARTITION_KW.getLine()), - ($IMP_INDEXED_PARTITION_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($iftrue.result), - ($iffalse.result), - new ArrayList() - ); - } - - | IMP_INDEXED_PARTITION_KW - non_text_value WS+ - iftrue=value_reference WS+ - iffalse=value_reference WS+ - value_list WS* - R_PAREN - { - $result = - IndexedPartition.build - ( - CONTEXT.get_origin_at - ( - ($IMP_INDEXED_PARTITION_KW.getLine()), - ($IMP_INDEXED_PARTITION_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($iftrue.result), - ($iffalse.result), - ($value_list.result) - ); - } - - | IMP_SORT_KW non_text_value WS+ value_reference WS* R_PAREN - { - $result = - Sort.build - ( - CONTEXT.get_origin_at - ( - ($IMP_SORT_KW.getLine()), - ($IMP_SORT_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($value_reference.result), - new ArrayList() - ); - } - - | IMP_SORT_KW non_text_value WS+ value_reference WS+ value_list WS* R_PAREN - { - $result = - Sort.build - ( - CONTEXT.get_origin_at - ( - ($IMP_SORT_KW.getLine()), - ($IMP_SORT_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($value_reference.result), - ($value_list.result) - ); - } - - - | IMP_SHUFFLE_KW value_reference WS* R_PAREN - { - $result = - Shuffle.build - ( - CONTEXT.get_origin_at - ( - ($IMP_SHUFFLE_KW.getLine()), - ($IMP_SHUFFLE_KW.getCharPositionInLine()) - ), - ($value_reference.result) - ); - } - - | IMP_SET_KW value_reference WS+ value WS* R_PAREN - { - $result = - SetValue.build - ( - CONTEXT.get_origin_at - ( - ($IMP_SET_KW.getLine()), - ($IMP_SET_KW.getCharPositionInLine()) - ), - ($value.result), - ($value_reference.result) - ); - } - - | ALLOCATE_KW value_reference WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($ALLOCATE_KW.getLine()), - ($ALLOCATE_KW.getCharPositionInLine()) - ); - - $result = Allocate.build(origin, ($value_reference.result)); - } - - - | FREE_KW value_reference WS* R_PAREN - { - $result = - new Free - ( - CONTEXT.get_origin_at - ( - ($FREE_KW.getLine()), - ($FREE_KW.getCharPositionInLine()) - ), - ($value_reference.result) - ); - } - - | IMP_SET_FIELDS_KW value_reference WS* field_value_list WS* R_PAREN - { - /* - * A bit of a lazy solution: build field references, then extract the data - */ - final List> assignments; - - assignments = new ArrayList>(); - - for - ( - final Cons> entry: - ($field_value_list.result) - ) - { - final FieldReference fr; - final Computation cp; - - fr = - FieldReference.build - ( - entry.get_car(), - ($value_reference.result), - entry.get_cdr().get_car() - ); - - cp = entry.get_cdr().get_cdr(); - - RecurrentChecks.assert_can_be_used_as(cp, fr.get_type()); - - assignments.add(new Cons(fr.get_field_name(), cp)); - } - - $result = - new SetFields - ( - CONTEXT.get_origin_at - ( - ($IMP_SET_FIELDS_KW.getLine()), - ($IMP_SET_FIELDS_KW.getCharPositionInLine()) - ), - ($value_reference.result), - assignments - ); - } - - | WHILE_KW non_text_value WS* - { - BREAKABLE_LEVELS++; - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - BREAKABLE_LEVELS--; - - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* - R_PAREN - { - $result = - While.build - ( - CONTEXT.get_origin_at - ( - ($WHILE_KW.getLine()), - ($WHILE_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($general_fate_sequence.result) - ); - } - - | DO_WHILE_KW non_text_value WS* - { - BREAKABLE_LEVELS++; - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - BREAKABLE_LEVELS--; - - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* - R_PAREN - { - $result = - DoWhile.build - ( - CONTEXT.get_origin_at - ( - ($DO_WHILE_KW.getLine()), - ($DO_WHILE_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($general_fate_sequence.result) - ); - } - - | {BREAKABLE_LEVELS > 0}? IMP_BREAK_KW - { - $result = - new Break - ( - CONTEXT.get_origin_at - ( - ($IMP_BREAK_KW.getLine()), - ($IMP_BREAK_KW.getCharPositionInLine()) - ) - ); - } - - | FOR_KW pre=general_fate_instr WS* - non_text_value WS* - post=general_fate_instr WS* - { - BREAKABLE_LEVELS++; - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - BREAKABLE_LEVELS--; - - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* R_PAREN - { - $result = - For.build - ( - CONTEXT.get_origin_at - ( - ($FOR_KW.getLine()), - ($FOR_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($pre.result), - ($general_fate_sequence.result), - ($post.result) - ); - } - - | FOR_EACH_KW - coll=non_text_value WS+ new_reference_name - { - final Map variable_map; - final Variable new_variable; - final Type collection_type; - Type elem_type; - - elem_type = Type.ANY; - - collection_type = ($coll.result).get_type(); - - if (collection_type instanceof CollectionType) - { - elem_type = ((CollectionType) collection_type).get_content_type(); - } - else - { - ErrorManager.handle - ( - new InvalidTypeException - ( - CONTEXT.get_origin_at - ( - ($FOR_EACH_KW.getLine()), - ($FOR_EACH_KW.getCharPositionInLine()) - ), - elem_type, - Type.COLLECTION_TYPES - ) - ); - - elem_type = Type.ANY; - } - - - new_variable = - new Variable - ( - CONTEXT.get_origin_at - ( - ($FOR_EACH_KW.getLine()), - ($FOR_EACH_KW.getCharPositionInLine()) - ), - elem_type, - ($new_reference_name.result), - false - ); - - variable_map = LOCAL_VARIABLES.peekFirst(); - - if (variable_map.containsKey(($new_reference_name.result))) - { - ErrorManager.handle - ( - new DuplicateLocalVariableException - ( - variable_map.get(($new_reference_name.result)), - new_variable - ) - ); - } - else - { - variable_map.put(($new_reference_name.result), new_variable); - } - } - WS+ - { - BREAKABLE_LEVELS++; - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - BREAKABLE_LEVELS--; - - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* - R_PAREN - { - $result = - new ForEach - ( - CONTEXT.get_origin_at - ( - ($FOR_EACH_KW.getLine()), - ($FOR_EACH_KW.getCharPositionInLine()) - ), - ($coll.result), - ($new_reference_name.result), - ($general_fate_sequence.result) - ); - - variable_map.remove(($new_reference_name.result)); - } - - | EXTRA_INSTRUCTION_KW WORD WS+ value_list WS* R_PAREN - { - final Origin origin; - final ExtraInstruction extra_instruction; - - origin = - CONTEXT.get_origin_at - ( - ($EXTRA_INSTRUCTION_KW.getLine()), - ($EXTRA_INSTRUCTION_KW.getCharPositionInLine()) - ); - - extra_instruction = WORLD.extra_instructions().get(origin, ($WORD.text)); - - $result = - extra_instruction.instantiate - ( - WORLD, - CONTEXT, - origin, - ($value_list.result) - ); - } - - | EXTRA_INSTRUCTION_KW WORD WS* R_PAREN - { - final Origin origin; - final ExtraInstruction extra_instruction; - - origin = - CONTEXT.get_origin_at - ( - ($EXTRA_INSTRUCTION_KW.getLine()), - ($EXTRA_INSTRUCTION_KW.getCharPositionInLine()) - ); - - extra_instruction = WORLD.extra_instructions().get(origin, ($WORD.text)); - - $result = - extra_instruction.instantiate - ( - WORLD, - CONTEXT, - origin, - new ArrayList() - ); - } - - | VISIT_KW WORD WS+ value_list WS* R_PAREN - { - final Origin origin; - final String sequence_name; - - origin = - CONTEXT.get_origin_at - ( - ($VISIT_KW.getLine()), - ($VISIT_KW.getCharPositionInLine()) - ); - - sequence_name = ($WORD.text); - - WORLD.add_sequence_use(origin, sequence_name, ($value_list.result)); - - $result = new SequenceCall(origin, sequence_name, ($value_list.result)); - } - - | VISIT_KW WORD WS* R_PAREN - { - final Origin origin; - final String sequence_name; - final List params; - - origin = - CONTEXT.get_origin_at - ( - ($VISIT_KW.getLine()), - ($VISIT_KW.getCharPositionInLine()) - ); - - params = new ArrayList(); - - sequence_name = ($WORD.text); - - WORLD.add_sequence_use(origin, sequence_name, params); - - $result = new SequenceCall(origin, sequence_name, params); - } - - | CONTINUE_AS_KW WORD WS+ value_list WS* R_PAREN - { - final Origin origin; - final String sequence_name; - - origin = - CONTEXT.get_origin_at - ( - ($CONTINUE_AS_KW.getLine()), - ($CONTINUE_AS_KW.getCharPositionInLine()) - ); - - sequence_name = ($WORD.text); - - WORLD.add_sequence_use(origin, sequence_name, ($value_list.result)); - - $result = new SequenceJump(origin, sequence_name, ($value_list.result)); - } - - | CONTINUE_AS_KW WORD WS* R_PAREN - { - final Origin origin; - final String sequence_name; - final List params; - - origin = - CONTEXT.get_origin_at - ( - ($CONTINUE_AS_KW.getLine()), - ($CONTINUE_AS_KW.getCharPositionInLine()) - ); - - params = new ArrayList(); - - sequence_name = ($WORD.text); - - WORLD.add_sequence_use(origin, sequence_name, params); - - $result = new SequenceJump(origin, sequence_name, params); - } - - | VISIT_KW value WS+ value_list WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($VISIT_KW.getLine()), - ($VISIT_KW.getCharPositionInLine()) - ); - - $result = - SequenceVariableCall.build - ( - origin, - ($value.result), - ($value_list.result) - ); - } - - | VISIT_KW value WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($VISIT_KW.getLine()), - ($VISIT_KW.getCharPositionInLine()) - ); - - $result = - SequenceVariableCall.build - ( - origin, - ($value.result), - new ArrayList() - ); - } - - | CONTINUE_AS_KW value WS+ value_list WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($CONTINUE_AS_KW.getLine()), - ($CONTINUE_AS_KW.getCharPositionInLine()) - ); - - $result = - SequenceVariableJump.build - ( - origin, - ($value.result), - ($value_list.result) - ); - } - - | CONTINUE_AS_KW value WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($CONTINUE_AS_KW.getLine()), - ($CONTINUE_AS_KW.getCharPositionInLine()) - ); - - $result = - SequenceVariableJump.build - ( - origin, - ($value.result), - new ArrayList() - ); - } - - | IMP_ASSERT_KW non_text_value WS+ paragraph WS* R_PAREN - { - $result = - Assert.build - ( - CONTEXT.get_origin_at - ( - ($IMP_ASSERT_KW.getLine()), - ($IMP_ASSERT_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($paragraph.result) - ); - } - - | IF_KW non_text_value WS* - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* R_PAREN - { - $result = - IfInstruction.build - ( - CONTEXT.get_origin_at - ( - ($IF_KW.getLine()), - ($IF_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($general_fate_sequence.result) - ); - } - - | IF_ELSE_KW - non_text_value - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - WS+ if_true=general_fate_instr - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - WS+ if_false=general_fate_instr - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - - } - WS* R_PAREN - { - $result = - IfElseInstruction.build - ( - CONTEXT.get_origin_at - ( - ($IF_ELSE_KW.getLine()), - ($IF_ELSE_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($if_true.result), - ($if_false.result) - ); - } - - | COND_KW instr_cond_list WS* R_PAREN - { - $result = - CondInstruction.build - ( - CONTEXT.get_origin_at - ( - ($COND_KW.getLine()), - ($COND_KW.getCharPositionInLine()) - ), - ($instr_cond_list.result) - ); - } - - | SWITCH_KW non_text_value WS* - instr_cond_list WS* - general_fate_instr WS* - R_PAREN - { - $result = - SwitchInstruction.build - ( - CONTEXT.get_origin_at - ( - ($SWITCH_KW.getLine()), - ($SWITCH_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($instr_cond_list.result), - ($general_fate_instr.result) - ); - } - - | PLAYER_CHOICE_KW - { - CHOICE_LIMITED_VARIABLES.push(new HashSet()); - } - player_choice_list WS* R_PAREN - { - $result = - new PlayerChoice - ( - CONTEXT.get_origin_at - ( - ($PLAYER_CHOICE_KW.getLine()), - ($PLAYER_CHOICE_KW.getCharPositionInLine()) - ), - ($player_choice_list.result) - ); - - CHOICE_LIMITED_VARIABLES.pop(); - } - - | IMP_DICT_TO_LIST_KW non_text_value WS+ value_reference WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_FROM_LIST_KW non_text_value WS+ value_reference WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_KEYS_KW non_text_value WS+ value_reference WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_VALUES_KW non_text_value WS+ value_reference WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_MERGE_KW - dict0r=value_reference WS+ - dict1=non_text_value WS+ - fun=non_text_value WS* - R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_MERGE_KW - dict0r=value_reference WS+ - dict1=non_text_value WS+ - fun=non_text_value WS+ - value_list WS* - R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_FILTER_KW - dict0r=value_reference WS+ - fun=non_text_value WS* - R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_FILTER_KW - dict0r=value_reference WS+ - fun=non_text_value WS+ - value_list WS* - R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_SET_KW - key=value WS+ - val=value WS+ - dict0r=value_reference WS* - R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_REMOVE_KW key=value WS+ dict0r=value_reference WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | IMP_DICT_HAS_KW - key=value WS+ - dict0=non_text_value WS+ - value_reference WS* - R_PAREN - { - - /* TODO */ - $result = null; - } - - | IMP_DICT_GET_KW - key=value WS+ - dict0=non_text_value WS+ - value_reference WS* - R_PAREN - { - - /* TODO */ - $result = null; - } - - | IMP_DICT_GET_POINTER_KW - key=value WS+ - dict0=non_text_value WS+ - value_reference WS* - R_PAREN - { - - /* TODO */ - $result = null; - } - - | paragraph - { - $result = - new Display - ( - ($paragraph.result.get_origin()), - ($paragraph.result) - ); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -instr_cond_list -returns [List> result] -@init -{ - Computation condition; - - condition = null; - - $result = new ArrayList>(); -} -: - ( - ( - ( - (L_PAREN WS* non_text_value WS+) - { - condition = ($non_text_value.result); - } - ) - | - ( - something_else=. - { - condition = - VariableFromWord.generate - ( - WORLD, - LOCAL_VARIABLES, - CONTEXT.get_origin_at - ( - ($something_else.getLine()), - ($something_else.getCharPositionInLine()) - ), - ($something_else.text).substring(1).trim() - ); - } - ) - ) - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_instr - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* R_PAREN - { - $result.add - ( - new Cons(condition, ($general_fate_instr.result)) - ); - } - WS* - )+ - { - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -instr_switch_list -returns [List> result] -@init -{ - $result = new ArrayList>(); -} -: - ( - L_PAREN WS* value WS+ - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_instr - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* R_PAREN - { - $result.add - ( - new Cons(($value.result), ($general_fate_instr.result)) - ); - } - WS* - )+ - { - } -; - -player_choice_list -returns [List result] -@init -{ - $result = new ArrayList(); -} -: - (WS* - player_choice - { - $result.add(($player_choice.result)); - } - WS*)* - { - } -; - -player_choice -returns [Instruction result] -: - TEXT_OPTION_KW - L_PAREN WS* paragraph WS* R_PAREN WS+ - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* - R_PAREN - { - $result = - new TextOption - ( - CONTEXT.get_origin_at - ( - ($TEXT_OPTION_KW.getLine()), - ($TEXT_OPTION_KW.getCharPositionInLine()) - ), - ($paragraph.result), - ($general_fate_sequence.result) - ); - } - - | EVENT_OPTION_KW - L_PAREN WS* WORD WS* R_PAREN WS+ - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* - R_PAREN - { - final Origin origin; - final Event event; - - origin = - CONTEXT.get_origin_at - ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ); - - event = WORLD.events().get(origin, ($WORD.text)); - - $result = - new EventOption - ( - origin, - event, - ($general_fate_sequence.result) - ); - } - - | EVENT_OPTION_KW - L_PAREN WS* WORD WS+ value_list WS* R_PAREN WS+ - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - general_fate_sequence - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* - R_PAREN - { - final Origin origin; - final Event event; - - origin = - CONTEXT.get_origin_at - ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ); - - event = WORLD.events().get(origin, ($WORD.text)); - - $result = - EventOption.build - ( - origin, - event, - ($value_list.result), - ($general_fate_sequence.result) - ); - } - - | IF_KW - non_text_value WS+ - player_choice_list WS* - R_PAREN - { - $result = - IfInstruction.build - ( - CONTEXT.get_origin_at - ( - ($IF_KW.getLine()), - ($IF_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($player_choice_list.result) - ); - } - - | IF_ELSE_KW - non_text_value WS+ - if_true=player_choice WS+ - if_false=player_choice WS* - R_PAREN - { - $result = - IfElseInstruction.build - ( - CONTEXT.get_origin_at - ( - ($IF_ELSE_KW.getLine()), - ($IF_ELSE_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($if_true.result), - ($if_false.result) - ); - } - - | COND_KW player_choice_cond_list WS* R_PAREN - { - $result = - CondInstruction.build - ( - CONTEXT.get_origin_at - ( - ($COND_KW.getLine()), - ($COND_KW.getCharPositionInLine()) - ), - ($player_choice_cond_list.result) - ); - } - - | SWITCH_KW value WS* player_choice_switch_list WS+ player_choice WS* R_PAREN - { - $result = - SwitchInstruction.build - ( - CONTEXT.get_origin_at - ( - ($SWITCH_KW.getLine()), - ($SWITCH_KW.getCharPositionInLine()) - ), - ($value.result), - ($player_choice_switch_list.result), - ($player_choice.result) - ); - } - - | FOR_KW - l0=L_PAREN - choice_for_variable_list WS* - R_PAREN WS* - non_text_value WS* - l1=L_PAREN - choice_for_update_variable_list WS* - R_PAREN WS* - player_choice_list - WS* R_PAREN - { - $result = - For.build - ( - CONTEXT.get_origin_at - ( - ($FOR_KW.getLine()), - ($FOR_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - new InstructionList - ( - CONTEXT.get_origin_at - ( - ($l0.getLine()), - ($l0.getCharPositionInLine()) - ), - ($choice_for_variable_list.result) - ), - ($player_choice_list.result), - new InstructionList - ( - CONTEXT.get_origin_at - ( - ($l1.getLine()), - ($l1.getCharPositionInLine()) - ), - ($choice_for_update_variable_list.result) - ) - ); - } - - | FOR_EACH_KW - non_text_value WS+ new_reference_name - { - final Map variable_map; - final Variable new_variable; - final Type collection_type; - Type elem_type; - - elem_type = Type.ANY; - - collection_type = ($non_text_value.result).get_type(); - - if (collection_type instanceof CollectionType) - { - elem_type = ((CollectionType) collection_type).get_content_type(); - } - else - { - ErrorManager.handle - ( - new InvalidTypeException - ( - CONTEXT.get_origin_at - ( - ($FOR_EACH_KW.getLine()), - ($FOR_EACH_KW.getCharPositionInLine()) - ), - elem_type, - Type.COLLECTION_TYPES - ) - ); - - elem_type = Type.ANY; - } - - - new_variable = - new Variable - ( - CONTEXT.get_origin_at - ( - ($FOR_EACH_KW.getLine()), - ($FOR_EACH_KW.getCharPositionInLine()) - ), - elem_type, - ($new_reference_name.result), - false - ); - - variable_map = LOCAL_VARIABLES.peekFirst(); - - if (variable_map.containsKey(($new_reference_name.result))) - { - ErrorManager.handle - ( - new DuplicateLocalVariableException - ( - variable_map.get(($new_reference_name.result)), - new_variable - ) - ); - } - else - { - variable_map.put(($new_reference_name.result), new_variable); - } - } - WS+ - { - HIERARCHICAL_VARIABLES.push(new ArrayList()); - } - player_choice_list - { - for (final String s: HIERARCHICAL_VARIABLES.pop()) - { - LOCAL_VARIABLES.peekFirst().remove(s); - } - } - WS* - R_PAREN - { - $result = - new ForEach - ( - CONTEXT.get_origin_at - ( - ($FOR_EACH_KW.getLine()), - ($FOR_EACH_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($new_reference_name.result), - ($player_choice_list.result) - ); - - variable_map.remove(($new_reference_name.result)); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -player_choice_cond_list -returns [List> result] -@init -{ - Computation condition; - - condition = null; - - $result = new ArrayList>(); -} -: - ( - ( - ( - (L_PAREN WS* non_text_value WS+) - { - condition = ($non_text_value.result); - } - ) - | - ( - something_else=. - { - condition = - VariableFromWord.generate - ( - WORLD, - LOCAL_VARIABLES, - CONTEXT.get_origin_at - ( - ($something_else.getLine()), - ($something_else.getCharPositionInLine()) - ), - ($something_else.text).substring(1).trim() - ); - } - ) - ) - player_choice WS* R_PAREN - { - $result.add(new Cons(condition, ($player_choice.result))); - } - WS* - )+ - { - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -player_choice_switch_list -returns [List> result] -@init -{ - $result = new ArrayList>(); -} -: - ( - L_PAREN WS* value WS+ player_choice WS* R_PAREN - { - $result.add(new Cons(($value.result), ($player_choice.result))); - } - WS* - )+ - { - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -paragraph -returns [TextNode result] -@init -{ - final List content = new ArrayList(); -} -: - first=text_value - { - content.add(($first.result)); - } - ( - (WS+ next_a=text_value) - { - if (!(content.get(content.size() - 1) instanceof Newline)) - { - content.add - ( - ValueToText.build - ( - Constant.build_string - ( - $next_a.result.get_origin(), - " " - ) - ) - ); - } - - content.add(($next_a.result)); - } - | - (next_b=text_value) - { - content.add(($next_b.result)); - } - )* - { - if (content.size() == 1) - { - $result = content.get(0); - } - else - { - $result = - new Paragraph - ( - ($first.result.get_origin()), - content - ); - } - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -actual_text_value -returns [TextNode result]: - TEXT_KW paragraph WS* R_PAREN - { - $result = ($paragraph.result); - } - - | JOIN_KW value WS+ non_text_value WS* R_PAREN - { - $result = - TextJoin.build - ( - CONTEXT.get_origin_at - ( - ($JOIN_KW.getLine()), - ($JOIN_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($value.result) - ); - } - - | ENABLE_TEXT_EFFECT_KW WORD WS+ paragraph WS* R_PAREN - { - final TextEffect effect; - - effect = - WORLD.text_effects().get - ( - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ), - ($WORD.text) - ); - - $result = - TextWithEffect.build - ( - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ), - effect, - new ArrayList(), - ($paragraph.result) - ); - } - - | ENABLE_TEXT_EFFECT_KW - L_PAREN - WORD WS+ - value_list WS* - R_PAREN WS+ - paragraph WS* - R_PAREN - { - final TextEffect effect; - - effect = - WORLD.text_effects().get - ( - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ), - ($WORD.text) - ); - - $result = - TextWithEffect.build - ( - CONTEXT.get_origin_at - ( - ($ENABLE_TEXT_EFFECT_KW.getLine()), - ($ENABLE_TEXT_EFFECT_KW.getCharPositionInLine()) - ), - effect, - ($value_list.result), - ($paragraph.result) - ); - } - - | NEWLINE_KW - { - $result = - new Newline - ( - CONTEXT.get_origin_at - ( - ($NEWLINE_KW.getLine()), - ($NEWLINE_KW.getCharPositionInLine()) - ) - ); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -value_as_text -returns [TextNode result]: - sentence - { - $result = ValueToText.build(($sentence.result)); - } - - | non_text_value - { - $result = ValueToText.build(($non_text_value.result)); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -text_value -returns [TextNode result]: - actual_text_value - { - $result = $actual_text_value.result; - } - - | value_as_text - { - $result = $value_as_text.result; - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} -sentence -returns [Computation result] -@init -{ - final StringBuilder string_builder = new StringBuilder(); -} -: - STRING_KW sentence WS* R_PAREN - { - $result = ($sentence.result); - } - - | - first_word=WORD - { - string_builder.append(($first_word.text)); - } - ( - WS+ next_word=WORD - { - string_builder.append(" "); - string_builder.append(($next_word.text)); - } - )* - { - $result = - Constant.build_string - ( - CONTEXT.get_origin_at - ( - ($first_word.getLine()), - ($first_word.getCharPositionInLine()) - ), - string_builder.toString() - ); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -type -returns [Type result] -: - WORD - { - $result = - WORLD.types().get - ( - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ), - ($WORD.text) - ); - } - - | REF_KW type WS* R_PAREN - { - final Origin start_origin; - - start_origin = - CONTEXT.get_origin_at - ( - ($REF_KW.getLine()), - ($REF_KW.getCharPositionInLine()) - ); - - $result = - new PointerType - ( - start_origin, - ($type.result), - ("anonymous (" + ($type.result).get_name() + ") ref type") - ); - } - - | DICT_KW key_type=type WS+ val_type=type WS* R_PAREN - { - final Origin start_origin; - - start_origin = - CONTEXT.get_origin_at - ( - ($DICT_KW.getLine()), - ($DICT_KW.getCharPositionInLine()) - ); - - $result = - DictionaryType.build - ( - start_origin, - ($key_type.result), - ($val_type.result), - ( - "anonymous (" - + ($key_type.result) - + " " - + $val_type.result - + ") dictionary type" - ) - ); - } - - | SET_KW type WS* R_PAREN - { - final Origin start_origin; - - start_origin = - CONTEXT.get_origin_at - ( - ($SET_KW.getLine()), - ($SET_KW.getCharPositionInLine()) - ); - - $result = - CollectionType.build - ( - start_origin, - ($type.result), - true, - ("anonymous (" + ($type.result).get_name() + ") set type") - ); - } - - | LIST_KW type WS* R_PAREN - { - final Origin start_origin; - - start_origin = - CONTEXT.get_origin_at - ( - ($LIST_KW.getLine()), - ($LIST_KW.getCharPositionInLine()) - ); - - $result = - CollectionType.build - ( - start_origin, - ($type.result), - false, - ("anonymous (" + ($type.result).get_name() + ") list type") - ); - } - - | CONS_KW t0=type WS+ t1=type WS* R_PAREN - { - $result = - new ConsType - ( - CONTEXT.get_origin_at - ( - ($CONS_KW.getLine()), - ($CONS_KW.getCharPositionInLine()) - ), - ($t0.result), - ($t1.result), - ("anonymous (" + ($type.result).get_name() + ") list type") - ); - } - - | LAMBDA_KW type WS* L_PAREN WS* type_list WS* R_PAREN WS* R_PAREN - { - final Origin start_origin; - - start_origin = - CONTEXT.get_origin_at - ( - ($LAMBDA_KW.getLine()), - ($LAMBDA_KW.getCharPositionInLine()) - ); - - $result = - new LambdaType - ( - start_origin, - ($type.result), - "auto_generated", - ($type_list.result) - ); - } - - | SEQUENCE_KW type_list WS* R_PAREN - { - final Origin start_origin; - - start_origin = - CONTEXT.get_origin_at - ( - ($SEQUENCE_KW.getLine()), - ($SEQUENCE_KW.getCharPositionInLine()) - ); - - $result = - new SequenceType - ( - start_origin, - "auto_generated", - ($type_list.result) - ); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -type_list -returns [List result] -@init -{ - $result = new ArrayList(); -} -: - ( - type - { - $result.add(($type.result)); - } - )? - (WS+ - type - { - $result.add(($type.result)); - } - )* - { - } -; - -let_variable_list -returns [List> result] -@init -{ - String var_name; - - var_name = null; - - Map variables; - - variables = LOCAL_VARIABLES.peekFirst(); - - $result = new ArrayList>(); -} -: - ( - WS* - ( - ( - L_PAREN WS* new_reference_name - { - var_name = ($new_reference_name.result); - } - ) - | - ( - something_else=. - { - var_name = ($something_else.text).substring(1).trim(); - } - ) - ) - WS+ value WS* R_PAREN - { - final Variable v; - - v = - new Variable - ( - CONTEXT.get_origin_at - ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ), - ($value.result).get_type(), - var_name, - false - ); - - if (variables.containsKey(var_name)) - { - ErrorManager.handle - ( - new DuplicateLocalVariableException - ( - variables.get(var_name), - v - ) - ); - } - - variables.put(var_name, v); - - $result.add(new Cons(v, ($value.result))); - } - )* - { - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -choice_for_update_variable_list -returns [List result] -@init -{ - Collection allowed_variables; - String var_name; - Origin origin; - - allowed_variables = CHOICE_LIMITED_VARIABLES.peek(); - var_name = null; - origin = null; - - $result = new ArrayList(); -} -: - ( - WS* - ( - ( - L_PAREN WS* new_reference_name - { - var_name = ($new_reference_name.result); - origin = - CONTEXT.get_origin_at - ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ); - } - ) - | - ( - something_else=. - { - var_name = ($something_else.text).substring(1).trim(); - origin = - CONTEXT.get_origin_at - ( - ($something_else.getLine()), - ($something_else.getCharPositionInLine()) - ); - } - ) - ) - WS+ value WS* R_PAREN - { - $result.add - ( - SetValue.build - ( - origin, - ($value.result), - VariableFromWord.generate - ( - WORLD, - LOCAL_VARIABLES, - origin, - var_name - ) - ) - ); - - if (!allowed_variables.contains(var_name)) - { - ErrorManager.handle - ( - new UpdatingIllegalVariableFromChoiceException(origin, var_name) - ); - } - } - )* - { - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -choice_for_variable_list -returns [List result] -@init -{ - Collection allowed_variables; - String var_name; - Origin origin; - - allowed_variables = CHOICE_LIMITED_VARIABLES.peek(); - var_name = null; - origin = null; - - $result = new ArrayList(); -} -: - ( - WS* - ( - ( - L_PAREN WS* new_reference_name - { - var_name = ($new_reference_name.result); - origin = - CONTEXT.get_origin_at - ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ); - } - ) - | - ( - something_else=. - { - var_name = ($something_else.text).substring(1).trim(); - origin = - CONTEXT.get_origin_at - ( - ($something_else.getLine()), - ($something_else.getCharPositionInLine()) - ); - } - ) - ) - WS+ value WS* R_PAREN - { - $result.add - ( - SetValue.build - ( - origin, - ($value.result), - VariableFromWord.generate - ( - WORLD, - LOCAL_VARIABLES, - origin, - var_name - ) - ) - ); - - allowed_variables.add(var_name); - } - )* - { - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -variable_list -returns [VariableList result] -@init -{ - Type next_type; - Origin origin; - - next_type = null; - origin = null; - - $result = new VariableList(); -} -: - ( - WS* - ( - ( - ( - L_PAREN WS* type WS+ - ) - { - origin = - CONTEXT.get_origin_at - ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ); - next_type = ($type.result); - } - ) - | - ( - something_else=. - { - origin = - CONTEXT.get_origin_at - ( - ($something_else.getLine()), - ($something_else.getCharPositionInLine()) - ); - - next_type = - WORLD.types().get - ( - origin, - ($something_else.text).substring(1).trim() - ); - } - ) - ) - WS* new_reference_name WS* R_PAREN - { - $result.add - ( - new Variable - ( - origin, - next_type, - ($new_reference_name.result), - false - ) - ); - } - )* - { - } - | -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -field_value_list -returns [List>> result] -@init -{ - String field_name; - - field_name = null; - - $result = new ArrayList>>(); -} -: - ( - WS* - ( - ( - L_PAREN WS* WORD WS+ - { - field_name = ($WORD.text); - } - ) - | - ( - something_else=. - { - field_name = ($something_else.text).substring(1).trim(); - } - ) - ) - value WS* R_PAREN - { - $result.add - ( - new Cons - ( - CONTEXT.get_origin_at - ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) - ), - new Cons - ( - field_name, - ($value.result) - ) - ) - ); - } - )* - { - } -; - -new_reference_name -returns [String result] -: - WORD - { - if (($WORD.text).indexOf('.') != -1) - { - ErrorManager.handle - ( - new IllegalReferenceNameException - ( - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ), - ($WORD.text) - ) - ); - } - - $result = ($WORD.text); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -/******************************************************************************/ -/**** VALUES ******************************************************************/ -/******************************************************************************/ -boolean_expression -returns [Computation result]: - TRUE_KW - { - $result = - Constant.build_boolean - ( - CONTEXT.get_origin_at - ( - ($TRUE_KW.getLine()), - ($TRUE_KW.getCharPositionInLine()) - ), - true - ); - } - - | FALSE_KW - { - $result = - Constant.build_boolean - ( - CONTEXT.get_origin_at - ( - ($FALSE_KW.getLine()), - ($FALSE_KW.getCharPositionInLine()) - ), - false - ); - } - - | AND_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($AND_KW.getLine()), - ($AND_KW.getCharPositionInLine()) - ), - Operator.AND, - ($non_text_value_list.result) - ); - } - - | OR_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($OR_KW.getLine()), - ($OR_KW.getCharPositionInLine()) - ), - Operator.OR, - ($non_text_value_list.result) - ); - } - - | ONE_IN_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($ONE_IN_KW.getLine()), - ($ONE_IN_KW.getCharPositionInLine()) - ), - Operator.ONE_IN, - ($non_text_value_list.result) - ); - } - - | NOT_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($NOT_KW.getLine()), - ($NOT_KW.getCharPositionInLine()) - ), - Operator.NOT, - ($non_text_value_list.result) - ); - } - - | IMPLIES_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($IMPLIES_KW.getLine()), - ($IMPLIES_KW.getCharPositionInLine()) - ), - Operator.IMPLIES, - ($non_text_value_list.result) - ); - } - - | LOWER_THAN_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($LOWER_THAN_KW.getLine()), - ($LOWER_THAN_KW.getCharPositionInLine()) - ), - Operator.LOWER_THAN, - ($non_text_value_list.result) - ); - } - - | LOWER_EQUAL_THAN_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($LOWER_EQUAL_THAN_KW.getLine()), - ($LOWER_EQUAL_THAN_KW.getCharPositionInLine()) - ), - Operator.LOWER_EQUAL_THAN, - ($non_text_value_list.result) - ); - } - - | EQUALS_KW value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($EQUALS_KW.getLine()), - ($EQUALS_KW.getCharPositionInLine()) - ), - Operator.EQUALS, - ($value_list.result) - ); - } - - | GREATER_EQUAL_THAN_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($GREATER_EQUAL_THAN_KW.getLine()), - ($GREATER_EQUAL_THAN_KW.getCharPositionInLine()) - ), - Operator.GREATER_EQUAL_THAN, - ($non_text_value_list.result) - ); - } - - | GREATER_THAN_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($GREATER_THAN_KW.getLine()), - ($GREATER_THAN_KW.getCharPositionInLine()) - ), - Operator.GREATER_THAN, - ($non_text_value_list.result) - ); - } - - | IS_MEMBER_KW value WS+ value_reference WS* R_PAREN - { - $result = - IsMemberOperator.build - ( - CONTEXT.get_origin_at - ( - ($IS_MEMBER_KW.getLine()), - ($IS_MEMBER_KW.getCharPositionInLine()) - ), - ($value.result), - ($value_reference.result) - ); - } - - | IS_EMPTY_KW value_reference WS* R_PAREN - { - $result = - IsEmpty.build - ( - CONTEXT.get_origin_at - ( - ($IS_EMPTY_KW.getLine()), - ($IS_EMPTY_KW.getCharPositionInLine()) - ), - ($value_reference.result) - ); - } - - | INDEX_OF_KW value WS+ value_reference WS* R_PAREN - { - $result = - IndexOfOperator.build - ( - CONTEXT.get_origin_at - ( - ($INDEX_OF_KW.getLine()), - ($INDEX_OF_KW.getCharPositionInLine()) - ), - ($value.result), - ($value_reference.result) - ); - } - - | SIZE_KW value_reference WS* R_PAREN - { - $result = - SizeOperator.build - ( - CONTEXT.get_origin_at - ( - ($SIZE_KW.getLine()), - ($SIZE_KW.getCharPositionInLine()) - ), - ($value_reference.result) - ); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -math_expression -returns [Computation result]: - PLUS_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($PLUS_KW.getLine()), - ($PLUS_KW.getCharPositionInLine()) - ), - Operator.PLUS, - ($non_text_value_list.result) - ); - } - - | MINUS_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($MINUS_KW.getLine()), - ($MINUS_KW.getCharPositionInLine()) - ), - Operator.MINUS, - ($non_text_value_list.result) - ); - } - - | MIN_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($MIN_KW.getLine()), - ($MIN_KW.getCharPositionInLine()) - ), - Operator.MIN, - ($non_text_value_list.result) - ); - } - - | MAX_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($MAX_KW.getLine()), - ($MAX_KW.getCharPositionInLine()) - ), - Operator.MAX, - ($non_text_value_list.result) - ); - } - - | CLAMP_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($CLAMP_KW.getLine()), - ($CLAMP_KW.getCharPositionInLine()) - ), - Operator.CLAMP, - ($non_text_value_list.result) - ); - } - - | ABS_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($ABS_KW.getLine()), - ($ABS_KW.getCharPositionInLine()) - ), - Operator.ABS, - ($non_text_value_list.result) - ); - } - - | MODULO_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($MODULO_KW.getLine()), - ($MODULO_KW.getCharPositionInLine()) - ), - Operator.MODULO, - ($non_text_value_list.result) - ); - } - - | TIMES_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($TIMES_KW.getLine()), - ($TIMES_KW.getCharPositionInLine()) - ), - Operator.TIMES, - ($non_text_value_list.result) - ); - } - - | DIVIDE_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($DIVIDE_KW.getLine()), - ($DIVIDE_KW.getCharPositionInLine()) - ), - Operator.DIVIDE, - ($non_text_value_list.result) - ); - } - - | POWER_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($POWER_KW.getLine()), - ($POWER_KW.getCharPositionInLine()) - ), - Operator.POWER, - ($non_text_value_list.result) - ); - } - - | RANDOM_KW non_text_value_list WS* R_PAREN - { - $result = - Operation.build - ( - CONTEXT.get_origin_at - ( - ($RANDOM_KW.getLine()), - ($RANDOM_KW.getCharPositionInLine()) - ), - Operator.RANDOM, - ($non_text_value_list.result) - ); - } - - | COUNT_KW value WS+ non_text_value WS* R_PAREN - { - $result = - CountOperator.build - ( - CONTEXT.get_origin_at - ( - ($COUNT_KW.getLine()), - ($COUNT_KW.getCharPositionInLine()) - ), - ($value.result), - ($non_text_value.result) - ); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -value -returns [Computation result] -: - WORD - { - $result = - Constant.build - ( - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ), - ($WORD.text) - ); - } - - | IGNORE_ERROR_KW WORD WS+ value WS* R_PAREN - { - $result = ($value.result); - /* TODO: temporarily disable an compiler error category */ - } - - | STRING_KW sentence WS* R_PAREN - { - $result = ($sentence.result); - } - - | L_PAREN WS+ sentence WS* R_PAREN - { - $result = ($sentence.result); - } - - | actual_text_value - { - $result = ($actual_text_value.result); - } - - | non_text_value - { - $result = ($non_text_value.result); - } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } -} - -non_text_value -returns [Computation result] -: - STRING_KW sentence WS* R_PAREN - { - $result = $sentence.result; - } - - | IF_ELSE_KW cond=non_text_value WS+ - if_true=value WS+ - if_false=value WS* - R_PAREN - { - $result = - IfElseValue.build - ( - CONTEXT.get_origin_at - ( - ($IF_ELSE_KW.getLine()), - ($IF_ELSE_KW.getCharPositionInLine()) - ), - ($cond.result), - ($if_true.result), - ($if_false.result) - ); - } - - | CONS_KW v0=value WS+ v1=value WS* R_PAREN - { - $result = - new ConsComputation - ( - CONTEXT.get_origin_at - ( - ($CONS_KW.getLine()), - ($CONS_KW.getCharPositionInLine()) - ), - ($v0.result), - ($v1.result) - ); - } - - | CAR_KW non_text_value WS* R_PAREN - { - $result = - CarCdr.build - ( - CONTEXT.get_origin_at - ( - ($CAR_KW.getLine()), - ($CAR_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - true - ); - } - - | CDR_KW non_text_value WS* R_PAREN - { - $result = - CarCdr.build - ( - CONTEXT.get_origin_at - ( - ($CDR_KW.getLine()), - ($CDR_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - false - ); - } - - | ACCESS_KW index=non_text_value WS+ collection=non_text_value WS* R_PAREN - { - $result = - Access.build - ( - CONTEXT.get_origin_at - ( - ($ACCESS_KW.getLine()), - ($ACCESS_KW.getCharPositionInLine()) - ), - ($collection.result), - ($index.result) - ); - } - - | ACCESS_POINTER_KW non_text_value WS+ value_reference WS* R_PAREN - { - $result = - AccessPointer.build - ( - CONTEXT.get_origin_at - ( - ($ACCESS_POINTER_KW.getLine()), - ($ACCESS_POINTER_KW.getCharPositionInLine()) - ), - ($value_reference.result), - ($non_text_value.result) - ); - } - - | FIELD_ACCESS_KW WORD WS+ non_text_value WS* R_PAREN - { - $result = - FieldAccess.build - ( - CONTEXT.get_origin_at - ( - ($FIELD_ACCESS_KW.getLine()), - ($FIELD_ACCESS_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - ($WORD.text) - ); - } - - | FOLDL_KW - fun=non_text_value WS+ - init=value WS+ - inr=non_text_value WS* - R_PAREN - { - $result = - Fold.build - ( - CONTEXT.get_origin_at - ( - ($FOLDL_KW.getLine()), - ($FOLDL_KW.getCharPositionInLine()) - ), - ($fun.result), - ($init.result), - ($inr.result), - true, - new ArrayList() - ); - } - - | FOLDL_KW - fun=non_text_value WS+ - init=value WS+ - inr=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - Fold.build - ( - CONTEXT.get_origin_at - ( - ($FOLDL_KW.getLine()), - ($FOLDL_KW.getCharPositionInLine()) - ), - ($fun.result), - ($init.result), - ($inr.result), - true, - ($value_list.result) - ); - } - - | FOLDR_KW - fun=non_text_value WS+ - init=value WS+ - inr=non_text_value WS* - R_PAREN - { - $result = - Fold.build - ( - CONTEXT.get_origin_at - ( - ($FOLDR_KW.getLine()), - ($FOLDR_KW.getCharPositionInLine()) - ), - ($fun.result), - ($init.result), - ($inr.result), - false, - new ArrayList() - ); - } - - | FOLDR_KW - fun=non_text_value WS+ - init=value WS+ - inr=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - Fold.build - ( - CONTEXT.get_origin_at - ( - ($FOLDR_KW.getLine()), - ($FOLDR_KW.getCharPositionInLine()) - ), - ($fun.result), - ($init.result), - ($inr.result), - false, - ($value_list.result) - ); - } - - | LIST_KW value_list WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($LIST_KW.getLine()), - ($LIST_KW.getCharPositionInLine()) - ); - - if ($value_list.result.size() == 0) - { - ErrorManager.handle(new InvalidArityException(origin, 0, 1, -1)); - } - - - $result = - new Default - ( - origin, - CollectionType.build - ( - origin, - ($value_list.result).get(0).get_type(), - false, - "Autogenerated List Type" - ) - ); - - for (final Computation val: $value_list.result) - { - $result = - AddElementComputation.build - ( - origin, - val, - $result - ); - } - } - - | SET_KW value_list WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($SET_KW.getLine()), - ($SET_KW.getCharPositionInLine()) - ); - - if ($value_list.result.size() == 0) - { - ErrorManager.handle(new InvalidArityException(origin, 0, 1, -1)); - } - - $result = - new Default - ( - origin, - CollectionType.build - ( - origin, - ($value_list.result).get(0).get_type(), - true, - "Autogenerated Set Type" - ) - ); - - for (final Computation val: $value_list.result) - { - $result = - AddElementComputation.build - ( - origin, - val, - $result - ); - } - } - - | RANGE_KW - vstart=non_text_value WS+ - vend=non_text_value WS+ - inc=non_text_value WS* - R_PAREN - { - $result = - Range.build - ( - CONTEXT.get_origin_at - ( - ($RANGE_KW.getLine()), - ($RANGE_KW.getCharPositionInLine()) - ), - ($vstart.result), - ($vend.result), - ($inc.result) - ); - } - - | DEFAULT_KW type WS* R_PAREN - { - $result = - new Default - ( - CONTEXT.get_origin_at - ( - ($DEFAULT_KW.getLine()), - ($DEFAULT_KW.getCharPositionInLine()) - ), - ($type.result) - ); - } - - | COND_KW value_cond_list WS* R_PAREN - { - $result = - CondValue.build - ( - CONTEXT.get_origin_at - ( - ($COND_KW.getLine()), - ($COND_KW.getCharPositionInLine()) - ), - ($value_cond_list.result) - ); - } - - | SWITCH_KW - target=value WS* - value_switch_list WS* - default_val=value WS* - R_PAREN - { - $result = - SwitchValue.build - ( - CONTEXT.get_origin_at - ( - ($SWITCH_KW.getLine()), - ($SWITCH_KW.getCharPositionInLine()) - ), - ($target.result), - ($value_switch_list.result), - ($default_val.result) - ); - } - - | boolean_expression - { - $result = ($boolean_expression.result); - } - - | LAMBDA_KW - L_PAREN WS* variable_list WS* R_PAREN - { - final Map variable_map; - - variable_map = new HashMap(); - - variable_map.putAll(($variable_list.result).as_map()); - - LOCAL_VARIABLES.push(variable_map); - } - WS* - value - WS* - R_PAREN - { - $result = - LambdaExpression.build - ( - CONTEXT.get_origin_at - ( - ($LAMBDA_KW.getLine()), - ($LAMBDA_KW.getCharPositionInLine()) - ), - ($variable_list.result).get_entries(), - ($value.result) - ); - - LOCAL_VARIABLES.pop(); - } - - | LET_KW - L_PAREN WS* let_variable_list WS* R_PAREN - WS* - value - WS* - R_PAREN - { - final List> let_list; - - let_list = ($let_variable_list.result); - - $result = - new Let - ( - CONTEXT.get_origin_at - ( - ($LET_KW.getLine()), - ($LET_KW.getCharPositionInLine()) - ), - let_list, - ($value.result) - ); - - for (final Cons entry: let_list) - { - LOCAL_VARIABLES.peekFirst().remove(entry.get_car().get_name()); - } - } - - | math_expression - { - $result = ($math_expression.result); - } - - | REF_KW value_reference WS* R_PAREN - { - $result = - new AddressOperator - ( - CONTEXT.get_origin_at - ( - ($REF_KW.getLine()), - ($REF_KW.getCharPositionInLine()) - ), - ($value_reference.result) - ); - } - - | CAST_KW WORD WS+ value WS* R_PAREN - { - final Origin target_type_origin; - final Type target_type; - - target_type_origin = - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ); - - target_type = WORLD.types().get(target_type_origin, ($WORD.text)); - - $result = - Cast.build - ( - CONTEXT.get_origin_at - ( - ($CAST_KW.getLine()), - ($CAST_KW.getCharPositionInLine()) - ), - target_type, - ($value.result), - false - ); - } - - | EXTRA_COMPUTATION_KW WORD WS+ value_list WS* R_PAREN - { - final Origin origin; - final ExtraComputation extra_computation; - - origin = - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ); - - extra_computation = WORLD.extra_computations().get(origin, ($WORD.text)); - - $result = - extra_computation.instantiate - ( - WORLD, - CONTEXT, - origin, - ($value_list.result) - ); - } - - | EXTRA_COMPUTATION_KW WORD WS* R_PAREN - { - final Origin origin; - final ExtraComputation extra_computation; - - origin = - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ); - - extra_computation = WORLD.extra_computations().get(origin, ($WORD.text)); - - $result = - extra_computation.instantiate - ( - WORLD, - CONTEXT, - origin, - new ArrayList() - ); - } - - | SEQUENCE_KW WORD WS* R_PAREN - { - final SequenceReference sr; - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($SEQUENCE_KW.getLine()), - ($SEQUENCE_KW.getCharPositionInLine()) - ); - - sr = new SequenceReference(origin, ($WORD.text)); - $result = sr; - - WORLD.add_sequence_variable(sr); - } - - | EVAL_KW value_reference WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($EVAL_KW.getLine()), - ($EVAL_KW.getCharPositionInLine()) - ); - - $result = - LambdaEvaluation.build - ( - origin, - ($value_reference.result), - new ArrayList() - ); - } - - | EVAL_KW value_reference WS+ value_list WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($EVAL_KW.getLine()), - ($EVAL_KW.getCharPositionInLine()) - ); - - $result = - LambdaEvaluation.build - ( - origin, - ($value_reference.result), - ($value_list.result) - ); - } - - | ADD_KW vall=value_list WS+ coll=non_text_value WS* R_PAREN - { - $result = ($coll.result); - - for (final Computation value: ($vall.result)) - { - $result = - AddElementComputation.build - ( - CONTEXT.get_origin_at - ( - ($ADD_KW.getLine()), - ($ADD_KW.getCharPositionInLine()) - ), - value, - $result - ); - } - } - - | ADD_AT_KW - index=non_text_value WS+ - element=value WS+ - coll=non_text_value WS* - R_PAREN - { - $result = - AddElementAtComputation.build - ( - CONTEXT.get_origin_at - ( - ($ADD_AT_KW.getLine()), - ($ADD_AT_KW.getCharPositionInLine()) - ), - ($index.result), - ($element.result), - ($coll.result) - ); - } - - | ADD_ALL_KW - sourcev=non_text_value WS+ - targetv=non_text_value WS* - R_PAREN - { - $result = - AddElementsOfComputation.build - ( - CONTEXT.get_origin_at - ( - ($ADD_ALL_KW.getLine()), - ($ADD_ALL_KW.getCharPositionInLine()) - ), - ($sourcev.result), - ($targetv.result) - ); - } - - | REMOVE_ONE_KW val=value WS+ coll=non_text_value WS* R_PAREN - { - $result = - RemoveElementComputation.build - ( - CONTEXT.get_origin_at - ( - ($REMOVE_ONE_KW.getLine()), - ($REMOVE_ONE_KW.getCharPositionInLine()) - ), - ($val.result), - ($coll.result) - ); - } - - | REMOVE_AT_KW val=value WS+ coll=non_text_value WS* R_PAREN - { - $result = - RemoveElementAtComputation.build - ( - CONTEXT.get_origin_at - ( - ($REMOVE_AT_KW.getLine()), - ($REMOVE_AT_KW.getCharPositionInLine()) - ), - ($val.result), - ($coll.result) - ); - } - | REMOVE_ALL_KW val=value WS+ coll=non_text_value WS* R_PAREN - { - $result = - RemoveAllOfElementComputation.build - ( - CONTEXT.get_origin_at - ( - ($REMOVE_ALL_KW.getLine()), - ($REMOVE_ALL_KW.getCharPositionInLine()) - ), - ($val.result), - ($coll.result) - ); + $result = t.build(type_list); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | REVERSE_KW non_text_value WS* R_PAREN +type_list +returns [List result] +@init +{ + $result = new ArrayList(); +} +: + type { - $result = - ReverseListComputation.build - ( - CONTEXT.get_origin_at - ( - ($REVERSE_KW.getLine()), - ($REVERSE_KW.getCharPositionInLine()) - ), - ($non_text_value.result) - ); + $result.add(($type.result)); } - - | PUSH_LEFT_KW val=value WS+ coll=non_text_value WS* R_PAREN + (WS+ + type + { + $result.add(($type.result)); + } + )* { - $result = - PushElementComputation.build - ( - CONTEXT.get_origin_at - ( - ($PUSH_LEFT_KW.getLine()), - ($PUSH_LEFT_KW.getCharPositionInLine()) - ), - ($val.result), - ($coll.result), - true - ); } +; - | PUSH_RIGHT_KW val=value WS+ coll=non_text_value WS* R_PAREN +maybe_type_list +returns [List result] +@init +{ + $result = new ArrayList(); +} +: + (WS+ + type + { + $result.add(($type.result)); + } + )* { - $result = - PushElementComputation.build - ( - CONTEXT.get_origin_at - ( - ($PUSH_RIGHT_KW.getLine()), - ($PUSH_RIGHT_KW.getCharPositionInLine()) - ), - ($val.result), - ($coll.result), - false - ); } +; - | POP_LEFT_KW non_text_value WS* R_PAREN - { - $result = - PopElementComputation.build - ( - CONTEXT.get_origin_at - ( - ($POP_LEFT_KW.getLine()), - ($POP_LEFT_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - true - ); - } +let_variable_list +returns [List> result] +@init +{ + String var_name; - | POP_RIGHT_KW non_text_value WS* R_PAREN - { - $result = - PopElementComputation.build - ( - CONTEXT.get_origin_at - ( - ($POP_RIGHT_KW.getLine()), - ($POP_RIGHT_KW.getCharPositionInLine()) - ), - ($non_text_value.result), - false - ); - } + var_name = null; - | MAP_KW fun=non_text_value WS+ inv=non_text_value WS* R_PAREN - { - $result = - tonkadur.fate.v1.lang.computation.MapComputation.build + $result = new ArrayList>(); +} +: + ( + WS* ( - CONTEXT.get_origin_at ( - ($MAP_KW.getLine()), - ($MAP_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv.result), - new ArrayList() - ); - } - - | MAP_KW fun=non_text_value WS+ inv=non_text_value WS+ value_list WS* R_PAREN - { - $result = - tonkadur.fate.v1.lang.computation.MapComputation.build - ( - CONTEXT.get_origin_at + L_PAREN WS* identifier + { + var_name = ($identifier.result); + } + ) + | ( - ($MAP_KW.getLine()), - ($MAP_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv.result), - ($value_list.result) - ); - } + something_else=. + { + var_name = ($something_else.text).substring(1).trim(); + } + ) + ) + WS+ value WS* R_PAREN + { + final Variable v; - | INDEXED_MAP_KW fun=non_text_value WS+ inv=non_text_value WS* R_PAREN - { - $result = - IndexedMapComputation.build - ( - CONTEXT.get_origin_at + v = + new Variable ( - ($INDEXED_MAP_KW.getLine()), - ($INDEXED_MAP_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv.result), - new ArrayList() - ); - } + PARSER.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) + ), + ($value.result).get_type(), + var_name, + false + ); - | INDEXED_MAP_KW - fun=non_text_value WS+ - inv=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - IndexedMapComputation.build - ( - CONTEXT.get_origin_at + if (PARSER.current_context_variable_level_has(var_name)) + { + ErrorManager.handle ( - ($INDEXED_MAP_KW.getLine()), - ($INDEXED_MAP_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv.result), - ($value_list.result) - ); - } + new DuplicateLocalVariableException + ( + variables.get(var_name), + v + ) + ); + } - | INDEXED_MERGE_TO_LIST_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS* - R_PAREN - { - $result = - IndexedMergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($INDEXED_MERGE_TO_LIST_KW.getLine()), - ($INDEXED_MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - false, - new ArrayList() - ); - } + PARSER.add_context_variable(v); - | INDEXED_MERGE_TO_LIST_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN + $result.add(new Cons(v, ($value.result))); + } + )* { - $result = - IndexedMergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($INDEXED_MERGE_TO_LIST_KW.getLine()), - ($INDEXED_MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - false, - ($value_list.result) - ); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | SAFE_INDEXED_MERGE_TO_LIST_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS* - R_PAREN - { - $result = - IndexedMergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($SAFE_INDEXED_MERGE_TO_LIST_KW.getLine()), - ($SAFE_INDEXED_MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - false, - new ArrayList() - ); - } +choice_for_update_variable_list +returns [List result] +@init +{ + Collection allowed_variables; + String var_name; + Origin origin; - | SAFE_INDEXED_MERGE_TO_LIST_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - IndexedMergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($SAFE_INDEXED_MERGE_TO_LIST_KW.getLine()), - ($SAFE_INDEXED_MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - false, - ($value_list.result) - ); - } + allowed_variables = PARSER.get_choice_limited_variables().peek(); + var_name = null; + origin = null; - | MERGE_TO_LIST_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS* - R_PAREN - { - $result = - MergeComputation.build + $result = new ArrayList(); +} +: + ( + WS* ( - CONTEXT.get_origin_at ( - ($MERGE_TO_LIST_KW.getLine()), - ($MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - false, - new ArrayList() - ); - } - - | MERGE_TO_LIST_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - MergeComputation.build - ( - CONTEXT.get_origin_at + L_PAREN WS* identifier + { + var_name = ($identifier.result); + origin = + PARSER.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) + ); + } + ) + | ( - ($MERGE_TO_LIST_KW.getLine()), - ($MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - false, - ($value_list.result) - ); - } - - | SAFE_MERGE_TO_LIST_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS* - R_PAREN - { - $result = - MergeComputation.build + something_else=. + { + var_name = ($something_else.text).substring(1).trim(); + origin = + PARSER.get_origin_at + ( + ($something_else.getLine()), + ($something_else.getCharPositionInLine()) + ); + } + ) + ) + WS+ computation WS* R_PAREN + { + $result.add ( - CONTEXT.get_origin_at + SetValue.build ( - ($SAFE_MERGE_TO_LIST_KW.getLine()), - ($SAFE_MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - false, - new ArrayList() + origin, + ($computation.result), + VariableFromWord.generate + ( + PARSER, + origin, + var_name + ) + ) ); - } - | SAFE_MERGE_TO_LIST_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - MergeComputation.build - ( - CONTEXT.get_origin_at + if (!allowed_variables.contains(var_name)) + { + ErrorManager.handle ( - ($SAFE_MERGE_TO_LIST_KW.getLine()), - ($SAFE_MERGE_TO_LIST_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - false, - ($value_list.result) - ); - } - - | INDEXED_MERGE_TO_SET_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS* - R_PAREN + new UpdatingIllegalVariableFromChoiceException(origin, var_name) + ); + } + } + )* { - $result = - IndexedMergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($INDEXED_MERGE_TO_SET_KW.getLine()), - ($INDEXED_MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - true, - new ArrayList() - ); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | INDEXED_MERGE_TO_SET_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - IndexedMergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($INDEXED_MERGE_TO_SET_KW.getLine()), - ($INDEXED_MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - true, - ($value_list.result) - ); - } +choice_for_variable_list +returns [List result] +@init +{ + Collection allowed_variables; + String var_name; + Origin origin; - | SAFE_INDEXED_MERGE_TO_SET_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS* - R_PAREN - { - $result = - IndexedMergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($SAFE_INDEXED_MERGE_TO_SET_KW.getLine()), - ($SAFE_INDEXED_MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - true, - new ArrayList() - ); - } + allowed_variables = PARSER.get_choice_limited_variables().peek(); + var_name = null; + origin = null; - | SAFE_INDEXED_MERGE_TO_SET_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - IndexedMergeComputation.build + $result = new ArrayList(); +} +: + ( + WS* ( - CONTEXT.get_origin_at ( - ($SAFE_INDEXED_MERGE_TO_SET_KW.getLine()), - ($SAFE_INDEXED_MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - true, - ($value_list.result) - ); - } - - | MERGE_TO_SET_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS* - R_PAREN - { - $result = - MergeComputation.build - ( - CONTEXT.get_origin_at + L_PAREN WS* identifier + { + var_name = ($identifier.result); + origin = + PARSER.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) + ); + } + ) + | ( - ($MERGE_TO_SET_KW.getLine()), - ($MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - true, - new ArrayList() - ); - } - - | MERGE_TO_SET_KW - fun=non_text_value WS+ - inv0=non_text_value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - MergeComputation.build + something_else=. + { + var_name = ($something_else.text).substring(1).trim(); + origin = + PARSER.get_origin_at + ( + ($something_else.getLine()), + ($something_else.getCharPositionInLine()) + ); + } + ) + ) + WS+ computation WS* R_PAREN + { + $result.add ( - CONTEXT.get_origin_at + SetValue.build ( - ($MERGE_TO_SET_KW.getLine()), - ($MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - null, - ($inv1.result), - null, - true, - ($value_list.result) + origin, + ($computation.result), + VariableFromWord.generate(PARSER, origin, var_name) + ) ); - } - | SAFE_MERGE_TO_SET_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS* - R_PAREN + allowed_variables.add(var_name); + } + )* { - $result = - MergeComputation.build - ( - CONTEXT.get_origin_at - ( - ($SAFE_MERGE_TO_SET_KW.getLine()), - ($SAFE_MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - true, - new ArrayList() - ); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | SAFE_MERGE_TO_SET_KW - fun=non_text_value WS+ - def0=value WS+ - inv0=non_text_value WS+ - def1=value WS+ - inv1=non_text_value WS+ - value_list WS* - R_PAREN - { - $result = - MergeComputation.build +variable_list +returns [VariableList result] +@init +{ + Type next_type; + Origin origin; + + next_type = null; + origin = null; + + $result = new VariableList(); +} +: + ( + WS* + ( ( - CONTEXT.get_origin_at ( - ($SAFE_MERGE_TO_SET_KW.getLine()), - ($SAFE_MERGE_TO_SET_KW.getCharPositionInLine()) - ), - ($fun.result), - ($inv0.result), - ($def0.result), - ($inv1.result), - ($def1.result), - true, - ($value_list.result) - ); - } + L_PAREN WS* type WS+ + ) + { + origin = + PARSER.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) + ); + next_type = ($type.result); + } + ) + | + ( + something_else=. + { + origin = + PARSER.get_origin_at + ( + ($something_else.getLine()), + ($something_else.getCharPositionInLine()) + ); - | SUB_LIST_KW - vstart=non_text_value WS+ - vend=non_text_value WS+ - inv=non_text_value WS* - R_PAREN - { - $result = - SubListComputation.build + next_type = + PARSER.get_world().types().get + ( + origin, + ($something_else.text).substring(1).trim() + ); + } + ) + ) + WS* identifier WS* R_PAREN + { + $result.add ( - CONTEXT.get_origin_at + new Variable ( - ($SUB_LIST_KW.getLine()), - ($SUB_LIST_KW.getCharPositionInLine()) - ), - ($vstart.result), - ($vend.result), - ($inv.result) + origin, + next_type, + ($identifier.result), + false + ) ); + } + )* + { } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} - | FILTER_KW fun=non_text_value WS+ coll=non_text_value WS* R_PAREN - { - $result = - FilterComputation.build +field_value_list +returns [List>> result] +@init +{ + String field_name; + + field_name = null; + + $result = new ArrayList>>(); +} +: + ( + WS* + ( + ( + L_PAREN WS* WORD WS+ + { + field_name = ($WORD.text); + } + ) + | + ( + something_else=. + { + field_name = ($something_else.text).substring(1).trim(); + } + ) + ) + computation WS* R_PAREN + { + $result.add ( - CONTEXT.get_origin_at + new Cons ( - ($FILTER_KW.getLine()), - ($FILTER_KW.getCharPositionInLine()) - ), - ($fun.result), - ($coll.result), - new ArrayList() + PARSER.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) + ), + new Cons(field_name, ($computation.result)) + ) ); + } + )* + { } +; - | FILTER_KW - fun=non_text_value WS+ - coll=non_text_value WS+ - value_list WS* - R_PAREN +identifier +returns [String result] +: + WORD { - $result = - FilterComputation.build + if (($WORD.text).indexOf('.') != -1) + { + ErrorManager.handle ( - CONTEXT.get_origin_at + new IllegalReferenceNameException ( - ($FILTER_KW.getLine()), - ($FILTER_KW.getCharPositionInLine()) - ), - ($fun.result), - ($coll.result), - ($value_list.result) + PARSER.get_origin_at + ( + ($WORD.getLine()), + ($WORD.getCharPositionInLine()) + ), + ($WORD.text) + ) ); + } + + $result = ($WORD.text); } +; +catch [final Throwable e] +{ + PARSER.handle_error(e); +} + +/******************************************************************************/ +/**** VALUES ******************************************************************/ +/******************************************************************************/ - | INDEXED_FILTER_KW fun=non_text_value WS+ coll=non_text_value WS* R_PAREN +computation +returns [Computation result] +@init +{ + Parser.LocalVariables previous_local_variables_stack; +} +: + WORD { $result = - IndexedFilterComputation.build + AmbiguousWord.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($INDEXED_FILTER_KW.getLine()), - ($INDEXED_FILTER_KW.getCharPositionInLine()) + ($WORD.getLine()), + ($WORD.getCharPositionInLine()) ), - ($fun.result), - ($coll.result), - new ArrayList() + ($WORD.text) ); } - | INDEXED_FILTER_KW - fun=non_text_value WS+ - coll=non_text_value WS+ - value_list WS* - R_PAREN + | IGNORE_ERROR_KW WORD WS+ computation WS* R_PAREN { - $result = - IndexedFilterComputation.build - ( - CONTEXT.get_origin_at - ( - ($INDEXED_FILTER_KW.getLine()), - ($INDEXED_FILTER_KW.getCharPositionInLine()) - ), - ($fun.result), - ($coll.result), - ($value_list.result) - ); + $result = ($computation.result); + /* TODO: temporarily disable an compiler error category */ } - | PARTITION_KW fun=non_text_value WS+ coll=non_text_value WS* R_PAREN + | STRING_KW sentence WS* R_PAREN { - $result = - PartitionComputation.build - ( - CONTEXT.get_origin_at - ( - ($PARTITION_KW.getLine()), - ($PARTITION_KW.getCharPositionInLine()) - ), - ($fun.result), - ($coll.result), - new ArrayList() - ); + $result = ($sentence.result); } - | PARTITION_KW - fun=non_text_value WS+ - coll=non_text_value WS+ - value_list WS* - R_PAREN + | L_PAREN WS+ sentence WS* R_PAREN { - $result = - PartitionComputation.build - ( - CONTEXT.get_origin_at - ( - ($PARTITION_KW.getLine()), - ($PARTITION_KW.getCharPositionInLine()) - ), - ($fun.result), - ($coll.result), - ($value_list.result) - ); + $result = ($sentence.result); } - | INDEXED_PARTITION_KW fun=non_text_value WS+ coll=non_text_value WS* R_PAREN + | FIELD_ACCESS_KW WORD WS+ computation WS* R_PAREN { $result = - IndexedPartitionComputation.build + FieldAccess.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($INDEXED_PARTITION_KW.getLine()), - ($INDEXED_PARTITION_KW.getCharPositionInLine()) + ($FIELD_ACCESS_KW.getLine()), + ($FIELD_ACCESS_KW.getCharPositionInLine()) ), - ($fun.result), - ($coll.result), - new ArrayList() + ($computation.result), + ($WORD.text) ); } - | INDEXED_PARTITION_KW - fun=non_text_value WS+ - coll=non_text_value WS+ - value_list WS* - R_PAREN + | DEFAULT_KW type WS* R_PAREN { $result = - IndexedPartitionComputation.build + new Default ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($INDEXED_PARTITION_KW.getLine()), - ($INDEXED_PARTITION_KW.getCharPositionInLine()) + ($DEFAULT_KW.getLine()), + ($DEFAULT_KW.getCharPositionInLine()) ), - ($fun.result), - ($coll.result), - ($value_list.result) + ($type.result) ); } - | SORT_KW fun=non_text_value WS+ coll=non_text_value WS* R_PAREN + | COND_KW computation_cond_list WS* R_PAREN { $result = - SortComputation.build + CondValue.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($SORT_KW.getLine()), - ($SORT_KW.getCharPositionInLine()) + ($COND_KW.getLine()), + ($COND_KW.getCharPositionInLine()) ), - ($fun.result), - ($coll.result), - new ArrayList() + ($computation_cond_list.result) ); } - | SORT_KW - fun=non_text_value WS+ - coll=non_text_value WS+ - value_list WS* + | SWITCH_KW + target=computation WS* + computation_switch_list WS* + default_val=computation WS* R_PAREN { $result = - SortComputation.build + SwitchValue.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($SORT_KW.getLine()), - ($SORT_KW.getCharPositionInLine()) + ($SWITCH_KW.getLine()), + ($SWITCH_KW.getCharPositionInLine()) ), - ($fun.result), - ($coll.result), - ($value_list.result) + ($target.result), + ($computation_switch_list.result), + ($default_val.result) ); } - | DICT_TO_LIST_KW non_text_value WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | DICT_FROM_LIST_KW non_text_value WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | DICT_KEYS_KW non_text_value WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | DICT_VALUES_KW non_text_value WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | DICT_MERGE_KW - dict0=non_text_value WS+ - dict1=non_text_value WS+ - fun=non_text_value WS* - R_PAREN - { - /* TODO */ - $result = null; - } - - | DICT_MERGE_KW - dict0=non_text_value WS+ - dict1=non_text_value WS+ - fun=non_text_value WS+ - value_list WS* + | LAMBDA_KW + { + previous_local_variables_stack = PARSER.get_local_variables_stack(); + PARSER.discard_local_variables_stack(); + PARSER.increase_local_variables_hierarchy(); + } + L_PAREN WS* variable_list WS* R_PAREN + { + PARSER.add_local_variables(($variable_lists.result).as_map()); + } + WS* + computation + WS* R_PAREN - { - /* TODO */ - $result = null; - } + { + PARSER.restore_local_variables_stack(previous_local_variables_stack); - | DICT_FILTER_KW - dict0=non_text_value WS+ - fun=non_text_value WS* - R_PAREN - { - /* TODO */ - $result = null; - } + $result = + LambdaExpression.build + ( + PARSER.get_origin_at + ( + ($LAMBDA_KW.getLine()), + ($LAMBDA_KW.getCharPositionInLine()) + ), + ($variable_list.result).get_entries(), + ($computation.result) + ); + } - | DICT_FILTER_KW - dict0=non_text_value WS+ - fun=non_text_value WS+ - value_list WS* + | LET_KW + { + PARSER.increase_local_variables_hierarchy(); + } + L_PAREN WS* let_variable_list WS* R_PAREN + WS* + computation + WS* R_PAREN - { - /* TODO */ - $result = null; - } - - | DICT_SET_KW key=value WS+ val=value WS+ dict0=non_text_value WS* R_PAREN - { - /* TODO */ - $result = null; - } + { + final List> let_list; - | DICT_REMOVE_KW key=value WS+ dict0=non_text_value WS* R_PAREN - { - /* TODO */ - $result = null; - } + PARSER.decrease_local_variables_hierarchy(); - | DICT_HAS_KW key=value WS+ dict0=non_text_value WS* R_PAREN - { + let_list = ($let_variable_list.result); - /* TODO */ - $result = null; - } + $result = + new Let + ( + PARSER.get_origin_at + ( + ($LET_KW.getLine()), + ($LET_KW.getCharPositionInLine()) + ), + let_list, + ($computation.result) + ); + } - | DICT_GET_KW key=value WS+ dict0=non_text_value WS* R_PAREN + | CAST_KW type WS+ computation WS* R_PAREN { + final Origin target_type_origin; + final Type target_type; - /* TODO */ - $result = null; - } - - | DICT_GET_POINTER_KW key=value WS+ dict0=non_text_value WS* R_PAREN - { + target_type_origin = + PARSER.get_origin_at + ( + ($WORD.getLine()), + ($WORD.getCharPositionInLine()) + ); - /* TODO */ - $result = null; - } + target_type = + PARSER.get_world().types().get(target_type_origin, ($WORD.text)); - | SHUFFLE_KW non_text_value WS* R_PAREN - { $result = - ShuffleComputation.build + Cast.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($SHUFFLE_KW.getLine()), - ($SHUFFLE_KW.getCharPositionInLine()) + ($CAST_KW.getLine()), + ($CAST_KW.getCharPositionInLine()) ), - ($non_text_value.result) + target_type, + ($computation.result), + false ); } - | SET_FIELDS_KW non_text_value WS* field_value_list WS* R_PAREN + | SET_FIELDS_KW computation WS* field_computation_list WS* R_PAREN { /* * A bit of a lazy solution: build field references, then extract the data @@ -5827,7 +2119,7 @@ returns [Computation result] for ( final Cons> entry: - ($field_value_list.result) + ($field_computation_list.result) ) { final FieldReference fr; @@ -5837,7 +2129,7 @@ returns [Computation result] FieldReference.build ( entry.get_car(), - ($non_text_value.result), + ($computation.result), entry.get_cdr().get_car() ); @@ -5851,24 +2143,24 @@ returns [Computation result] $result = new SetFieldsComputation ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($SET_FIELDS_KW.getLine()), ($SET_FIELDS_KW.getCharPositionInLine()) ), - ($non_text_value.result), + ($computation.result), assignments ); } - | WORD + | ENABLE_TEXT_EFFECT_KW WORD WS+ paragraph WS* R_PAREN { - $result = null; + final TextEffect effect; - $result = - Constant.build_if_non_string + effect = + PARSER.get_world().text_effects().get ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( ($WORD.getLine()), ($WORD.getCharPositionInLine()) @@ -5876,103 +2168,76 @@ returns [Computation result] ($WORD.text) ); - if ($result == null) - { - $result = - VariableFromWord.generate + $result = + TextWithEffect.build + ( + PARSER.get_origin_at ( - WORLD, - LOCAL_VARIABLES, - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ), - ($WORD.text) - ); - } - } - - | value_reference - { - $result = ($value_reference.result); + ($WORD.getLine()), + ($WORD.getCharPositionInLine()) + ), + effect, + new ArrayList(), + ($paragraph.result) + ); } -; -catch [final Throwable e] -{ - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else + | ENABLE_TEXT_EFFECT_KW + L_PAREN + WORD WS+ + value_list WS* + R_PAREN WS+ + paragraph WS* + R_PAREN { - throw new ParseCancellationException(e); - } -} + final TextEffect effect; -value_reference -returns [Reference result] -: - AT_KW non_text_value WS* R_PAREN - { - $result = - AtReference.build + effect = + PARSER.get_world().text_effects().get ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($AT_KW.getLine()), - ($AT_KW.getCharPositionInLine()) + ($WORD.getLine()), + ($WORD.getCharPositionInLine()) ), - ($non_text_value.result) + ($WORD.text) ); - } - | FIELD_KW WORD WS+ value_reference WS* R_PAREN - { $result = - FieldReference.build + TextWithEffect.build ( - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($FIELD_KW.getLine()), - ($FIELD_KW.getCharPositionInLine()) + ($ENABLE_TEXT_EFFECT_KW.getLine()), + ($ENABLE_TEXT_EFFECT_KW.getCharPositionInLine()) ), - ($value_reference.result), - ($WORD.text) + effect, + ($value_list.result), + ($paragraph.result) ); } - - | (WORD | (VARIABLE_KW WORD WS* R_PAREN)) + | L_PAREN identifier maybe_computation_list WS* R_PAREN { $result = - VariableFromWord.generate + GenericComputation.build ( - WORLD, - LOCAL_VARIABLES, - CONTEXT.get_origin_at + PARSER.get_origin_at ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) ), - ($WORD.text) + ($identifier.result), + ($maybe_computation_list.result) ); } ; catch [final Throwable e] { - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } + PARSER.handle_error(e); } -value_cond_list +computation_cond_list returns [List> result] @init { @@ -5987,7 +2252,7 @@ returns [List> result] ( ( ( - L_PAREN WS* c=non_text_value WS+ + L_PAREN WS* c=computation WS+ ) { condition = ($c.result); @@ -6000,9 +2265,8 @@ returns [List> result] condition = VariableFromWord.generate ( - WORLD, - LOCAL_VARIABLES, - CONTEXT.get_origin_at + PARSER, + PARSER.get_origin_at ( ($something_else.getLine()), ($something_else.getCharPositionInLine()) @@ -6012,7 +2276,7 @@ returns [List> result] } ) ) - v=value WS* R_PAREN WS* + v=computation WS* R_PAREN WS* { $result.add(new Cons(condition, ($v.result))); } @@ -6022,17 +2286,10 @@ returns [List> result] ; catch [final Throwable e] { - if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) - { - throw new ParseCancellationException(CONTEXT.toString() + ((e.getMessage() == null) ? "" : e.getMessage()), e); - } - else - { - throw new ParseCancellationException(e); - } + PARSER.handle_error(e); } -value_switch_list +computation_switch_list returns [List> result] @init { @@ -6040,7 +2297,7 @@ returns [List> result] } : ( - L_PAREN WS* c=value WS+ v=value WS* R_PAREN WS* + L_PAREN WS* c=computation WS+ v=computation WS* R_PAREN WS* { $result.add(new Cons(($c.result), ($v.result))); } @@ -6049,30 +2306,7 @@ returns [List> result] } ; -value_list -returns [List result] -@init -{ - $result = new ArrayList(); -} -: - ( - value - { - ($result).add(($value.result)); - } - )* - (WS+ - value - { - ($result).add(($value.result)); - } - )* - { - } -; - -non_text_value_list +maybe_computation_list returns [List result] @init { @@ -6080,15 +2314,15 @@ returns [List result] } : ( - non_text_value + computation { - ($result).add(($non_text_value.result)); + ($result).add(($computation.result)); } )* (WS+ - non_text_value + computation { - ($result).add(($non_text_value.result)); + ($result).add(($computation.result)); } )* { diff --git a/src/core/src/tonkadur/fate/v1/parser/Parser.java b/src/core/src/tonkadur/fate/v1/parser/Parser.java new file mode 100644 index 0000000..d746fb5 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/parser/Parser.java @@ -0,0 +1,229 @@ +package tonkadur.fate.v1.parser; + +import java.io.IOException; + +import java.util.Collection; +import java.util.Deque; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; + +import tonkadur.parser.Context; +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.lang.Variable; +import tonkadur.fate.v1.lang.World; + +public class Parser +{ + final World world; + final Context context; + + LocalVariables local_variables; + + int breakable_levels; + int continue_levels; + + public Parser (final World world) + { + this.world = world; + + local_variables = new LocalVariables(); + + breakable_levels = 0; + continue_levels = 0; + + context = new Context(); + } + + public Origin get_origin_at (final int line, final int column) + { + return context.get_origin_at(line, column); + } + + public World get_world () + { + return world; + } + + public Context get_context () + { + return context; + } + + /**** BREAKABLE LEVELS *****************************************************/ + public boolean can_use_break () + { + return (breakable_levels > 0); + } + + public void increment_breakable_levels () + { + breakable_levels++; + } + + public void decrement_breakable_levels () + { + breakable_levels--; + } + + /**** CONTINUE LEVELS ******************************************************/ + public boolean can_use_continue () + { + return (continue_levels > 0); + } + + public void increment_continue_levels () + { + continue_levels++; + } + + public void decrement_continue_levels () + { + continue_levels--; + } + + /**** LOCAL VARIABLES ******************************************************/ + public Variable maybe_get_local_variable (final String name) + { + for (final Map level: context_variables) + { + final Variable v; + + v = level.get(name); + + if (v != null) + { + return v; + } + } + + return null; + } + + public void add_local_variables (final Map collection) + throws Trowable + { + for (final Variable variable: collection.values()) + { + add_local_variable(variable); + } + } + + public void add_local_variable (final Variable variable) + throws Trowable + { + final Map current_hierarchy = local_variables.peek(); + final Variable previous_entry; + + previous_entry = current_hierarchy.get(variable.get_name()); + + if (previous_entry != null) + { + ErrorManager.handle + ( + new DuplicateLocalVariableException + ( + previous_entry, + variable + ) + ); + } + + current_hierarchy.put(variable.get_name(), variable); + } + + public void increase_local_variables_hierarchy () + { + local_variables.push(new HashMap()); + } + + public void decrease_local_variables_hierarchy () + { + local_variables.pop(); + } + + /* I don't think it's needed ATM. */ + public Collection get_local_variables_at_current_hierarchy () + { + return local_variables.peek().values(); + } + + public LocalVariables get_local_variables_stack () + { + return local_variables; + } + + public void restore_local_variables_stack + ( + final LocalVariables local_variables + ) + { + this.local_variables = local_variables; + } + + public void discard_local_variables_stack () + { + local_variables = new LocalVariables(); + } + + /**** ERROR HANDLING *******************************************************/ + public void handle_error (final Throwable e) + { + if ((e.getMessage() == null) || !e.getMessage().startsWith("Require")) + { + kthrow + new ParseCancellationException + ( + ( + context.toString() + + ((e.getMessage() == null) ? "" : e.getMessage()) + ), + e + ); + } + else + { + throw new ParseCancellationException(e); + } + } + + public void add_file_content (final Origin origin, final String filename) + throws IOException + { + final CommonTokenStream tokens; + final TinyFateLexer lexer; + final TinyFateParser parser; + + lexer = new TinyFateLexer(CharStreams.fromFileName(filename)); + tokens = new CommonTokenStream(lexer); + parser = new TinyFateParser(tokens); + + if (origin != null) + { + context.push(origin); + } + + parser.fate_file(this); + + if (origin != null) + { + context.pop(); + } + + world.add_loaded_file(filename); + + if (parser.getNumberOfSyntaxErrors() > 0) + { + throw new IOException("There were syntaxic errors in " + filename); + } + } + + public static class LocalVariables extends Deque> + { + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java index 405d32a..0936e19 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java @@ -471,37 +471,6 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor } } - @Override - public void visit_field_reference - ( - final tonkadur.fate.v1.lang.computation.FieldReference n - ) - throws Throwable - { - final ComputationCompiler n_cc; - - n_cc = new ComputationCompiler(compiler); - - n.get_parent().get_visited_by(n_cc); - - assimilate(n_cc); - - result_as_address = - new RelativeAddress - ( - n_cc.get_address(), - new Constant(Type.STRING, n.get_field_name()), - TypeCompiler.compile - ( - compiler, - ( - (tonkadur.fate.v1.lang.type.StructType) - n.get_parent().get_type() - ).get_field_type(null, n.get_field_name()) - ) - ); - } - @Override public void visit_field_access ( -- cgit v1.2.3-70-g09d2