source: java/Framclipse/com.framsticks.framclipse/src/com/framsticks/framclipse/FramScript.xtext @ 440

Last change on this file since 440 was 440, checked in by Mateusz Poszwa, 8 years ago
  • Updated Xtext-based Framclipse
  • Deleted previous version of Framclipse
  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
1grammar com.framsticks.framclipse.FramScript with org.eclipse.xtext.common.Terminals hidden(SL_COMMENT, ML_COMMENT, WS)
2
3import "http://www.eclipse.org/emf/2002/Ecore" as ecore
4
5generate framScript "http://www.framsticks.com/framclipse/FramScript"
6
7Model :
8        (Neuro|Expdef|Style|Show|Script|Include) ;
9       
10FramclipseClass :
11        (Neuro|Expdef|Style|Show|Script|Property|State) ;
12       
13Neuro :
14        'class:'
15//      'name:' name=ID
16        headers+=NeuroHeader+
17        code=CodeSection
18        (propertyImports+=PropertyIncludeDeclaration |
19        properties+=Property)*
20        ;
21
22Expdef :
23        'expdef:'
24        headers+=Header+
25        code=CodeSection
26        (propertyImports+=PropertyIncludeDeclaration |
27        properties+=Property |
28        states+=State)*
29        ;
30       
31Style :
32        'style:'
33        headers+=Header+
34        code=CodeSection
35        (propertyImports+=PropertyIncludeDeclaration |
36        properties+=Property)*
37        ;
38       
39Show :
40        'show:'
41        headers+=ShowHeader+
42        code=CodeSection
43        (propertyImports+=PropertyIncludeDeclaration |
44        properties+=Property)*
45        ;
46       
47Script :
48        'script:'
49        headers+=Header+
50        code=CodeSection
51        ;
52       
53Property :
54        'prop:'
55        headers += PropertyHeader*
56        'id:' name=ID
57        headers += PropertyHeader+
58        ;
59
60State :
61        'state:'
62        headers += PropertyHeader*
63        'id:' name=ID
64        headers += PropertyHeader+
65        ;
66
67Include : {Include}
68        (
69                code=Code?
70                (properties+=Property |
71                states+=State)*
72        ) | {Block} statements+=Statement+
73;
74
75Header :
76        name=HEADER_ID value=HEADER_VALUE ;
77
78NeuroHeader 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
84Icon :
85        INT (',' INT)+ ;
86
87PropertyHeader returns Header :
88        TypeHeader |
89        FlagsHeader |
90        Header ;
91
92ShowHeader returns Header :
93        name='expdef:' importURI=ID |
94        Header ;
95
96FlagsHeader returns Header :
97{IntHeader}     name='flags:' intValue=INT ;
98
99TypeHeader :
100        name=TYPE_HEADER type=PropertyType ;
101
102// This terminal is defined separately for use with FramScriptAuxiliaryLexer.
103terminal TYPE_HEADER : 'type:';
104
105PropertyType :
106        name=ID (min=Number max=Number default=(Number|HEADER_VALUE)? enums+=ENUM_LITERAL* )? ;
107
108Number : IntNumber | DoubleNumber ;
109IntNumber : '-'? INT ;
110DoubleNumber : '-'? DOUBLE ;
111
112CodeSection returns Code :
113        'code:~' Code '~' ;
114
115Code :
116        (includes += IncludeDeclaration |
117        globals += Global |
118        functions += Function)+;
119       
120Global returns VariableDeclarations :
121        'global' vars+=VariableDeclaration (',' vars+=VariableDeclaration)* ';' ;
122       
123Function :
124        'function' name=ID (',' aliases+=ID)* '(' (params+=VariableDeclaration (',' params+=VariableDeclaration)*)? ')' code=Block ;
125       
126IncludeDeclaration :
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 */
135PropertyIncludeDeclaration :
136        PROP_INCLUDE importURI=STRING ;
137
138// keyword separated for use in FramScriptAuxiliaryLexer
139terminal PROP_INCLUDE : '#include' ;
140
141Block :
142        '{' statements+=Statement+ '}' ;
143
144Statement :
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
162VariableDeclarationStatement returns VariableDeclarations :
163        'var' vars+=VariableDeclaration (',' vars+=VariableDeclaration)* ;
164
165VariableDeclaration :
166        name=ID ('=' value=Expression)?;
167       
168IfStatement :
169        'if' '(' condition=Expression ')' if=Statement (=>'else' else=Statement)? ;
170
171ForStatement :
172        'for' '(' (init=Assignment | init=VariableDeclarationStatement)? ';' condition=Expression? ';' step=Assignment? ')' body=Statement ;
173
174ForEachStatement :
175        'for' '(' (decl=Iterator|ref=[VariableDeclaration]) 'in' colection=Expression ')' body=Statement ;
176
177Iterator returns VariableDeclaration :
178        'var' name=ID
179;
180
181WhileStatement :
182        'while' '(' condition=Expression ')' body=Statement ;
183       
184DoStatement :
185        'do' body=Statement 'while' condition=ParExpression ;
186       
187LabeledStatement :
188        name=ID ':' body=Statement ;
189       
190GotoStatment :
191        'goto' dest=[LabeledStatement] ;
192       
193ReturnStatement :
194{ReturnStatement} 'return' (expr=Expression)? ;
195       
196ContinueStatement :
197{ContinueStatement}     'continue' (dest=[LabeledStatement])? ;
198       
199BreakStatement :
200{BreakStatement} 'break' (dest=[LabeledStatement])? ;
201       
202SwitchStatement :
203        'switch' condition=ParExpression '{' labels+=SwitchGroup* '}' ;
204
205SwitchGroup :
206        label=SwitchLabel body=SwitchBlock ;
207       
208SwitchBlock returns Block :
209{Block} statements+=Statement* ;
210
211SwitchLabel:
212        'case' expr = Expression ':' |
213{DefaultSwitchLabel} 'default' ':' ;
214       
215ExpressionStatement :
216        Assignment ;
217       
218AssignmentOperator :
219        '=' | '+=' | '-=' | '*=' | '/=' ;
220       
221ParExpression returns Expression:
222        '(' Expression ')' ;
223       
224Assignment :
225        Expression ( {Assignment.left=current} op=AssignmentOperator right=Assignment)? ;
226
227Expression :
228        UnaryExpression ({Expression.left=current} op=Operator right=Expression)? |
229        WildcardExpression ;
230
231UnaryExpression :
232        QualifiedExpression2 ({UnaryExpression.arg=current} op = IncrementOperator)? |
233        op = (IncrementOperator | UnaryOperator) arg = QualifiedExpression2 |
234        FunctionLiteral |
235        Literal ;
236
237QualifiedExpression2 returns QualifiedExpression :
238        QualifiedExpression |
239        (PropertyAccess|StateAccess) ({QualifiedExpression.parent=current}'.' child=QualifiedExpression )? ;
240
241QualifiedExpression :
242        MemberAccess ({QualifiedExpression.parent=current}'.' child=QualifiedExpression )? ;
243
244MemberAccess returns QualifiedExpression:
245        ArrayElementExpression ( {MemberAccess.left=current} '.[' child=Expression ']')? ;
246
247ArrayElementExpression :
248        TerminalExpression ({ArrayElementExpression.array=current} ('[' indexes+=Expression ']')+ )? ;
249
250TerminalExpression:
251                                Cast |
252                                TypeOf |
253                                Invocation |
254                                Array |
255                                Vector |
256                                Dictionary |
257{VariableRef}   var=[VariableDeclaration];
258
259Cast :
260        type=('int'|'float'|'string') '(' expression=Expression ')' ;
261
262TypeOf :
263        'typeof' '(' expression=Expression ')' ;
264
265Invocation :
266        function=[Function] '(' (args+=Expression (',' args+=Expression)*)? ')' ;
267
268WildcardExpression :
269        value=ID '.*' ;
270
271PropertyAccess returns QualifiedExpression :
272{PropertyAccess}        ('Fields'|'ExpParams'|'ShowParams'|'VisParams') ('.*' | =>('.' property=[Property]) | '.[' (=>property=[Property|STRING] | child=Expression ) ']')? ;
273
274StateAccess returns QualifiedExpression :
275{StateAccess}   'ExpState' (".*" | =>('.' state=[State]) | '.[' (=>state=[State|STRING] | child=Expression ) ']')? ;
276
277FunctionLiteral:
278        'function' function=[Function] ;
279
280Literal:
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
288Array :
289{Array} '[' (elements+=Expression (',' elements+=Expression)*)? ']' ;
290
291Vector  :
292        '(' elements+=Expression (',' elements+=Expression)* ')' ;
293
294Dictionary :
295{Dictionary}    '{' ( keys+=DictKey values+=Expression (',' keys+=DictKey values+=Expression)* )? '}' ;
296
297DictKey :
298        STRING ':' ;
299
300Operator :
301        '+' | '-' | '*'| '/' |
302        '&'| '|' | '%' |
303        '==' | '!=' | '>=' | '<=' | '<' | '>' |
304        '&&' | '||' ;
305
306UnaryOperator :
307        '!' | '+' | '-';
308
309IncrementOperator :
310        '++' | '--' ;
311       
312IntValue returns ecore::EInt hidden() :
313        ('+'|'-')? INT ;
314       
315DoubleValue hidden() :
316        ('+'|'-')? DOUBLE ;
317
318terminal DOUBLE :
319        ('0'..'9')*'.'('0'..'9')+ ('e' INT)? |
320        INT 'e' INT ;
321
322terminal ENUM_LITERAL :
323        '~' !('~'|'\r'|'\n')+ ;
324
325terminal HEX :
326        '0x' (('0'..'9')|('a'..'f')|('A'..'F'))+ ;
327
328terminal ML_STRING :
329        '"""' -> '"""' |
330        "'''" -> "'''"
331;
332
333terminal SL_COMMENT : ('//' | '#') -> NL ;
334
335terminal 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 */
363terminal HEADER_VALUE : 'name:';
364
365terminal fragment NL : '\r' '\n'? | '\n' ;
Note: See TracBrowser for help on using the repository browser.