Skip to content

Commit

Permalink
Fix nested polymorphic types (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
phannebohm authored Sep 18, 2024
1 parent 9c2a142 commit 52dc749
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
54 changes: 40 additions & 14 deletions syntaxes/metamodelica.tmGrammar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ patterns:
1:
name: comment.line
patterns:
- include: '#enumeration'
- include: '#enummember'

## Type declaration
- match: \b(type)\b\s+\b(\w+)\b\s+(=)
Expand All @@ -42,22 +42,28 @@ patterns:
- match: \b(Real|Integer|Boolean|String|enumeration|type)\b
name: storage.type

## Polymorphic types
- begin: \b(list|tuple|array|Option)\s*<
end: '>'
beginCaptures:
1:
name: storage.type
patterns:
- include: '#polymorphic_type'

- begin: \b(\w+)\s*<
end : '>'
beginCaptures:
1:
name: entity.name.type
patterns:
- include: '#polymorphic_type'

## Variability Prefix
- match: \b(parameter|constant)\b
name: storage.modifier
- match: \b(replaceable|redeclare|import)\b
name: storage.modifier
- begin: \b(list|tuple|array|Option)<
end: '>'
name: storage.type
- match: \b(uniontype)\s+(\w+)\s*(".*")*
captures:
1:
name: keyword
2:
name: entity.name.type
3:
name: string.quoted.double

# Keywords
- match: \b(for|if|when|while|in|then|loop|guard|end if|end when|end for|end while|else|elsewhen|break|return|each|elseif|input|output)\b
Expand Down Expand Up @@ -137,15 +143,35 @@ repository:
match: '\\(x\h{2}|[0-2][0-7]{0,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?|.)'
name: constant.character.escape

enumeration:
enummember:
patterns:
- match: \b(\w+)\s*("([^"]|\\")*(?<!\\)")?
captures:
1:
name: variable.other.enummember
2:
name: comment.line
- include: "source.modelica"
- include: "source.metamodelica"

polymorphic_type:
patterns:
- begin: '\b(list|tuple|array|Option)\s*<'
end: '>'
beginCaptures:
1:
name: storage.type
patterns:
- include: '#polymorphic_type'
- begin: '\b(\w+)\s*<'
end: '>'
beginCaptures:
1:
name: entity.name.type
patterns:
- include: '#polymorphic_type'
- match: \b(Real|Integer|Boolean|String|enumeration|type)\b
name: storage.type
- match: \b([\w\.]+)\b
name: entity.name.type

scopeName: source.metamodelica
6 changes: 4 additions & 2 deletions test/metamodelica/Function.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ function myMetaModelicaFunction
// ^^^^^^^^^^^^^^^^^^^^^^ source.metamodelica entity.name.type
input list<Real> x;
//^^^^^ source.metamodelica keyword.control
// ^^^^^^^^^^ source.metamodelica storage.type
// ^^^^ source.metamodelica storage.type
// ^^^^ source.metamodelica storage.type
output list<Integer> y = {};
//^^^^^^ source.metamodelica keyword.control
// ^^^^^^^^^^^^^ storage.type
// ^^^^ source.metamodelica storage.type
// ^^^^^^^ source.metamodelica storage.type
// ^ source.metamodelica keyword.operator.assignment
algorithm
//<-- source.metamodelica keyword
Expand Down
3 changes: 2 additions & 1 deletion test/metamodelica/FunctionDoc.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public function foo
// ^^^^ source.metamodelica constant.language
output Option<SimCode.Awesome> awesome;
//^^^^^^ source.metamodelica keyword.control
// ^^^^^^^^^^^^^^^^^^^^^^^ source.metamodelica storage.type
// ^^^^^^ source.metamodelica storage.type
// ^^^^^^^^^^^^^^^ source.metamodelica entity.name.type
// [...]
end foo;

Expand Down
26 changes: 21 additions & 5 deletions test/metamodelica/Types.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@

type L = list<P>;
// ^^^^ source.metamodelica storage.type
// ^ source.metamodelica storage.type
// ^ source.metamodelica entity.name.type
type T = tuple<P, Q>;
// ^^^^^ source.metamodelica storage.type
// ^ source.metamodelica storage.type
// ^ source.metamodelica storage.type
// ^ source.metamodelica entity.name.type
// ^ source.metamodelica entity.name.type
type A = array<P>;
// ^^^^^ source.metamodelica storage.type
// ^ source.metamodelica storage.type
// ^ source.metamodelica entity.name.type
type O = Option<P>;
// ^^^^^^ source.metamodelica storage.type
// ^ source.metamodelica storage.type
// ^ source.metamodelica entity.name.type

type LL = list<list<P>>;
// ^^^^ source.metamodelica storage.type
// ^^^^ source.metamodelica storage.type
// ^ source.metamodelica entity.name.type

type TL = tuple<list<P>, Q>;
// ^^^^^ source.metamodelica storage.type
// ^^^^ source.metamodelica storage.type
// ^ source.metamodelica entity.name.type
// ^ source.metamodelica entity.name.type

type TL = UnorderedMap<Key, Value>;
// ^^^^^^^^^^^^ source.metamodelica entity.name.type
// ^^^ source.metamodelica entity.name.type
// ^^^^^ source.metamodelica entity.name.type

0 comments on commit 52dc749

Please sign in to comment.