Skip to content

Commit

Permalink
Adding MetaModelica 2.0 with some updates (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnHeuermann authored Apr 15, 2024
1 parent 2e73e09 commit 7ea467b
Show file tree
Hide file tree
Showing 33 changed files with 3,458 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ target/

# VisualStudio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

# tree-sitter-metamodelica

An [open-source](OSMC-License.txt) MetaModelica ([Meta-Programming and Language
Modeling with MetaModelica
1.0 Appendix A](https://www.researchgate.net/publication/272070589_Meta-Programming_and_Language_Modeling_with_MetaModelica_10))
An [open-source](OSMC-License.txt) MetaModelica ([MetaModelica 2.0](https://liu.diva-portal.org/smash/record.jsf?pid=diva2%3A418188&dswid=-9758))
grammar and highlighting-query for
[tree-sitter](https://github.com/tree-sitter/tree-sitter).

Expand Down Expand Up @@ -39,9 +37,8 @@ npx tree-sitter parse examples/Main.mo

## Highlighting

There is also a highlighting query included. Make sure that the [tree-sitter
per-user
configuration](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#per-user-configuration)
There is also a highlighting query included. Make sure that the
[tree-sitter per-user configuration](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#per-user-configuration)
are pointing to the parent directory of `tree-sitter-metamodelica`. So if this
directory is in `/home/USER/workspace/tree-sitter-metamodelica` add
`/home/USER/workspace` to the parser directories:
Expand All @@ -63,8 +60,7 @@ npx tree-sitter highlight examples/Main.mo

## Usage

Use [Web
Tree-sitter](https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/README.md)
Use [Web Tree-sitter](https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/README.md)
`tree-sitter-metamodelica.wasm` in your application:

```typescript
Expand All @@ -73,8 +69,8 @@ import * as Parser from 'web-tree-sitter'
await Parser.init()
const parser = new Parser

const Modelica = await Parser.Language.load(`tree-sitter-metamodelica.wasm`)
parser.setLanguage(Modelica)
const MetaModelica = await Parser.Language.load(`tree-sitter-metamodelica.wasm`)
parser.setLanguage(MetaModelica)
```

## Current Status
Expand Down
19 changes: 17 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,23 @@
module.exports = grammar({
name: "metamodelica",

extras: $ => [
$.COMMENT,
$.BLOCK_COMMENT,
$._SPACE
],

word: $ => $.IDENT,

rules: {
// TODO: add the actual grammar rules
source_file: $ => 'hello'
...require("./rules/a1-classAndMainGrammar"),
...require("./rules/a2-extends"),
...require("./rules/a3-modification"),
...require("./rules/a4-equations"),
...require("./rules/a5-expressions"),
...require("./rules/a6-metamodelicaExtensions"),
...require("./rules/builtin"),
...require("./rules/keywords"),
...require("./rules/lexicalConventions"),
}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"scope": "source.metamodelica",
"file-types": [
"mo"
]
],
"highlights": "queries/highlights.scm"
}
]
}
177 changes: 177 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
;;;;; Highlights in the examples happen between these symbols: >...<
; List of available captures for tree-sitter highlights can be found in
; https://github.com/tree-sitter/tree-sitter/blob/dcb7acede4b31fedfc9d65b902fc95f6fe5ea099/cli/src/highlight.rs#L143-L171


;;; Errors
(ERROR) @error ;; when something is placed wrong
; error has to be added to the config.json file to be shown correctly ;;"error": {"color": "red", "bold": true, "underline": true}

;;; Strings
(STRING) @string ;; R(unit=>"Ohm"<)

;;; Comments
[
(BLOCK_COMMENT) ;; >/* comment */<
(COMMENT) ;; >// comment<
]
(string_comment (STRING) @comment) ;; model foo >"description"<

;;; Numbers
[
(UNSIGNED_INTEGER) ;; >220<
(UNSIGNED_REAL) ;; >3.14159<
] @number

;;; Types
(type_specifier (name_path (IDENT) @type )) ;; >A<.>MyType< x
(T_REAL) @type.builtin ;; >Real< x
(T_INTEGER) @type.builtin ;; >Integer< x
(T_BOOLEAN) @type.builtin ;; >Boolean< x
(T_STRING) @type.builtin ;; >String< x
(T_LIST) @type.builtin ;; >List<<Real> x
(T_OPTION) @type.builtin ;; >Option< <Real> x
(T_TUPLE) @type.builtin ;; >Tuple< <T1, T2, T3> x

;;; Variables
(declaration (IDENT) @variable.parameter) ;; Real >x<
(component_reference_function_call componentReference: (component_reference) @variable.parameter) ;; >x<

;;; Function calls
(component_reference_function_call functionName: (component_reference) @function)

;;; Classes
(class_definition (class_type class: (CLASS))(class_specifier (identifier) @module)) ;; class >A< end >A<;
(class_definition (class_type optimization: (OPTIMIZATION))(class_specifier (identifier) @module)) ;; optimization >A< end >A<;
(class_definition (class_type model: (MODEL))(class_specifier (identifier) @module)) ;; model >A< end >A<;
(class_definition (class_type record: (RECORD))(class_specifier (identifier) @type)) ;; record >R< end >R<;
(class_definition (class_type connector: (CONNECTOR))(class_specifier (identifier) @module)) ;; connector >C< end >C<;
(class_definition (class_type type: (TYPE))(class_specifier (identifier) @type)) ;; type >T< end >T<;
(class_definition (class_type package: (PACKAGE))(class_specifier (identifier) @module)) ;; package >A< end >A<;
(class_definition (class_type function: (FUNCTION))(class_specifier (identifier) @function)) ;; function >foo< end >foo<;
(class_definition (class_type uniontype: (UNIONTYPE))(class_specifier (identifier) @type)) ;; uniontype >T< end >T<;

;;; Within
(within_clause namePath: (name_path (IDENT) @module)) ;; withing >PackageA<.>PackageB<;

;;; Import
(explicit_import_name (IDENT) @module) ;; import >A< = PackageA;
(explicit_import_name namePath: (name_path (IDENT) @module)) ;; import B = >PackageA<.>PackageB<;
(implicit_import_name namePathStar: (name_path_star identifier: (IDENT) @module)) ;; import >PackageA<.>PackageB<.*;

;;; Function Builtins
(EQUALITY) @function.builtin ;; >equality<(id = id2);
(FAILURE) @function.builtin ;; >failure<(equality(id = id2));

;; KEYWORDS
[
(AS)
(BLOCK)
(BREAK)
(CASE)
(CLASS)
(CONNECT)
(CONNECTOR)
(CONSTANT)
(CONSTRAINEDBY)
(CONSTRAINT)
(DER)
(DISCRETE)
(EACH)
(ELSE)
(ELSEIF)
(ELSEWHEN)
(ENCAPSULATED)
(ENUMERATION)
(EQUALITY)
(EQUATION)
(EXPANDABLE)
(EXTENDS)
(EXTERNAL)
(FAILURE)
(FINAL)
(FINAL)
(FLOW)
(FOR)
(FUNCTION)
(GUARD)
(IF)
(IMPORT)
(INITIAL)
(INNER)
(LOCAL)
(LOOP)
(MATCH)
(MATCHCONTINUE)
(MODEL)
(OPERATOR)
(OPTIMIZATION)
(OVERLOAD)
(PACKAGE)
(PARAMETER)
(PARTIAL)
(PROTECTED)
(PUBLIC)
(RECORD)
(REDECLARE)
(REPLACEABLE)
(RETURN)
(STREAM)
(T_ALGORITHM)
(T_AND)
(T_ANNOTATION)
(T_END)
(T_FALSE)
(T_IN)
(T_INPUT)
(T_NOT)
(T_OR)
(T_OUTER)
(T_OUTPUT)
(T_TRUE)
(THEN)
(TRY)
(TYPE)
(UNIONTYPE)
(WHEN)
(WHILE)
(WITHIN)
] @keyword

;; PUNCTUATION BRACKET
[
(LBRACE)
(LBRACK)
(LPAR)
(RBRACE)
(RBRACK)
(RPAR)
] @punctuation.bracket

;; OPERATOR
[
(ALLWILD)
(ASSIGN)
(COLON)
(COLONCOLON)
(COMMA)
(DOT)
(EQEQ)
(EQUALS)
(GREATER)
(GREATEREQ)
(LESS)
(LESSEQ)
(LESSGT)
(MINUS_EW)
(MINUS)
(PLUS_EW)
(PLUS)
(POWER_EW)
(POWER)
(SLASH_EW)
(SLASH)
(STAR_EW)
(STAR)
(WILD)
] @operator
Loading

0 comments on commit 7ea467b

Please sign in to comment.