Skip to content

Commit 84902b0

Browse files
authored
Add Langium Language (#7649)
* Add Langium Language * added unique id * swap c4 w/ ox
1 parent 1695bcb commit 84902b0

File tree

9 files changed

+516
-0
lines changed

9 files changed

+516
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@
755755
[submodule "vendor/grammars/language-kotlin"]
756756
path = vendor/grammars/language-kotlin
757757
url = https://github.com/nishtahir/language-kotlin
758+
[submodule "vendor/grammars/language-langium"]
759+
path = vendor/grammars/language-langium
760+
url = https://github.com/eclipse-langium/language-langium.git
758761
[submodule "vendor/grammars/language-less"]
759762
path = vendor/grammars/language-less
760763
url = https://github.com/atom/language-less.git

grammars.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ vendor/grammars/language-kickstart:
701701
- source.kickstart
702702
vendor/grammars/language-kotlin:
703703
- source.kotlin
704+
vendor/grammars/language-langium:
705+
- source.langium
704706
vendor/grammars/language-less:
705707
- source.css.less
706708
vendor/grammars/language-m68k:

lib/linguist/languages.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4028,6 +4028,14 @@ LabVIEW:
40284028
codemirror_mode: xml
40294029
codemirror_mime_type: text/xml
40304030
language_id: 194
4031+
Langium:
4032+
type: programming
4033+
color: "#2c8c87"
4034+
extensions:
4035+
- ".langium"
4036+
tm_scope: source.langium
4037+
ace_mode: text
4038+
language_id: 548603830
40314039
Lark:
40324040
type: data
40334041
color: "#2980B9"

samples/Langium/ox.langium

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
grammar Ox
2+
3+
entry OxProgram:
4+
elements+=OxElement*;
5+
6+
OxElement:
7+
Block |
8+
IfStatement |
9+
WhileStatement |
10+
ForStatement |
11+
FunctionDeclaration |
12+
VariableDeclaration ';' |
13+
AssignmentStatement ';' |
14+
PrintStatement ';' |
15+
ReturnStatement ';' |
16+
Expression ';'
17+
;
18+
19+
IfStatement:
20+
'if' '(' condition=Expression ')' block=Block
21+
('else' elseBlock=Block)?
22+
;
23+
24+
WhileStatement:
25+
'while' '(' condition=Expression ')' block=Block
26+
;
27+
28+
ForStatement:
29+
'for' '(' counter=VariableDeclaration? ';' condition=Expression? ';' execution=AssignmentStatement? ')' block=Block
30+
;
31+
32+
PrintStatement: 'print' value=Expression;
33+
34+
ReturnStatement: 'return' value=Expression?;
35+
36+
Block: '{'
37+
elements+=OxElement*
38+
'}';
39+
40+
VariableDeclaration returns NamedElement:
41+
{infer VariableDeclaration} 'var' name=ID (':' type=TypeReference) (assignment?='=' value=Expression)?
42+
;
43+
44+
AssignmentStatement:
45+
varRef=[VariableDeclaration:ID] '=' value=Expression
46+
;
47+
48+
Expression:
49+
Addition;
50+
51+
Addition infers Expression:
52+
Multiplication ({infer BinaryExpression.left=current} operator=('+' | '-') right=Multiplication)*;
53+
54+
Multiplication infers Expression:
55+
Logical ({infer BinaryExpression.left=current} operator=('*' | '/') right=Logical)*;
56+
57+
Logical infers Expression:
58+
Comparison ({infer BinaryExpression.left=current} operator=('and' | 'or') right=Comparison)*;
59+
60+
Comparison infers Expression:
61+
Primary ({infer BinaryExpression.left=current} operator=('<' | '<=' | '>' | '>=' | '==' | '!=') right=Primary)*;
62+
63+
Primary infers Expression:
64+
'(' Expression ')' |
65+
UnaryExpression |
66+
BooleanLiteral |
67+
NumberLiteral |
68+
MemberCall;
69+
70+
MemberCall:
71+
element=[NamedElement:ID]
72+
(
73+
explicitOperationCall?='('
74+
(
75+
arguments+=Expression (',' arguments+=Expression)*
76+
)?
77+
')'
78+
)?;
79+
80+
UnaryExpression:
81+
operator=('!' | '-') value=Expression;
82+
83+
NumberLiteral: value=NUMBER;
84+
BooleanLiteral: value?='true' | 'false';
85+
86+
FunctionDeclaration:
87+
'fun' name=ID '(' (parameters+=Parameter (',' parameters+=Parameter)*)? ')' ':' returnType=TypeReference body=Block;
88+
89+
Parameter: name=ID ':' type=TypeReference;
90+
91+
TypeReference: primitive=("number" | "boolean" | "void");
92+
93+
type NamedElement = Parameter | FunctionDeclaration | VariableDeclaration;
94+
95+
hidden terminal WS: /\s+/;
96+
terminal ID: /[_a-zA-Z][\w_]*/;
97+
terminal NUMBER returns number: /[0-9]+(\.[0-9]+)?/;
98+
// terminal STRING: /"[^"]*"/;
99+
100+
hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
101+
hidden terminal SL_COMMENT: /\/\/[^\n\r]*/;

samples/Langium/treemap.langium

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Treemap grammar for Langium
3+
* Converted from mindmap grammar
4+
*
5+
* The ML_COMMENT and NL hidden terminals handle whitespace, comments, and newlines
6+
* before the treemap keyword, allowing for empty lines and comments before the
7+
* treemap declaration.
8+
*/
9+
grammar TreemapGrammar
10+
11+
fragment TitleAndAccessibilities:
12+
((accDescr=ACC_DESCR | accTitle=ACC_TITLE | title=TITLE))+
13+
;
14+
15+
terminal BOOLEAN returns boolean: 'true' | 'false';
16+
17+
terminal ACC_DESCR: /[\t ]*accDescr(?:[\t ]*:([^\n\r]*?(?=%%)|[^\n\r]*)|\s*{([^}]*)})/;
18+
terminal ACC_TITLE: /[\t ]*accTitle[\t ]*:(?:[^\n\r]*?(?=%%)|[^\n\r]*)/;
19+
terminal TITLE: /[\t ]*title(?:[\t ][^\n\r]*?(?=%%)|[\t ][^\n\r]*|)/;
20+
21+
// Interface declarations for data types
22+
interface Item {
23+
name: string
24+
classSelector?: string // For ::: class
25+
}
26+
interface Section extends Item {
27+
}
28+
interface Leaf extends Item {
29+
value: number
30+
}
31+
interface ClassDefStatement {
32+
className: string
33+
styleText: string // Optional style text
34+
}
35+
interface Treemap {
36+
TreemapRows: TreemapRow[]
37+
title?: string
38+
accTitle?: string
39+
accDescr?: string
40+
}
41+
42+
entry Treemap returns Treemap:
43+
TREEMAP_KEYWORD
44+
(
45+
TitleAndAccessibilities
46+
| TreemapRows+=TreemapRow
47+
)*;
48+
terminal TREEMAP_KEYWORD: 'treemap-beta' | 'treemap';
49+
50+
terminal CLASS_DEF: /classDef\s+([a-zA-Z_][a-zA-Z0-9_]+)(?:\s+([^;\r\n]*))?(?:;)?/;
51+
terminal STYLE_SEPARATOR: ':::';
52+
terminal SEPARATOR: ':';
53+
terminal COMMA: ',';
54+
55+
hidden terminal WS: /[ \t]+/; // One or more spaces or tabs for hidden whitespace
56+
hidden terminal ML_COMMENT: /\%\%[^\n]*/;
57+
hidden terminal NL: /\r?\n/;
58+
59+
TreemapRow:
60+
indent=INDENTATION? (item=Item | ClassDef);
61+
62+
// Class definition statement handled by the value converter
63+
ClassDef returns string:
64+
CLASS_DEF;
65+
66+
Item returns Item:
67+
Leaf | Section;
68+
69+
// Use a special rule order to handle the parsing precedence
70+
Section returns Section:
71+
name=STRING2 (STYLE_SEPARATOR classSelector=ID2)?;
72+
73+
Leaf returns Leaf:
74+
name=STRING2 INDENTATION? (SEPARATOR | COMMA) INDENTATION? value=MyNumber (STYLE_SEPARATOR classSelector=ID2)?;
75+
76+
// This should be processed before whitespace is ignored
77+
terminal INDENTATION: /[ \t]{1,}/; // One or more spaces/tabs for indentation
78+
79+
// Keywords with fixed text patterns
80+
terminal ID2: /[a-zA-Z_][a-zA-Z0-9_]*/;
81+
// Define as a terminal rule
82+
terminal NUMBER2: /[0-9_\.\,]+/;
83+
84+
// Then create a data type rule that uses it
85+
MyNumber returns number: NUMBER2;
86+
87+
terminal STRING2: /"[^"]*"|'[^']*'/;
88+
// Modified indentation rule to have higher priority than WS

0 commit comments

Comments
 (0)