-
Notifications
You must be signed in to change notification settings - Fork 2
/
Leila.g4
378 lines (301 loc) · 12.2 KB
/
Leila.g4
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
grammar Leila;
import LeilaLiterals;
// A program has a name and is composed of a list of actors.
// The term 'actor' is used to describe one entity in the Scratch world.
program : fileType ident actorDefinitionList EOF ;
// We allow to mark files as either 'program' or program 'module' (typically used for libraries).
// This has no further semantic implications.
fileType : 'program' | 'module' ;
// Actors in a list of actors are separated by whitespace
actorDefinitionList : actorDefinition* ;
// An actor has a mode and an unique identifier.
// It is composed of a set of resources, a set of variables,
// a list of variable initializers, a set of procedure declarations
// and definitions, and a list of scripts.
// Along to the (user defined) set of variables, an actor has
// *attributes* that influence its representation and behavior.
actorDefinition : actorMode ident inheritsFrom 'begin' actorComponentsDefinition 'end' ;
inheritsFrom : 'is' ident (',' ident)* | ;
// Actors in the mode `role` are abstract: they only provide
// functionality to inherit and are not instantiated.
actorMode : 'actor' # ConcreteActorMode | 'role' # ActorRoleMode ;
actorComponentsDefinition : resourceList declarationStmtList setStmtList methodDefinitionList scriptList ;
// The Scratch programming language is typically used to
// write small games that take produce graphics and sounds as output.
// We use the term 'resources' to denote all images and sounds that
// define the static UI content to compose a Scratch program.
// The type 'image' is used both for the *backdrops* of the stage
// and for the *costumes* of the sprites.
resource : resourceType ident resourceLocator ;
// We support image resources and sound resources
resourceType : 'image' # ImageResource | 'sound' # SoundResource ;
// A list of resources is separated by whitespaces.
resourceList : resource* ;
// Declaration of a variable. Depending on the position of the
// declaration statement, the variable is either local to the actor
// or local to the current stack of a script execution.
declarationStmt : 'declare' ident 'as' type # DeclareVariable ;
// A list of variable declarations.
declarationStmtList : declarationStmt* ;
// The list of datatypes that are allowed for the declaration
// of variables and attributes.
type :
'list' 'of' type # ListType
| 'actor' # ActorType
| primitiveType # Primitive
;
// 'int' is deprecated (use 'integer' instead)
primitiveType:
('int' | 'integer') # IntegerType
| 'float' # FloatingPointType
| 'boolean' # BooleanType
| 'string' # StringType
| 'enum' '[' expressionListPlain ']' # EnumType
;
// A script is the central unit that of a Scratch program that
// defines the behavior (and with it the control and data flow).
script : 'script' scriptIdent 'on' event 'do' scriptAttributeList stmtList ;
scriptIdent : ident # NamedScriptIdent | # AnonymousScriptIdent ;
// A list of scripts.
scriptList : script* ;
// Attributes that can be assigned to a script
scriptAttributeList : scriptAttribute* ;
scriptAttribute : 'restart' # RestartScript ;
// Scratch programs are written in an event-driven manner.
// The different events that can trigger the execution
// of a script are defined by the non-terminal `event`.
event :
'never' # NeverEvent
| 'bootstrap' # BootstrapEvent
| ('bootstrap' 'finished' | 'setup') # AfterBootstrapMonitoringEvent
| 'startup' # StartupEvent
| 'started' 'as' 'clone' # CloneStartEvent
| 'message' stringExpr parameterList messageNamespace # MessageReceivedEvent
| 'condition' boolExpr # ConditionReachedEvent
| 'statement' 'finished' # AfterStatementMonitoringEvent
;
messageNamespace : 'in' String # QualifiedNamespace | #UnqualifiedNamespace ;
// Scratch allows to define procedures, that is,
// reusable code blocks (also known as 'custom blocks').
// A procedure is identified by a name and is parameterized
// to take a number of arguments.
methodDefinitionList : methodDefinition* ;
methodDefinition :
metaAttributeList 'define' methodAttributeList ident parameterList stmtList methodResultDeclaration # FullMethodDefinition
| metaAttributeList 'extern' ident parameterList externMethodResultDeclaration # ExternMethodDefinition
;
methodResultDeclaration :
'returns' ident ':' type # FunctionReturnDefinition
| # VoidReturnDefinition
;
externMethodResultDeclaration :
'returns' type # ExternFunctionReturnDefinition
| # ExternVoidReturnDefinition
;
// Attributes that can be assigned to a method
methodAttributeList : methodAttribute* ;
methodAttribute : 'atomic' # AtomicMethod ;
// A procedure parameter.
parameter : ident ':' type ;
// A list of method parameters in brackets.
parameterList : '(' parameterListPlain ')' ;
// A plain list of method parameters.
// Parameters are separated by comma.
parameterListPlain : parameter (',' parameter)* | ;
// A list of program statements.
// Some statements that terminate the control flow
// are only allowed at the end of the list to
// make their semantics clearer for the programmer.
stmtList : blockMode 'begin' stmtListPlain (terminationStmt)? 'end' ;
blockMode: 'atomic' # AtomicBlock | # NonAtomicBloc ;
// A plain list of program statements.
// Statements are separated by whitespace.
stmtListPlain : stmt* ;
// The control flow of Scratch program is controlled by
// designated control-flow statements.
controlStmt :
ifStmt
| untilStmt
| repeatTimesStmt
| repeatForeverStmt
| callStmt
;
// A conditional statement. Either in the form of an 'if ... then ...'
// or an 'if ... then ... else ...'.
ifStmt : 'if' boolExpr 'then' stmtList elseCase ;
elseCase :
'else' stmtList # PureElse
| 'else' ifStmt # ElseIfCase
| # EmptyElseCase
;
// Scratch uses `until` instead of `while` which is in
// favour of the game-like nature of most programs written in it.
untilStmt : 'until' boolExpr 'repeat' stmtList ;
// Repeat a given list of statements N times.
repeatTimesStmt : 'repeat' numExpr 'times' stmtList ;
// Repeat a given list of statements forever---until the
// program termintates.
repeatForeverStmt : 'repeat' 'forever' stmtList ;
// A statement to call user-defined procedures (custom blocks).
callStmt : ident expressionList ;
// A list of expressions that encapsulates, for example, the arguments
// for procedure calls.
expressionList : '(' expressionListPlain ')' ;
// The expression list without brackets.
// Expressions are separated by comma.
expressionListPlain : expression (',' expression)* | ;
// An expression statement.
// The main aim of having such a statement is to not
// loose information when using Scratch ASTs as
// an transformers representation.
// This statement will get more relevant if functions (custom
// blocks with return values get introduced to Scratch.
expressionStmt : 'evaluate' expression ;
// The list of statements that are available in Scratch.
stmt :
controlStmt # ControlStatement
| nonCtrlStmt # NonControlStatement
| stmtList # StmtListStatement
;
metaAttributeList : metaAttribute* ;
metaAttribute : '@' Identifier expression ;
nonCtrlStmt :
expressionStmt
| commonStmt
| listStmt
| declarationStmt
;
commonStmt :
'wait' numExpr 'seconds' # WaitSecsStatement
| 'wait' 'until' boolExpr # WaitUntilStatement
| 'stop' 'other' 'scripts' 'in' 'actor' # StopOthersInActorStatement
| 'create' 'clone' 'of' stringExpr # CreateCloneOfStatement
| 'broadcast' message # BroadcastMessageStatement
| 'broadcast' message 'and' 'wait' # BroadcastAndWaitStatement
| 'reset' 'timer' # ResetTimerStatement
| 'epsilon' # EpsilonStatement
| 'assume' boolExpr # AssumeStatement
| 'precision' 'push' boolExpr # PrecisionPushStatement
| 'precision' 'pop' # PrecisionPopStatement
| setStmt # SetStatement
;
listStmt :
'delete' 'all' 'from' variable # DeleteAllFromStatement
| 'delete' numExpr 'of' variable # DeleteIthFromStatement
| 'add' stringExpr 'to' variable # AddElementToStatement
| 'insert' stringExpr 'at' numExpr 'of' variable # InsertAtStatement
| 'replace' 'item' numExpr 'of' variable 'by' stringExpr # ReplaceElementAtStatement
;
setStmt :
'define' variable 'as' expression # StoreEvalResultStatement
| 'define' variable 'as' callStmt # StoreCallResultStatement
;
setStmtList : setStmt* ;
terminationStmt :
'stop' 'all' # StopAll
| 'stop' 'this' 'script' # StopThis
| 'delete' 'this' 'clone' # DeleteThisClone
;
stringExpr :
String # StringLiteralExpression
| variable # StringVariableExpression
| '(' stringExpr ')' # StringParanthExpression
| callStmt # StringCallStatementExpression
| 'cast' numExpr 'to' 'string' # NumAsStringExpression
| 'cast' boolExpr 'to' 'string' # BoolAsStringExpression
| 'attribute' stringExpr 'of' actorExpr # StringAttributeOfExpression // query an attribute value of an actor (sprites, the stage)
| 'join' stringExpr stringExpr # JoinStringsExpression
| 'letter' numExpr 'of' stringExpr # IthLetterOfStringExpression
| 'item' numExpr 'of' variable # IthStringItemOfExpression
| 'default' String 'for' stringExpr # DefaultStringExpression
| '?string' # UnspecifiedStringExpression
;
boolExpr :
Boolean # BoolLiteralExpression
| variable # BoolVariableExpression
| '(' boolExpr ')' # BoolParanthExpression
| callStmt # BoolCallStatementExpression
| 'cast' numExpr 'to' 'boolean' # NumAsBoolExpression
| 'cast' stringExpr 'to' 'boolean' # StringAsBoolExpression
| 'not' boolExpr # NegatedBoolExpression
| boolExpr 'and' boolExpr # BoolAndExpression
| boolExpr 'or' boolExpr # BoolOrExpression
| numOrStringExpr '>=' numOrStringExpr # GreaterEqualExpression
| numOrStringExpr '>' numOrStringExpr # GreaterThanExpression
| numOrStringExpr '<' numOrStringExpr # LessThanExpression
| numOrStringExpr '<=' numOrStringExpr # LessEqualExpression
| numOrStringExpr '=' numOrStringExpr # EqualsExpression
| stringExpr 'contains' stringExpr # StrContainsExpression
| 'default' Boolean 'for' boolExpr # DefaultBoolExpression
| '?bool' # UnspecifiedBoolExpression
;
numOrStringExpr :
numExpr # NumberExpression
| stringExpr # StringExpression
;
numExpr :
number # NumLiteralExpression
| variable # NumVariableExpression
| '(' numExpr ')' # NumBrackets
| callStmt # NumCallStatementExpression
| 'cast' stringExpr 'to' 'float' # StringToFloatExpression
| 'cast' stringExpr 'to' ('int' | 'integer') # StringToIntExpression
| 'cast' boolExpr 'to' ('int' | 'integer') # BoolToIntExpression
| 'cast' numExpr 'to' 'float' # NumToFloatExpression
| 'cast' numExpr 'to' ('int' | 'integer') # NumToIntExpression
| 'timer' # TimerExpression
| 'length' 'of' stringExpr # LengthOfStringExpression
| 'length' 'of' 'list' variable # LengthOfListExpression
| 'index' 'of' expression 'in' variable # IndexOfExpression
| numExpr '*' numExpr # NumMulExpression
| numExpr '/' numExpr # NumDivExpression
| numExpr 'mod' numExpr # NumModExpression
| numExpr '+' numExpr # NumPlusExpression
| numExpr '-' numExpr # NumMinusExpression
| 'default' number 'for' numExpr # DefaultNumExpr
| '?number' # UnspecifiedNumExpr
;
listExpr :
variable # ListVariableExpression
| '[' expressionListPlain ']' # ListWithElementsExpression
;
actorExpr:
variable # ActorVariableExpression
| 'self' # ActorSelfExpression
| 'locate' ('actor' | ) stringExpr # LocateActorExpression
| 'start' 'clone' 'of' actorExpr # StartCloneActorExpression
| 'start' ('actor' | ) stringExpr 'as' ident # UsherActorExpression
;
expression :
stringExpr
| numExpr
| boolExpr
| listExpr
| actorExpr
| unspecifiedExpr
;
// This type of expression is used to allow for representing
// Scratch programs for that not all expressions have been specified
// as an abstract tree.
unspecifiedExpr : '?expr' ;
variable :
ident # FlatVariable
| ident '.' ident # QualifiedVariable
;
ident :
Identifier # IdentExpression
| 'strid' String # StrIdentExpression
;
number :
IntegerLiteral # IntegerLiteralExpression
| DecimalLiteral # DecimalLiteralExpression
;
Boolean : Bool ;
resourceLocator : String ;
message :
stringExpr # UserMessage
| stringExpr expressionList 'to' messageDestination # SystemMessage
;
messageDestination :
String # NamedMessageDestination
| actorExpr # ActorMessageDestination ;