-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.xtext
170 lines (130 loc) · 2.81 KB
/
grammar.xtext
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
grammar org.xtext.compiler.go.MyGo with org.eclipse.xtext.common.Terminals
generate myGo "http://www.xtext.org/compiler/go/MyGo"
// https://tour.golang.org/basics/13
Model:
package+=Package
imports+=Imports
declarations+=Declarations*
;
Declarations:
functionDeclaration=FunctionDeclaration |
typeDeclaration=TypeDeclaration
;
Package:
'package' ID
;
Imports:
('import' STRING)*
;
FunctionDeclaration:
'func' (receiver=Receiver)? id=ID '(' (parameters=ParametersDeclaration)? ')' (type=Type)? '{' body=Body '}'
;
Receiver:
'(' idName=ID idType=ID ')'
;
Body:
{Body} lineCommand += LineCommand*
;
LineCommand:
FuncCall ";"? |
VariableDeclaration ";"? |
ReturnStatement ";"? |
ForRange ";"? |
Assignment ";"?
;
Assignment:
id=ID '=' expression=Expression
;
ForRange:
'for' id1=ID ':=' 'range' expression=Expression '{' lineCommand += LineCommand* '}'|
'for' id1=ID ',' id2=ID ':=' 'range' expression=Expression '{' lineCommand += LineCommand* '}'
;
ParametersDeclaration:
idType+=IdType (',' idType+=IdType)*
;
ParametersValues:
{ParametersValues} (expression += Expression (',' expression += Expression)*)?
;
Type:
'int' | 'float64' | 'bool' | 'string' | ID
;
TypeDeclaration:
'type' id=ID type=Type
;
Value:
intValue=IntValue |
floatValue=FloatValue |
stringValue=StringValue |
boolValue=BoolValue
;
IntValue:
INT
;
FloatValue:
INT '.' INT
;
StringValue:
STRING
;
BoolValue:
'true' | 'false'
;
IdType:
id=ID type=Type
;
FuncCall:
receiverInstance=ReceiverInstance? id=ID '(' parametersValues=ParametersValues ')'
;
ReceiverInstance:
id=ID '.'
;
Expression returns Expression:
Expression2 ({Expression.left=current} binaryOp=BinaryOpPrecedence1 right=Expression2)*
;
Expression2 returns Expression:
Expression3 ({Expression2.left=current} binaryOp=BinaryOpPrecedence2 right=Expression3)*
;
Expression3 returns Expression:
Expression4 ({Expression3.left=current} binaryOp=BinaryOpPrecedence3 right=Expression4)*
;
Expression4 returns Expression:
Expression5 ({Expression4.left=current} binaryOp=BinaryOpPrecedence4 right=Expression5)*
;
Expression5 returns Expression:
UnaryExpr ({Expression5.left=current} binaryOp=BinaryOpPrecedence5 right=UnaryExpr)*
;
UnaryExpr returns Expression:
Value |
Variable |
FuncCall |
'(' Expression ')'
;
ExplicitCast:
type=Type '(' expression=Expression ')'
;
Variable:
id=ID
;
BinaryOpPrecedence1:
'||'
;
BinaryOpPrecedence2:
'&&'
;
BinaryOpPrecedence3:
'==' | '!=' | '<' | '<=' | '>' | '>='
;
BinaryOpPrecedence4:
'+' | '-' | '|' | '^'
;
BinaryOpPrecedence5:
'*' | '/' | '%' | '<<' | '>>' | '&' | '&^'
;
VariableDeclaration:
'var' ids+=ID (',' ids += ID)* '=' expression=Expression |
'var' ids+=ID (',' ids += ID)* type=Type |
'var' ids+=ID (',' ids += ID)* type=Type '=' expression=Expression
;
ReturnStatement:
'return' expression=Expression
;