From 93def7ac6f413142a1c8d612cbdf312fa8bd94ea Mon Sep 17 00:00:00 2001 From: Andreas <38031952+AnHeuermann@users.noreply.github.com> Date: Wed, 17 Apr 2024 12:53:19 +0200 Subject: [PATCH] Adding rules for latest MetaModelica used by OpenModelica (#6) * Adding subtypeof * Adding threaded for keyword * Adding input output type prefix * Adding guard in match * Allowing empty expression list in braces. * Match else case algorithm section * Resolving positional and named arguments error * Adding polymorphism * Rename component_reference__function_call * Adding pure/inpure * Add $overload * Fixing external_clause * Fixing pure identifier name * Adding some parts of CODE * Bumping version --- .vscode/tasks.json | 19 +++ examples/test.mo | 4 + grammar.js | 7 + package-lock.json | 10 +- package.json | 2 +- queries/highlights.scm | 22 ++- rules/a1-classAndMainGrammar.js | 42 +++-- rules/a2-extends.js | 26 ++- rules/a5-expressions.js | 70 ++++++-- rules/a6-metamodelicaExtensions.js | 27 ++- rules/keywords.js | 7 +- test/corpus/a1-10_external.txt | 79 +++++++++ test/corpus/a1-7_class_specifier.txt | 103 +++++++++++ test/corpus/a1-8_class_type_impure.txt | 113 +++++++++++++ test/corpus/a1-9_overload.txt | 64 +++++++ test/corpus/a2-2_type_prefix.txt | 51 ++++++ test/corpus/a4-2_algorithm_clause2.txt | 6 +- test/corpus/a5-1_for_expression_threaded.txt | 117 +++++++++++++ test/corpus/a5-2_function_arguments.txt | 169 +++++++++++++++++++ test/corpus/a5-3_polymorphicType.txt | 107 ++++++++++++ test/corpus/a5-4_pure_function.txt | 68 ++++++++ test/corpus/a6-1_match_expression1.txt | 14 +- test/corpus/a6-2_match_expression2.txt | 22 +-- test/corpus/a6-3_match_expression3.txt | 50 ++++-- test/corpus/a6-4_match_expression4.txt | 154 +++++++++++++++++ test/corpus/a6-4try.txt | 8 +- test/highlight/Polymorphism.mo | 27 +++ 27 files changed, 1299 insertions(+), 89 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 examples/test.mo create mode 100644 test/corpus/a1-10_external.txt create mode 100644 test/corpus/a1-7_class_specifier.txt create mode 100644 test/corpus/a1-8_class_type_impure.txt create mode 100644 test/corpus/a1-9_overload.txt create mode 100644 test/corpus/a2-2_type_prefix.txt create mode 100644 test/corpus/a5-1_for_expression_threaded.txt create mode 100644 test/corpus/a5-2_function_arguments.txt create mode 100644 test/corpus/a5-3_polymorphicType.txt create mode 100644 test/corpus/a5-4_pure_function.txt create mode 100644 test/corpus/a6-4_match_expression4.txt create mode 100644 test/highlight/Polymorphism.mo diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..54b8828 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,19 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Generate parser", + "type": "shell", + "command": "npx tree-sitter generate", + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "reveal": "silent", + "panel": "dedicated", + "showReuseMessage": false, + } + } + ] +} diff --git a/examples/test.mo b/examples/test.mo new file mode 100644 index 0000000..ac52cde --- /dev/null +++ b/examples/test.mo @@ -0,0 +1,4 @@ +pure function foo +algorithm + pure := foldExp(fn, function checkPureCall(fn = fn), true); +end foo; diff --git a/grammar.js b/grammar.js index 263ef77..8dcbc4b 100644 --- a/grammar.js +++ b/grammar.js @@ -46,6 +46,13 @@ module.exports = grammar({ $._SPACE ], + conflicts: $ => [ + [$._for_or_expression_list], + [$.for_indices], + [$.component_reference__function_call], // No way to tell if x< will be function polymorphism or logic compare + //[$._primary, $.string_comment], + ], + word: $ => $.IDENT, rules: { diff --git a/package-lock.json b/package-lock.json index 2e267e1..fbbda07 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "tree-sitter-metamodelica", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "tree-sitter-metamodelica", - "version": "0.1.0", + "version": "0.2.0", "hasInstallScript": true, "license": "SEE LICENSE IN OSMC-License.txt", "dependencies": { @@ -165,9 +165,9 @@ "dev": true }, "node_modules/node-abi": { - "version": "3.57.0", - "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.57.0.tgz", - "integrity": "sha512-Dp+A9JWxRaKuHP35H77I4kCKesDy5HUDEmScia2FyncMTOXASMyg251F5PhFoDA5uqBrDDffiLpbqnrZmNXW+g==", + "version": "3.58.0", + "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.58.0.tgz", + "integrity": "sha512-pXY1jnGf5T7b8UNzWzIqf0EkX4bx/w8N2AvwlGnk2SYYA/kzDVPaH0Dh0UG4EwxBB5eKOIZKPr8VAHSHL1DPGg==", "dev": true, "dependencies": { "semver": "^7.3.5" diff --git a/package.json b/package.json index 1513e79..94d64bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tree-sitter-metamodelica", - "version": "0.1.0", + "version": "0.2.0", "description": "MetaModelica grammar for tree-sitter", "main": "index.js", "types": "bindings/node", diff --git a/queries/highlights.scm b/queries/highlights.scm index 72f1470..1e70538 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -26,19 +26,21 @@ ;;; Types (type_specifier (name_path (IDENT) @type )) ;; >A<.>MyType< x (T_REAL) @type.builtin ;; >Real< x -(T_INTEGER) @type.builtin ;; >Integer< x -(T_BOOLEAN) @type.builtin ;; >Boolean< x -(T_STRING) @type.builtin ;; >String< x -(T_LIST) @type.builtin ;; >List< x -(T_OPTION) @type.builtin ;; >Option< x -(T_TUPLE) @type.builtin ;; >Tuple< x +(T_INTEGER) @type.builtin ;; >Integer< x +(T_BOOLEAN) @type.builtin ;; >Boolean< x +(T_STRING) @type.builtin ;; >String< x +(T_LIST) @type.builtin ;; >List< x +(T_OPTION) @type.builtin ;; >Option< x +(T_TUPLE) @type.builtin ;; >Tuple< x +(T_ANY) @type.builtin ;; subtypeof >Any< +(polymorphic_type_specifier (name_path)@type) ;;; Variables (declaration (IDENT) @variable.parameter) ;; Real >x< -(component_reference_function_call componentReference: (component_reference) @variable.parameter) ;; >x< +(component_reference__function_call componentReference: (component_reference) @variable.parameter) ;; >x< ;;; Function calls -(component_reference_function_call functionName: (component_reference) @function) +(component_reference__function_call functionName: (component_reference) @function) ;;; Classes (class_definition (class_type class: (CLASS))(class_specifier (identifier) @module)) ;; class >A< end >A<; @@ -97,6 +99,7 @@ (GUARD) (IF) (IMPORT) + (IMPURE) (INITIAL) (INNER) (LOCAL) @@ -112,11 +115,13 @@ (PARTIAL) (PROTECTED) (PUBLIC) + (PURE) (RECORD) (REDECLARE) (REPLACEABLE) (RETURN) (STREAM) + (SUBTYPEOF) (T_ALGORITHM) (T_AND) (T_ANNOTATION) @@ -130,6 +135,7 @@ (T_OUTPUT) (T_TRUE) (THEN) + (THREADED) (TRY) (TYPE) (UNIONTYPE) diff --git a/rules/a1-classAndMainGrammar.js b/rules/a1-classAndMainGrammar.js index e4d1f48..59cddf8 100644 --- a/rules/a1-classAndMainGrammar.js +++ b/rules/a1-classAndMainGrammar.js @@ -83,7 +83,18 @@ module.exports = { ), field("type", $.TYPE), field("package", $.PACKAGE), - field("function", $.FUNCTION), + seq( + optional(choice( + $.PURE, + $.IMPURE + )), + //optional(choice( + // $.OPERATOR, + // $.T_PARALLEL, + // $.T_KERNEL + //)), + field("function", $.FUNCTION), + ), field("uniontype", $.UNIONTYPE), seq( field("operator", $.OPERATOR), @@ -95,6 +106,7 @@ module.exports = { identifier: $ => choice( $.IDENT, $.DER, + $.CODE, $.EQUALITY, $.INITIAL ), @@ -151,6 +163,10 @@ module.exports = { seq( $.EQUALS, field("overloading", $.overloading) + ), + seq( + $.SUBTYPEOF, + $.type_specifier ) ), @@ -226,11 +242,11 @@ module.exports = { ), _composition2: $ => choice( - choice( + seq( repeat1( choice( - $._public__element_list, - $._protected__element_list, + $._public_element_list, + $._protected_element_list, $.initial_equation_clause, $.initial_algorithm_clause, $.equation_clause, @@ -242,8 +258,8 @@ module.exports = { ), repeat1( choice( - $._public__element_list, - $._protected__element_list, + $._public_element_list, + $._protected_element_list, $.initial_equation_clause, $.initial_algorithm_clause, $.equation_clause, @@ -263,7 +279,7 @@ module.exports = { field("componentReference", $.component_reference), $.EQUALS )), - $.IDENT, + field("functionName", $.IDENT), $.LPAR, optional($._expression_list), $.RPAR @@ -279,19 +295,18 @@ module.exports = { $._SEMICOLON ), - _public__element_list: $ => seq( + _public_element_list: $ => seq( $.PUBLIC, repeat($._element_list) ), - _protected__element_list: $ => seq( + _protected_element_list: $ => seq( $.PROTECTED, repeat($._element_list) ), language_specification: $ => seq( - $.STRING, - $._SEMICOLON + $.STRING ), // _element_list could be empty list, use repeat(_element_list) everywhere @@ -339,7 +354,10 @@ module.exports = { // TODO: What is CODE? explicit_import_name: $ => seq( - $.IDENT, + choice( + $.IDENT, + $.CODE + ), $.EQUALS, field("namePath", $.name_path) ), diff --git a/rules/a2-extends.js b/rules/a2-extends.js index 34f97e6..8526c53 100644 --- a/rules/a2-extends.js +++ b/rules/a2-extends.js @@ -80,15 +80,22 @@ module.exports = { $.FLOW, $.STREAM ), + // TODO: Add T_LOCAL and T_GLOBAL + //optional(choice( + // $.T_LOCAL, + // $.T_GLOBAL + //)), optional(choice( $.DISCRETE, $.PARAMETER, $.CONSTANT )), - optional(choice( + optional( $.T_INPUT, + ), + optional( $.T_OUTPUT - )) + ) ), seq( choice( @@ -96,15 +103,20 @@ module.exports = { $.PARAMETER, $.CONSTANT ), - optional(choice( + optional( $.T_INPUT, + ), + optional( $.T_OUTPUT - )) + ) ), - choice( + seq( $.T_INPUT, - $.T_OUTPUT - ) + optional( + $.T_OUTPUT + ) + ), + $.T_OUTPUT ), type_specifier: $ => seq( diff --git a/rules/a5-expressions.js b/rules/a5-expressions.js index da69e4a..5e0f98e 100644 --- a/rules/a5-expressions.js +++ b/rules/a5-expressions.js @@ -85,7 +85,10 @@ module.exports = { $.IDENT, optional(seq( optional(seq( - $.GUARD, + choice( + $.IF, + $.GUARD, + ), $.expression )), $.T_IN, @@ -221,14 +224,21 @@ module.exports = { $.STRING, $.T_FALSE, $.T_TRUE, - $.component_reference_function_call, + $.component_reference__function_call, seq( $.DER, $.function_call ), + // TODO: omc isn't using this construct + //seq( + // $.PURE, + // $.function_call + //), seq( $.LPAR, - $._output_expression_list + $._output_expression_list, + // TODO: Add (array_subscripts)? + //optional($.array_subscripts) ), seq( $.LBRACK, @@ -237,7 +247,8 @@ module.exports = { ), seq( $.LBRACE, - $._for_or_expression_list, + // TODO: Is this correct? + optional($._for_or_expression_list), $.RBRACE ), $.T_END @@ -252,25 +263,47 @@ module.exports = { )) ), - component_reference_function_call: $ => prec(2,choice( + component_reference__function_call: $ => choice( seq( field("functionName", $.component_reference), + field("polymorphicType", $.polymorphic_type_specifier), $.function_call ), + seq( + field("functionName", $.component_reference), + $.function_call, + // TODO: Add DOT expression? + //optional(seq( + // $.DOT, + // $.expression + //)) + ), field("componentReference", $.component_reference), seq( $.INITIAL, $.LPAR, $.RPAR ) - )), + ), + + polymorphic_type_specifier: $ => seq( + $.LESS, + $._name_list, + $.GREATER, + ), name_path: $ => seq( optional($.DOT), - $.IDENT, + choice( + field("identifier", $.IDENT), + $.CODE + ), repeat(seq( $.DOT, - $.IDENT + choice( + field("identifier", $.IDENT), + $.CODE + ) )) ), @@ -283,11 +316,17 @@ module.exports = { _name_path_star2: $ => choice( seq( - field("identifier", $.IDENT), + choice( + field("identifier", $.IDENT), + $.CODE + ), optional($.STAR_EW) ), seq( - field("identifier", $.IDENT), + choice( + field("identifier", $.IDENT), + $.CODE + ), $.DOT, $._name_path_star2 ), @@ -336,7 +375,10 @@ module.exports = { function_arguments: $ => choice( seq( $._for_or_expression_list, - optional($._named_arguments) + optional(seq( + $.COMMA, + $._named_arguments + )) ), $._named_arguments ), @@ -350,6 +392,7 @@ module.exports = { $.expression )), seq( + optional($.THREADED), $.FOR, $.for_indices ) @@ -445,5 +488,8 @@ module.exports = { annotation: $ => seq( $.T_ANNOTATION, $.class_modification - ) + ), + + // TODO: Add code_expression + }; diff --git a/rules/a6-metamodelicaExtensions.js b/rules/a6-metamodelicaExtensions.js index 5148bc5..374207b 100644 --- a/rules/a6-metamodelicaExtensions.js +++ b/rules/a6-metamodelicaExtensions.js @@ -70,17 +70,25 @@ module.exports = { cases: $ => seq( field("case", repeat1($.onecase)), - field("elsecase", optional($.elsecase)) + field("elsecase", optional($.cases2)) ), - elsecase: $ => seq( + cases2: $ => seq( $.ELSE, optional(seq( optional($.string_comment), - optional(seq( - $.EQUATION, - repeat($._equation_list_then) - )), + // TODO: Add local clause? + //$.local_clause, + choice( + optional(seq( + $.EQUATION, + repeat($._equation_list_then) + )), + optional(seq( + $.ALGORITHM, + repeat($._algorithm_annotation_list) + )), + ), $.THEN )), $.expression, @@ -90,6 +98,13 @@ module.exports = { onecase: $ => seq( $.CASE, $._pattern, + optional(seq( + choice( + $.IF, + $.GUARD, + ), + $.expression + )), optional($.string_comment), choice( optional(seq( diff --git a/rules/keywords.js b/rules/keywords.js index 9850f0e..bfe0bbf 100644 --- a/rules/keywords.js +++ b/rules/keywords.js @@ -76,6 +76,7 @@ module.exports = { BREAK: $ => "break", CASE: $ => "case", CLASS: $ => "class", + CODE: $ => "$Code", CONNECT: $ => "connect", CONNECTOR: $ => "connector", CONSTANT: $ => "constant", @@ -103,6 +104,7 @@ module.exports = { GUARD: $ => "guard", IF: $ => "if", IMPORT: $ => "import", + IMPURE: $ => "impure", INITIAL: $ => "initial", INNER: $ => "inner", LOCAL: $ => "local", @@ -112,17 +114,19 @@ module.exports = { MODEL: $ => "model", OPERATOR: $ => "operator", OPTIMIZATION: $ => "optimization", - OVERLOAD: $ => "overload", + OVERLOAD: $ => "$overload", PACKAGE: $ => "package", PARAMETER: $ => "parameter", PARTIAL: $ => "partial", PROTECTED: $ => "protected", PUBLIC: $ => "public", + PURE: $ => "pure", RECORD: $ => "record", REDECLARE: $ => "redeclare", REPLACEABLE: $ => "replaceable", RETURN: $ => "return", STREAM: $ => "stream", + SUBTYPEOF: $ => "subtypeof", T_ALGORITHM: $ => "algorithm", T_AND: $ => "and", T_ANNOTATION: $ => "annotation", @@ -136,6 +140,7 @@ module.exports = { T_OUTPUT: $ => "output", T_TRUE: $ => "true", THEN: $ => "then", + THREADED: $ => "threaded", TRY: $ => "try", TYPE: $ => "type", UNIONTYPE: $ => "uniontype", diff --git a/test/corpus/a1-10_external.txt b/test/corpus/a1-10_external.txt new file mode 100644 index 0000000..46c0753 --- /dev/null +++ b/test/corpus/a1-10_external.txt @@ -0,0 +1,79 @@ +============ +A.1 external +============ + +function threadData + output ThreadData threadData; +protected + record ThreadData + end ThreadData; +external "builtin"; +annotation(Documentation(info=" +

Used to access thread-specific data in external functions.

+")); +end threadData; + + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (element + (component_clause + (type_prefix + (T_OUTPUT)) + (type_specifier + (name_path + (IDENT))) + (component_declaration + (declaration + (IDENT))))) + (PROTECTED) + (element + (class_definition + (class_type + (RECORD)) + (class_specifier + (identifier + (IDENT)) + (T_END) + (identifier + (IDENT))))) + (external_clause + (EXTERNAL) + (language_specification + (STRING)) + (external_annotation + (annotation + (T_ANNOTATION) + (class_modification + (LPAR) + (argument + (element_modification_or_replaceable + (element_modification + (component_reference + (IDENT)) + (modification + (class_modification + (LPAR) + (argument + (element_modification_or_replaceable + (element_modification + (component_reference + (IDENT)) + (modification + (EQUALS) + (expression + (simple_expression + (STRING))))))) + (RPAR)))))) + (RPAR)))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a1-7_class_specifier.txt b/test/corpus/a1-7_class_specifier.txt new file mode 100644 index 0000000..cefb80b --- /dev/null +++ b/test/corpus/a1-7_class_specifier.txt @@ -0,0 +1,103 @@ +============================= +A.1 class_specifier subtypeof +============================= + +function foo + input Option inTypeAOption; + input FuncTypeType_aToString inFuncTypeTypeAToString; + output String outString; + replaceable type Type_a subtypeof Any; + partial function FuncTypeType_aToString + input Type_a inTypeA; + output String outString; + end FuncTypeType_aToString; +end foo; + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (element + (component_clause + (type_prefix + (T_INPUT)) + (type_specifier + (T_OPTION) + (LESS) + (type_specifier + (name_path + (IDENT))) + (GREATER)) + (component_declaration + (declaration + (IDENT))))) + (element + (component_clause + (type_prefix + (T_INPUT)) + (type_specifier + (name_path + (IDENT))) + (component_declaration + (declaration + (IDENT))))) + (element + (component_clause + (type_prefix + (T_OUTPUT)) + (type_specifier + (T_STRING)) + (component_declaration + (declaration + (IDENT))))) + (element + (REPLACEABLE) + (class_definition + (class_type + (TYPE)) + (class_specifier + (identifier + (IDENT)) + (SUBTYPEOF) + (type_specifier + (T_ANY))))) + (element + (class_definition + (PARTIAL) + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (element + (component_clause + (type_prefix + (T_INPUT)) + (type_specifier + (name_path + (IDENT))) + (component_declaration + (declaration + (IDENT))))) + (element + (component_clause + (type_prefix + (T_OUTPUT)) + (type_specifier + (T_STRING)) + (component_declaration + (declaration + (IDENT)))))) + (T_END) + (identifier + (IDENT)))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a1-8_class_type_impure.txt b/test/corpus/a1-8_class_type_impure.txt new file mode 100644 index 0000000..ea067c7 --- /dev/null +++ b/test/corpus/a1-8_class_type_impure.txt @@ -0,0 +1,113 @@ +===================== +A.1 class_type impure +===================== + +impure function subSampleExpression<__Any> + input __Any u; + parameter input Integer factor(min=0)=0 annotation(__OpenModelica_functionVariability=true); + output __Any y; + external "builtin" y=subSample(u,factor); +end subSampleExpression; + +--- + +(stored_definition + (class_definition + (class_type + (IMPURE) + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (LESS) + (IDENT) + (GREATER) + (composition + (element + (component_clause + (type_prefix + (T_INPUT)) + (type_specifier + (name_path + (IDENT))) + (component_declaration + (declaration + (IDENT))))) + (element + (component_clause + (type_prefix + (PARAMETER) + (T_INPUT)) + (type_specifier + (T_INTEGER)) + (component_declaration + (declaration + (IDENT) + (modification + (class_modification + (LPAR) + (argument + (element_modification_or_replaceable + (element_modification + (component_reference + (IDENT)) + (modification + (EQUALS) + (expression + (simple_expression + (UNSIGNED_INTEGER))))))) + (RPAR)) + (EQUALS) + (expression + (simple_expression + (UNSIGNED_INTEGER))))) + (comment + (annotation + (T_ANNOTATION) + (class_modification + (LPAR) + (argument + (element_modification_or_replaceable + (element_modification + (component_reference + (IDENT)) + (modification + (EQUALS) + (expression + (simple_expression + (T_TRUE))))))) + (RPAR))))))) + (element + (component_clause + (type_prefix + (T_OUTPUT)) + (type_specifier + (name_path + (IDENT))) + (component_declaration + (declaration + (IDENT))))) + (external_clause + (EXTERNAL) + (language_specification + (STRING)) + (component_reference + (IDENT)) + (EQUALS) + (IDENT) + (LPAR) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (RPAR))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a1-9_overload.txt b/test/corpus/a1-9_overload.txt new file mode 100644 index 0000000..92fb9a5 --- /dev/null +++ b/test/corpus/a1-9_overload.txt @@ -0,0 +1,64 @@ +============ +A.1 overload +============ + +function interval = $overload(OpenModelica.Internal.intervalInferred, OpenModelica.Internal.intervalExpression) + "Returns the interval between the previous and present tick of the clock of its argument" + annotation(Documentation(info=" + See interval() +")); + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (EQUALS) + (overloading + (OVERLOAD) + (LPAR) + (name_path + (IDENT) + (DOT) + (IDENT) + (DOT) + (IDENT)) + (COMMA) + (name_path + (IDENT) + (DOT) + (IDENT) + (DOT) + (IDENT)) + (RPAR) + (comment + (string_comment + (STRING)) + (annotation + (T_ANNOTATION) + (class_modification + (LPAR) + (argument + (element_modification_or_replaceable + (element_modification + (component_reference + (IDENT)) + (modification + (class_modification + (LPAR) + (argument + (element_modification_or_replaceable + (element_modification + (component_reference + (IDENT)) + (modification + (EQUALS) + (expression + (simple_expression + (STRING))))))) + (RPAR)))))) + (RPAR)))))))) diff --git a/test/corpus/a2-2_type_prefix.txt b/test/corpus/a2-2_type_prefix.txt new file mode 100644 index 0000000..a4ed359 --- /dev/null +++ b/test/corpus/a2-2_type_prefix.txt @@ -0,0 +1,51 @@ +============================ +A.2 type_prefix input output +============================ + +function foo + input Integer x; + output Integer y; + input output Integer z; +end foo; + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (element + (component_clause + (type_prefix + (T_INPUT)) + (type_specifier + (T_INTEGER)) + (component_declaration + (declaration + (IDENT))))) + (element + (component_clause + (type_prefix + (T_OUTPUT)) + (type_specifier + (T_INTEGER)) + (component_declaration + (declaration + (IDENT))))) + (element + (component_clause + (type_prefix + (T_INPUT) + (T_OUTPUT)) + (type_specifier + (T_INTEGER)) + (component_declaration + (declaration + (IDENT)))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a4-2_algorithm_clause2.txt b/test/corpus/a4-2_algorithm_clause2.txt index 1bcef83..d7079d0 100644 --- a/test/corpus/a4-2_algorithm_clause2.txt +++ b/test/corpus/a4-2_algorithm_clause2.txt @@ -52,17 +52,17 @@ end sum; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)))) (ASSIGN) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))) (PLUS) - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))))) (T_END) diff --git a/test/corpus/a5-1_for_expression_threaded.txt b/test/corpus/a5-1_for_expression_threaded.txt new file mode 100644 index 0000000..0fdce07 --- /dev/null +++ b/test/corpus/a5-1_for_expression_threaded.txt @@ -0,0 +1,117 @@ +================================ +A.5 for_expression threaded for +================================ + +function foo + input Scope scope1,scope2; + output Boolean eq; +algorithm + eq := min(PacketA.bar(r1)==PacketA.bar(r2) threaded for r1 in scope1, r2 in scope2); +end foo; + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (element + (component_clause + (type_prefix + (T_INPUT)) + (type_specifier + (name_path + (IDENT))) + (component_declaration + (declaration + (IDENT))) + (COMMA) + (component_declaration + (declaration + (IDENT))))) + (element + (component_clause + (type_prefix + (T_OUTPUT)) + (type_specifier + (T_BOOLEAN)) + (component_declaration + (declaration + (IDENT))))) + (algorithm_clause + (T_ALGORITHM) + (algorithm + (assign_clause_a + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT) + (DOT) + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (RPAR))) + (EQEQ) + (component_reference__function_call + (component_reference + (IDENT) + (DOT) + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (RPAR))))) + (THREADED) + (FOR) + (for_indices + (for_index + (IDENT) + (T_IN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (COMMA) + (for_indices + (for_index + (IDENT) + (T_IN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))))))) + (RPAR))))))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a5-2_function_arguments.txt b/test/corpus/a5-2_function_arguments.txt new file mode 100644 index 0000000..19970d3 --- /dev/null +++ b/test/corpus/a5-2_function_arguments.txt @@ -0,0 +1,169 @@ +====================== +A.5 function_arguments +====================== + +function foo +equation + res := bar(); + res := bar(a=true, b=true); + res := bar(a, b=false); + res := bar(a, b, c=false, d=1.0); + res := bar(a, b, c); +end foo; + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (equation_clause + (EQUATION) + (equation + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (RPAR)))))) + (equation + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (named_argument + (IDENT) + (EQUALS) + (expression + (simple_expression + (T_TRUE)))) + (COMMA) + (named_argument + (IDENT) + (EQUALS) + (expression + (simple_expression + (T_TRUE))))) + (RPAR)))))) + (equation + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (named_argument + (IDENT) + (EQUALS) + (expression + (simple_expression + (T_FALSE))))) + (RPAR)))))) + (equation + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (named_argument + (IDENT) + (EQUALS) + (expression + (simple_expression + (T_FALSE)))) + (COMMA) + (named_argument + (IDENT) + (EQUALS) + (expression + (simple_expression + (UNSIGNED_REAL))))) + (RPAR)))))) + (equation + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (RPAR)))))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a5-3_polymorphicType.txt b/test/corpus/a5-3_polymorphicType.txt new file mode 100644 index 0000000..c84da2c --- /dev/null +++ b/test/corpus/a5-3_polymorphicType.txt @@ -0,0 +1,107 @@ +============================== +A.5 polymorphic_type_specifier +============================== + +function foo +algorithm + fn_cache := new(a, b); + fn_cache := new(a, b); + x := y < 1.0; +end foo; + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (algorithm_clause + (T_ALGORITHM) + (algorithm + (assign_clause_a + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (polymorphic_type_specifier + (LESS) + (name_path + (IDENT)) + (GREATER)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (RPAR))))))) + (algorithm + (assign_clause_a + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (polymorphic_type_specifier + (LESS) + (name_path + (IDENT)) + (COMMA) + (name_path + (IDENT)) + (GREATER)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (RPAR))))))) + (algorithm + (assign_clause_a + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))) + (LESS) + (UNSIGNED_REAL))))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a5-4_pure_function.txt b/test/corpus/a5-4_pure_function.txt new file mode 100644 index 0000000..49ca440 --- /dev/null +++ b/test/corpus/a5-4_pure_function.txt @@ -0,0 +1,68 @@ +============================== +A.5 pure function +============================== + +pure function foo +algorithm + pure := foldExp(fn, function checkPureCall(fn = fn), true); +end foo; + +--- + +(stored_definition + (class_definition + (class_type + (PURE) + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (algorithm_clause + (T_ALGORITHM) + (algorithm + (assign_clause_a + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (part_eval_function_expression + (FUNCTION) + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (named_argument + (IDENT) + (EQUALS) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))))) + (RPAR)))) + (COMMA) + (expression + (simple_expression + (T_TRUE)))) + (RPAR))))))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a6-1_match_expression1.txt b/test/corpus/a6-1_match_expression1.txt index 11a8fff..7d7ed5f 100644 --- a/test/corpus/a6-1_match_expression1.txt +++ b/test/corpus/a6-1_match_expression1.txt @@ -74,7 +74,7 @@ end A; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)))) (ASSIGN) @@ -86,13 +86,13 @@ end A; (LPAR) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))) (COMMA) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))) (RPAR))) @@ -104,7 +104,7 @@ end A; (LPAR) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT) (DOT) @@ -122,7 +122,7 @@ end A; (COMMA) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (WILD))))) (RPAR))) @@ -130,11 +130,11 @@ end A; (expression (simple_expression (STRING)))) - (elsecase + (cases2 (ELSE) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))) (T_END) diff --git a/test/corpus/a6-2_match_expression2.txt b/test/corpus/a6-2_match_expression2.txt index 84eeaad..8ecf32c 100644 --- a/test/corpus/a6-2_match_expression2.txt +++ b/test/corpus/a6-2_match_expression2.txt @@ -55,7 +55,7 @@ end makeDebugResult; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (WILD)))) (ASSIGN) @@ -67,13 +67,13 @@ end makeDebugResult; (LPAR) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))) (COMMA) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))) (RPAR))) @@ -104,7 +104,7 @@ end makeDebugResult; (LPAR) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT) (DOT) @@ -117,14 +117,14 @@ end makeDebugResult; (EQUALS) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))) (RPAR))))) (COMMA) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (WILD))))) (RPAR))) @@ -135,7 +135,7 @@ end makeDebugResult; (EQUALS) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT) (DOT) @@ -145,19 +145,19 @@ end makeDebugResult; (function_arguments (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)))))) (RPAR)))))) (equation (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)))) (EQUALS) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT) (DOT) @@ -178,7 +178,7 @@ end makeDebugResult; (THEN) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))) (T_END) diff --git a/test/corpus/a6-3_match_expression3.txt b/test/corpus/a6-3_match_expression3.txt index 6917882..d002dba 100644 --- a/test/corpus/a6-3_match_expression3.txt +++ b/test/corpus/a6-3_match_expression3.txt @@ -17,6 +17,10 @@ algorithm debugstr := Print.getString(); print("Hello World"); then res_with_debug; + else + algorithm + print("This is the else case"); + then res_with_debug; end matchcontinue; end makeDebugResult; @@ -56,7 +60,7 @@ end makeDebugResult; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (WILD)))) (ASSIGN) @@ -68,13 +72,13 @@ end makeDebugResult; (LPAR) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))) (COMMA) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))) (RPAR))) @@ -112,7 +116,7 @@ end makeDebugResult; (LPAR) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT) (DOT) @@ -125,14 +129,14 @@ end makeDebugResult; (EQUALS) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))) (RPAR))))) (COMMA) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (WILD))))) (RPAR))) @@ -140,25 +144,25 @@ end makeDebugResult; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)))) (ASSIGN) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))) (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)))) (ASSIGN) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT) (DOT) @@ -169,7 +173,29 @@ end makeDebugResult; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (STRING)))) + (RPAR)))))) + (THEN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (cases2 + (ELSE) + (ALGORITHM) + (algorithm + (assign_clause_a + (simple_expression + (component_reference__function_call (component_reference (IDENT)) (function_call @@ -182,7 +208,7 @@ end makeDebugResult; (THEN) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))) (T_END) diff --git a/test/corpus/a6-4_match_expression4.txt b/test/corpus/a6-4_match_expression4.txt new file mode 100644 index 0000000..e57bf0f --- /dev/null +++ b/test/corpus/a6-4_match_expression4.txt @@ -0,0 +1,154 @@ +========================== +A.6 match_expression guard +========================== + +function foo +algorithm + res := match(x, y) + case (_, z) guard bar1(bar2(x)) + then listReverse(x::z); + case ({},_) + then listReverse(x::z); + end match; +end foo; + +--- + +(stored_definition + (class_definition + (class_type + (FUNCTION)) + (class_specifier + (identifier + (IDENT)) + (composition + (algorithm_clause + (T_ALGORITHM) + (algorithm + (assign_clause_a + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))) + (ASSIGN) + (expression + (match_expression + (MATCH) + (expression + (simple_expression + (LPAR) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (RPAR))) + (cases + (onecase + (CASE) + (expression + (simple_expression + (LPAR) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (WILD))))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))) + (RPAR))) + (GUARD) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)))))) + (RPAR)))))) + (RPAR))))) + (THEN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))) + (COLONCOLON) + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))))) + (RPAR)))))) + (onecase + (CASE) + (expression + (simple_expression + (LPAR) + (expression + (simple_expression + (LBRACE) + (RBRACE))) + (COMMA) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (WILD))))) + (RPAR))) + (THEN) + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT)) + (function_call + (LPAR) + (function_arguments + (expression + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))) + (COLONCOLON) + (simple_expression + (component_reference__function_call + (component_reference + (IDENT))))))) + (RPAR))))))) + (T_END) + (MATCH))))))) + (T_END) + (identifier + (IDENT))))) diff --git a/test/corpus/a6-4try.txt b/test/corpus/a6-4try.txt index 0eba9d8..ab9abfb 100644 --- a/test/corpus/a6-4try.txt +++ b/test/corpus/a6-4try.txt @@ -60,17 +60,17 @@ end foo; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)))) (ASSIGN) (expression (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))) (SLASH) - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT))))))) (algorithm @@ -79,7 +79,7 @@ end foo; (algorithm (assign_clause_a (simple_expression - (component_reference_function_call + (component_reference__function_call (component_reference (IDENT)) (function_call diff --git a/test/highlight/Polymorphism.mo b/test/highlight/Polymorphism.mo new file mode 100644 index 0000000..a40bcd5 --- /dev/null +++ b/test/highlight/Polymorphism.mo @@ -0,0 +1,27 @@ +function foo +//<- keyword +// ^ function +algorithm +//<- keyword + result := new(a, b); +//^ variable.parameter +// ^ operator +// ^ function +// ^ operator +// ^ type +// ^ operator +// ^ variable.parameter +// ^ variable.parameter + result := new(a, b); +//^ variable.parameter +// ^ operator +// ^ function +// ^ operator +// ^ type +// ^ type +// ^ operator +// ^ variable.parameter +// ^ variable.parameter +end foo; +//<- keyword +// ^ function