-
Notifications
You must be signed in to change notification settings - Fork 0
/
pascalscript_parser.g4
349 lines (298 loc) · 7.27 KB
/
pascalscript_parser.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
/*
This file is part of the PascalScript Pascal interpreter.
SPDX-FileCopyrightText: 2024 Christophe "CHiPs" Petit <chips44@gmail.com>
SPDX-License-Identifier: GPL-3.0-or-later
*/
/* ******************** PascalScript ANTLR4 ******************** */
// npm i -g --save-dev antlr-format-cli
// antlr-format -v pascalscript_parser.g4
// $antlr-format alignTrailingComments true
// $antlr-format columnLimit 150
// $antlr-format minEmptyLines 1
// $antlr-format maxEmptyLinesToKeep 1
// $antlr-format reflowComments false
// $antlr-format useTab false
// $antlr-format allowShortRulesOnASingleLine true
// $antlr-format allowShortBlocksOnASingleLine true
// $antlr-format alignSemicolons ownLine
// $antlr-format alignColons hanging
grammar pascalscript_parser;
options {
// needs antlr4 >= 4.10
caseInsensitive = true;
tokenVocab = pascalscript_lexer;
}
/* ******************** PROGRAM ******************** */
pascalFile
: pascalProgram
// | pascalUnit
;
pascalProgram
: PROGRAM IDENTIFIER SEMI_COLON
// usesBlock?
pascalHeader instructionBlock DOT EOF
;
/* NB: units disabled for now
pascalUnit
: UNIT IDENTIFIER SEMI_COLON
INTERFACE
usesBlock?
interfaceBlock*
IMPLEMENTATION
usesBlock?
implementationBlock
END DOT
;
usesBlock
: USES identifierList SEMI_COLON
;
interfaceBlock
: constBlock
| typeBlock
| varBlock
| procedureDeclarationHeader
| functionDeclarationHeader
;
implementationBlock
: pascalBlock
initializationBlock?
implementationBlock?
;
initializationBlock
: INITIALIZATION
| (instruction | instructionBlock SEMI_COLON)
;
finalizationBlock
: FINALIZATION
| (instruction | instructionBlock SEMI_COLON)
;
*/
pascalHeader: pascalBlock*;
pascalBlock
: constBlock
// | typeBlock
| varBlock
// | procedureFunctionBlock
;
instructionBlock: BEGIN (instruction)* END;
instruction
: assignment
// | ifBlock
// | repeatBlock
// | whileBlock
// | forBlock
| procedureCall
;
assignment: variableReference DOT_COLON expression SEMI_COLON;
// ifBlock
// : IF expression THEN
// ( instruction | instructionBlock ) SEMI_COLON
// ( ELSE ( instruction | instructionBlock ) SEMI_COLON )?
// ;
// repeatBlock
// : REPEAT ( instruction )* UNTIL expression SEMI_COLON
// ;
// whileBlock
// : WHILE expression DO (instruction | instructionBlock SEMI_COLON)
// ;
// forBlock
// : FOR variableReference DOT_COLON expression ( TO | DOWNTO ) expression
// instruction | instructionBlock SEMI_COLON
// ;
procedureCall
: ( /*IDENTIFIER | */ WRITE | WRITELN) (LEFT_PARENTHESIS parameterList RIGHT_PARENTHESIS)? SEMI_COLON
;
// functionCall
// : IDENTIFIER ( LEFT_PARENTHESIS parameterList RIGHT_PARENTHESIS )?
// ;
parameterList: parameter (COMMA parameter)*;
parameter: expression | variableReference | constantReference;
/* ******************** LABELS & GOTO ******************** */
/*
label
: DECIMAL_DIGIT_SEQUENCE
| IDENTIFIER
;
labelList
: label ( COMMA label )*
;
labelDeclaration
: LABEL labelList SEMI_COLON
;
gotoBlock
: GOTO label SEMI_COLON
;
*/
/* ******************** CONSTANTS ******************** */
/*
NB: no expressions in constants, only values
no typed constants, only numbers, chars & strings
Const
One = 1;
KibiByte = 1024;
MaxWord = $FFFF;
Pi = 3.141592653589793;
Star = '*';
HelloWorld = 'Hello, world!';
*/
constBlock: CONST constantDeclaration+;
constantDeclaration
: IDENTIFIER EQUAL expression SEMI_COLON
; // : IDENTIFIER EQUAL constantValue SEMI_COLON
/*
constantValue
: SIGN UNSIGNED_INTEGER_VALUE
| UNSIGNED_INTEGER_VALUE
| SIGN UNSIGNED_REAL_VALUE
| UNSIGNED_REAL_VALUE
| CHARACTER_VALUE
| COMPOSED_STRING_VALUE
| IDENTIFIER
;
*/
/* ******************** TYPES ******************** */
/*
Type
TextLine = String[80];
*/
/*
typeBlock
: TYPE typeDeclaration+
;
typeDeclaration
: IDENTIFIER EQUAL typeDefinition SEMI_COLON
;
*/
typeReference
: scalarType
// | stringType
// : IDENTIFIER
// | typeDefinition
;
/*
typeDefinition
: REAL
| scalarType
| stringType
// | arrayType
// | recordType
;
*/
scalarType
: INTEGER
| CARDINAL
| BOOLEAN
| CHAR
// | enumType
// | subrangeType
;
/*
enumType
: LEFT_PARENTHESIS IDENTIFIER ( COMMA IDENTIFIER )* RIGHT_PARENTHESIS
;
subrangeType
: subrangeLimit DOT_DOT subrangeLimit
;
subrangeLimit
: SIGN? UNSIGNED_INTEGER
| SIGN? IDENTIFIER
;
*/
/*
stringSize
: UNSIGNED_INTEGER | IDENTIFIER
;
*/
/*
stringType
: STRING
// | STRING ( LEFT_BRACKET stringSize RIGHT_BRACKET )?
;
*/
/* ******************** ARRAYS ******************** */
/*
arrayType
: ARRAY LEFT_BRACKET arrayLimits RIGHT_BRACKET OF typeDefinition
;
arrayLimits
: arrayLimit ( COMMA arrayLimit )*
;
arrayLimit
: scalarType
| IDENTIFIER
;
*/
/* ******************** RECORDS ******************** */
/*
recordType
: RECORD ( identifierList COLON typeDefinition SEMI_COLON )* END
;
*/
/* ******************** VARIABLES ******************** */
varBlock: VAR variableDeclaration+;
variableDeclaration: identifierList COLON typeReference SEMI_COLON;
identifierList: IDENTIFIER (COMMA IDENTIFIER)*;
/* ******************** PROCEDURES & FUNCTIONS ******************** */
/*
procedureFunctionBlock
: ( procedureDeclaration | functionDeclaration )+
;
procedureDeclaration
: procedureDeclarationHeader
procedureOrFunctionBody SEMI_COLON
;
procedureDeclarationHeader
: PROCEDURE IDENTIFIER ( LEFT_PARENTHESIS parameterDeclarationList RIGHT_PARENTHESIS )? SEMI_COLON
;
functionDeclaration
: functionDeclarationHeader
procedureOrFunctionBody SEMI_COLON
;
functionDeclarationHeader
: FUNCTION IDENTIFIER ( LEFT_PARENTHESIS parameterDeclarationList RIGHT_PARENTHESIS )? COLON typeReference SEMI_COLON
;
parameterDeclaration
: VAR? identifierList COLON typeReference
;
parameterDeclarationList
: ( parameterDeclaration ( COMMA parameterDeclaration )* )?
;
procedureOrFunctionBody
: pascalHeader instructionBlock
;
*/
/* ******************** EXPRESSIONS ******************** */
expression
: unaryOperator expression
// : expression relationalOperator expression
| expression multiplicativeOperator expression
| expression additiveOperator expression
| expression relationalOperator expression
| UNSIGNED_INTEGER_VALUE
| UNSIGNED_REAL_VALUE
| CHARACTER_VALUE
| BOOLEAN_VALUE
| COMPOSED_STRING_VALUE
| variableReference
| constantReference
// | functionCall
// | unaryOperator expression
| LEFT_PARENTHESIS expression RIGHT_PARENTHESIS
;
unaryOperator: PLUS | MINUS | NOT;
relationalOperator
: EQUAL
| NOT_EQUAL
| LESS_THAN
| LESS_OR_EQUAL
| GREATER_THAN
| GREATER_OR_EQUAL
;
additiveOperator: PLUS | MINUS | OR | XOR;
multiplicativeOperator: STAR | SLASH | DIV | MOD | AND | SHL | SHR;
variableReference
: IDENTIFIER
// | IDENTIFIER LEFT_BRACKET expression ( COMMA expression )* RIGHT_BRACKET
;
constantReference: IDENTIFIER;
// EOF