├── .gitattributes
├── test
├── corpus
│ ├── a2-1_stored_definition.txt
│ ├── a2-2_short_class.txt
│ ├── a2-3_Extends.txt
│ ├── a2-2_der_enum_extend_classes.txt
│ ├── a2-5_modifications.txt
│ ├── a2-2_external_function_call.txt
│ ├── a2-2_long_class.txt
│ ├── a2-4_component_clause.txt
│ ├── a2-6-2_equations.txt
│ ├── a2-7_expressions
│ └── a2-6_equations.txt
└── highlight
│ ├── Resistor.mo
│ └── SimpleEquation.mo
├── .github
├── dependabot.yml
└── workflows
│ └── build-parser.yml
├── examples
└── SimpleMath.mo
├── .gitignore
├── package.json
├── README.md
├── queries
└── highlights.scm
├── OSMC-License.txt
└── grammar.js
/.gitattributes:
--------------------------------------------------------------------------------
1 | package-lock.json -diff linguist-generated=true
2 |
--------------------------------------------------------------------------------
/test/corpus/a2-1_stored_definition.txt:
--------------------------------------------------------------------------------
1 | ================================
2 | A.2.1 Stored Definition – Within
3 | ================================
4 |
5 | within a091234;
6 |
7 | ---
8 |
9 | (stored_definitions
10 | (within_clause
11 | (name
12 | (IDENT))))
13 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | # Set update schedule for GitHub Actions
2 |
3 | version: 2
4 | updates:
5 |
6 | - package-ecosystem: "github-actions"
7 | directory: "/"
8 | schedule:
9 | # Check for updates to GitHub Actions every week
10 | interval: "weekly"
11 |
--------------------------------------------------------------------------------
/examples/SimpleMath.mo:
--------------------------------------------------------------------------------
1 | within Modelica;
2 | class SimpleMath
3 | "Simple Model"
4 | extends Modelica.Icons.Example;
5 |
6 | parameter Real a "Parameter a";
7 | parameter Real b "Parameter b";
8 |
9 | Real result "The Result";
10 |
11 | equation
12 | result = a * b; // Multiplication of the Parameters
13 | end SimpleMath;
14 |
--------------------------------------------------------------------------------
/test/highlight/Resistor.mo:
--------------------------------------------------------------------------------
1 | model Resistor
2 | // <- keyword
3 | // ^ function.builtin
4 | equation
5 | // <- keyword
6 | v = i * R "Ohm's Law";
7 | //^ variable.builtin
8 | // ^ operator
9 | // ^ variable.builtin
10 | // ^ operator
11 | // ^ variable.builtin
12 | // ^ comment
13 | end Resistor;
14 | // <- keyword
15 | // ^ function.builtin
16 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Files generated by Node
2 | node_modules/
3 |
4 | # Files generated by tree-sitter
5 | .editorconfig
6 | binding.gyp
7 | bindings/
8 | build/
9 | Cargo.lock
10 | Cargo.toml
11 | Makefile
12 | Package.swift
13 | prebuilds/
14 | pyproject.toml
15 | setup.py
16 | src/
17 | target/
18 |
19 | # Build artifacts
20 | *.wasm
21 | *.so
22 | *.dll
23 | *.dylib
24 |
25 | # VisualStudio Code
26 | .vscode/*
27 | !.vscode/settings.json
28 | !.vscode/tasks.json
29 | !.vscode/launch.json
30 | !.vscode/extensions.json
31 | *.code-workspace
32 |
--------------------------------------------------------------------------------
/test/corpus/a2-2_short_class.txt:
--------------------------------------------------------------------------------
1 | ====================================
2 | Short Class with Block Comment
3 | ====================================
4 |
5 | model Foo = /* Comment */ input Bar;
6 |
7 | ---
8 |
9 | (stored_definitions
10 | (stored_definition
11 | (class_definition
12 | (class_prefixes)
13 | (short_class_specifier
14 | (IDENT)
15 | (BLOCK_COMMENT)
16 | (base_prefix)
17 | (type_specifier
18 | (name
19 | (IDENT)))))))
20 |
21 | ===============================
22 | Short Class with String Comment
23 | ===============================
24 |
25 | model Foo = Bar "string comment";
26 |
27 | ---
28 |
29 | (stored_definitions
30 | (stored_definition
31 | (class_definition
32 | (class_prefixes)
33 | (short_class_specifier
34 | (IDENT)
35 | (type_specifier
36 | (name
37 | (IDENT)))
38 | (description_string
39 | (STRING))))))
40 |
--------------------------------------------------------------------------------
/test/highlight/SimpleEquation.mo:
--------------------------------------------------------------------------------
1 | within Modelica;
2 | // <- keyword
3 | // ^ function.builtin
4 | class SimpleMath
5 | // <- keyword
6 | // ^ function.builtin
7 | "Simple Model"
8 | //^ comment
9 | extends Modelica.Icons.Example;
10 | //^ keyword
11 | // ^ type.builtin
12 |
13 | parameter Real a "Parameter a";
14 | //^ keyword ^ type.builtin
15 | // ^ variable.builtin
16 | // ^ comment
17 | parameter Real b "Parameter b";
18 | //^ keyword ^ type.builtin
19 | // ^ variable.builtin
20 | // ^ comment
21 | Real result "The Result";
22 | //^ type.builtin
23 | // ^ variable.builtin
24 | // ^ comment
25 |
26 | equation
27 | // <- keyword
28 | result = a * b; // Multiplication of the Parameters
29 | //^ variable.builtin
30 | // ^ operator
31 | // ^ variable.builtin
32 | // ^ operator
33 | // ^ variable.builtin
34 | // ^ comment
35 | end SimpleMath;
36 | // <- keyword
37 | // ^ function.builtin
38 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tree-sitter-modelica",
3 | "version": "0.2.2",
4 | "description": "Modelica grammar for tree-sitter",
5 | "main": "bindings/node",
6 | "types": "bindings/node",
7 | "license": "SEE LICENSE IN OSMC-License.txt",
8 | "engines": {
9 | "node": "20"
10 | },
11 | "scripts": {
12 | "build": "tree-sitter generate && tree-sitter build --wasm --docker .",
13 | "test": "tree-sitter test",
14 | "preinstall": "tree-sitter generate",
15 | "install": "node-gyp-build",
16 | "prebuildify": "prebuildify --napi --strip"
17 | },
18 | "dependencies": {
19 | "node-addon-api": "^7.1.0",
20 | "node-gyp-build": "^4.8.0"
21 | },
22 | "peerDependencies": {
23 | "tree-sitter": "^0.21.1"
24 | },
25 | "peerDependenciesMeta": {
26 | "tree_sitter": {
27 | "optional": true
28 | }
29 | },
30 | "devDependencies": {
31 | "prebuildify": "^6.0.0",
32 | "tree-sitter-cli": "0.22.5"
33 | },
34 | "tree-sitter": [
35 | {
36 | "scope": "source.modelica",
37 | "file-types": [
38 | "mo"
39 | ]
40 | }
41 | ],
42 | "files": [
43 | "grammar.js",
44 | "binding.gyp",
45 | "prebuilds/**",
46 | "bindings/node/*",
47 | "queries/*",
48 | "src/**"
49 | ]
50 | }
51 |
--------------------------------------------------------------------------------
/.github/workflows/build-parser.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 | run-name: Build tree-sitter-modelica
3 |
4 | on:
5 | pull_request:
6 | branches:
7 | - master
8 | push:
9 | branches:
10 | - master
11 | tags:
12 | - "v*.*.*"
13 |
14 | jobs:
15 | build:
16 | runs-on: ubuntu-latest
17 | steps:
18 | - uses: actions/checkout@v6
19 | - uses: actions/setup-node@v6
20 | with:
21 | node-version: '20'
22 | - name: Clean install
23 | run: npm ci
24 | - name: Run build script
25 | run: npm run build
26 | - name: Run test script
27 | run: npm test
28 |
29 | - name: Archive production artifacts
30 | uses: actions/upload-artifact@v5
31 | with:
32 | name: tree-sitter-modelica.wasm
33 | path: |
34 | tree-sitter-modelica.wasm
35 | if-no-files-found: error
36 |
37 | release:
38 | if: startsWith(github.ref, 'refs/tags/')
39 | needs: build
40 | runs-on: ubuntu-latest
41 | permissions:
42 | contents: write
43 | steps:
44 | - uses: actions/download-artifact@v6
45 | with:
46 | name: tree-sitter-modelica.wasm
47 |
48 | - name: Release
49 | uses: softprops/action-gh-release@v2
50 | with:
51 | files: |
52 | tree-sitter-modelica.wasm
53 | fail_on_unmatched_files: true
54 | generate_release_notes: true
55 | append_body: true
56 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://github.com/OpenModelica/tree-sitter-modelica/actions/workflows/build-parser.yml)
2 |
3 | # tree-sitter-modelica
4 |
5 | An [open-source](OSMC-License.txt) Modelica
6 | ([Modelica Language Specification v3.5](https://specification.modelica.org/maint/3.5/MLS.html))
7 | grammar and highlighting-query for
8 | [tree-sitter](https://github.com/tree-sitter/tree-sitter).
9 |
10 | ## Dependencies
11 |
12 | - Node.js
13 | - Docker
14 |
15 | ## Installation
16 |
17 | ```bash
18 | npm install
19 | npm run build
20 | ```
21 |
22 | To generate the C code to parse Modelica run:
23 |
24 | ```bash
25 | npx tree-sitter generate
26 | ```
27 |
28 | > [!NOTE]
29 | > If you have ./node_modules/.bin in your `PATH` environment variable you can skip `npx`
30 | > ```bash
31 | > tree-sitter generate
32 | > ```
33 |
34 | ## Unit Tests
35 |
36 | There is a number of tests included. To run all tests defined in [test/](./test/) just run:
37 |
38 | ```bash
39 | npx tree-sitter test
40 | ```
41 |
42 | ### Examples
43 |
44 | To test the parser on a Modelica file run:
45 |
46 | ```bash
47 | npx tree-sitter parse examples/SimpleMath.mo
48 | ```
49 |
50 | ## Highlighting
51 |
52 | There is also a highlighting query included.
53 | Make sure that the
54 | [tree-sitter per-user configuration](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#per-user-configuration)
55 | are pointing to the parent directory of `tree-sitter-modelica`.
56 | So if this directory is in `/home/USER/workspace/tree-sitter-modelica` add
57 | `/home/USER/workspace` to the parser directories:
58 |
59 | **config.json**
60 | ```json
61 | {
62 | "parser-directories": [
63 | "/home/USER/workspace"
64 | ],
65 | }
66 | ```
67 |
68 | To test the highlighting configure run:
69 |
70 | ```bash
71 | npx tree-sitter highlight examples/SimpleMath.mo
72 | ```
73 |
74 | ## Usage
75 |
76 | Use [Web Tree-sitter](https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/README.md)
77 | `tree-sitter-modelica.wasm` in your application:
78 |
79 | ```typescript
80 | import * as Parser from 'web-tree-sitter'
81 |
82 | await Parser.init()
83 | const parser = new Parser
84 |
85 | const Modelica = await Parser.Language.load(`tree-sitter-modelica.wasm`)
86 | parser.setLanguage(Modelica)
87 | ```
88 |
89 | ## Current Status
90 |
91 | Tree-sitter-modelica has been tested on a "Save Total" version of the
92 | [Modelica.Fluid.Examples.DrumBoiler.DrumBoiler](./examples/DrumBoiler.mo) which was
93 | successfully parsed and highlighted.
94 |
95 | ```bash
96 | npx tree-sitter parse examples/DrumBoiler.mo
97 | npx tree-sitter highlight examples/DrumBoiler.mo
98 | ```
99 |
--------------------------------------------------------------------------------
/test/corpus/a2-3_Extends.txt:
--------------------------------------------------------------------------------
1 | ==============
2 | Extends Clause
3 | ==============
4 |
5 | class Parent
6 | Real value = 10.0;
7 | end Parent;
8 |
9 | class Child extends Parent;
10 | end Child;
11 |
12 | ---
13 |
14 | (stored_definitions
15 | (stored_definition
16 | (class_definition
17 | (class_prefixes)
18 | (long_class_specifier
19 | (IDENT)
20 | (element_list
21 | (named_element
22 | (component_clause
23 | (type_specifier
24 | (name
25 | (IDENT)))
26 | (component_list
27 | (component_declaration
28 | (declaration
29 | (IDENT)
30 | (modification
31 | (expression
32 | (simple_expression
33 | (primary_expression
34 | (literal_expression
35 | (unsigned_real_literal_expression
36 | (UNSIGNED_REAL)))))))))))))
37 | (IDENT))))
38 | (stored_definition
39 | (class_definition
40 | (class_prefixes)
41 | (long_class_specifier
42 | (IDENT)
43 | (element_list
44 | (extends_clause
45 | (type_specifier
46 | (name
47 | (IDENT)))))
48 | (IDENT)))))
49 |
50 | =====================
51 | Constrained By Clause
52 | =====================
53 |
54 | model ElectricalSource
55 | replaceable SineSource source constrainedby MO ( final n =5);
56 | end ElectricalSource;
57 |
58 | ---
59 |
60 | (stored_definitions
61 | (stored_definition
62 | (class_definition
63 | (class_prefixes)
64 | (long_class_specifier
65 | (IDENT)
66 | (element_list
67 | (named_element
68 | (component_clause
69 | (type_specifier
70 | (name
71 | (IDENT)))
72 | (component_list
73 | (component_declaration
74 | (declaration
75 | (IDENT)))))
76 | (constraining_clause
77 | (type_specifier
78 | (name
79 | (IDENT)))
80 | (class_modification
81 | (argument_list
82 | (element_modification
83 | (name
84 | (IDENT))
85 | (modification
86 | (expression
87 | (simple_expression
88 | (primary_expression
89 | (literal_expression
90 | (unsigned_integer_literal_expression
91 | (UNSIGNED_INTEGER)))))))))))))
92 | (IDENT)))))
93 |
--------------------------------------------------------------------------------
/test/corpus/a2-2_der_enum_extend_classes.txt:
--------------------------------------------------------------------------------
1 | ================
2 | Derivative Class
3 | ================
4 |
5 | model foo = der(bar, foo);
6 |
7 | ---
8 |
9 | (stored_definitions
10 | (stored_definition
11 | (class_definition
12 | (class_prefixes)
13 | (derivative_class_specifier
14 | (IDENT)
15 | (type_specifier
16 | (name
17 | (IDENT)))
18 | (IDENT)))))
19 |
20 | ===============================================
21 | Enumeration Unspecified Class with Line Comment
22 | ===============================================
23 |
24 | model foo = enumeration (:);
25 | // Line Comment
26 |
27 | ---
28 |
29 | (stored_definitions
30 | (stored_definition
31 | (class_definition
32 | (class_prefixes)
33 | (enumeration_class_specifier
34 | (IDENT))))
35 | (comment))
36 |
37 | ========================================
38 | Enumeration Class with Annotation Clause
39 | ========================================
40 |
41 | model foo = enumeration (Red, Green)
42 | annotation(bar);
43 |
44 | ---
45 |
46 | (stored_definitions
47 | (stored_definition
48 | (class_definition
49 | (class_prefixes)
50 | (enumeration_class_specifier
51 | (IDENT)
52 | (enum_list
53 | (enumeration_literal
54 | (IDENT))
55 | (enumeration_literal
56 | (IDENT)))
57 | (annotation_clause
58 | (class_modification
59 | (argument_list
60 | (element_modification
61 | (name
62 | (IDENT))))))))))
63 |
64 | ====================================================
65 | Extends Class with Public and Protected Element List
66 | ====================================================
67 |
68 | model extends foo (bar [:] redecl)
69 | public
70 | Real x;
71 | protected
72 | Real y;
73 | end foo;
74 |
75 | ---
76 |
77 | (stored_definitions
78 | (stored_definition
79 | (class_definition
80 | (class_prefixes)
81 | (extends_class_specifier
82 | (IDENT)
83 | (class_modification
84 | (argument_list
85 | (component_redeclaration
86 | (component_clause
87 | (type_specifier
88 | (name
89 | (IDENT)))
90 | (array_subscripts
91 | (subscript))
92 | (component_list
93 | (component_declaration
94 | (declaration
95 | (IDENT))))))))
96 | (public_element_list
97 | (named_element
98 | (component_clause
99 | (type_specifier
100 | (name
101 | (IDENT)))
102 | (component_list
103 | (component_declaration
104 | (declaration
105 | (IDENT)))))))
106 | (protected_element_list
107 | (named_element
108 | (component_clause
109 | (type_specifier
110 | (name
111 | (IDENT)))
112 | (component_list
113 | (component_declaration
114 | (declaration
115 | (IDENT)))))))
116 | (IDENT)))))
117 |
--------------------------------------------------------------------------------
/test/corpus/a2-5_modifications.txt:
--------------------------------------------------------------------------------
1 | =============================
2 | Long Class with Redeclaration
3 | =============================
4 |
5 | model Resistor
6 | extends Library.Component(final a= /* */ 5, redeclare model A=B);
7 | end Resistor;
8 |
9 | ---
10 |
11 | (stored_definitions
12 | (stored_definition
13 | (class_definition
14 | (class_prefixes)
15 | (long_class_specifier
16 | (IDENT)
17 | (element_list
18 | (extends_clause
19 | (type_specifier
20 | (name
21 | (name
22 | (IDENT))
23 | (IDENT)))
24 | (class_modification
25 | (argument_list
26 | (element_modification
27 | (name
28 | (IDENT))
29 | (modification
30 | (BLOCK_COMMENT)
31 | (expression
32 | (simple_expression
33 | (primary_expression
34 | (literal_expression
35 | (unsigned_integer_literal_expression
36 | (UNSIGNED_INTEGER))))))))
37 | (class_redeclaration
38 | (short_class_definition
39 | (class_prefixes)
40 | (short_class_specifier
41 | (IDENT)
42 | (type_specifier
43 | (name
44 | (IDENT))))))))))
45 | (IDENT)))))
46 |
47 |
48 | ==================
49 | Replaceable Clause
50 | ==================
51 |
52 | model ReplaceableExample
53 | replaceable Modelica.Blocks.Math.Add adder;
54 | Real output;
55 |
56 | equation
57 | output = adder.y;
58 | end ReplaceableExample;
59 |
60 | ---
61 |
62 | (stored_definitions
63 | (stored_definition
64 | (class_definition
65 | (class_prefixes)
66 | (long_class_specifier
67 | (IDENT)
68 | (element_list
69 | (named_element
70 | (component_clause
71 | (type_specifier
72 | (name
73 | (name
74 | (name
75 | (name
76 | (IDENT))
77 | (IDENT))
78 | (IDENT))
79 | (IDENT)))
80 | (component_list
81 | (component_declaration
82 | (declaration
83 | (IDENT))))))
84 | (named_element
85 | (component_clause
86 | (type_specifier
87 | (name
88 | (IDENT)))
89 | (component_list
90 | (component_declaration
91 | (declaration
92 | (IDENT)))))))
93 | (equation_section
94 | (equation_list
95 | (simple_equation
96 | (simple_expression
97 | (primary_expression
98 | (component_reference
99 | (IDENT))))
100 | (expression
101 | (simple_expression
102 | (primary_expression
103 | (component_reference
104 | (component_reference
105 | (IDENT))
106 | (IDENT))))))))
107 | (IDENT)))))
108 |
109 |
--------------------------------------------------------------------------------
/test/corpus/a2-2_external_function_call.txt:
--------------------------------------------------------------------------------
1 | ======================
2 | External Function Call
3 | ======================
4 |
5 | model ExternalFunctionCallExample
6 | Real input = 5.0;
7 | Real output;
8 |
9 | function MyExternalFunction
10 | input Real x;
11 | output Real y;
12 | external "C" y = my_external_function(x);
13 | end MyExternalFunction;
14 |
15 | equation
16 | output = MyExternalFunction(input);
17 | end ExternalFunctionCallExample;
18 |
19 | ---
20 |
21 | (stored_definitions
22 | (stored_definition
23 | (class_definition
24 | (class_prefixes)
25 | (long_class_specifier
26 | (IDENT)
27 | (element_list
28 | (named_element
29 | (component_clause
30 | (type_specifier
31 | (name
32 | (IDENT)))
33 | (component_list
34 | (component_declaration
35 | (declaration
36 | (IDENT)
37 | (modification
38 | (expression
39 | (simple_expression
40 | (primary_expression
41 | (literal_expression
42 | (unsigned_real_literal_expression
43 | (UNSIGNED_REAL))))))))))))
44 | (named_element
45 | (component_clause
46 | (type_specifier
47 | (name
48 | (IDENT)))
49 | (component_list
50 | (component_declaration
51 | (declaration
52 | (IDENT))))))
53 | (named_element
54 | (class_definition
55 | (class_prefixes)
56 | (long_class_specifier
57 | (IDENT)
58 | (element_list
59 | (named_element
60 | (component_clause
61 | (type_specifier
62 | (name
63 | (IDENT)))
64 | (component_list
65 | (component_declaration
66 | (declaration
67 | (IDENT))))))
68 | (named_element
69 | (component_clause
70 | (type_specifier
71 | (name
72 | (IDENT)))
73 | (component_list
74 | (component_declaration
75 | (declaration
76 | (IDENT)))))))
77 | (external_clause
78 | (language_specification
79 | (STRING))
80 | (external_function
81 | (component_reference
82 | (IDENT))
83 | (IDENT)
84 | (expression_list
85 | (expression
86 | (simple_expression
87 | (primary_expression
88 | (component_reference
89 | (IDENT))))))))
90 | (IDENT)))))
91 | (equation_section
92 | (equation_list
93 | (simple_equation
94 | (simple_expression
95 | (primary_expression
96 | (component_reference
97 | (IDENT))))
98 | (expression
99 | (simple_expression
100 | (primary_expression
101 | (function_application
102 | (component_reference
103 | (IDENT))
104 | (function_call_args
105 | (function_arguments
106 | (expression
107 | (simple_expression
108 | (primary_expression
109 | (component_reference
110 | (IDENT))))))))))))))
111 | (IDENT)))))
--------------------------------------------------------------------------------
/test/corpus/a2-2_long_class.txt:
--------------------------------------------------------------------------------
1 | ================
2 | Long Empty Class
3 | ================
4 |
5 | model Foo
6 | end Foo;
7 |
8 | ---
9 |
10 | (stored_definitions
11 | (stored_definition
12 | (class_definition
13 | (class_prefixes)
14 | (long_class_specifier
15 | (IDENT)
16 | (IDENT)))))
17 |
18 | ==============================
19 | Long Class with String Comment
20 | ==============================
21 |
22 | model Foo "Test"
23 | end Foo;
24 |
25 | ---
26 |
27 | (stored_definitions
28 | (stored_definition
29 | (class_definition
30 | (class_prefixes)
31 | (long_class_specifier
32 | (IDENT)
33 | (description_string
34 | (STRING))
35 | (IDENT)))))
36 |
37 | ======================
38 | Long Class with Import
39 | ======================
40 |
41 | model Resistor
42 | import SIunits = Modelica.SIunits;
43 | import Modelica.{SIunits};
44 | end Resistor;
45 |
46 | ---
47 |
48 | (stored_definitions
49 | (stored_definition
50 | (class_definition
51 | (class_prefixes)
52 | (long_class_specifier
53 | (IDENT)
54 | (element_list
55 | (import_clause
56 | (IDENT)
57 | (name
58 | (name
59 | (IDENT))
60 | (IDENT)))
61 | (import_clause
62 | (name
63 | (IDENT))
64 | (import_list
65 | (IDENT))))
66 | (IDENT)))))
67 |
68 | =============================================
69 | Long Class with Equation Container Expression
70 | =============================================
71 |
72 | model Resistor
73 | equation
74 | v = i * R "Ohm's Law";
75 | end Resistor;
76 |
77 | ---
78 |
79 | (stored_definitions
80 | (stored_definition
81 | (class_definition
82 | (class_prefixes)
83 | (long_class_specifier
84 | (IDENT)
85 | (equation_section
86 | (equation_list
87 | (simple_equation
88 | (simple_expression
89 | (primary_expression
90 | (component_reference
91 | (IDENT))))
92 | (expression
93 | (simple_expression
94 | (binary_expression
95 | (simple_expression
96 | (primary_expression
97 | (component_reference
98 | (IDENT))))
99 | (simple_expression
100 | (primary_expression
101 | (component_reference
102 | (IDENT)))))))
103 | (description_string
104 | (STRING)))))
105 | (IDENT)))))
106 |
107 | =============================
108 | Long Class with Function Call
109 | =============================
110 |
111 | model Resistor
112 | equation
113 | v = f(i);
114 | end Resistor;
115 |
116 | ---
117 |
118 | (stored_definitions
119 | (stored_definition
120 | (class_definition
121 | (class_prefixes)
122 | (long_class_specifier
123 | (IDENT)
124 | (equation_section
125 | (equation_list
126 | (simple_equation
127 | (simple_expression
128 | (primary_expression
129 | (component_reference
130 | (IDENT))))
131 | (expression
132 | (simple_expression
133 | (primary_expression
134 | (function_application
135 | (component_reference
136 | (IDENT))
137 | (function_call_args
138 | (function_arguments
139 | (expression
140 | (simple_expression
141 | (primary_expression
142 | (component_reference
143 | (IDENT))))))))))))))
144 | (IDENT)))))
145 |
146 |
147 | =======================================================
148 | Long Class with Function Call with Expression Arguments
149 | =======================================================
150 |
151 | model Resistor
152 | equation
153 | v = f(i*R);
154 | end Resistor;
155 |
156 | ---
157 |
158 | (stored_definitions
159 | (stored_definition
160 | (class_definition
161 | (class_prefixes)
162 | (long_class_specifier
163 | (IDENT)
164 | (equation_section
165 | (equation_list
166 | (simple_equation
167 | (simple_expression
168 | (primary_expression
169 | (component_reference
170 | (IDENT))))
171 | (expression
172 | (simple_expression
173 | (primary_expression
174 | (function_application
175 | (component_reference
176 | (IDENT))
177 | (function_call_args
178 | (function_arguments
179 | (expression
180 | (simple_expression
181 | (binary_expression
182 | (simple_expression
183 | (primary_expression
184 | (component_reference
185 | (IDENT))))
186 | (simple_expression
187 | (primary_expression
188 | (component_reference
189 | (IDENT))))))))))))))))
190 | (IDENT)))))
191 |
--------------------------------------------------------------------------------
/test/corpus/a2-4_component_clause.txt:
--------------------------------------------------------------------------------
1 | ================
2 | Component Clause
3 | ================
4 |
5 | model ComponentClauseExample
6 | Real temperature;
7 | Real pressure;
8 |
9 | Modelica.Blocks.Sources.Sine sine1(amplitude=1, frequency=2);
10 | Modelica.Blocks.Sources.Sine sine2(amplitude=0.5, frequency=5);
11 |
12 | equation
13 | temperature = sine1.y;
14 | pressure = sine2.y;
15 | end ComponentClauseExample;
16 |
17 | ---
18 |
19 | (stored_definitions
20 | (stored_definition
21 | (class_definition
22 | (class_prefixes)
23 | (long_class_specifier
24 | (IDENT)
25 | (element_list
26 | (named_element
27 | (component_clause
28 | (type_specifier
29 | (name
30 | (IDENT)))
31 | (component_list
32 | (component_declaration
33 | (declaration
34 | (IDENT))))))
35 | (named_element
36 | (component_clause
37 | (type_specifier
38 | (name
39 | (IDENT)))
40 | (component_list
41 | (component_declaration
42 | (declaration
43 | (IDENT))))))
44 | (named_element
45 | (component_clause
46 | (type_specifier
47 | (name
48 | (name
49 | (name
50 | (name
51 | (IDENT))
52 | (IDENT))
53 | (IDENT))
54 | (IDENT)))
55 | (component_list
56 | (component_declaration
57 | (declaration
58 | (IDENT)
59 | (modification
60 | (class_modification
61 | (argument_list
62 | (element_modification
63 | (name
64 | (IDENT))
65 | (modification
66 | (expression
67 | (simple_expression
68 | (primary_expression
69 | (literal_expression
70 | (unsigned_integer_literal_expression
71 | (UNSIGNED_INTEGER))))))))
72 | (element_modification
73 | (name
74 | (IDENT))
75 | (modification
76 | (expression
77 | (simple_expression
78 | (primary_expression
79 | (literal_expression
80 | (unsigned_integer_literal_expression
81 | (UNSIGNED_INTEGER))))))))))))))))
82 | (named_element
83 | (component_clause
84 | (type_specifier
85 | (name
86 | (name
87 | (name
88 | (name
89 | (IDENT))
90 | (IDENT))
91 | (IDENT))
92 | (IDENT)))
93 | (component_list
94 | (component_declaration
95 | (declaration
96 | (IDENT)
97 | (modification
98 | (class_modification
99 | (argument_list
100 | (element_modification
101 | (name
102 | (IDENT))
103 | (modification
104 | (expression
105 | (simple_expression
106 | (primary_expression
107 | (literal_expression
108 | (unsigned_real_literal_expression
109 | (UNSIGNED_REAL))))))))
110 | (element_modification
111 | (name
112 | (IDENT))
113 | (modification
114 | (expression
115 | (simple_expression
116 | (primary_expression
117 | (literal_expression
118 | (unsigned_integer_literal_expression
119 | (UNSIGNED_INTEGER)))))))))))))))))
120 | (equation_section
121 | (equation_list
122 | (simple_equation
123 | (simple_expression
124 | (primary_expression
125 | (component_reference
126 | (IDENT))))
127 | (expression
128 | (simple_expression
129 | (primary_expression
130 | (component_reference
131 | (component_reference
132 | (IDENT))
133 | (IDENT))))))
134 | (simple_equation
135 | (simple_expression
136 | (primary_expression
137 | (component_reference
138 | (IDENT))))
139 | (expression
140 | (simple_expression
141 | (primary_expression
142 | (component_reference
143 | (component_reference
144 | (IDENT))
145 | (IDENT))))))))
146 | (IDENT)))))
147 |
148 | ============================
149 | Long Class with Declarations
150 | ============================
151 |
152 | model Resistor
153 | Modelica.Electrical.Analog.Interface.PositivePin p;
154 | Modelica.Electrical.Analog.Interface.NegativePin n;
155 | Modelica.SIunits.Voltage v "Voltage";
156 | Modelica.SIunits.Current i "Current";
157 | end Resistor;
158 |
159 | ---
160 |
161 | (stored_definitions
162 | (stored_definition
163 | (class_definition
164 | (class_prefixes)
165 | (long_class_specifier
166 | (IDENT)
167 | (element_list
168 | (named_element
169 | (component_clause
170 | (type_specifier
171 | (name
172 | (name
173 | (name
174 | (name
175 | (name
176 | (IDENT))
177 | (IDENT))
178 | (IDENT))
179 | (IDENT))
180 | (IDENT)))
181 | (component_list
182 | (component_declaration
183 | (declaration
184 | (IDENT))))))
185 | (named_element
186 | (component_clause
187 | (type_specifier
188 | (name
189 | (name
190 | (name
191 | (name
192 | (name
193 | (IDENT))
194 | (IDENT))
195 | (IDENT))
196 | (IDENT))
197 | (IDENT)))
198 | (component_list
199 | (component_declaration
200 | (declaration
201 | (IDENT))))))
202 | (named_element
203 | (component_clause
204 | (type_specifier
205 | (name
206 | (name
207 | (name
208 | (IDENT))
209 | (IDENT))
210 | (IDENT)))
211 | (component_list
212 | (component_declaration
213 | (declaration
214 | (IDENT))
215 | (description_string
216 | (STRING))))))
217 | (named_element
218 | (component_clause
219 | (type_specifier
220 | (name
221 | (name
222 | (name
223 | (IDENT))
224 | (IDENT))
225 | (IDENT)))
226 | (component_list
227 | (component_declaration
228 | (declaration
229 | (IDENT))
230 | (description_string
231 | (STRING)))))))
232 | (IDENT)))))
233 |
--------------------------------------------------------------------------------
/test/corpus/a2-6-2_equations.txt:
--------------------------------------------------------------------------------
1 | ================================================================
2 | If Statement with Multiple Output Function Application Statement
3 | ================================================================
4 |
5 | model IfExample
6 | Real x = 10;
7 | Real y;
8 |
9 | function foo
10 | input Real bar;
11 | output Real result2;
12 | algorithm
13 | result2 := 3 * bar;
14 | end foo;
15 |
16 | algorithm
17 | if x > 5 then
18 | (y, _) := foo(bar = x);
19 | else
20 | (y, _) := foo(bar = -x);
21 | end if;
22 | end IfExample;
23 |
24 | ---
25 |
26 | (stored_definitions
27 | (stored_definition
28 | (class_definition
29 | (class_prefixes)
30 | (long_class_specifier
31 | (IDENT)
32 | (element_list
33 | (named_element
34 | (component_clause
35 | (type_specifier
36 | (name
37 | (IDENT)))
38 | (component_list
39 | (component_declaration
40 | (declaration
41 | (IDENT)
42 | (modification
43 | (expression
44 | (simple_expression
45 | (primary_expression
46 | (literal_expression
47 | (unsigned_integer_literal_expression
48 | (UNSIGNED_INTEGER))))))))))))
49 | (named_element
50 | (component_clause
51 | (type_specifier
52 | (name
53 | (IDENT)))
54 | (component_list
55 | (component_declaration
56 | (declaration
57 | (IDENT))))))
58 | (named_element
59 | (class_definition
60 | (class_prefixes)
61 | (long_class_specifier
62 | (IDENT)
63 | (element_list
64 | (named_element
65 | (component_clause
66 | (type_specifier
67 | (name
68 | (IDENT)))
69 | (component_list
70 | (component_declaration
71 | (declaration
72 | (IDENT))))))
73 | (named_element
74 | (component_clause
75 | (type_specifier
76 | (name
77 | (IDENT)))
78 | (component_list
79 | (component_declaration
80 | (declaration
81 | (IDENT)))))))
82 | (algorithm_section
83 | (statement_list
84 | (assignment_statement
85 | (component_reference
86 | (IDENT))
87 | (expression
88 | (simple_expression
89 | (binary_expression
90 | (simple_expression
91 | (primary_expression
92 | (literal_expression
93 | (unsigned_integer_literal_expression
94 | (UNSIGNED_INTEGER)))))
95 | (simple_expression
96 | (primary_expression
97 | (component_reference
98 | (IDENT))))))))))
99 | (IDENT)))))
100 | (algorithm_section
101 | (statement_list
102 | (if_statement
103 | (expression
104 | (simple_expression
105 | (binary_expression
106 | (simple_expression
107 | (primary_expression
108 | (component_reference
109 | (IDENT))))
110 | (simple_expression
111 | (primary_expression
112 | (literal_expression
113 | (unsigned_integer_literal_expression
114 | (UNSIGNED_INTEGER))))))))
115 | (statement_list
116 | (multiple_output_function_application_statement
117 | (parenthesized_expression
118 | (output_expression_list
119 | (expression
120 | (simple_expression
121 | (primary_expression
122 | (component_reference
123 | (IDENT)))))
124 | (expression
125 | (simple_expression
126 | (primary_expression
127 | (component_reference
128 | (IDENT)))))))
129 | (component_reference
130 | (IDENT))
131 | (function_call_args
132 | (named_arguments
133 | (named_argument
134 | (IDENT)
135 | (expression
136 | (simple_expression
137 | (primary_expression
138 | (component_reference
139 | (IDENT))))))))))
140 | (statement_list
141 | (multiple_output_function_application_statement
142 | (parenthesized_expression
143 | (output_expression_list
144 | (expression
145 | (simple_expression
146 | (primary_expression
147 | (component_reference
148 | (IDENT)))))
149 | (expression
150 | (simple_expression
151 | (primary_expression
152 | (component_reference
153 | (IDENT)))))))
154 | (component_reference
155 | (IDENT))
156 | (function_call_args
157 | (named_arguments
158 | (named_argument
159 | (IDENT)
160 | (expression
161 | (simple_expression
162 | (unary_expression
163 | (simple_expression
164 | (primary_expression
165 | (component_reference
166 | (IDENT)))))))))))))))
167 | (IDENT)))))
168 |
169 | =============================================
170 | Long Class with Function Application Equation
171 | =============================================
172 |
173 | model foo
174 | equation
175 | fae (bar = function ident());
176 | end foo;
177 |
178 | ---
179 |
180 | (stored_definitions
181 | (stored_definition
182 | (class_definition
183 | (class_prefixes)
184 | (long_class_specifier
185 | (IDENT)
186 | (equation_section
187 | (equation_list
188 | (function_application_equation
189 | (component_reference
190 | (IDENT))
191 | (function_call_args
192 | (named_arguments
193 | (named_argument
194 | (IDENT)
195 | (function_partial_application
196 | (type_specifier
197 | (name
198 | (IDENT))))))))))
199 | (IDENT)))))
200 |
201 | ==============================================
202 | Long Class with Function Application Statement
203 | ==============================================
204 |
205 | model foo
206 | algorithm
207 | fae (bar = function ident());
208 | end foo;
209 |
210 | ---
211 |
212 | (stored_definitions
213 | (stored_definition
214 | (class_definition
215 | (class_prefixes)
216 | (long_class_specifier
217 | (IDENT)
218 | (algorithm_section
219 | (statement_list
220 | (function_application_statement
221 | (component_reference
222 | (IDENT))
223 | (function_call_args
224 | (named_arguments
225 | (named_argument
226 | (IDENT)
227 | (function_partial_application
228 | (type_specifier
229 | (name
230 | (IDENT))))))))))
231 | (IDENT)))))
232 |
--------------------------------------------------------------------------------
/queries/highlights.scm:
--------------------------------------------------------------------------------
1 | ;;;;; Highlights in the examples happen between these symbols: >...<
2 |
3 |
4 | (ERROR) @error ;; when something is placed wrongfully
5 | ; error has to be added to the config.json file to be shown correctly ;;"error": {"color": "red", "bold": true, "underline": true}
6 |
7 | (string_literal_expression) @string ;; R(unit=>"Ohm"<)
8 |
9 |
10 | ;;; Comments
11 | [
12 | (BLOCK_COMMENT) ;; >/* comment */<
13 | (comment) ;; >// comment<
14 | (description_string) ;; model foo >"description"<
15 | ] @comment
16 |
17 |
18 | ;;; Numbers
19 | [
20 | (UNSIGNED_INTEGER) ;; >220<
21 | (UNSIGNED_REAL) ;; >3.14159<
22 | ] @number
23 |
24 |
25 | ;;; Types
26 | (type_specifier) @type.builtin ;; >Real< x
27 |
28 |
29 | ;;; Variables
30 | (enumeration_literal identifier: (IDENT) @variable.builtin) ;; type foo = enumeration(>bar<, >test<);
31 | (declaration identifier: (IDENT) @variable.builtin) ;; Resistor >R1< (R=10);
32 | (element_modification name: (name) @variable.parameter) ;; Resistor R1( >R< =10);
33 | (modification (expression (simple_expression (primary_expression (component_reference) @variable.parameter))))
34 | ;; e.g. annotation(derivative = >tsat_der<);
35 | ;; e.g. Resistor R1(R= >S<);
36 |
37 |
38 | ;;; Function Builtins
39 | (within_clause name: (name)) @function.builtin ;; within >Modelica<
40 | (extends_class_specifier identifier: (IDENT) @function.builtin) ;; extends >foo<
41 | (long_class_specifier identifier: (IDENT) @function.builtin) ;; function >foo<
42 | (short_class_specifier identifier: (IDENT) @function.builtin) ;; function >foo<
43 | (enumeration_class_specifier identifier: (IDENT) @function.builtin) ;; function >foo<
44 | (extends_class_specifier endIdentifier: (IDENT) @function.builtin) ;; end >foo<
45 | (long_class_specifier endIdentifier: (IDENT) @function.builtin) ;; end >foo<
46 |
47 |
48 | ;;; Imports
49 | (import_clause name: (name) @module) ;; import C = >Modelica.Units.SI<;
50 | (import_clause alias: (IDENT) @variable.builtin) ;; import >C< = Modelica.Units.SI;
51 |
52 |
53 | ;;; Constants
54 | (logical_literal_expression) @constant.builtin ;; >true< / >false<
55 |
56 |
57 | ;;; Function Application
58 | (function_application_statement functionReference: (component_reference) @function.builtin)
59 | ;; e.g. >assert<(ref==res, "unary minus failed");
60 | (function_application_equation functionReference: (component_reference) @function.builtin)
61 | ;; e.g. >assert<(ref==res, "unary minus failed");
62 | (function_application functionReference: (component_reference) @function.builtin)
63 | ;; e.g. Integer nX = >size<(X_boundary, 1);
64 | ;; e.g. final constant Integer Integer_inf = >OpenModelica.Internal<
65 | ;; e.g. Res:= >Modelica.ComplexMath.real<(c1)
66 | (multiple_output_function_application_statement (component_reference) @function.builtin)
67 | ;; e.g. (a, b, nextEventScaled, last) := >getInterpolationCoefficients<(table, offset, startTime/timeScale)
68 |
69 |
70 | ;;; Function Call Arguments
71 | (function_call_args (named_arguments (named_argument (IDENT) @variable.builtin)))
72 | ;; e.g. y = homotopy(>actual< = smooth(0));
73 | ;; e.g. y = homotopy(>simplified< = simplifiedExpr);
74 |
75 | ;;; Assignment Statements
76 | (assignment_statement (component_reference) @variable.builtin)
77 | ;; e.g. >Kelvin< := Celsius - Modelica.Constants.T_zero;
78 |
79 | ;;; For Loop
80 | (for_index (IDENT)) @variable.builtin
81 | ;; e.g. for >i< in >1:nPort< loop
82 |
83 | ;;; Connect Clause
84 | (connect_clause (component_reference) @variable.builtin)
85 | ;; e.g. connect(furnace.port, evaporator.heatPort);
86 |
87 | ;;; External Functions
88 | (language_specification value: (STRING) @module) ;; external >"builtin"< y = log(u);
89 | (external_function identifier: (IDENT) @function.builtin) ;; external "builtin" y = >log<(u);
90 | (external_function (component_reference) @variable.builtin) ;; external "builtin" >y< = log(u);
91 |
92 |
93 |
94 |
95 |
96 | ;;; Variables for Binary Expressions:
97 | (simple_expression (binary_expression (primary_expression (component_reference) @variable.builtin)))
98 |
99 | ;-------------------------------------
100 |
101 | ;;; binary expressions:
102 | ;(expression (simple_expression (binary_expression (primary_expression (component_reference))))) @variable.builtin
103 | ;; e.g. o[1] := >pi<^0.25;
104 |
105 | ;;; binary expressions within a binary_expression that has a component_references/text:
106 | ;(binary_expression (simple_expression (binary_expression (primary_expression (component_reference) @variable.builtin))))
107 | ;; e.g. h := (n[1] + n[2]*pi + n[3]*>pi<^2 + n[4]*>pi<^3)*hstar;
108 | ;; e.g. T := sum(n[i]*(pi + 0.240)^>I<[i]*(eta - 0.615)^>J<[i] for i in 1:31)*Tstar;
109 | ;; e.g. S := C[6]/>deltaTREL<^(3/5);
110 |
111 | ;-------------------------------------
112 |
113 |
114 |
115 | ;;; Variables:
116 | (simple_expression (primary_expression (component_reference) @variable.builtin))
117 |
118 | ;----------------------------------------
119 |
120 | ;;; every expression that starts with 'not', '+'' or '-'
121 | ;(unary_expression (simple_expression (primary_expression (component_reference) @variable.parameter)))
122 | ;; e.g. parameter Real uMin = ->uMax<;
123 |
124 | ;;; function call args:
125 | ;(function_call_args (function_arguments (expression (simple_expression (binary_expression (simple_expression (primary_expression (component_reference) @variable.builtin)))))))
126 | ;; e.g. assert(>ref<==>res<, "unary minus failed");
127 |
128 | ;(function_call_args (function_arguments (expression (simple_expression (primary_expression (component_reference) @variable.builtin)))))
129 | ;; e.g. Integer nX = size(>X_boundary<, 1);
130 | ;; e.g. if initType == Init.SteadyState then der(>x<) = 0;
131 |
132 | ;(function_call_args (named_arguments (named_argument (expression (simple_expression (primary_expression (component_reference) @variable.builtin))))))
133 | ;; e.g. y = homotopy(simplified = >simplifiedExpr<);
134 |
135 | ;;; assignment statements:
136 | ;(assignment_statement (expression (simple_expression (primary_expression (component_reference) @variable.builtin))))
137 | ;; algorithm b := >offset<;
138 |
139 | ;;; if_equation with binary expression that has a component_references/text (both sides):
140 | ;(if_equation (expression (simple_expression (binary_expression (simple_expression (primary_expression (component_reference) @variable.builtin))))))
141 | ;; e.g. if >initType< == >Init.SteadyState< then
142 |
143 | ;;; if_equation that has a component_reference/text:
144 | ;(if_equation (expression (simple_expression (primary_expression (component_reference) @variable.builtin))))
145 |
146 | ;;; elseif_clause that has a component_reference/text (both sides):
147 | ;(else_if_equation_clause (expression (simple_expression (binary_expression (simple_expression (primary_expression (component_reference) @variable.builtin))))))
148 | ;; e.g. elseif >initType< == >Init.InitialState< then
149 |
150 | ;;; then-expression that has a component_reference/text:
151 | ;(if_expression (expression (simple_expression (primary_expression (component_reference) @variable.builtin))))
152 |
153 | ;;; binary expressions that has a component_references/text:
154 | ;(binary_expression (simple_expression (primary_expression (component_reference) @variable.builtin)))
155 | ;; e.g. Real mu_0 = 4*>pi<;
156 | ;; e.g. der(x) = >u>T<;
157 | ;; e.g. y = >k<*(>x< + >u<);
158 |
159 | ;;; simple_equations:
160 | ;(simple_equation (simple_expression (primary_expression (component_reference) @variable.builtin)))
161 | ;; e.g. elseif initType == Init.InitialOutput then >y< = y_start;
162 |
163 | ;(simple_equation (expression (simple_expression (primary_expression (component_reference) @variable.builtin))))
164 | ;; e.g. elseif initType == Init.InitialOutput then y = >y_start<;
165 |
166 | ;(expression (simple_expression (primary_expression (component_reference) @variable.builtin)))
167 | ;; e.g. Blocks.Interfaces.RealInput q_F(unit = "MW") if >use_inputs<;
168 |
169 | ;;; expressions in parentheses:
170 | ;(output_expression_list (expression (simple_expression (primary_expression (component_reference) @variable.builtin))))
171 | ;; e.g. gamma := if aux.region == 3 then aux.T/(>aux.cv<) else -1/aux;
172 | ;; e.g. (>a<, >b<, >nextEventScaled<, >last<) := getInterpolationCoefficients
173 |
174 | ;----------------------------------------
175 |
176 |
177 |
178 | ;; KEYWORDS
179 |
180 | [
181 | (class_prefixes) ; >partial< / >class< / >connector< / >model< etc.
182 | (base_prefix) ; >input< / >output<
183 |
184 | "within"
185 | "encapsulated"
186 | "external"
187 |
188 | ; component clause
189 | "flow"
190 | "stream"
191 | "constant"
192 | "discrete"
193 | "parameter"
194 | "input"
195 | "output"
196 |
197 | ; element modification
198 | "each"
199 | "final"
200 | "inner"
201 | "outer"
202 |
203 | ; redecleration
204 | "redeclare"
205 | "replaceable"
206 |
207 | ; expression
208 |
209 | "initial"
210 | "pure"
211 | "public"
212 | "protected"
213 | "import"
214 | "der"
215 | "enumeration"
216 | "extends"
217 | "annotation"
218 | "function"
219 | "equation"
220 | "algorithm"
221 | "end"
222 | "constrainedby"
223 | ] @keyword [
224 |
225 | ;; equation and statement
226 |
227 | "or"
228 | "and"
229 | "not"
230 | "break"
231 | "return"
232 | "connect"
233 | "for"
234 | "in"
235 | "loop"
236 | "if"
237 | "elseif"
238 | "then"
239 | "else"
240 | "when"
241 | "elsewhen"
242 | "while"
243 | ] @constant
244 |
245 | ;; PUNCTUATION BRACKET
246 |
247 | [
248 | "("
249 | ")"
250 | "{"
251 | "}"
252 | "["
253 | "]"
254 | ] @punctuation.bracket
255 |
256 | ;; PUNCTUATION DELIMITER
257 |
258 | ;[ "," ";" ":"] @punctuation.delimiter
259 |
260 | ;; OPERATOR
261 |
262 | [
263 | ":"
264 | "="
265 | ":="
266 |
267 | "<"
268 | "<="
269 | ">"
270 | ">="
271 | "=="
272 | "<>"
273 | "+"
274 | "-"
275 | ".+"
276 | ".-"
277 | "*"
278 | "/"
279 | ".*"
280 | "./"
281 | "^"
282 | ".^"
283 | ] @operator
284 |
--------------------------------------------------------------------------------
/OSMC-License.txt:
--------------------------------------------------------------------------------
1 | --- Start of Definition of OSMC Public License ---
2 |
3 | /*
4 | * This file is part of OpenModelica.
5 | *
6 | * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
7 | * c/o Linköpings universitet, Department of Computer and Information Science,
8 | * SE-58183 Linköping, Sweden.
9 | *
10 | * All rights reserved.
11 | *
12 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
13 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
14 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
15 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
16 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
17 | *
18 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
19 | * Public License (OSMC-PL) are obtained from OSMC, either from the above
20 | * address, from the URLs:
21 | * http://www.openmodelica.org or
22 | * https://github.com/OpenModelica/ or
23 | * http://www.ida.liu.se/projects/OpenModelica,
24 | * and in the OpenModelica distribution.
25 | *
26 | * GNU AGPL version 3 is obtained from:
27 | * https://www.gnu.org/licenses/licenses.html#GPL
28 | *
29 | * This program is distributed WITHOUT ANY WARRANTY; without
30 | * even the implied warranty of MERCHANTABILITY or FITNESS
31 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
32 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
33 | *
34 | * See the full OSMC Public License conditions for more details.
35 | *
36 | */
37 |
38 | --- End of OSMC Public License Header ---
39 |
40 | The OSMC-PL is a public license for OpenModelica with three modes/alternatives
41 | (AGPL, OSMC-Internal-EPL, OSMC-External-EPL) for use and redistribution,
42 | in source and/or binary/object-code form:
43 |
44 | * AGPL. Any party (member or non-member of OSMC) may use and redistribute
45 | OpenModelica under GNU AGPL version 3.
46 |
47 | * Level 1 members of OSMC may also use and redistribute OpenModelica under
48 | OSMC-Internal-EPL conditions.
49 |
50 | * Level 2 members of OSMC may also use and redistribute OpenModelica under
51 | OSMC-Internal-EPL or OSMC-External-EPL conditions.
52 | Definitions of OSMC Public license modes:
53 |
54 | * AGPL = GNU AGPL version 3.
55 |
56 | * OSMC-Internal-EPL = These OSMC Public license conditions together with
57 | Internally restricted EPL, i.e., EPL version 1.0 with the Additional
58 | Condition that use and redistribution by an OSMC member is only allowed
59 | within the OSMC member's own organization (i.e., its own legal entity),
60 | or for an OSMC member paying an annual fee corresponding to the size
61 | of the organization including all its affiliates, use and redistribution
62 | is allowed within/between its affiliates.
63 |
64 | * OSMC-External-EPL = These OSMC Public license conditions together with
65 | Externally restricted EPL, i.e., EPL version 1.0 with the Additional
66 | Condition that use and redistribution by an OSMC member, or by a Licensed
67 | Third Party Distributor having a redistribution agreement with that member,
68 | to parties external to the OSMC member’s own organization (i.e., its own
69 | legal entity) is only allowed in binary/object-code form, except the case of
70 | redistribution to other OSMC members to which source is also allowed to be
71 | distributed.
72 |
73 | [This has the consequence that an external party who wishes to use
74 | OpenModelica in source form together with its own proprietary software in all
75 | cases must be a member of OSMC].
76 |
77 | In all cases of usage and redistribution by recipients, the following
78 | conditions also apply:
79 |
80 | a) Redistributions of source code must retain the above copyright notice,
81 | all definitions, and conditions. It is sufficient if the OSMC-PL Header
82 | is present in each source file, if the full OSMC-PL is available in a
83 | prominent and easily located place in the redistribution.
84 |
85 | b) Redistributions in binary/object-code form must reproduce the above
86 | copyright notice, all definitions, and conditions. It is sufficient if the
87 | OSMC-PL Header and the location in the redistribution of the full OSMC-PL
88 | are present in the documentation and/or other materials provided with the
89 | redistribution, if the full OSMC-PL is available in a prominent and easily
90 | located place in the redistribution.
91 |
92 | c) A recipient must clearly indicate its chosen usage mode of OSMC-PL,
93 | in accompanying documentation and in a text file OSMC-USAGE-MODE.txt,
94 | provided with the distribution.
95 |
96 | d) Contributor(s) making a Contribution to OpenModelica thereby also makes a
97 | Transfer of Contribution Copyright. In return, upon the effective date of
98 | the transfer, OSMC grants the Contributor(s) a Contribution License of the
99 | Contribution. OSMC has the right to accept or refuse Contributions.
100 |
101 | Definitions:
102 |
103 | "Subsidiary license conditions" means:
104 |
105 | The additional license conditions depending on the by the recipient chosen
106 | mode of OSMC-PL, defined by GNU AGPL version 3.0 for AGPL, and by EPL for
107 | OSMC-Internal-EPL and OSMC-External-EPL.
108 | "OSMC-PL" means:
109 |
110 | Open Source Modelica Consortium Public License version 1.8, i.e., the license
111 | defined here (the text between
112 | "--- Start of Definition of OSMC Public License ---" and
113 | "--- End of Definition of OSMC Public License ---", or later versions thereof.
114 |
115 | "OSMC-PL Header" means:
116 |
117 | Open Source Modelica Consortium Public License Header version 1.8, i.e., the
118 | text between "--- Start of Definition of OSMC Public License ---" and
119 | "--- End of OSMC Public License Header ---", or later versions thereof.
120 |
121 | "Contribution" means:
122 |
123 | a) in the case of the initial Contributor, the initial code and documentation
124 | distributed under OSMC-PL, and
125 |
126 | b) in the case of each subsequent Contributor:
127 | i) changes to OpenModelica, and
128 | ii) additions to OpenModelica;
129 |
130 | where such changes and/or additions to OpenModelica originate from and are
131 | distributed by that particular Contributor. A Contribution 'originates' from
132 | a Contributor if it was added to OpenModelica by such Contributor itself or
133 | anyone acting on such Contributor's behalf.
134 |
135 | For Contributors licensing OpenModelica under OSMC-Internal-EPL or
136 | OSMC-External-EPL conditions, the following conditions also hold:
137 |
138 | Contributions do not include additions to the distributed Program which: (i)
139 | are separate modules of software distributed in conjunction with OpenModelica
140 | under their own license agreement, (ii) are separate modules which are not
141 | derivative works of OpenModelica, and (iii) are separate modules of software
142 | distributed in conjunction with OpenModelica under their own license agreement
143 | where these separate modules are merged with (weaved together with) modules of
144 | OpenModelica to form new modules that are distributed as object code or source
145 | code under their own license agreement, as allowed under the Additional
146 | Condition of internal distribution according to OSMC-Internal-EPL and/or
147 | Additional Condition for external distribution according to OSMC-External-EPL.
148 |
149 | "Transfer of Contribution Copyright" means that the Contributors of a
150 | Contribution transfer the ownership and the copyright of the Contribution to
151 | Open Source Modelica Consortium, the OpenModelica Copyright owner, for
152 | inclusion in OpenModelica. The transfer takes place upon the effective date
153 | when the Contribution is made available on the OSMC web site under OSMC-PL, by
154 | such Contributors themselves or anyone acting on such Contributors' behalf.
155 | The transfer is free of charge. If the Contributors or OSMC so wish,
156 | an optional Copyright transfer agreement can be signed between OSMC and the
157 | Contributors, as specified in an Appendix of the OSMC Bylaws.
158 |
159 | "Contribution License" means a license from OSMC to the Contributors of the
160 | Contribution, effective on the date of the Transfer of Contribution Copyright,
161 | where OSMC grants the Contributors a non-exclusive, world-wide, transferable,
162 | free of charge, perpetual license, including sublicensing rights, to use,
163 | have used, modify, have modified, reproduce and or have reproduced the
164 | contributed material, for business and other purposes, including but not
165 | limited to evaluation, development, testing, integration and merging with
166 | other software and distribution. The warranty and liability disclaimers of
167 | OSMC-PL apply to this license.
168 |
169 | "Contributor" means any person or entity that distributes (part of)
170 | OpenModelica.
171 |
172 | "The Program" means the Contributions distributed in accordance with OSMC-PL.
173 |
174 | "OpenModelica" means the Contributions distributed in accordance with OSMC-PL.
175 |
176 | "Recipient" means anyone who receives OpenModelica under OSMC-PL,
177 | including all Contributors.
178 |
179 | "Licensed Third Party Distributor" means a reseller/distributor having signed
180 | a redistribution/resale agreement in accordance with OSMC-PL and OSMC Bylaws,
181 | with an OSMC Level 2 organizational member which is not an Affiliate of the
182 | reseller/distributor, for distributing a product containing part(s) of
183 | OpenModelica. The Licensed Third Party Distributor shall only be allowed
184 | further redistribution to other resellers if the Level 2 member is granting
185 | such a right to it in the redistribution/resale agreement between the
186 | Level 2 member and the Licensed Third Party Distributor.
187 |
188 | "Affiliate" shall mean any legal entity, directly or indirectly, through one
189 | or more intermediaries, controlling or controlled by or under common control
190 | with any other legal entity, as the case may be. For purposes of this
191 | definition, the term "control" (including the terms "controlling",
192 | "controlled by" and "under common control with") means the possession,
193 | direct or indirect, of the power to direct or cause the direction of the
194 | management and policies of a legal entity, whether through the ownership of
195 | voting securities, by contract or otherwise.
196 |
197 | NO WARRANTY
198 |
199 | EXCEPT AS EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY
200 | LICENSE CONDITIONS OF OSMC-PL, OPENMODELICA IS PROVIDED ON AN "AS IS"
201 | BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
202 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF
203 | TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
204 | PURPOSE. Each Recipient is solely responsible for determining the
205 | appropriateness of using and distributing OPENMODELICA and assumes all risks
206 | associated with its exercise of rights under OSMC-PL , including but not
207 | limited to the risks and costs of program errors, compliance with applicable
208 | laws, damage to or loss of data, programs or equipment, and unavailability
209 | or interruption of operations.
210 |
211 | DISCLAIMER OF LIABILITY
212 |
213 | EXCEPT AS EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY
214 | LICENSE CONDITIONS OF OSMC-PL, NEITHER RECIPIENT NOR ANY CONTRIBUTORS
215 | SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
216 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
217 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
218 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
219 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF OPENMODELICA OR THE
220 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
221 | POSSIBILITY OF SUCH DAMAGES.
222 |
223 | A Contributor licensing OpenModelica under OSMC-Internal-EPL or
224 | OSMC-External-EPL may choose to distribute (parts of) OpenModelica in object
225 | code form under its own license agreement, provided that:
226 |
227 | a) it complies with the terms and conditions of OSMC-PL; or for the case of
228 | redistribution of OpenModelica together with proprietary code it is a dual
229 | license where the OpenModelica parts are distributed under OSMC-PL compatible
230 | conditions and the proprietary code is distributed under proprietary license
231 | conditions; and
232 |
233 | b) its license agreement:
234 | i) effectively disclaims on behalf of all Contributors all warranties and
235 | conditions, express and implied, including warranties or conditions of title
236 | and non-infringement, and implied warranties or conditions of merchantability
237 | and fitness for a particular purpose;
238 | ii) effectively excludes on behalf of all Contributors all liability for
239 | damages, including direct, indirect, special, incidental and consequential
240 | damages, such as lost profits;
241 | iii) states that any provisions which differ from OSMC-PL are offered by that
242 | Contributor alone and not by any other party; and
243 | iv) states from where the source code for OpenModelica is available, and
244 | informs licensees how to obtain it in a reasonable manner on or through a
245 | medium customarily used for software exchange.
246 |
247 | When OPENMODELICA is made available in source code form:
248 |
249 | a) it must be made available under OSMC-PL; and
250 |
251 | b) a copy of OSMC-PL must be included with each copy of OPENMODELICA.
252 |
253 | c) a copy of the subsidiary license associated with the selected mode of
254 | OSMC-PL must be included with each copy of OPENMODELICA.
255 |
256 | Contributors may not remove or alter any copyright notices contained within
257 | OPENMODELICA.
258 |
259 | If there is a conflict between OSMC-PL and the subsidiary license conditions,
260 | OSMC-PL has priority.
261 |
262 | This Agreement is governed by the laws of Sweden. The place of jurisdiction
263 | for all disagreements related to this Agreement, is Linköping, Sweden.
264 |
265 | The EPL 1.0 license definition has been obtained from:
266 | http://www.eclipse.org/legal/epl-v10.html. It is also reproduced in Appendix B
267 | of the OSMC Bylaws, and in the OpenModelica distribution.
268 |
269 | The AGPL Version 3 license definition has been obtained from
270 | https://www.gnu.org/licenses/licenses.html#GPL. It is also reproduced in
271 | Appendix C of the OSMC Bylaws, and in the OpenModelica distribution.
272 |
273 | --- End of Definition of OSMC Public License ---
274 |
--------------------------------------------------------------------------------
/test/corpus/a2-7_expressions:
--------------------------------------------------------------------------------
1 | ===============
2 | If Expression
3 | ===============
4 |
5 | model Test
6 | Real x;
7 | Real y;
8 | Real z = if a>0 then 1.0 elseif a<0 then 3.0 else 2.0;
9 |
10 | parameter Real a = 10;
11 | equation
12 | x = if a>0 then 1.0 else 2.0;
13 |
14 | if a>0 then
15 | y = 1.0;
16 | else
17 | y = 2.0;
18 | end if;
19 | end Test;
20 |
21 | ---
22 |
23 | (stored_definitions
24 | (stored_definition
25 | (class_definition
26 | (class_prefixes)
27 | (long_class_specifier
28 | (IDENT)
29 | (element_list
30 | (named_element
31 | (component_clause
32 | (type_specifier
33 | (name
34 | (IDENT)))
35 | (component_list
36 | (component_declaration
37 | (declaration
38 | (IDENT))))))
39 | (named_element
40 | (component_clause
41 | (type_specifier
42 | (name
43 | (IDENT)))
44 | (component_list
45 | (component_declaration
46 | (declaration
47 | (IDENT))))))
48 | (named_element
49 | (component_clause
50 | (type_specifier
51 | (name
52 | (IDENT)))
53 | (component_list
54 | (component_declaration
55 | (declaration
56 | (IDENT)
57 | (modification
58 | (expression
59 | (if_expression
60 | (expression
61 | (simple_expression
62 | (binary_expression
63 | (simple_expression
64 | (primary_expression
65 | (component_reference
66 | (IDENT))))
67 | (simple_expression
68 | (primary_expression
69 | (literal_expression
70 | (unsigned_integer_literal_expression
71 | (UNSIGNED_INTEGER))))))))
72 | (expression
73 | (simple_expression
74 | (primary_expression
75 | (literal_expression
76 | (unsigned_real_literal_expression
77 | (UNSIGNED_REAL))))))
78 | (else_if_clause
79 | (expression
80 | (simple_expression
81 | (binary_expression
82 | (simple_expression
83 | (primary_expression
84 | (component_reference
85 | (IDENT))))
86 | (simple_expression
87 | (primary_expression
88 | (literal_expression
89 | (unsigned_integer_literal_expression
90 | (UNSIGNED_INTEGER))))))))
91 | (expression
92 | (simple_expression
93 | (primary_expression
94 | (literal_expression
95 | (unsigned_real_literal_expression
96 | (UNSIGNED_REAL)))))))
97 | (expression
98 | (simple_expression
99 | (primary_expression
100 | (literal_expression
101 | (unsigned_real_literal_expression
102 | (UNSIGNED_REAL))))))))))))))
103 | (named_element
104 | (component_clause
105 | (type_specifier
106 | (name
107 | (IDENT)))
108 | (component_list
109 | (component_declaration
110 | (declaration
111 | (IDENT)
112 | (modification
113 | (expression
114 | (simple_expression
115 | (primary_expression
116 | (literal_expression
117 | (unsigned_integer_literal_expression
118 | (UNSIGNED_INTEGER)))))))))))))
119 | (equation_section
120 | (equation_list
121 | (simple_equation
122 | (simple_expression
123 | (primary_expression
124 | (component_reference
125 | (IDENT))))
126 | (expression
127 | (if_expression
128 | (expression
129 | (simple_expression
130 | (binary_expression
131 | (simple_expression
132 | (primary_expression
133 | (component_reference
134 | (IDENT))))
135 | (simple_expression
136 | (primary_expression
137 | (literal_expression
138 | (unsigned_integer_literal_expression
139 | (UNSIGNED_INTEGER))))))))
140 | (expression
141 | (simple_expression
142 | (primary_expression
143 | (literal_expression
144 | (unsigned_real_literal_expression
145 | (UNSIGNED_REAL))))))
146 | (expression
147 | (simple_expression
148 | (primary_expression
149 | (literal_expression
150 | (unsigned_real_literal_expression
151 | (UNSIGNED_REAL)))))))))
152 | (if_equation
153 | (expression
154 | (simple_expression
155 | (binary_expression
156 | (simple_expression
157 | (primary_expression
158 | (component_reference
159 | (IDENT))))
160 | (simple_expression
161 | (primary_expression
162 | (literal_expression
163 | (unsigned_integer_literal_expression
164 | (UNSIGNED_INTEGER))))))))
165 | (equation_list
166 | (simple_equation
167 | (simple_expression
168 | (primary_expression
169 | (component_reference
170 | (IDENT))))
171 | (expression
172 | (simple_expression
173 | (primary_expression
174 | (literal_expression
175 | (unsigned_real_literal_expression
176 | (UNSIGNED_REAL))))))))
177 | (equation_list
178 | (simple_equation
179 | (simple_expression
180 | (primary_expression
181 | (component_reference
182 | (IDENT))))
183 | (expression
184 | (simple_expression
185 | (primary_expression
186 | (literal_expression
187 | (unsigned_real_literal_expression
188 | (UNSIGNED_REAL)))))))))))
189 | (IDENT)))))
190 |
191 | =====================================
192 | Array Constructor and Array Arguments
193 | =====================================
194 |
195 | model foo
196 | Real arrayA = {1, 2, 3, 4, 5};
197 | end foo;
198 |
199 | ---
200 |
201 | (stored_definitions
202 | (stored_definition
203 | (class_definition
204 | (class_prefixes)
205 | (long_class_specifier
206 | (IDENT)
207 | (element_list
208 | (named_element
209 | (component_clause
210 | (type_specifier
211 | (name
212 | (IDENT)))
213 | (component_list
214 | (component_declaration
215 | (declaration
216 | (IDENT)
217 | (modification
218 | (expression
219 | (simple_expression
220 | (primary_expression
221 | (array_constructor
222 | (array_arguments
223 | (expression
224 | (simple_expression
225 | (primary_expression
226 | (literal_expression
227 | (unsigned_integer_literal_expression
228 | (UNSIGNED_INTEGER))))))
229 | (expression
230 | (simple_expression
231 | (primary_expression
232 | (literal_expression
233 | (unsigned_integer_literal_expression
234 | (UNSIGNED_INTEGER))))))
235 | (expression
236 | (simple_expression
237 | (primary_expression
238 | (literal_expression
239 | (unsigned_integer_literal_expression
240 | (UNSIGNED_INTEGER))))))
241 | (expression
242 | (simple_expression
243 | (primary_expression
244 | (literal_expression
245 | (unsigned_integer_literal_expression
246 | (UNSIGNED_INTEGER))))))
247 | (expression
248 | (simple_expression
249 | (primary_expression
250 | (literal_expression
251 | (unsigned_integer_literal_expression
252 | (UNSIGNED_INTEGER))))))))))))))))))
253 | (IDENT)))))
254 |
255 | ========================
256 | Array Comprehension
257 | ========================
258 |
259 | model foo
260 | Real acom = {10 for y};
261 | end foo;
262 |
263 | ---
264 |
265 | (stored_definitions
266 | (stored_definition
267 | (class_definition
268 | (class_prefixes)
269 | (long_class_specifier
270 | (IDENT)
271 | (element_list
272 | (named_element
273 | (component_clause
274 | (type_specifier
275 | (name
276 | (IDENT)))
277 | (component_list
278 | (component_declaration
279 | (declaration
280 | (IDENT)
281 | (modification
282 | (expression
283 | (simple_expression
284 | (primary_expression
285 | (array_comprehension
286 | (expression
287 | (simple_expression
288 | (primary_expression
289 | (literal_expression
290 | (unsigned_integer_literal_expression
291 | (UNSIGNED_INTEGER))))))
292 | (for_indices
293 | (for_index
294 | (IDENT))))))))))))))
295 | (IDENT)))))
296 |
297 | ===============================================
298 | Array Concatenation, Logical and String Literal
299 | ===============================================
300 |
301 | model foo
302 | Real array_c = [10; true; "STRING"];
303 | end foo;
304 |
305 | ---
306 |
307 | (stored_definitions
308 | (stored_definition
309 | (class_definition
310 | (class_prefixes)
311 | (long_class_specifier
312 | (IDENT)
313 | (element_list
314 | (named_element
315 | (component_clause
316 | (type_specifier
317 | (name
318 | (IDENT)))
319 | (component_list
320 | (component_declaration
321 | (declaration
322 | (IDENT)
323 | (modification
324 | (expression
325 | (simple_expression
326 | (primary_expression
327 | (array_concatenation
328 | (expression_list
329 | (expression
330 | (simple_expression
331 | (primary_expression
332 | (literal_expression
333 | (unsigned_integer_literal_expression
334 | (UNSIGNED_INTEGER)))))))
335 | (expression_list
336 | (expression
337 | (simple_expression
338 | (primary_expression
339 | (literal_expression
340 | (logical_literal_expression))))))
341 | (expression_list
342 | (expression
343 | (simple_expression
344 | (primary_expression
345 | (literal_expression
346 | (string_literal_expression
347 | (STRING))))))))))))))))))
348 | (IDENT)))))
349 |
--------------------------------------------------------------------------------
/test/corpus/a2-6_equations.txt:
--------------------------------------------------------------------------------
1 | ===============
2 | Simple Equation
3 | ===============
4 |
5 | model Resistor
6 | equation
7 | v = 2.0;
8 | end Resistor;
9 |
10 | ---
11 |
12 | (stored_definitions
13 | (stored_definition
14 | (class_definition
15 | (class_prefixes)
16 | (long_class_specifier
17 | (IDENT)
18 | (equation_section
19 | (equation_list
20 | (simple_equation
21 | (simple_expression
22 | (primary_expression
23 | (component_reference
24 | (IDENT))))
25 | (expression
26 | (simple_expression
27 | (primary_expression
28 | (literal_expression
29 | (unsigned_real_literal_expression
30 | (UNSIGNED_REAL)))))))))
31 | (IDENT)))))
32 |
33 | ===========
34 | If Equation
35 | ===========
36 |
37 | model IfEquationExample
38 | Real x = 10;
39 | Real y;
40 |
41 | equation
42 | if x > 5 then
43 | y = x;
44 | elseif x > 6 then
45 | y = 8;
46 | else
47 | y = 0;
48 | end if;
49 | end IfEquationExample;
50 |
51 | ---
52 |
53 | (stored_definitions
54 | (stored_definition
55 | (class_definition
56 | (class_prefixes)
57 | (long_class_specifier
58 | (IDENT)
59 | (element_list
60 | (named_element
61 | (component_clause
62 | (type_specifier
63 | (name
64 | (IDENT)))
65 | (component_list
66 | (component_declaration
67 | (declaration
68 | (IDENT)
69 | (modification
70 | (expression
71 | (simple_expression
72 | (primary_expression
73 | (literal_expression
74 | (unsigned_integer_literal_expression
75 | (UNSIGNED_INTEGER))))))))))))
76 | (named_element
77 | (component_clause
78 | (type_specifier
79 | (name
80 | (IDENT)))
81 | (component_list
82 | (component_declaration
83 | (declaration
84 | (IDENT)))))))
85 | (equation_section
86 | (equation_list
87 | (if_equation
88 | (expression
89 | (simple_expression
90 | (binary_expression
91 | (simple_expression
92 | (primary_expression
93 | (component_reference
94 | (IDENT))))
95 | (simple_expression
96 | (primary_expression
97 | (literal_expression
98 | (unsigned_integer_literal_expression
99 | (UNSIGNED_INTEGER))))))))
100 | (equation_list
101 | (simple_equation
102 | (simple_expression
103 | (primary_expression
104 | (component_reference
105 | (IDENT))))
106 | (expression
107 | (simple_expression
108 | (primary_expression
109 | (component_reference
110 | (IDENT)))))))
111 | (else_if_equation_clause_list
112 | (else_if_equation_clause
113 | (expression
114 | (simple_expression
115 | (binary_expression
116 | (simple_expression
117 | (primary_expression
118 | (component_reference
119 | (IDENT))))
120 | (simple_expression
121 | (primary_expression
122 | (literal_expression
123 | (unsigned_integer_literal_expression
124 | (UNSIGNED_INTEGER))))))))
125 | (equation_list
126 | (simple_equation
127 | (simple_expression
128 | (primary_expression
129 | (component_reference
130 | (IDENT))))
131 | (expression
132 | (simple_expression
133 | (primary_expression
134 | (literal_expression
135 | (unsigned_integer_literal_expression
136 | (UNSIGNED_INTEGER))))))))))
137 | (equation_list
138 | (simple_equation
139 | (simple_expression
140 | (primary_expression
141 | (component_reference
142 | (IDENT))))
143 | (expression
144 | (simple_expression
145 | (primary_expression
146 | (literal_expression
147 | (unsigned_integer_literal_expression
148 | (UNSIGNED_INTEGER)))))))))))
149 | (IDENT)))))
150 |
151 | ============
152 | If Statement
153 | ============
154 |
155 | model IfStatementExample
156 | Real x = 10;
157 | Real y;
158 |
159 | algorithm
160 | if x > 5 then
161 | y := x;
162 | elseif x > 6 then
163 | return;
164 | else
165 | break;
166 | end if;
167 | end IfStatementExample;
168 |
169 | ---
170 |
171 | (stored_definitions
172 | (stored_definition
173 | (class_definition
174 | (class_prefixes)
175 | (long_class_specifier
176 | (IDENT)
177 | (element_list
178 | (named_element
179 | (component_clause
180 | (type_specifier
181 | (name
182 | (IDENT)))
183 | (component_list
184 | (component_declaration
185 | (declaration
186 | (IDENT)
187 | (modification
188 | (expression
189 | (simple_expression
190 | (primary_expression
191 | (literal_expression
192 | (unsigned_integer_literal_expression
193 | (UNSIGNED_INTEGER))))))))))))
194 | (named_element
195 | (component_clause
196 | (type_specifier
197 | (name
198 | (IDENT)))
199 | (component_list
200 | (component_declaration
201 | (declaration
202 | (IDENT)))))))
203 | (algorithm_section
204 | (statement_list
205 | (if_statement
206 | (expression
207 | (simple_expression
208 | (binary_expression
209 | (simple_expression
210 | (primary_expression
211 | (component_reference
212 | (IDENT))))
213 | (simple_expression
214 | (primary_expression
215 | (literal_expression
216 | (unsigned_integer_literal_expression
217 | (UNSIGNED_INTEGER))))))))
218 | (statement_list
219 | (assignment_statement
220 | (component_reference
221 | (IDENT))
222 | (expression
223 | (simple_expression
224 | (primary_expression
225 | (component_reference
226 | (IDENT)))))))
227 | (else_if_statement_clause_list
228 | (else_if_statement_clause
229 | (expression
230 | (simple_expression
231 | (binary_expression
232 | (simple_expression
233 | (primary_expression
234 | (component_reference
235 | (IDENT))))
236 | (simple_expression
237 | (primary_expression
238 | (literal_expression
239 | (unsigned_integer_literal_expression
240 | (UNSIGNED_INTEGER))))))))
241 | (statement_list
242 | (return_statement))))
243 | (statement_list
244 | (break_statement)))))
245 | (IDENT)))))
246 |
247 | ============
248 | For Equation
249 | ============
250 |
251 | model ForLoopExample
252 | parameter Integer n = 5;
253 | Real sum = 0;
254 |
255 | equation
256 | for i in 1:n loop
257 | sum = sum + i;
258 | end for;
259 | end ForLoopExample;
260 |
261 | ---
262 |
263 | (stored_definitions
264 | (stored_definition
265 | (class_definition
266 | (class_prefixes)
267 | (long_class_specifier
268 | (IDENT)
269 | (element_list
270 | (named_element
271 | (component_clause
272 | (type_specifier
273 | (name
274 | (IDENT)))
275 | (component_list
276 | (component_declaration
277 | (declaration
278 | (IDENT)
279 | (modification
280 | (expression
281 | (simple_expression
282 | (primary_expression
283 | (literal_expression
284 | (unsigned_integer_literal_expression
285 | (UNSIGNED_INTEGER))))))))))))
286 | (named_element
287 | (component_clause
288 | (type_specifier
289 | (name
290 | (IDENT)))
291 | (component_list
292 | (component_declaration
293 | (declaration
294 | (IDENT)
295 | (modification
296 | (expression
297 | (simple_expression
298 | (primary_expression
299 | (literal_expression
300 | (unsigned_integer_literal_expression
301 | (UNSIGNED_INTEGER)))))))))))))
302 | (equation_section
303 | (equation_list
304 | (for_equation
305 | (for_indices
306 | (for_index
307 | (IDENT)
308 | (expression
309 | (range_expression
310 | (simple_expression
311 | (primary_expression
312 | (literal_expression
313 | (unsigned_integer_literal_expression
314 | (UNSIGNED_INTEGER)))))
315 | (simple_expression
316 | (primary_expression
317 | (component_reference
318 | (IDENT))))))))
319 | (equation_list
320 | (simple_equation
321 | (simple_expression
322 | (primary_expression
323 | (component_reference
324 | (IDENT))))
325 | (expression
326 | (simple_expression
327 | (binary_expression
328 | (simple_expression
329 | (primary_expression
330 | (component_reference
331 | (IDENT))))
332 | (simple_expression
333 | (primary_expression
334 | (component_reference
335 | (IDENT))))))))))))
336 | (IDENT)))))
337 |
338 | =============
339 | For Statement
340 | =============
341 |
342 | model ForStatementExample
343 | parameter Integer n = 5;
344 | Real sum = 0;
345 |
346 | algorithm
347 | for i in 1:n loop
348 | sum := sum + i;
349 | end for;
350 | end ForStatementExample;
351 |
352 | ---
353 |
354 | (stored_definitions
355 | (stored_definition
356 | (class_definition
357 | (class_prefixes)
358 | (long_class_specifier
359 | (IDENT)
360 | (element_list
361 | (named_element
362 | (component_clause
363 | (type_specifier
364 | (name
365 | (IDENT)))
366 | (component_list
367 | (component_declaration
368 | (declaration
369 | (IDENT)
370 | (modification
371 | (expression
372 | (simple_expression
373 | (primary_expression
374 | (literal_expression
375 | (unsigned_integer_literal_expression
376 | (UNSIGNED_INTEGER))))))))))))
377 | (named_element
378 | (component_clause
379 | (type_specifier
380 | (name
381 | (IDENT)))
382 | (component_list
383 | (component_declaration
384 | (declaration
385 | (IDENT)
386 | (modification
387 | (expression
388 | (simple_expression
389 | (primary_expression
390 | (literal_expression
391 | (unsigned_integer_literal_expression
392 | (UNSIGNED_INTEGER)))))))))))))
393 | (algorithm_section
394 | (statement_list
395 | (for_statement
396 | (for_indices
397 | (for_index
398 | (IDENT)
399 | (expression
400 | (range_expression
401 | (simple_expression
402 | (primary_expression
403 | (literal_expression
404 | (unsigned_integer_literal_expression
405 | (UNSIGNED_INTEGER)))))
406 | (simple_expression
407 | (primary_expression
408 | (component_reference
409 | (IDENT))))))))
410 | (statement_list
411 | (assignment_statement
412 | (component_reference
413 | (IDENT))
414 | (expression
415 | (simple_expression
416 | (binary_expression
417 | (simple_expression
418 | (primary_expression
419 | (component_reference
420 | (IDENT))))
421 | (simple_expression
422 | (primary_expression
423 | (component_reference
424 | (IDENT))))))))))))
425 | (IDENT)))))
426 |
427 | ===============
428 | While Statement
429 | ===============
430 |
431 | model WhileStatementExample
432 | Real x = 1;
433 | Real sum = 0;
434 |
435 | algorithm
436 | while x <= 10 loop
437 | sum := sum + x;
438 | x := x + 1;
439 | end while;
440 | end WhileStatementExample;
441 |
442 |
443 | ---
444 |
445 | (stored_definitions
446 | (stored_definition
447 | (class_definition
448 | (class_prefixes)
449 | (long_class_specifier
450 | (IDENT)
451 | (element_list
452 | (named_element
453 | (component_clause
454 | (type_specifier
455 | (name
456 | (IDENT)))
457 | (component_list
458 | (component_declaration
459 | (declaration
460 | (IDENT)
461 | (modification
462 | (expression
463 | (simple_expression
464 | (primary_expression
465 | (literal_expression
466 | (unsigned_integer_literal_expression
467 | (UNSIGNED_INTEGER))))))))))))
468 | (named_element
469 | (component_clause
470 | (type_specifier
471 | (name
472 | (IDENT)))
473 | (component_list
474 | (component_declaration
475 | (declaration
476 | (IDENT)
477 | (modification
478 | (expression
479 | (simple_expression
480 | (primary_expression
481 | (literal_expression
482 | (unsigned_integer_literal_expression
483 | (UNSIGNED_INTEGER)))))))))))))
484 | (algorithm_section
485 | (statement_list
486 | (while_statement
487 | (expression
488 | (simple_expression
489 | (binary_expression
490 | (simple_expression
491 | (primary_expression
492 | (component_reference
493 | (IDENT))))
494 | (simple_expression
495 | (primary_expression
496 | (literal_expression
497 | (unsigned_integer_literal_expression
498 | (UNSIGNED_INTEGER))))))))
499 | (statement_list
500 | (assignment_statement
501 | (component_reference
502 | (IDENT))
503 | (expression
504 | (simple_expression
505 | (binary_expression
506 | (simple_expression
507 | (primary_expression
508 | (component_reference
509 | (IDENT))))
510 | (simple_expression
511 | (primary_expression
512 | (component_reference
513 | (IDENT))))))))
514 | (assignment_statement
515 | (component_reference
516 | (IDENT))
517 | (expression
518 | (simple_expression
519 | (binary_expression
520 | (simple_expression
521 | (primary_expression
522 | (component_reference
523 | (IDENT))))
524 | (simple_expression
525 | (primary_expression
526 | (literal_expression
527 | (unsigned_integer_literal_expression
528 | (UNSIGNED_INTEGER)))))))))))))
529 | (IDENT)))))
530 |
531 | =============
532 | When Equation
533 | =============
534 |
535 | model WhenEquationExample
536 | Real x = 5;
537 | Real y = 0;
538 |
539 | equation
540 | when x > 0.5 then
541 | y = x * x;
542 | elsewhen x < -0.5 then
543 | y = -x * x;
544 | end when;
545 | end WhenEquationExample;
546 |
547 | ---
548 |
549 | (stored_definitions
550 | (stored_definition
551 | (class_definition
552 | (class_prefixes)
553 | (long_class_specifier
554 | (IDENT)
555 | (element_list
556 | (named_element
557 | (component_clause
558 | (type_specifier
559 | (name
560 | (IDENT)))
561 | (component_list
562 | (component_declaration
563 | (declaration
564 | (IDENT)
565 | (modification
566 | (expression
567 | (simple_expression
568 | (primary_expression
569 | (literal_expression
570 | (unsigned_integer_literal_expression
571 | (UNSIGNED_INTEGER))))))))))))
572 | (named_element
573 | (component_clause
574 | (type_specifier
575 | (name
576 | (IDENT)))
577 | (component_list
578 | (component_declaration
579 | (declaration
580 | (IDENT)
581 | (modification
582 | (expression
583 | (simple_expression
584 | (primary_expression
585 | (literal_expression
586 | (unsigned_integer_literal_expression
587 | (UNSIGNED_INTEGER)))))))))))))
588 | (equation_section
589 | (equation_list
590 | (when_equation
591 | (expression
592 | (simple_expression
593 | (binary_expression
594 | (simple_expression
595 | (primary_expression
596 | (component_reference
597 | (IDENT))))
598 | (simple_expression
599 | (primary_expression
600 | (literal_expression
601 | (unsigned_real_literal_expression
602 | (UNSIGNED_REAL))))))))
603 | (equation_list
604 | (simple_equation
605 | (simple_expression
606 | (primary_expression
607 | (component_reference
608 | (IDENT))))
609 | (expression
610 | (simple_expression
611 | (binary_expression
612 | (simple_expression
613 | (primary_expression
614 | (component_reference
615 | (IDENT))))
616 | (simple_expression
617 | (primary_expression
618 | (component_reference
619 | (IDENT)))))))))
620 | (else_when_equation_clause_list
621 | (else_when_equation_clause
622 | (expression
623 | (simple_expression
624 | (binary_expression
625 | (simple_expression
626 | (primary_expression
627 | (component_reference
628 | (IDENT))))
629 | (simple_expression
630 | (unary_expression
631 | (simple_expression
632 | (primary_expression
633 | (literal_expression
634 | (unsigned_real_literal_expression
635 | (UNSIGNED_REAL))))))))))
636 | (equation_list
637 | (simple_equation
638 | (simple_expression
639 | (primary_expression
640 | (component_reference
641 | (IDENT))))
642 | (expression
643 | (simple_expression
644 | (binary_expression
645 | (simple_expression
646 | (unary_expression
647 | (simple_expression
648 | (primary_expression
649 | (component_reference
650 | (IDENT))))))
651 | (simple_expression
652 | (primary_expression
653 | (component_reference
654 | (IDENT))))))))))))))
655 | (IDENT)))))
656 |
657 | ==============
658 | When Statement
659 | ==============
660 |
661 | model WhenStatementExample
662 | Real x = 5;
663 | Real y = 0;
664 |
665 | algorithm
666 | when x > 0.5 then
667 | y := x * x;
668 | elsewhen x < -0.5 then
669 | y := -x * x;
670 | end when;
671 | end WhenStatementExample;
672 |
673 | ---
674 |
675 | (stored_definitions
676 | (stored_definition
677 | (class_definition
678 | (class_prefixes)
679 | (long_class_specifier
680 | (IDENT)
681 | (element_list
682 | (named_element
683 | (component_clause
684 | (type_specifier
685 | (name
686 | (IDENT)))
687 | (component_list
688 | (component_declaration
689 | (declaration
690 | (IDENT)
691 | (modification
692 | (expression
693 | (simple_expression
694 | (primary_expression
695 | (literal_expression
696 | (unsigned_integer_literal_expression
697 | (UNSIGNED_INTEGER))))))))))))
698 | (named_element
699 | (component_clause
700 | (type_specifier
701 | (name
702 | (IDENT)))
703 | (component_list
704 | (component_declaration
705 | (declaration
706 | (IDENT)
707 | (modification
708 | (expression
709 | (simple_expression
710 | (primary_expression
711 | (literal_expression
712 | (unsigned_integer_literal_expression
713 | (UNSIGNED_INTEGER)))))))))))))
714 | (algorithm_section
715 | (statement_list
716 | (when_statement
717 | (expression
718 | (simple_expression
719 | (binary_expression
720 | (simple_expression
721 | (primary_expression
722 | (component_reference
723 | (IDENT))))
724 | (simple_expression
725 | (primary_expression
726 | (literal_expression
727 | (unsigned_real_literal_expression
728 | (UNSIGNED_REAL))))))))
729 | (statement_list
730 | (assignment_statement
731 | (component_reference
732 | (IDENT))
733 | (expression
734 | (simple_expression
735 | (binary_expression
736 | (simple_expression
737 | (primary_expression
738 | (component_reference
739 | (IDENT))))
740 | (simple_expression
741 | (primary_expression
742 | (component_reference
743 | (IDENT)))))))))
744 | (else_when_statement_clause_list
745 | (else_when_statement_clause
746 | (expression
747 | (simple_expression
748 | (binary_expression
749 | (simple_expression
750 | (primary_expression
751 | (component_reference
752 | (IDENT))))
753 | (simple_expression
754 | (unary_expression
755 | (simple_expression
756 | (primary_expression
757 | (literal_expression
758 | (unsigned_real_literal_expression
759 | (UNSIGNED_REAL))))))))))
760 | (statement_list
761 | (assignment_statement
762 | (component_reference
763 | (IDENT))
764 | (expression
765 | (simple_expression
766 | (binary_expression
767 | (simple_expression
768 | (unary_expression
769 | (simple_expression
770 | (primary_expression
771 | (component_reference
772 | (IDENT))))))
773 | (simple_expression
774 | (primary_expression
775 | (component_reference
776 | (IDENT))))))))))))))
777 | (IDENT)))))
778 |
779 | ================
780 | Connect Equation
781 | ================
782 |
783 | model ConnectEquationExample
784 | Real inputPort;
785 | Real outputPort;
786 |
787 | equation
788 | connect(inputPort, outputPort);
789 | end ConnectEquationExample;
790 |
791 | ---
792 |
793 | (stored_definitions
794 | (stored_definition
795 | (class_definition
796 | (class_prefixes)
797 | (long_class_specifier
798 | (IDENT)
799 | (element_list
800 | (named_element
801 | (component_clause
802 | (type_specifier
803 | (name
804 | (IDENT)))
805 | (component_list
806 | (component_declaration
807 | (declaration
808 | (IDENT))))))
809 | (named_element
810 | (component_clause
811 | (type_specifier
812 | (name
813 | (IDENT)))
814 | (component_list
815 | (component_declaration
816 | (declaration
817 | (IDENT)))))))
818 | (equation_section
819 | (equation_list
820 | (connect_clause
821 | (component_reference
822 | (IDENT))
823 | (component_reference
824 | (IDENT)))))
825 | (IDENT)))))
826 |
--------------------------------------------------------------------------------
/grammar.js:
--------------------------------------------------------------------------------
1 | /*
2 | * OMFrontend.js
3 | * Copyright (C) 2022 Perpetual Labs, Ltd.
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Affero General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Affero General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Affero General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | /**
20 | * @author Mohamad Omar Nachawati
21 | */
22 |
23 | module.exports = grammar({
24 | name: "modelica",
25 | conflicts: $ => [
26 | [$.component_list],
27 | [$.equation_list],
28 | [$.function_arguments]
29 | ],
30 | extras: $ => [
31 | $.comment,
32 | $.BLOCK_COMMENT,
33 | $._SPACE
34 | ],
35 | word: $ => $.IDENT,
36 | rules: {
37 |
38 | // A.2.1 Stored Definition – Within
39 |
40 | stored_definitions: $ => seq(
41 | optional($.BOM),
42 | optional($.within_clause),
43 | repeat(field("storedDefinitions", $.stored_definition))
44 | ),
45 | within_clause: $ => seq("within", optional(field("name", $.name)), ";"),
46 |
47 | stored_definition: $ => seq(
48 | optional(field("final", "final")),
49 | field("classDefinition", $.class_definition), ";"
50 | ),
51 |
52 | // A.2.2 Class Definition
53 |
54 | class_definition: $ => seq(
55 | optional(field("encapsulated", "encapsulated")),
56 | field("classPrefixes", $.class_prefixes),
57 | field("classSpecifier", $._class_specifier)
58 | ),
59 |
60 | class_prefixes: $ => seq(
61 | optional(field("partial", "partial")),
62 | choice(
63 | field("block", "block"),
64 | field("class", "class"),
65 | seq(
66 | optional(field("expandable", "expandable")),
67 | field("connector", "connector")),
68 | seq(
69 | optional(choice(field("impure", "impure"), field("pure", "pure"))),
70 | optional(field("operator", "operator")),
71 | field("function", "function")),
72 | field("model", "model"),
73 | field("operator", "operator"),
74 | field("package", "package"),
75 | seq(
76 | optional(field("operator", "operator")),
77 | field("record", "record")),
78 | field("type", "type")
79 | )
80 | ),
81 |
82 | _class_specifier: $ => choice(
83 | $.derivative_class_specifier,
84 | $.enumeration_class_specifier,
85 | $.extends_class_specifier,
86 | $.long_class_specifier,
87 | $.short_class_specifier
88 | ),
89 |
90 | derivative_class_specifier: $ => seq(
91 | field("identifier", $.IDENT), "=", "der",
92 | "(", field("typeSpecifier", $.type_specifier), ",",
93 | field("argument", $.IDENT), optional(seq(",", field("argument", $.IDENT))), ")",
94 | optional(field("descriptionString", $.description_string)),
95 | optional(field("annotationClause", $.annotation_clause))
96 | ),
97 |
98 | enumeration_class_specifier: $ => seq(
99 | field("identifier", $.IDENT), "=", "enumeration", "(",
100 | choice(
101 | optional(field("enumerationLiterals", $.enum_list)),
102 | field("unspecified", ":")
103 | ),
104 | ")",
105 | optional(field("descriptionString", $.description_string)),
106 | optional(field("annotationClause", $.annotation_clause))
107 | ),
108 |
109 | enum_list: $ => seq(
110 | field("enumerationLiteral", $.enumeration_literal), repeat(seq(",", field("enumerationLiteral", $.enumeration_literal)))
111 | ),
112 |
113 | enumeration_literal: $ => seq(
114 | field("identifier", $.IDENT),
115 | optional(field("descriptionString", $.description_string)),
116 | optional(field("annotationClause", $.annotation_clause))
117 | ),
118 |
119 | extends_class_specifier: $ => seq(
120 | "extends",
121 | field("identifier", $.IDENT),
122 | optional(field("classModification", $.class_modification)),
123 | optional(field("descriptionString", $.description_string)),
124 | optional($.element_list),
125 | repeat(choice(
126 | $.public_element_list,
127 | $.protected_element_list,
128 | $.algorithm_section,
129 | $.equation_section)),
130 | optional(field("externalClause", $.external_clause)),
131 | optional(seq(field("annotationClause", $.annotation_clause), ";")),
132 | "end",
133 | field("endIdentifier", $.IDENT)
134 | ),
135 |
136 | long_class_specifier: $ => seq(
137 | field("identifier", $.IDENT),
138 | optional(field("descriptionString", $.description_string)),
139 | optional($.element_list),
140 | repeat(choice(
141 | $.public_element_list,
142 | $.protected_element_list,
143 | $.algorithm_section,
144 | $.equation_section
145 | )),
146 | optional(field("externalClause", $.external_clause)),
147 | optional(seq(field("annotationClause", $.annotation_clause), ";")),
148 | "end",
149 | field("endIdentifier", $.IDENT)
150 | ),
151 |
152 | short_class_specifier: $ => seq(
153 | field("identifier", $.IDENT),
154 | "=",
155 | optional(field("basePrefix", $.base_prefix)),
156 | field("typeSpecifier", $.type_specifier),
157 | optional(field("subscripts", $.array_subscripts)),
158 | optional(field("classModification", $.class_modification)),
159 | optional(field("descriptionString", $.description_string)),
160 | optional(field("annotationClause", $.annotation_clause))
161 | ),
162 |
163 | external_clause: $ => seq(
164 | "external",
165 | optional(field("languageSpecification", $.language_specification)),
166 | optional(field("externalFunction", $.external_function)),
167 | optional(field("annotationClause", $.annotation_clause)),
168 | ";"
169 | ),
170 |
171 | language_specification: $ => field("value", $.STRING),
172 |
173 | external_function: $ => seq(
174 | optional(seq(field("componentReference", $.component_reference), "=")),
175 | field("identifier", $.IDENT), "(", optional(field("expressions", $.expression_list)), ")"
176 | ),
177 |
178 | base_prefix: $ => choice("input", "output"),
179 |
180 | element_list: $ => seq(
181 | repeat1(seq(field("element", $._element), ";"))
182 | ),
183 |
184 | public_element_list: $ => seq(
185 | "public", repeat(seq(field("element", $._element), ";"))
186 | ),
187 |
188 | protected_element_list: $ => seq(
189 | "protected", repeat(seq(field("element", $._element), ";"))
190 | ),
191 |
192 | _element: $ => choice(
193 | $.extends_clause,
194 | $.import_clause,
195 | $.named_element
196 | ),
197 |
198 | named_element: $ => seq(
199 | optional(field("redeclare", "redeclare")),
200 | optional(field("final", "final")),
201 | optional(field("inner", "inner")),
202 | optional(field("outer", "outer")),
203 | optional(field("replaceable", "replaceable")),
204 | choice(
205 | field("classDefinition", $.class_definition),
206 | field("componentClause", $.component_clause)
207 | ),
208 | optional(seq(
209 | field("constrainingClause", $.constraining_clause),
210 | optional(field("descriptionString", $.description_string)),
211 | optional(field("annotationClause", $.annotation_clause))
212 | )),
213 | ),
214 |
215 | import_clause: $ => seq(
216 | "import",
217 | choice(
218 | seq(field("alias", $.IDENT), "=", field("name", $.name)),
219 | seq(
220 | field("name", $.name),
221 | optional(
222 | seq(".",
223 | choice(
224 | field("wildcard", "*"),
225 | seq("{", field("imports", $.import_list), "}")
226 | )
227 | )
228 | )
229 | )
230 | ),
231 | optional(field("descriptionString", $.description_string)),
232 | optional(field("annotationClause", $.annotation_clause)),
233 | ),
234 |
235 | import_list: $ => seq(
236 | field("import", $.IDENT), repeat(seq(",", field("import", $.IDENT)))
237 | ),
238 |
239 | // A.2.3 Extends
240 |
241 | extends_clause: $ => seq(
242 | "extends",
243 | field("typeSpecifier", $.type_specifier),
244 | optional(field("classModification", $.class_modification)),
245 | optional(field("annotationClause", $.annotation_clause)),
246 | ),
247 |
248 | constraining_clause: $ => seq(
249 | "constrainedby",
250 | field("typeSpecifier", $.type_specifier),
251 | optional(field("classModification", $.class_modification))
252 | ),
253 |
254 | // A.2.4 Component Clause
255 |
256 | component_clause: $ => seq(
257 | optional(choice(field("flow", "flow"), field("stream", "stream"))),
258 | optional(choice(field("constant", "constant"), field("discrete", "discrete"), field("parameter", "parameter"))),
259 | optional(choice(field("input", "input"), field("output", "output"))),
260 | field("typeSpecifier", $.type_specifier),
261 | optional(field("subscripts", $.array_subscripts)),
262 | field("componentDeclarations", $.component_list)
263 | ),
264 |
265 | component_list: $ => seq(
266 | field("componentDeclaration", $.component_declaration),
267 | repeat(seq(",", field("componentDeclaration", $.component_declaration)))
268 | ),
269 |
270 | component_declaration: $ => seq(
271 | field("declaration", $.declaration),
272 | optional(seq("if", field("condition", $.expression))),
273 | optional(field("descriptionString", $.description_string)),
274 | optional(field("annotationClause", $.annotation_clause))
275 | ),
276 |
277 | declaration: $ => seq(
278 | field("identifier", $.IDENT),
279 | optional(field("subscripts", $.array_subscripts)),
280 | optional(field("modification", $.modification))
281 | ),
282 |
283 | // A.2.5 Modification
284 |
285 | modification: $ => choice(
286 | seq(field("classModification", $.class_modification), optional(seq("=", field("expression", $.expression)))),
287 | seq(choice("=", ":="), field("expression", $.expression))
288 | ),
289 |
290 | class_modification: $ => seq(
291 | "(", optional(field("arguments", $.argument_list)), ")"
292 | ),
293 |
294 | argument_list: $ => seq(
295 | field("argument", $._argument), repeat(seq(",", field("argument", $._argument)))
296 | ),
297 |
298 | _argument: $ => choice(
299 | $.element_modification,
300 | $._element_redeclaration
301 | ),
302 |
303 | element_modification: $ => seq(
304 | optional(field("each", "each")),
305 | optional(field("final", "final")),
306 | field("name", $.name),
307 | optional(field("modification", $.modification)),
308 | optional(field("descriptionString", $.description_string))
309 | ),
310 |
311 | _element_redeclaration: $ => choice(
312 | $.class_redeclaration,
313 | $.component_redeclaration
314 | ),
315 |
316 | class_redeclaration: $ => seq(
317 | optional(field("redeclare", "redeclare")),
318 | optional(field("each", "each")),
319 | optional(field("final", "final")),
320 | optional(field("replaceable", "replaceable")),
321 | field("classDefinition", $.short_class_definition),
322 | optional(field("constrainingClause", $.constraining_clause))
323 | ),
324 |
325 | component_redeclaration: $ => seq(
326 | optional(field("redeclare", "redeclare")),
327 | optional(field("each", "each")),
328 | optional(field("final", "final")),
329 | optional(field("replaceable", "replaceable")),
330 | field("componentClause", $.component_clause),
331 | optional(field("constrainingClause", $.constraining_clause))
332 | ),
333 |
334 | short_class_definition: $ => seq(
335 | field("classPrefixes", $.class_prefixes),
336 | field("classSpecifier", choice(
337 | $.enumeration_class_specifier,
338 | $.short_class_specifier
339 | ))
340 | ),
341 |
342 | // A.2.6 Equations
343 |
344 | equation_section: $ => prec.right(seq(
345 | optional(field("initial", "initial")), "equation",
346 | optional(field("equations", $.equation_list))
347 | )),
348 |
349 | algorithm_section: $ => prec.right(seq(
350 | optional(field("initial", "initial")), "algorithm",
351 | optional(field("statements", $.statement_list))
352 | )),
353 |
354 | equation_list: $ => repeat1(seq(field("equation", $._equation), ";")),
355 |
356 | _equation: $ => choice(
357 | $.connect_clause,
358 | $.for_equation,
359 | $.function_application_equation,
360 | $.if_equation,
361 | $.simple_equation,
362 | $.when_equation
363 | ),
364 |
365 | statement_list: $ => repeat1(seq(field("statement", $._statement), ";")),
366 |
367 | _statement: $ => choice(
368 | $.assignment_statement,
369 | $.break_statement,
370 | $.for_statement,
371 | $.function_application_statement,
372 | $.if_statement,
373 | $.multiple_output_function_application_statement,
374 | $.return_statement,
375 | $.when_statement,
376 | $.while_statement
377 | ),
378 |
379 | assignment_statement: $ => seq(
380 | field("targetExpression", $.component_reference), ":=", field("sourceExpression", $.expression),
381 | optional(field("descriptionString", $.description_string)),
382 | optional(field("annotationClause", $.annotation_clause))
383 | ),
384 |
385 | break_statement: $ => seq(
386 | "break",
387 | optional(field("descriptionString", $.description_string)),
388 | optional(field("annotationClause", $.annotation_clause))
389 | ),
390 |
391 | connect_clause: $ => seq(
392 | "connect",
393 | "(",
394 | field("component1", $.component_reference),
395 | ",",
396 | field("component2", $.component_reference),
397 | ")",
398 | optional(field("descriptionString", $.description_string)),
399 | optional(field("annotationClause", $.annotation_clause))
400 | ),
401 |
402 | for_equation: $ => seq(
403 | "for", field("indices", $.for_indices),
404 | "loop", optional(field("equations", $.equation_list)),
405 | "end", "for",
406 | optional(field("descriptionString", $.description_string)),
407 | optional(field("annotationClause", $.annotation_clause))
408 | ),
409 |
410 | for_statement: $ => seq(
411 | "for", field("indices", $.for_indices),
412 | "loop", optional(field("statements", $.statement_list)),
413 | "end", "for",
414 | optional(field("descriptionString", $.description_string)),
415 | optional(field("annotationClause", $.annotation_clause))
416 | ),
417 |
418 | for_indices: $ => seq(field("index", $.for_index), repeat(seq(",", field("index", $.for_index)))),
419 |
420 | for_index: $ => seq(field("identifier", $.IDENT), optional(seq("in", field("expression", $.expression)))),
421 |
422 | function_application_equation: $ => seq(
423 | field("functionReference", $.component_reference),
424 | field("arguments", $.function_call_args),
425 | optional(field("descriptionString", $.description_string)),
426 | optional(field("annotationClause", $.annotation_clause))
427 | ),
428 |
429 | function_application_statement: $ => seq(
430 | field("functionReference", $.component_reference),
431 | field("arguments", $.function_call_args),
432 | optional(field("descriptionString", $.description_string)),
433 | optional(field("annotationClause", $.annotation_clause))
434 | ),
435 |
436 | if_equation: $ => seq(
437 | "if", field("condition", $.expression),
438 | "then", optional(field("thenEquations", $.equation_list)),
439 | optional(field("elseIfClauses", $.else_if_equation_clause_list)),
440 | optional(seq("else", optional(field("elseEquations", $.equation_list)))),
441 | "end", "if",
442 | optional(field("descriptionString", $.description_string)),
443 | optional(field("annotationClause", $.annotation_clause))
444 | ),
445 |
446 | else_if_equation_clause_list: $ => repeat1($.else_if_equation_clause),
447 |
448 | else_if_equation_clause: $ => prec.right(seq(
449 | "elseif", field("condition", $.expression),
450 | "then", optional(field("equations", $.equation_list))
451 | )),
452 |
453 | if_statement: $ => seq(
454 | "if", field("condition", $.expression),
455 | "then", optional(field("thenStatements", $.statement_list)),
456 | optional(field("elseIfClauses", $.else_if_statement_clause_list)),
457 | optional(seq("else", optional(field("elseStatements", $.statement_list)))),
458 | "end", "if",
459 | optional(field("descriptionString", $.description_string)),
460 | optional(field("annotationClause", $.annotation_clause))
461 | ),
462 |
463 | else_if_statement_clause_list: $ => repeat1($.else_if_statement_clause),
464 |
465 | else_if_statement_clause: $ => prec.right(seq(
466 | "elseif", field("condition", $.expression),
467 | "then", optional(field("statements", $.statement_list))
468 | )),
469 |
470 | multiple_output_function_application_statement: $ => seq(
471 | field("targetExpression", $.parenthesized_expression), ":=",
472 | field("functionReference", $.component_reference),
473 | field("arguments", $.function_call_args),
474 | optional(field("descriptionString", $.description_string)),
475 | optional(field("annotationClause", $.annotation_clause))
476 | ),
477 |
478 | return_statement: $ => seq(
479 | "return",
480 | optional(field("descriptionString", $.description_string)),
481 | optional(field("annotationClause", $.annotation_clause))
482 | ),
483 |
484 | simple_equation: $ => seq(
485 | field("expression1", $.simple_expression), "=", field("expression2", $.expression),
486 | optional(field("descriptionString", $.description_string)),
487 | optional(field("annotationClause", $.annotation_clause)),
488 | ),
489 |
490 | when_equation: $ => seq(
491 | "when", field("condition", $.expression),
492 | "then", optional(field("equations", $.equation_list)),
493 | optional(field("elseWhenClauses", $.else_when_equation_clause_list)),
494 | "end", "when",
495 | optional(field("descriptionString", $.description_string)),
496 | optional(field("annotationClause", $.annotation_clause))
497 | ),
498 |
499 | else_when_equation_clause_list: $ => repeat1($.else_when_equation_clause),
500 |
501 | else_when_equation_clause: $ => prec.right(seq(
502 | "elsewhen", field("condition", $.expression),
503 | "then", optional(field("equations", $.equation_list))
504 | )),
505 |
506 | when_statement: $ => seq(
507 | "when", field("condition", $.expression),
508 | "then", optional(field("statements", $.statement_list)),
509 | optional(field("elseWhenClauses", $.else_when_statement_clause_list)),
510 | "end", "when",
511 | optional(field("descriptionString", $.description_string)),
512 | optional(field("annotationClause", $.annotation_clause))
513 | ),
514 |
515 | else_when_statement_clause_list: $ => repeat1($.else_when_statement_clause),
516 |
517 | else_when_statement_clause: $ => prec.right(seq(
518 | "elsewhen", field("condition", $.expression),
519 | "then", optional(field("statements", $.statement_list))
520 | )),
521 |
522 | while_statement: $ => seq(
523 | "while", field("condition", $.expression),
524 | "loop", optional(field("statements", $.statement_list)),
525 | "end", "while",
526 | optional(field("descriptionString", $.description_string)),
527 | optional(field("annotationClause", $.annotation_clause))
528 | ),
529 |
530 | // A.2.7 Expressions
531 |
532 | expression: $ => choice(
533 | $.if_expression,
534 | $.range_expression,
535 | $.simple_expression
536 | ),
537 |
538 | if_expression: $ => seq(
539 | "if", field("condition", $.expression),
540 | "then", field("thenExpression", $.expression),
541 | repeat(field("elseIfClause", $.else_if_clause)),
542 | "else", field("elseExpression", $.expression)
543 | ),
544 |
545 | else_if_clause: $ => seq(
546 | "elseif", field("condition", $.expression),
547 | "then", field("expression", $.expression)
548 | ),
549 |
550 | range_expression: $ => choice(
551 | seq(field("startExpression", $.simple_expression), ":",
552 | field("stepExpression", $.simple_expression), ":",
553 | field("stopExpression", $.simple_expression)),
554 | seq(field("startExpression", $.simple_expression), ":",
555 | field("stopExpression", $.simple_expression))
556 | ),
557 |
558 | simple_expression: $ => choice(
559 | $.unary_expression,
560 | $.binary_expression,
561 | $.primary_expression
562 | ),
563 |
564 | binary_expression: $ => choice(
565 | prec.left(1, seq(field("operand1", $.simple_expression), field("operator", "or"), field("operand2", $.simple_expression))),
566 | prec.left(2, seq(field("operand1", $.simple_expression), field("operator", "and"), field("operand2", $.simple_expression))),
567 | prec.right(3, seq(field("operand1", $.simple_expression), field("operator", "<"), field("operand2", $.simple_expression))),
568 | prec.right(3, seq(field("operand1", $.simple_expression), field("operator", "<="), field("operand2", $.simple_expression))),
569 | prec.right(3, seq(field("operand1", $.simple_expression), field("operator", ">"), field("operand2", $.simple_expression))),
570 | prec.right(3, seq(field("operand1", $.simple_expression), field("operator", ">="), field("operand2", $.simple_expression))),
571 | prec.right(3, seq(field("operand1", $.simple_expression), field("operator", "=="), field("operand2", $.simple_expression))),
572 | prec.right(3, seq(field("operand1", $.simple_expression), field("operator", "<>"), field("operand2", $.simple_expression))),
573 | prec.left(4, seq(field("operand1", $.simple_expression), field("operator", "+"), field("operand2", $.simple_expression))),
574 | prec.left(4, seq(field("operand1", $.simple_expression), field("operator", "-"), field("operand2", $.simple_expression))),
575 | prec.left(4, seq(field("operand1", $.simple_expression), field("operator", ".+"), field("operand2", $.simple_expression))),
576 | prec.left(4, seq(field("operand1", $.simple_expression), field("operator", ".-"), field("operand2", $.simple_expression))),
577 | prec.left(5, seq(field("operand1", $.simple_expression), field("operator", "*"), field("operand2", $.simple_expression))),
578 | prec.left(5, seq(field("operand1", $.simple_expression), field("operator", "/"), field("operand2", $.simple_expression))),
579 | prec.left(5, seq(field("operand1", $.simple_expression), field("operator", ".*"), field("operand2", $.simple_expression))),
580 | prec.left(5, seq(field("operand1", $.simple_expression), field("operator", "./"), field("operand2", $.simple_expression))),
581 | prec.right(6, seq(field("operand1", $.primary_expression), field("operator", "^"), field("operand2", $.primary_expression))),
582 | prec.right(6, seq(field("operand1", $.primary_expression), field("operator", ".^"), field("operand2", $.primary_expression))),
583 | ),
584 |
585 | unary_expression: $ => prec(7, choice(
586 | seq(field("operator", "not"), field("operand", $.simple_expression)),
587 | seq(field("operator", "+"), field("operand", $.simple_expression)),
588 | seq(field("operator", "-"), field("operand", $.simple_expression))
589 | )),
590 |
591 | primary_expression: $ => choice(
592 | $.array_comprehension,
593 | $.array_concatenation,
594 | $.array_constructor,
595 | $.component_reference,
596 | $.end_expression,
597 | $.function_application,
598 | $.literal_expression,
599 | $.parenthesized_expression
600 | ),
601 |
602 | end_expression: $ => "end",
603 |
604 | array_comprehension: $ => seq(
605 | "{", field("expression", $.expression), "for", field("indices", $.for_indices), "}"
606 | ),
607 |
608 | array_concatenation: $ => seq(
609 | "[", field("expressions", $.expression_list), repeat(seq(";", field("expressions", $.expression_list))), "]"
610 | ),
611 |
612 | array_constructor: $ => seq(
613 | "{", field("arguments", $.array_arguments), "}"
614 | ),
615 |
616 | array_arguments: $ => seq(
617 | field("argument", $.expression), repeat(seq(",", field("argument", $.expression)))
618 | ),
619 |
620 | parenthesized_expression: $ => seq(
621 | "(", optional(field("expressions", $.output_expression_list)), ")"
622 | ),
623 |
624 | function_application: $ => seq(
625 | choice(
626 | field("functionReference", $.component_reference),
627 | field("der", "der"), field("initial", "initial"), field("pure", "pure")
628 | ),
629 | field("arguments", $.function_call_args)
630 | ),
631 |
632 | literal_expression: $ => choice(
633 | $.logical_literal_expression,
634 | $.string_literal_expression,
635 | $._unsigned_number_literal_expression,
636 | ),
637 |
638 | logical_literal_expression: $ => choice("false", "true"),
639 |
640 | string_literal_expression: $ => $.STRING,
641 |
642 | _unsigned_number_literal_expression: $ => choice(
643 | $.unsigned_integer_literal_expression,
644 | $.unsigned_real_literal_expression,
645 | ),
646 |
647 | unsigned_integer_literal_expression: $ => $.UNSIGNED_INTEGER,
648 |
649 | unsigned_real_literal_expression: $ => $.UNSIGNED_REAL,
650 |
651 | type_specifier: $ => seq(
652 | optional(field("global", ".")), field("name", $.name)
653 | ),
654 |
655 | name: $ => prec.left(choice(
656 | seq(field("qualifier", $.name), ".", field("identifier", $.IDENT)),
657 | field("identifier", $.IDENT)
658 | )),
659 |
660 | component_reference: $ => prec.left(choice(
661 | seq(
662 | field("qualifier", $.component_reference), ".", field("identifier", $.IDENT), optional(field("subscripts", $.array_subscripts))
663 | ),
664 | seq(
665 | optional(field("global", ".")), field("identifier", $.IDENT), optional(field("subscripts", $.array_subscripts))
666 | )
667 | )),
668 |
669 | function_call_args: $ => choice(
670 | seq("(", field("arguments", $.function_arguments), optional(seq(",", field("namedArguments", $.named_arguments))), ")"),
671 | seq("(", optional(field("namedArguments", $.named_arguments)), ")"),
672 | seq("(", field("index", $.expression), "for", field("indices", $.for_indices), ")")
673 | ),
674 |
675 | function_arguments: $ => seq(
676 | field("argument", $._function_argument), repeat(seq(",", field("argument", $._function_argument)))
677 | ),
678 |
679 | named_arguments: $ => seq(
680 | field("namedArgument", $.named_argument), repeat(seq(",", field("namedArgument", $.named_argument)))
681 | ),
682 |
683 | named_argument: $ => seq(
684 | field("identifier", $.IDENT), "=", field("expression", $._function_argument)
685 | ),
686 |
687 | _function_argument: $ => choice(
688 | $.function_partial_application,
689 | $.expression
690 | ),
691 |
692 | function_partial_application: $ => seq(
693 | "function", field("typeSpecifier", $.type_specifier), "(", optional(field("namedArguments", $.named_arguments)), ")"
694 | ),
695 |
696 | output_expression_list: $ => choice(
697 | seq(field("expression", $.expression), repeat(seq(field("comma", ","), optional(field("expression", $.expression))))),
698 | repeat1(seq(field("comma", ","), optional(field("expression", $.expression))))
699 | ),
700 |
701 | expression_list: $ => seq(
702 | field("expression", $.expression), repeat(seq(",", field("expression", $.expression)))
703 | ),
704 |
705 | array_subscripts: $ => seq(
706 | "[", field("subscript", $.subscript), repeat(seq(",", field("subscript", $.subscript))), "]"
707 | ),
708 |
709 | subscript: $ => choice(
710 | ":", field("expression", $.expression)
711 | ),
712 |
713 | description_string: $ => seq(
714 | field("value", $.STRING), repeat(seq("+", field("value", $.STRING)))
715 | ),
716 |
717 | annotation_clause: $ => seq(
718 | "annotation", field("classModification", $.class_modification)
719 | ),
720 |
721 | // A.1 Lexical conventions
722 |
723 | BOM: $ => /\u00EF\u00BB\u00BF/,
724 |
725 | IDENT: $ => token(choice(
726 | seq(/[_a-zA-Z]/, repeat(choice(/[0-9]/, /[_a-zA-Z]/))),
727 | seq("’", repeat(choice(
728 | /[_a-zA-Z]/, /[0-9]/, "!", "#", "$", "%", "&", "(", ")",
729 | "*", "+", ",", "-", ".", "/", ":", ";", "<", ">", "=",
730 | "?", "@", "[", "]", "^", "{", "}", "|", "~", " ", "\"",
731 | seq("\\", choice("’", "'", "\"", "?", "\\", "a", "b", "f", "n", "r", "t", "v")))), "’"))),
732 |
733 | STRING: $ => token(seq("\"", repeat(choice(/[^"\\]/,
734 | seq("\\", choice("’", "'", "\"", "?", "\\", "a", "b", "f", "n", "r", "t", "v")))), "\"")),
735 |
736 | UNSIGNED_INTEGER: $ => /[0-9]+/,
737 |
738 | UNSIGNED_REAL: $ => token(choice(
739 | seq(/[0-9]+/, ".", optional(/[0-9]+/)),
740 | seq(/[0-9]+/, optional(seq(".", optional(/[0-9]+/))), choice("e", "E"), optional(choice("+", "-")), /[0-9]+/),
741 | seq(".", /[0-9]+/, optional(seq(choice("e", "E"), optional(choice("+", "-")), /[0-9]+/)))
742 | )),
743 |
744 | BLOCK_COMMENT: $ => token(
745 | seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/")
746 | ),
747 |
748 | comment: $ => token(
749 | seq("//", /[^\r\n]*/)
750 | ),
751 |
752 | _SPACE: $ => /\s+/
753 |
754 | }
755 |
756 | });
757 |
--------------------------------------------------------------------------------