-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy.peg
More file actions
266 lines (176 loc) · 8.42 KB
/
easy.peg
File metadata and controls
266 lines (176 loc) · 8.42 KB
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
# ===== LEXICAL =====
Identifier <- !Reserved [A-Za-z_] [A-Za-z0-9_]*
Real <- [0-9]+ '.' ( [0-9]+ / ![0-9] ) ( [eE] [+\-]? [0-9]+ )?
Integer <- [0-9]+
Boolean <- 'TRUE' / 'FALSE'
String <- '"' ( '""' / '\\' . / (!'"' .) )* '"'
/ "'" ( "''" / '\\' . / (!"'" .) )* "'"
WS1 <- [ \t\r\n]+
# Just in case if I would need to break number into pieces.
# Real <- whole:[0-9]+ '.' frac:[0-9]+ ([eE] sign:[+\-]? exp:[0-9]+)?
# Integer <- digits:[0-9]+
Reserved <- ( 'PROGRAM' / 'END' / 'EXTERNAL' / 'PROCEDURE' / 'FUNCTION'
/ 'TYPE' / 'IS' / 'ARRAY' / 'OF' / 'STRUCTURE' / 'FIELD'
/ 'DECLARE' / 'NAME' / 'BEGIN' / 'FOR' / 'DO' / 'BY' / 'TO'
/ 'WHILE' / 'IF' / 'THEN' / 'ELSE' / 'FI'
/ 'CALL' / 'RETURN' / 'EXIT'
/ 'SELECT' / 'CASE' / 'OTHERWISE'
/ 'REPEAT' / 'REPENT' / 'NPUT' / 'OUTPUT'
/ 'INTEGER' / 'REAL' / 'BOOLEAN' / 'STRING'
/ 'MOD' / 'XOR' / 'NOT' ) ![A-Za-z0-9_]
# ===== TOP LEVEL =====
compilation <- program_segment+
program_segment <- main:main_program / external:external_procedure
# ===== COMMENTS =====
Comment <- BlockComment / LineComment
BlockComment <- '/*' (!'*/' .)* '*/'
LineComment <- '//' (![\r\n] .)*
Skip <- ( [ \t\r\n] / Comment )*
# ===== PROGRAMS =====
main_program <-
'PROGRAM' name:Identifier ':' body:segment_body
'END' 'PROGRAM' end_name:Identifier ';'
# ===== EXTERNAL PROCEDURES =====
external_procedure <- external_subprogram / external_function
external_subprogram <-
external_procedure_head ':' body:segment_body
'END' 'EXTERNAL' 'PROCEDURE' end_name:Identifier ';'
external_function <-
external_function_head ':' body:segment_body
'END' 'EXTERNAL' 'FUNCTION' end_name:Identifier ';'
external_procedure_head <- 'EXTERNAL' 'PROCEDURE' name:external_procedure_name
external_function_head <- 'EXTERNAL' 'FUNCTION' name:external_procedure_name return_type:external_type
external_procedure_name <- Identifier / Identifier params:external_parameter_list
external_parameter_list <- '(' head:external_parameter (',' tail:external_parameter)* ')'
external_parameter <- id:Identifier type:external_type name:'NAME'?
external_type <- basic_type:basic_type
# ===== SEGMENTS =====
segment_body <-
type_definitions:type_definition*
variable_declarations:variable_declaration*
subroutine_definitions:subroutine_definition*
statements:executable_statement+
# ===== TYPES =====
type_definition <- 'TYPE' name:Identifier 'IS' def:type ';'
type <- basic_type:basic_type / arrayed_type:arrayed_type / structured_type:structured_type / ref:type_identifier
basic_type <- 'INTEGER' / 'REAL' / 'BOOLEAN' / 'STRING'
arrayed_type <- 'ARRAY' bounds 'OF' element_type:type
bounds <- '[' lo:expression ']' / '[' lo:expression ':' hi:expression ']'
structured_type <- 'STRUCTURE' fields:field_list 'END' 'STRUCTURE'
# right-recursive to avoid repetition corner cases
field_list <- head:field (',' tail:field_list)?
field <- 'FIELD' name:Identifier 'IS' t:type
type_identifier <- name:Identifier
# ===== DECLARATIONS =====
variable_declaration <- 'DECLARE' names:declared_names t:type ';'
declared_names <- declared_one_name / declared_multiple_names
declared_one_name <- Identifier
declared_multiple_names <- '(' head:Identifier (',' tail:Identifier)* ')'
# ===== INTERNAL PROCEDURES =====
subroutine_definition <- procedure_definition / function_definition / external_subprogram_def / external_function_def
procedure_definition <- procedure_head ':' body:segment_body 'END' 'PROCEDURE' end_name:Identifier ';'
function_definition <- function_head ':' body:segment_body 'END' 'FUNCTION' end_name:Identifier ';'
external_subprogram_def <- head:external_procedure_head ';'
external_function_def <- head:external_function_head ';'
procedure_head <- 'PROCEDURE' name:procedure_name
function_head <- 'FUNCTION' name:procedure_name return_type:type
procedure_name <- Identifier params:internal_parameter_list / Identifier
internal_parameter_list <- '(' head:internal_parameter (',' tail:internal_parameter)* ')'
internal_parameter <- id:Identifier parameter_type:type name:'NAME'?
# ===== EXECUTABLE STATEMENTS =====
executable_statement <-
assignment_statement
/ call_statement
/ return_statement
/ exit_statement
/ conditional_statement
/ compound_statement
/ iteration_statement
/ selection_statement
/ repeat_statement
/ repent_statement
/ input_statement
/ output_statement
/ null_statement
# ----- ASSIGNMENTS -----
assignment_statement <- 'SET' targets:target+ expr:expression ';'
target <- var:variable ':='
# ----- PROCEDURE CALLS -----
call_statement <- 'CALL' ref:procedure_reference ';'
procedure_reference <- id:procedure_identifier argument:argument_list / id:procedure_identifier
procedure_identifier <- name:Identifier
argument_list <- '(' head:expression (',' tail:expression)* ')'
# ----- RETURNS -----
return_statement <- 'RETURN' ';' / 'RETURN' value:expression ';'
# ----- EXITS -----
exit_statement <- 'EXIT' ';'
# ----- CONDITIONALS -----
conditional_statement <- simple_conditional_statement / (lbl:label simple_conditional_statement)
simple_conditional_statement <-
'IF' cond:expression 'THEN' then_body:segment_body 'FI' ';'
/ 'IF' cond:expression 'THEN' then_body:segment_body 'ELSE' else_body:segment_body 'FI' ';'
label <- name:Identifier ':'
# ----- COMPOUND -----
compound_statement <- simple_compound / (lbl:label simple_compound)
simple_compound <- 'BEGIN' body:segment_body ('END' end_name:Identifier ';' / 'END' ';')
# ----- ITERATIONS -----
# ----- ITERATIONS -----
iteration_statement <- simple_iteration_statement / lbl:label simple_iteration_statement
simple_iteration_statement <-
'FOR' tgt:iteration_target ctrl:control 'DO' body:segment_body
('END' 'FOR' end_name:Identifier ';' / 'END' 'FOR' ';')
iteration_target <- var:variable ':='
# Allow (step/limit) optionally followed by WHILE, or a bare WHILE by itself
control <- (step_control while_control?) / while_control
# Step/limit with flexible order (must include at least one of them)
step_control <- init:initial_value ( step:step limit:limit? / limit:limit step:step? )
initial_value <- expression
step <- 'BY' value:expression
limit <- 'TO' value:expression
while_control <- 'WHILE' cond:expression
# ----- SELECTION -----
selection_statement <- simple_selection / (lbl:label simple_selection)
simple_selection <-
'SELECT' sel:expression 'OF' body:selection_body end:selection_end
selection_body <- case_list escape_case?
selection_end <- 'END' 'SELECT' end_name:Identifier ';' / 'END' 'SELECT' ';'
case_list <- case+
case <- 'CASE' sel:selector ':' body:case_body
selector <- '(' head:expression (',' tail:expression)* ')'
escape_case <- 'OTHERWISE' ':' body:case_body
case_body <- segment_body
# ----- REPEAT / REPENT -----
repeat_statement <- 'REPEAT' name:Identifier ';'
repent_statement <- 'REPENT' name:Identifier ';'
# ----- INPUT / OUTPUT -----
input_statement <- 'INPUT' list:input_list ';'
input_list <- head:variable (',' tail:variable)*
output_statement <- 'OUTPUT' list:output_list ';'
output_list <- head:expression (',' tail:expression)*
# ----- NULL / LABEL -----
null_statement <- ';'
# ===== EXPRESSIONS =====
expression <- xor_expr
xor_expr <- head:or_expr ( 'XOR' tail:or_expr )*
or_expr <- head:and_expr ( '|' tail:and_expr )*
and_expr <- head:not_expr ( '&' tail:not_expr )*
not_expr <- 'NOT' inner:rel_expr / rel_expr
rel_expr <- head:concat_expr ( op:relation rhs:concat_expr )*
concat_expr <- head:add_expr ( '||' tail:add_expr )*
add_expr <- head:mul_expr ( op:adding_operator rhs:mul_expr )*
mul_expr <- head:unary_expr ( op:multiplying_operator rhs:unary_expr )*
unary_expr <- sign:adding_operator expr:postfix_expr / postfix_expr
postfix_expr <- primary
primary <- function_reference / variable / constant / '(' inner:expression ')'
relation <- '<=' / '>=' / '<>' / '<' / '>' / '='
adding_operator <- '+' / '-'
multiplying_operator <- '*' / '/' / 'MOD'
# ----- VARIABLES -----
variable <- base:Identifier parts:( ('.' field:Identifier) / ('[' index:expression ']') )*
# ----- CONSTANTS -----
constant <- Real / Integer / Boolean / String
# ----- FUNCTION CALLS -----
function_reference <-
fn:function_identifier '(' ')'
/ fn:function_identifier '(' head:expression (',' tail:expression)* ')'
function_identifier <- name:Identifier