-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathparser.rb
499 lines (445 loc) · 12.8 KB
/
parser.rb
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
require 'parserbase'
require 'sexp'
require 'utils'
require 'shunting'
require 'operators'
class Parser < ParserBase
@@requires = {}
attr_accessor :include_paths
def initialize(scanner, opts = {})
super(scanner)
@opts = opts
@sexp = SEXParser.new(scanner)
# FIXME:
# OpPrec::parser fails, though it works with MRI
@shunting = OpPrec.parser(scanner, self)
# FIXME: If member variable is not explicitly assigned, it
# will be 0 rather than nil. Need to ensure instance vars
# are cleared.
@include_paths = nil
@include_paths = opts[:include_paths].dup if opts[:include_paths]
@include_paths ||= []
path = File.expand_path(File.dirname(__FILE__)+"/lib")
@include_paths << path
# FIXME: This is a hack.
@include_paths << "./lib"
end
# name ::= atom
def parse_name
expect(Atom)
end
ASTERISK="*"
AMP="&"
LP="("
RP=")"
COMMA=","
COLON=":"
SEMICOLON=";"
PIPE="|"
# arglist ::= ("*" ws*)? name nolfws* ("," ws* arglist)?
def parse_arglist
prefix = literal(ASTERISK) || literal(AMP)
ws if prefix
## FIXME: If "name" is not mentioned here, it is not correctly recognised
name = nil
if !(name = parse_name)
expected("argument name following '#{prefix}'") if prefix
return
end
nolfws
default = nil
if literal("=")
nolfws
default = @shunting.parse([COMMA])
end
if prefix then args = [[name.to_sym, prefix == ASTERISK ? :rest : :block]]
elsif default
args = [[name.to_sym, :default, default]]
else
args = [name.to_sym]
end
nolfws
literal(COMMA) or return args
ws
more = parse_arglist or expected("argument")
return args + more
end
# args ::= nolfws* ( "(" ws* arglist ws* ")" | arglist )
def parse_args
nolfws
if literal(LP)
ws; args = parse_arglist; ws
literal(RP) or expected("')'")
return args
end
return parse_arglist
end
# condition ::= sexp | opprecexpr
def parse_condition
# :do is needed in the inhibited set because of ugly constructs like
# "while cond do end" where the "do .. end" block belongs to "while",
# not to any function in the condition.
pos = position
ret = @sexp.parse || @shunting.parse([:do])
return ret
end
# if_unless ::= ("if"|"unless") if_body
def parse_if_unless
pos = position
type = keyword(:if) || keyword(:unless) or return
parse_if_body(type.to_sym)
end
# FIXME: Weird parser bug: If '"then' appears together in the comment
# line before, it causes a parse failure
# if_body ::= ws* condition nolfws* ";"? nolfws* "then"? ws*
# defexp* ws* ("elsif" if_body | ("else" defexp*)? "end") .
def parse_if_body(type)
pos = position
ws
cond = parse_condition or expected("condition for '#{type.to_s}' block")
nolfws; literal(";")
nolfws; keyword(:then); ws;
exps = parse_opt_defexp
ws
# FIXME: Workaround for intialization error
elseexps = nil
if keyword(:elsif)
# We treat "if ... elif ... else ... end" as shorthand for "if ... else if ... else ... end; end"
elseexps = [parse_if_body(:if)]
else
if keyword(:else)
ws
elseexps = parse_opt_defexp
end
keyword(:end) or expected("expression or 'end' for open 'if'")
end
ret = E[pos,type.to_sym, cond, E[:do].concat(exps)]
ret << E[:do].concat(elseexps) if elseexps
return ret
end
# when ::= "when" ws* condition (nolfws* ":")? ws* defexp*
def parse_when
pos = position
keyword(:when) or return
ws
cond = parse_condition or expected("condition for 'when'")
nolfws
literal(COLON)
ws
return E[:when, cond, parse_opt_defexp]
end
# case ::= "case" ws* condition when* ("else" ws* defexp*) "end"
def parse_case
pos = position
keyword(:case) or return
ws
cond = parse_condition or expected("condition for 'case' block")
ws
whens = kleene { parse_when }
ws
if keyword(:else)
ws
elses = parse_opt_defexp
end
ws
keyword(:end) or expected("'end' for open 'case'")
return E[pos, :case, cond, whens, elses].compact
end
# while ::= "while" ws* condition "do"? defexp* "end"
def parse_while
pos = position
keyword(:while) or return
ws
cond = parse_condition or expected("condition for 'while' block")
nolfws; literal(SEMICOLON); nolfws; keyword(:do)
nolfws;
exps = parse_opt_defexp
keyword(:end) or expected("expression or 'end' for open 'while' block")
return E[pos, :while, cond, [:do]+exps]
end
# rescue ::= "rescue" (nolfws* name nolfws* ("=>" ws* name)?)? ws defexp*
def parse_rescue
pos = position
keyword(:rescue) or return
nolfws
if c = parse_name
nolfws
if literal("=>")
ws
name = parse_name or expected("variable to hold exception")
end
end
ws
return E[pos, :rescue, c, name, parse_opt_defexp]
end
# begin ::= "begin" ws* defexp* rescue? "end"
def parse_begin
pos = position
keyword(:begin) or return
ws
exps = parse_opt_defexp
rescue_ = parse_rescue
keyword(:end) or expected("expression or 'end' for open 'begin' block")
return E[pos, :block, [], exps, rescue_]
end
# subexp ::= exp nolfws*
def parse_subexp
pos = position
ret = @shunting.parse
if ret.is_a?(Array)
ret = E[pos] + ret
end
nolfws
return ret
end
# lambda ::= "lambda" *ws block
def parse_lambda
pos = position
keyword(:lambda) or return
ws
block = parse_block or expected("do .. end block")
return E[pos, :lambda, *block[1..-1]]
end
def parse_break
pos = position
return nil if !keyword(:break)
return E[pos, :break]
end
def parse_next
pos = position
return nil if !keyword(:next)
exps = parse_subexp
# FIXME: Compiler @bug workaround:
# Current splat handling crashes if argument is not
# an array.
exps = Array(exps)
return E[pos, :next, *exps]
end
# Later on "defexp" will allow anything other than "def"
# and "class".
# defexp ::= sexp | while | begin | case | if | lambda | subexp
def parse_defexp
pos = position
ws
ret = parse_sexp || parse_while || parse_begin || parse_case || parse_if_unless || parse_lambda || parse_break || parse_next || parse_subexp || parse_require
if ret.respond_to?(:position)
ret.position = pos
# FIXME: @bug this below is needed for MRI, but not for the selfhosted compiler...
# Unsure why, but they should not behave differently...
elsif ret.is_a?(Array)
ret = E[pos].concat(ret)
end
nolfws
if sym = expect(:if, :while, :rescue)
# FIXME: This is likely the wrong way to go in some situations involving blocks
# that have different semantics - parser may need a way of distinguishing them
# from "normal" :if/:while
ws
cond = parse_condition or expected("condition for '#{sym.to_s}' statement modifier")
nolfws; literal(SEMICOLON)
ret = E[pos, sym.to_sym, cond, ret]
end
#ws; literal(";"); ws
return ret
end
def parse_opt_defexp
exps = E[position]
while ret = parse_exp
exps << ret
end
exps
end
# block_body ::= ws * defexp*
def parse_block_exps
pos = position
ws
exps = E[pos]
while ret = parse_defexp
exps << ret
end
return exps
end
def parse_block(start = nil)
pos = position
return nil if start == nil and !(start = expect("{",:do))
close = (start.to_s == "{") ? "}" : :end
ws
args = []
if literal(PIPE)
ws
# FIXME:
# This is a workaround, as
# "begin ... end while ...." does not
# yet work correctly.
while name = parse_name
args << name
ws
break if !literal(COMMA)
ws
end
ws
literal(PIPE)
end
exps = parse_block_exps
ws
literal(close) or expected("'#{close.to_s}' for '#{start.to_s}'-block")
return E[pos, :proc ] if args.size == 0 and exps.size == 0
return E[pos, :proc, args, exps]
end
def parse_defname
name = expect(Methodname) || @shunting.parse or expected("function name")
if (expect("."))
name = [name]
ret = expect(Methodname) or expected("name following '#{name}.'")
name << ret
end
return name
end
# def ::= "def" ws* name args? block_body
def parse_def
pos = position
keyword(:def) or return nil
ws
name = parse_defname
args = parse_args || []
literal(";")
exps = parse_block_exps
keyword(:end) or expected("expression or 'end' for open def '#{name.to_s}'")
return E[pos, :defm, name, args, exps]
end
def parse_sexp; @sexp.parse; end
# module ::= "module" ws* name ws* exp* "end"
def parse_module
pos = position
type = keyword(:module) or return
ws
name = expect(Atom) || literal('<<') or expected("class name")
ws
error("A module can not have a super class") if @scanner.peek == ?<
exps = kleene { parse_exp }
keyword(:end) or expected("expression or \'end\'")
return E[pos, type.to_sym, name, :Object, exps]
end
# class ::= ("class" ws* (name|'<<') ws* (< ws* superclass)? ws* name ws* exp* "end"
def parse_class
pos = position
type = keyword(:class) or return
ws
name = expect(Atom) || literal("\<\<") or expected("class name")
if name == "<<"
ob = parse_exp
name = [:eigen, ob]
end
ws
# FIXME: Workaround for initialization error
superclass = nil
if expect("<")
ws
superclass = expect(Atom) or expected("superclass")
end
exps = kleene { parse_exp }
keyword(:end) or expected("expression or 'end'")
return E[pos, type.to_sym, name, superclass || :Object, exps]
end
# Returns the include paths relative to a given filename.
def rel_include_paths(filename)
if filename[0].chr == "/"
if filename[-3..-1] != ".rb"
return [filename +".rb"]
end
return [filename]
end
# FIXME: Hack due to codegen error
# regarding using argument inside block
fname = filename
@include_paths.collect do |path|
# FIXME: Hack due to codegen error
# regarding argument inside the block
fname
full = File.expand_path("#{path}/#{fname}")
full << ".rb" if full[-3..-1] != ".rb"
full
end
end
# Statically including a require'd file
#
# Not sure if I think this really belong in the parser,
# as opposed to being handled as post-processing later -
# may refactor this as a separate tree-rewriting step later.
def require q
return true if @@requires[q]
# FIXME: Handle include path
paths = rel_include_paths(q)
f = nil
fname = nil
paths.detect do |path|
fname = path
f = File.exists?(path) ? File.open(path) : nil
end
error("Unable to open '#{q}'") if !f
# STDERR.puts "NOTICE: Statically requiring '#{q}' from #{fname}"
# FIXME: This fails with
# @@requires[q] = []
# as well as with:
# @@requires[q] = Array.new
# (for apparently different reasons)
a = Array.new
@@requires[q] = a # Prevent include/require loops
s = Scanner.new(f)
pos = position
# FIXME: Is this change also down to parser bug?
parser = Parser.new(s, @opts)
expr = parser.parse(false)
e = E[pos,:required, expr]
@@requires[q] = e
e
end
# require ::= "require" ws* subexp
def parse_require
pos = position
keyword(:require) or return
ws
q = parse_subexp or expected("name of source to require")
ws
if q.is_a?(Array) || @opts[:norequire]
STDERR.puts "WARNING: NOT processing require for #{q.inspect}"
return E[pos, :require, q]
end
self.require(q)
end
# include ::= "include" ws* name w
def parse_include
pos = position
keyword(:include) or return
ws
n = parse_name or expected("name of module to include")
ws
return E[pos, :include, n]
end
# exp ::= ws* (class | def | sexp)
def parse_exp
ws
pos = position
ret = parse_class || parse_module || parse_def || parse_include || parse_defexp || literal("protected")
if ret.is_a?(Array)
ret = E[pos].concat(ret)
elsif ret.respond_to?(:position) && !ret.position
ret.position = pos
end
ws; expect(SEMICOLON); ws
return ret
end
# program ::= exp* ws*
def parse(require_core = true)
res = E[position, :do]
# FIXME: This does not work in the face of compiling the compiler somewhere where
# the paths are different than where it will run. A lesson in the compile-time vs.
# runtime issue.
# res << self.require(File.expand_path(File.dirname(__FILE__)+"/lib/core/core.rb")) if require_core and !@opts[:norequire]
res << self.require("core/core.rb") if require_core and !@opts[:norequire]
res.concat(kleene { parse_exp })
ws
error("Expected EOF") if scanner.peek
return res
end
end