1 | grammar com.framsticks.framclipse.FramScript with org.eclipse.xtext.common.Terminals hidden(SL_COMMENT, ML_COMMENT, WS) |
---|
2 | |
---|
3 | import "http://www.eclipse.org/emf/2002/Ecore" as ecore |
---|
4 | |
---|
5 | generate framScript "http://www.framsticks.com/framclipse/FramScript" |
---|
6 | |
---|
7 | Model : |
---|
8 | (Neuro|Expdef|Style|Show|Script|Include) ; |
---|
9 | |
---|
10 | FramclipseClass : |
---|
11 | (Neuro|Expdef|Style|Show|Script|Property|State) ; |
---|
12 | |
---|
13 | Neuro : |
---|
14 | 'class:' |
---|
15 | // 'name:' name=ID |
---|
16 | headers+=NeuroHeader+ |
---|
17 | code=CodeSection |
---|
18 | (propertyImports+=PropertyIncludeDeclaration | |
---|
19 | properties+=Property)* |
---|
20 | ; |
---|
21 | |
---|
22 | Expdef : |
---|
23 | 'expdef:' |
---|
24 | headers+=Header+ |
---|
25 | code=CodeSection |
---|
26 | (propertyImports+=PropertyIncludeDeclaration | |
---|
27 | properties+=Property | |
---|
28 | states+=State)* |
---|
29 | ; |
---|
30 | |
---|
31 | Style : |
---|
32 | 'style:' |
---|
33 | headers+=Header+ |
---|
34 | code=CodeSection |
---|
35 | (propertyImports+=PropertyIncludeDeclaration | |
---|
36 | properties+=Property)* |
---|
37 | ; |
---|
38 | |
---|
39 | Show : |
---|
40 | 'show:' |
---|
41 | headers+=ShowHeader+ |
---|
42 | code=CodeSection |
---|
43 | (propertyImports+=PropertyIncludeDeclaration | |
---|
44 | properties+=Property)* |
---|
45 | ; |
---|
46 | |
---|
47 | Script : |
---|
48 | 'script:' |
---|
49 | headers+=Header+ |
---|
50 | code=CodeSection |
---|
51 | ; |
---|
52 | |
---|
53 | Property : |
---|
54 | 'prop:' |
---|
55 | headers += PropertyHeader* |
---|
56 | 'id:' name=ID |
---|
57 | headers += PropertyHeader+ |
---|
58 | ; |
---|
59 | |
---|
60 | State : |
---|
61 | 'state:' |
---|
62 | headers += PropertyHeader* |
---|
63 | 'id:' name=ID |
---|
64 | headers += PropertyHeader+ |
---|
65 | ; |
---|
66 | |
---|
67 | Include : {Include} |
---|
68 | ( |
---|
69 | code=Code? |
---|
70 | (properties+=Property | |
---|
71 | states+=State)* |
---|
72 | ) | {Block} statements+=Statement+ |
---|
73 | ; |
---|
74 | |
---|
75 | Header : |
---|
76 | name=HEADER_ID value=HEADER_VALUE ; |
---|
77 | |
---|
78 | NeuroHeader returns Header : |
---|
79 | {IntHeader} name=('prefinputs:' | 'prefoutput:' | 'preflocation:' | 'vhints:') intValue=IntValue | |
---|
80 | {IconHeader} name='icon:' icon=Icon | |
---|
81 | {IconHeader} name='icon:~' icon=Icon '~' | |
---|
82 | Header ; |
---|
83 | |
---|
84 | Icon : |
---|
85 | INT (',' INT)+ ; |
---|
86 | |
---|
87 | PropertyHeader returns Header : |
---|
88 | TypeHeader | |
---|
89 | FlagsHeader | |
---|
90 | Header ; |
---|
91 | |
---|
92 | ShowHeader returns Header : |
---|
93 | name='expdef:' importURI=ID | |
---|
94 | Header ; |
---|
95 | |
---|
96 | FlagsHeader returns Header : |
---|
97 | {IntHeader} name='flags:' intValue=INT ; |
---|
98 | |
---|
99 | TypeHeader : |
---|
100 | name=TYPE_HEADER type=PropertyType ; |
---|
101 | |
---|
102 | // This terminal is defined separately for use with FramScriptAuxiliaryLexer. |
---|
103 | terminal TYPE_HEADER : 'type:'; |
---|
104 | |
---|
105 | PropertyType : |
---|
106 | name=ID (min=Number max=Number default=(Number|HEADER_VALUE)? enums+=ENUM_LITERAL* )? ; |
---|
107 | |
---|
108 | Number : IntNumber | DoubleNumber ; |
---|
109 | IntNumber : '-'? INT ; |
---|
110 | DoubleNumber : '-'? DOUBLE ; |
---|
111 | |
---|
112 | CodeSection returns Code : |
---|
113 | 'code:~' Code '~' ; |
---|
114 | |
---|
115 | Code : |
---|
116 | (includes += IncludeDeclaration | |
---|
117 | globals += Global | |
---|
118 | functions += Function)+; |
---|
119 | |
---|
120 | Global returns VariableDeclarations : |
---|
121 | 'global' vars+=VariableDeclaration (',' vars+=VariableDeclaration)* ';' ; |
---|
122 | |
---|
123 | Function : |
---|
124 | 'function' name=ID (',' aliases+=ID)* '(' (params+=VariableDeclaration (',' params+=VariableDeclaration)*)? ')' code=Block ; |
---|
125 | |
---|
126 | IncludeDeclaration : |
---|
127 | '@include' importURI=STRING ; |
---|
128 | |
---|
129 | /* |
---|
130 | * For some reason, the '#include' keyword is not matched. |
---|
131 | * It is possible it has been overshadowed by SL_COMMENT, |
---|
132 | * despite the order in which the two have been defined. |
---|
133 | * This has been worked around in FramScriptAuxiliaryLexer. |
---|
134 | */ |
---|
135 | PropertyIncludeDeclaration : |
---|
136 | PROP_INCLUDE importURI=STRING ; |
---|
137 | |
---|
138 | // keyword separated for use in FramScriptAuxiliaryLexer |
---|
139 | terminal PROP_INCLUDE : '#include' ; |
---|
140 | |
---|
141 | Block : |
---|
142 | '{' statements+=Statement+ '}' ; |
---|
143 | |
---|
144 | Statement : |
---|
145 | ( VariableDeclarationStatement | |
---|
146 | ExpressionStatement | |
---|
147 | DoStatement | |
---|
148 | ContinueStatement | |
---|
149 | BreakStatement | |
---|
150 | ReturnStatement | |
---|
151 | GotoStatment ) ';' | |
---|
152 | IncludeDeclaration | |
---|
153 | Block | |
---|
154 | SwitchStatement | |
---|
155 | LabeledStatement | |
---|
156 | IfStatement | |
---|
157 | WhileStatement | |
---|
158 | ForStatement | |
---|
159 | ForEachStatement | |
---|
160 | {EmptyStatement} ';' ; |
---|
161 | |
---|
162 | VariableDeclarationStatement returns VariableDeclarations : |
---|
163 | 'var' vars+=VariableDeclaration (',' vars+=VariableDeclaration)* ; |
---|
164 | |
---|
165 | VariableDeclaration : |
---|
166 | name=ID ('=' value=Expression)?; |
---|
167 | |
---|
168 | IfStatement : |
---|
169 | 'if' '(' condition=Expression ')' if=Statement (=>'else' else=Statement)? ; |
---|
170 | |
---|
171 | ForStatement : |
---|
172 | 'for' '(' (init=Assignment | init=VariableDeclarationStatement)? ';' condition=Expression? ';' step=Assignment? ')' body=Statement ; |
---|
173 | |
---|
174 | ForEachStatement : |
---|
175 | 'for' '(' (decl=Iterator|ref=[VariableDeclaration]) 'in' colection=Expression ')' body=Statement ; |
---|
176 | |
---|
177 | Iterator returns VariableDeclaration : |
---|
178 | 'var' name=ID |
---|
179 | ; |
---|
180 | |
---|
181 | WhileStatement : |
---|
182 | 'while' '(' condition=Expression ')' body=Statement ; |
---|
183 | |
---|
184 | DoStatement : |
---|
185 | 'do' body=Statement 'while' condition=ParExpression ; |
---|
186 | |
---|
187 | LabeledStatement : |
---|
188 | name=ID ':' body=Statement ; |
---|
189 | |
---|
190 | GotoStatment : |
---|
191 | 'goto' dest=[LabeledStatement] ; |
---|
192 | |
---|
193 | ReturnStatement : |
---|
194 | {ReturnStatement} 'return' (expr=Expression)? ; |
---|
195 | |
---|
196 | ContinueStatement : |
---|
197 | {ContinueStatement} 'continue' (dest=[LabeledStatement])? ; |
---|
198 | |
---|
199 | BreakStatement : |
---|
200 | {BreakStatement} 'break' (dest=[LabeledStatement])? ; |
---|
201 | |
---|
202 | SwitchStatement : |
---|
203 | 'switch' condition=ParExpression '{' labels+=SwitchGroup* '}' ; |
---|
204 | |
---|
205 | SwitchGroup : |
---|
206 | label=SwitchLabel body=SwitchBlock ; |
---|
207 | |
---|
208 | SwitchBlock returns Block : |
---|
209 | {Block} statements+=Statement* ; |
---|
210 | |
---|
211 | SwitchLabel: |
---|
212 | 'case' expr = Expression ':' | |
---|
213 | {DefaultSwitchLabel} 'default' ':' ; |
---|
214 | |
---|
215 | ExpressionStatement : |
---|
216 | Assignment ; |
---|
217 | |
---|
218 | AssignmentOperator : |
---|
219 | '=' | '+=' | '-=' | '*=' | '/=' ; |
---|
220 | |
---|
221 | ParExpression returns Expression: |
---|
222 | '(' Expression ')' ; |
---|
223 | |
---|
224 | Assignment : |
---|
225 | Expression ( {Assignment.left=current} op=AssignmentOperator right=Assignment)? ; |
---|
226 | |
---|
227 | Expression : |
---|
228 | UnaryExpression ({Expression.left=current} op=Operator right=Expression)? | |
---|
229 | WildcardExpression ; |
---|
230 | |
---|
231 | UnaryExpression : |
---|
232 | QualifiedExpression2 ({UnaryExpression.arg=current} op = IncrementOperator)? | |
---|
233 | op = (IncrementOperator | UnaryOperator) arg = QualifiedExpression2 | |
---|
234 | FunctionLiteral | |
---|
235 | Literal ; |
---|
236 | |
---|
237 | QualifiedExpression2 returns QualifiedExpression : |
---|
238 | QualifiedExpression | |
---|
239 | (PropertyAccess|StateAccess) ({QualifiedExpression.parent=current}'.' child=QualifiedExpression )? ; |
---|
240 | |
---|
241 | QualifiedExpression : |
---|
242 | MemberAccess ({QualifiedExpression.parent=current}'.' child=QualifiedExpression )? ; |
---|
243 | |
---|
244 | MemberAccess returns QualifiedExpression: |
---|
245 | ArrayElementExpression ( {MemberAccess.left=current} '.[' child=Expression ']')? ; |
---|
246 | |
---|
247 | ArrayElementExpression : |
---|
248 | TerminalExpression ({ArrayElementExpression.array=current} ('[' indexes+=Expression ']')+ )? ; |
---|
249 | |
---|
250 | TerminalExpression: |
---|
251 | Cast | |
---|
252 | TypeOf | |
---|
253 | Invocation | |
---|
254 | Array | |
---|
255 | Vector | |
---|
256 | Dictionary | |
---|
257 | {VariableRef} var=[VariableDeclaration]; |
---|
258 | |
---|
259 | Cast : |
---|
260 | type=('int'|'float'|'string') '(' expression=Expression ')' ; |
---|
261 | |
---|
262 | TypeOf : |
---|
263 | 'typeof' '(' expression=Expression ')' ; |
---|
264 | |
---|
265 | Invocation : |
---|
266 | function=[Function] '(' (args+=Expression (',' args+=Expression)*)? ')' ; |
---|
267 | |
---|
268 | WildcardExpression : |
---|
269 | value=ID '.*' ; |
---|
270 | |
---|
271 | PropertyAccess returns QualifiedExpression : |
---|
272 | {PropertyAccess} ('Fields'|'ExpParams'|'ShowParams'|'VisParams') ('.*' | =>('.' property=[Property]) | '.[' (=>property=[Property|STRING] | child=Expression ) ']')? ; |
---|
273 | |
---|
274 | StateAccess returns QualifiedExpression : |
---|
275 | {StateAccess} 'ExpState' (".*" | =>('.' state=[State]) | '.[' (=>state=[State|STRING] | child=Expression ) ']')? ; |
---|
276 | |
---|
277 | FunctionLiteral: |
---|
278 | 'function' function=[Function] ; |
---|
279 | |
---|
280 | Literal: |
---|
281 | {IntLiteral} value=IntValue | |
---|
282 | {DoubleLiteral} value=DoubleValue | |
---|
283 | {StringLiteral} value=STRING | |
---|
284 | {RawStringLiteral} value=ML_STRING | |
---|
285 | {HexLiteral} value=HEX | |
---|
286 | {NullLiteral} 'null' ; |
---|
287 | |
---|
288 | Array : |
---|
289 | {Array} '[' (elements+=Expression (',' elements+=Expression)*)? ']' ; |
---|
290 | |
---|
291 | Vector : |
---|
292 | '(' elements+=Expression (',' elements+=Expression)* ')' ; |
---|
293 | |
---|
294 | Dictionary : |
---|
295 | {Dictionary} '{' ( keys+=DictKey values+=Expression (',' keys+=DictKey values+=Expression)* )? '}' ; |
---|
296 | |
---|
297 | DictKey : |
---|
298 | STRING ':' ; |
---|
299 | |
---|
300 | Operator : |
---|
301 | '+' | '-' | '*'| '/' | |
---|
302 | '&'| '|' | '%' | |
---|
303 | '==' | '!=' | '>=' | '<=' | '<' | '>' | |
---|
304 | '&&' | '||' ; |
---|
305 | |
---|
306 | UnaryOperator : |
---|
307 | '!' | '+' | '-'; |
---|
308 | |
---|
309 | IncrementOperator : |
---|
310 | '++' | '--' ; |
---|
311 | |
---|
312 | IntValue returns ecore::EInt hidden() : |
---|
313 | ('+'|'-')? INT ; |
---|
314 | |
---|
315 | DoubleValue hidden() : |
---|
316 | ('+'|'-')? DOUBLE ; |
---|
317 | |
---|
318 | terminal DOUBLE : |
---|
319 | ('0'..'9')*'.'('0'..'9')+ ('e' INT)? | |
---|
320 | INT 'e' INT ; |
---|
321 | |
---|
322 | terminal ENUM_LITERAL : |
---|
323 | '~' !('~'|'\r'|'\n')+ ; |
---|
324 | |
---|
325 | terminal HEX : |
---|
326 | '0x' (('0'..'9')|('a'..'f')|('A'..'F'))+ ; |
---|
327 | |
---|
328 | terminal ML_STRING : |
---|
329 | '"""' -> '"""' | |
---|
330 | "'''" -> "'''" |
---|
331 | ; |
---|
332 | |
---|
333 | terminal SL_COMMENT : ('//' | '#') -> NL ; |
---|
334 | |
---|
335 | terminal HEADER_ID : |
---|
336 | ( |
---|
337 | 'name' | |
---|
338 | 'longname' | |
---|
339 | 'description' | |
---|
340 | 'group' | |
---|
341 | 'help' | |
---|
342 | 'info' | |
---|
343 | 'neurons' |
---|
344 | ) ':' |
---|
345 | ; |
---|
346 | |
---|
347 | /* |
---|
348 | * This rule should not match anything, |
---|
349 | * so expect an error when generating the artifacts. |
---|
350 | * If it is not there, something went wrong. |
---|
351 | * This rule is here just to make Xtext generate a constant |
---|
352 | * to be used in a custom lexer (FramScriptAuriliaryLexer), |
---|
353 | * which takes context into account, |
---|
354 | * so it is able to recognise HEADER_VALUE |
---|
355 | * without conflicting with Framscript code syntax. |
---|
356 | * It should match the following rules, |
---|
357 | * but only when preceded with HEADER_ID: |
---|
358 | * '~' NL ('\\~'|!'~')* '~' NL | -> NL, |
---|
359 | * where NL is defined as: '\r' '\n'? | '\n', |
---|
360 | * The final NL is not matched, but it is a part of context. |
---|
361 | * This token is also emitted for the default value in PropertyType. |
---|
362 | */ |
---|
363 | terminal HEADER_VALUE : 'name:'; |
---|
364 | |
---|
365 | terminal fragment NL : '\r' '\n'? | '\n' ; |
---|