This repository has been archived by the owner on May 13, 2019. It is now read-only.
/
parser.citrus
293 lines (284 loc) · 9.8 KB
/
parser.citrus
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
grammar Rasp::Parser
rule vbscript
(statement
|class_def
|sub_def
|function_def
)* { Rasp::AST::File.new(matches.map {|m| m.value }.compact) }
end
rule class_def
`class` identifier ENDSTATEMENT
class_body
`end` `class` ENDSTATEMENT
end
rule scope
`public` | `private`
end
rule class_body
((scope SP)? ident_def ENDSTATEMENT
|(scope SP)? sub_def
|(scope SP)? function_def
|(scope SP)? property_def
|EOL
)* { matches.map {|m| m.value }.compact }
end
rule property_def
`property` SP (`get` | `let` | `set`) SP identifier SP? arg_def_list? ENDSTATEMENT
statement*
`end` SP `property` ENDSTATEMENT
end
rule sub_def
`sub` SP identifier SP? arg_def_list? ENDSTATEMENT
statement*
`end` SP `function` ENDSTATEMENT
end
rule function_def
`function` SP identifier SP? arg_def_list? ENDSTATEMENT
statement*
`end` SP `function` ENDSTATEMENT
end
rule arg_def_list
("(" SP? ( list:(arg_def SP? "," SP?)* arg_def SP? )? ")") { list.map {|l| l.arg_def.value } + [arg_def.value] }
end
rule arg_def
((`byref` SP | `byval` SP)? identifier) { identifier.value }
end
rule statement
(full_if_block
|single_line_if
|do_loop
|for_next
|for_each
|select_block
|with_block
|(simple_statement ENDSTATEMENT) { simple_statement.value }
|EOL
)
end
rule with_block
`with` SP expression ENDSTATEMENT statement* `end` SP `with` ENDSTATEMENT
end
rule select_block
`select` SP `case` SP expression ENDSTATEMENT
(`case` SP expression_list ENDSTATEMENT statement*)+
(`case` SP `else` statement*)?
`end` SP `select` ENDSTATEMENT
end
rule for_each
`for` SP `each` SP identifier SP `in` SP expression ENDSTATEMENT statement* `next` ENDSTATEMENT
end
rule do_loop
(`do` ENDSTATEMENT statement* `loop` ENDSTATEMENT
|`do` SP (`while` | `until`) SP expression ENDSTATEMENT statement* `loop` ENDSTATEMENT
|`do` ENDSTATEMENT statement* `loop` SP (`while` | `until`) SP expression ENDSTATEMENT
|`while` SP expression ENDSTATEMENT statement* `wend` ENDSTATEMENT
)
end
rule for_next
(`for` SP identifier SP? "=" SP? start:expression SP `to` SP finish:expression (SP `step` SP step:expression)? ENDSTATEMENT body:(statement*) `next` ENDSTATEMENT)
{ Rasp::AST::ForLoop.new(identifier.value, start.value, finish.value, first(:step) && first(:step).value, body.matches.map {|m| m.value }) }
end
rule single_line_if
`if` SP expression SP `then` SP (single_line_if | simple_statement_chain (SP `else` SP (single_line_if | simple_statement_chain EOL) | EOL) )
end
rule full_if_block
`if` SP expression SP `then` ENDSTATEMENT statement*
(`elseif` SP expression SP `then` ENDSTATEMENT statement*)*
(`else` ENDSTATEMENT statement*)?
`end` SP `if` ENDSTATEMENT
end
rule simple_statement_chain
(simple_statement SP? ":" SP?)* simple_statement
end
rule simple_statement
(assignment
|(`dim` SP identifier list:(SP? "," SP? identifier)*) { Rasp::AST::Declaration.new([identifier.value] + list.matches.map {|x| x.identifier.value }) }
|exit_statement
|on_error_statement
|const_assignment
|`call` SP identifier ("(" SP? (expression_list SP?)? ")")?
|(`option` SP `explicit`) { Rasp::AST::OptionExplicit.new }
|`randomize` (SP expression)? { Rasp::AST::Randomize.new expression.value }
|sub_call
)
end
rule on_error_statement
(`on` SP `error` SP `resume` SP `next`) { Rasp::AST::ErrorControl.new(true) }
| (`on` SP `error` SP `goto` SP "0") { Rasp::AST::ErrorControl.new(false) }
end
rule exit_statement
(`exit` SP type:(`sub` | `function` | `property` | `do` | `for`)) { Rasp::AST::ExitStatement.new type.downcase.to_sym }
end
rule assignment
(objvalue SP? "=" SP? expression
|`set` SP objvalue SP? "=" SP? expression
)
end
rule const_assignment
(`const` SP identifier SP? "=" SP? literal) { Rasp::AST::ConstAssignment.new(identifier.value, literal.value) }
end
rule sub_call
(
subref:(init:((local:identifier | "." withprop:identifier) ("(" SP? (expression_list SP?)? ")")?)
{ Rasp::AST::Call.new(first(:withprop) ? Rasp::AST::WithRef.new : nil,
(first(:local) || first(:withprop)).value,
first(:expression_list) && first(:expression_list).value) }
list:("." identifier ("(" SP? (expression_list SP?)? ")")?)*)
{
list.matches.inject(init.value) do |full, x|
Rasp::AST::Call.new(full, x.identifier.value, x.first(:expression_list) && x.first(:expression_list).value)
end
}
args:(!")" SP? expression_list)?
) {
v = subref.value
a = args.matches.first && args.matches.first.first(:expression_list)
v.args = a.value if a
v
}
end
rule objvalue
(init:((local:identifier | "." withprop:identifier) ("(" SP? (expression_list SP?)? ")")?)
{ Rasp::AST::Call.new(first(:withprop) ? Rasp::AST::WithRef.new : nil,
(first(:local) || first(:withprop)).value,
first(:expression_list) && first(:expression_list).value) }
list:("." identifier ("(" SP? (expression_list SP?)? ")")?)*)
{
list.matches.inject(init.value) do |full, x|
Rasp::AST::Call.new(full, x.identifier.value, x.first(:expression_list) && x.first(:expression_list).value)
end
}
end
rule identifier
(!reserved ident:/[A-Za-z_][A-Za-z0-9_]*/) { ident.to_sym } | "[" ident:/[^\]]+/ "]" { ident.to_sym }
end
rule reserved
active_reserved | other_reserved
end
rule active_reserved
# Reserved words that are used by the language, either to avoid
# ambiguity, as a convenience in the reference parser. Either way,
# we must reject them in order to be fully conforming.
`select` | `case` | `do` | `while` | `until` | `loop` | `end` | `wend`
| `if` | `then` | `elseif` | `else` | `byval` | `byref`
| `with` | `for` | `each` | `next` | `public` | `private`
| `class` | `function` | `sub` | `property` | `get` | `set` | `let`
| `imp` | `eqv` | `xor` | `or` | `and` | `not` | `is` | `mod`
| `new` | `const` | `dim` | `call` | `exit` | `option` | `randomize`
# Things you might think are reserved words, but actually aren't:
# `in`, `to`, `step`
end
rule other_reserved
# Reserved by spec, though unused in the language. Most likely, a
# reserved word in VB proper.
`typeof`
| `redim` | `preserve`
| `erase`
end
rule expression
(lhs:eqv_expr list:(SP op:`imp` SP rhs:eqv_expr
)*) { Rasp::AST.binary_op(Rasp::AST::ImpOp, lhs, list) }
end
rule eqv_expr
(lhs:xor_expr list:(SP op:`eqv` SP rhs:xor_expr
)*) { Rasp::AST.binary_op(Rasp::AST::EqvOp, lhs, list) }
end
rule xor_expr
(lhs:or_expr list:(SP op:`xor` SP rhs:or_expr
)*) { Rasp::AST.binary_op(Rasp::AST::XorOp, lhs, list) }
end
rule or_expr
(lhs:and_expr list:(SP op:`or` SP rhs:and_expr
)*) { Rasp::AST.binary_op(Rasp::AST::OrOp, lhs, list) }
end
rule and_expr
(lhs:not_expr list:(SP op:`and` SP rhs:not_expr
)*) { Rasp::AST.binary_op(Rasp::AST::AndOp, lhs, list) }
end
rule not_expr
(`not` SP cmp_expr) { Rasp::AST::NotOp.new(cmp_expr.value) } | cmp_expr
end
rule cmp_expr
(lhs:amp_expr list:(SP? op:"=" SP? rhs:amp_expr
|SP? op:"<>" SP? rhs:amp_expr
|SP? op:"<=" SP? rhs:amp_expr
|SP? op:">=" SP? rhs:amp_expr
|SP? op:"<" SP? rhs:amp_expr
|SP? op:">" SP? rhs:amp_expr
|SP op:`is` SP rhs:amp_expr
)*) { Rasp::AST.math_op(self) }
end
rule amp_expr
(lhs:add_expr list:(SP? op:"&" SP? rhs:add_expr
)*) { Rasp::AST.binary_op(Rasp::AST::StringAppend, lhs, list) }
end
rule add_expr
(lhs:mod_expr list:(SP? op:"+" SP? rhs:mod_expr
|SP? op:"-" SP? rhs:mod_expr
)*) { Rasp::AST.math_op(self) }
end
rule mod_expr
(lhs:idiv_expr list:(SP op:`mod` SP rhs:idiv_expr
)*) { Rasp::AST.math_op(self, :%) }
end
rule idiv_expr
(lhs:mul_expr list:(SP? op:"\\" SP? rhs:mul_expr
)*) { Rasp::AST.math_op(self) }
end
rule mul_expr
(lhs:exp_expr list:(SP? op:"*" SP? rhs:exp_expr
|SP? op:"/" SP? rhs:exp_expr
)*) { Rasp::AST.math_op(self) }
end
rule exp_expr
# Online references conflict on this point, but a manual test shows
# that unary minus binds more tightly than '^'
(lhs:unary_expr list:(SP? op:"^" SP? unary_expr
)*) { Rasp::AST.math_op(self) }
end
rule unary_expr
("+" SP? value_expr { Rasp::AST::UnaryPlus.new value_expr }
|"-" SP? value_expr { Rasp::AST::UnaryMinus.new value_expr }
|value_expr
)
end
rule value_expr
(literal
|(`new` SP class_name:(identifier ("." identifier)*)) { Rasp::AST::NewObject.new class_name }
|("(" SP? expression SP? ")") { expression.value }
|objvalue
)
end
rule literal
(float
|integer
|string
|`true` { Rasp::AST::TrueValue.new }
|`false` { Rasp::AST::FalseValue.new }
)
end
rule integer
/[+-]?[0-9]+/ { Rasp::AST::Integer.new to_i }
end
rule float
/[0-9]+.[0-9]+/ { Rasp::AST::Float.new to_f }
end
rule string
/"([^"]|"")*"/ { Rasp::AST::String.new to_s }
end
rule ENDSTATEMENT
/[ \t]*(?::|(?:'[^\n]+)?\n)[ \t]*/ { nil }
end
rule expression_list
((expression SP? "," SP?)* expression) { find(:expression).map {|e| e.value } }
end
rule EOL
/[ \t]*(?:(?:'[^\n]+)?\n[ \t]*)+/ { nil }
end
rule comment
/'[^\n]+/ { nil }
end
rule SP
/[ \t]+/ { nil }
end
end