Skip to content

Commit

Permalink
Adding rules for latest MetaModelica used by OpenModelica (#6)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AnHeuermann authored Apr 17, 2024
1 parent 7f825d7 commit 93def7a
Show file tree
Hide file tree
Showing 27 changed files with 1,299 additions and 89 deletions.
19 changes: 19 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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,
}
}
]
}
4 changes: 4 additions & 0 deletions examples/test.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pure function foo
algorithm
pure := foldExp(fn, function checkPureCall(fn = fn), true);
end foo;
7 changes: 7 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 14 additions & 8 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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<<Real> x
(T_OPTION) @type.builtin ;; >Option< <Real> x
(T_TUPLE) @type.builtin ;; >Tuple< <T1, T2, T3> x
(T_INTEGER) @type.builtin ;; >Integer< x
(T_BOOLEAN) @type.builtin ;; >Boolean< x
(T_STRING) @type.builtin ;; >String< x
(T_LIST) @type.builtin ;; >List<<Real> x
(T_OPTION) @type.builtin ;; >Option< <Real> x
(T_TUPLE) @type.builtin ;; >Tuple< <T1, T2, T3> 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<;
Expand Down Expand Up @@ -97,6 +99,7 @@
(GUARD)
(IF)
(IMPORT)
(IMPURE)
(INITIAL)
(INNER)
(LOCAL)
Expand All @@ -112,11 +115,13 @@
(PARTIAL)
(PROTECTED)
(PUBLIC)
(PURE)
(RECORD)
(REDECLARE)
(REPLACEABLE)
(RETURN)
(STREAM)
(SUBTYPEOF)
(T_ALGORITHM)
(T_AND)
(T_ANNOTATION)
Expand All @@ -130,6 +135,7 @@
(T_OUTPUT)
(T_TRUE)
(THEN)
(THREADED)
(TRY)
(TYPE)
(UNIONTYPE)
Expand Down
42 changes: 30 additions & 12 deletions rules/a1-classAndMainGrammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -95,6 +106,7 @@ module.exports = {
identifier: $ => choice(
$.IDENT,
$.DER,
$.CODE,
$.EQUALITY,
$.INITIAL
),
Expand Down Expand Up @@ -151,6 +163,10 @@ module.exports = {
seq(
$.EQUALS,
field("overloading", $.overloading)
),
seq(
$.SUBTYPEOF,
$.type_specifier
)
),

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -263,7 +279,7 @@ module.exports = {
field("componentReference", $.component_reference),
$.EQUALS
)),
$.IDENT,
field("functionName", $.IDENT),
$.LPAR,
optional($._expression_list),
$.RPAR
Expand All @@ -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
Expand Down Expand Up @@ -339,7 +354,10 @@ module.exports = {

// TODO: What is CODE?
explicit_import_name: $ => seq(
$.IDENT,
choice(
$.IDENT,
$.CODE
),
$.EQUALS,
field("namePath", $.name_path)
),
Expand Down
26 changes: 19 additions & 7 deletions rules/a2-extends.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,43 @@ 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(
$.DISCRETE,
$.PARAMETER,
$.CONSTANT
),
optional(choice(
optional(
$.T_INPUT,
),
optional(
$.T_OUTPUT
))
)
),
choice(
seq(
$.T_INPUT,
$.T_OUTPUT
)
optional(
$.T_OUTPUT
)
),
$.T_OUTPUT
),

type_specifier: $ => seq(
Expand Down
Loading

0 comments on commit 93def7a

Please sign in to comment.