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