-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathparser.rb
More file actions
1276 lines (1166 loc) · 38.7 KB
/
Copy pathparser.rb
File metadata and controls
1276 lines (1166 loc) · 38.7 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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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)
@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(extra_stop_tokens = [])
# Check for argument forwarding: ... (Ruby 2.7+)
# This must come before checking for range operators
saved_pos = @scanner.position
if literal(".")
if literal(".")
if literal(".")
# This is ... argument forwarding
# Return special marker for argument forwarding
return [[:forward_args]]
else
# Not ..., backtrack
@scanner.unget(".")
@scanner.unget(".")
end
else
# Not .., backtrack
@scanner.unget(".")
end
end
# Check for nested destructuring: |(a, b)|
if literal("(")
ws
# Parse nested argument list
nested_args = parse_arglist([")"])
ws
literal(")") or expected("')' to close destructuring")
# Build destruct node - keep nested structure, don't flatten
args = [[:destruct] + nested_args]
# Check if there are more parameters after the destructuring
nolfws
if literal(COMMA)
ws
more = parse_arglist(extra_stop_tokens) or expected("argument")
return args + more
end
return args
end
# Check for **, *, or & prefix
prefix = nil
if literal(ASTERISK)
# Check if it's ** (keyword splat) or * (splat)
if literal(ASTERISK)
prefix = "**"
else
prefix = ASTERISK
end
elsif literal(AMP)
prefix = AMP
end
ws if prefix
## FIXME: If "name" is not mentioned here, it is not correctly recognised
name = nil
if !(name = parse_name)
# Allow bare splat (e.g., def foo(*); end) - use special name :_
# Also allow bare keyword splat (e.g., def foo(**); end)
# Also allow bare block (e.g., def foo(&); end) - anonymous block forwarding (Ruby 3.1+)
if prefix == ASTERISK || prefix == "**" || prefix == AMP
name = :_
elsif prefix
expected("argument name following '#{prefix}'")
else
return
end
end
nolfws
default = nil
is_keyword_arg = false
# Build stop tokens list for default value parsing
# Include ; as stop token since it's a statement separator
stop_tokens = [COMMA, ";"] + extra_stop_tokens
# Check for keyword argument syntax: name: or name: value
if literal(":")
is_keyword_arg = true
nolfws
# Check if there's a default value after the colon
# Peek to see if next token is not comma/close paren/pipe
peek_char = @scanner.peek
if peek_char != "," && peek_char != ")" && peek_char != "|"
default = @shunting.parse(stop_tokens)
end
elsif literal("=")
nolfws
default = @shunting.parse(stop_tokens)
end
if prefix == "**"
args = [[name.to_sym, :keyrest]]
elsif prefix == ASTERISK
args = [[name.to_sym, :rest]]
elsif prefix == AMP
args = [[name.to_sym, :block]]
elsif is_keyword_arg
# Keyword argument: kw: or kw: default
if default
args = [[name.to_sym, :key, default]]
else
args = [[name.to_sym, :keyreq]]
end
elsif default
args = [[name.to_sym, :default, default]]
else
args = [name.to_sym]
end
nolfws
literal(COMMA) or return args
ws
# Check for trailing comma: if next char is a close delimiter, allow it
peek_char = @scanner.peek
if peek_char == ")" || peek_char == "|"
# Trailing comma is allowed, return current args
return args
end
more = parse_arglist(extra_stop_tokens) 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.
# ; is also inhibited since it's a statement separator after the condition
pos = position
ret = @sexp.parse || @shunting.parse([:do, ";", "\n"])
return ret
end
# if_unless ::= ("if"|"unless") if_body
# 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)]
if elseexps
ret << E[:do].concat(elseexps)
else
ret << E[:do, :nil]
end
return ret
end
# when ::= "when" ws* condition (nolfws* (":" | "then" | ";"))? ws* defexp*
def parse_when
pos = position
keyword(:when) or return
ws
cond = parse_condition or expected("condition for 'when'")
nolfws
literal(COLON) || keyword(:then) || literal(SEMICOLON)
ws
return E[:when, cond, parse_opt_defexp]
end
# Parse pattern contents inside Hash[...] or Array[...]
# Handles special pattern syntax like a:, b: (keyword pattern bindings)
def parse_pattern_list
patterns = []
loop do
ws
break if @scanner.peek == ']' || @scanner.peek == ')'
# Parse a single pattern element
# Try to parse keyword pattern (a:, a: value) or positional pattern
name = parse_name
if name
ws
if literal(':')
# Keyword pattern - could be shorthand (a:) or full (a: 0)
ws
# Check if this is shorthand (followed by comma or close) or full form
next_char = @scanner.peek
if next_char == ',' || next_char == ')' || next_char == ']'
# Shorthand: a: (binds key :a to variable a)
patterns << E[:pattern_key, name]
else
# Full form: a: value
# Use shunting with stop tokens to avoid consuming comma
value = @shunting.parse([',', ')', ']'])
if !value
expected("value after ':' in pattern")
end
# Create a pair for hash pattern matching
patterns << E[:pair, E[:sexp, name.inspect.to_sym], value]
end
ws
if literal(',')
ws
next
else
break
end
else
# Just a name, could be a constant or variable
patterns << name
ws
if literal(',')
ws
next
else
break
end
end
else
# Check for hash splat: ** or **rest
# Need to handle this specially before parse_subexp because
# bare ** has no operand and would fail in shunting yard
if literal('**')
ws
rest_var = parse_name
if rest_var
patterns << E[:hash_splat, rest_var]
else
patterns << E[:hash_splat]
end
ws
if literal(',')
ws
next
else
break
end
end
# Try to parse other pattern forms
exp = parse_subexp
break if !exp
patterns << exp
ws
if literal(',')
ws
next
else
break
end
end
end
patterns
end
# Parse a bare hash pattern like: a: 1, b: 2
# Called when we've already consumed the first name and seen that it's followed by :
# first_name: the name we already consumed
# Returns a hash node
def parse_hash_pattern_after_name(first_name)
pairs = []
# Process first pair (name already consumed)
ws
literal(':') or expected("':' after hash pattern key")
ws
value = parse_subexp
if !value
expected("value after ':' in hash pattern")
end
pairs << E[:pair, E[:sexp, first_name.inspect.to_sym], value]
# Parse remaining pairs
loop do
ws
break unless literal(',')
ws
name = parse_name
break if !name
ws
literal(':') or expected("':' after hash pattern key")
ws
value = parse_subexp
if !value
expected("value after ':' in hash pattern")
end
pairs << E[:pair, E[:sexp, name.inspect.to_sym], value]
end
return E[:hash] + pairs
end
# Parse a pattern for pattern matching (Ruby 3.0+)
# Handles special syntax like Hash[a:, b:] and bare hash patterns like a: 1, b: 2
def parse_pattern
pos = position
# Try to parse a constant name followed by [ or (
name = parse_name
if name
ws
# Check for ConstantName[pattern] or ConstantName(pattern) syntax
if literal('[')
# Parse pattern list inside brackets using special pattern syntax
ws
pattern_contents = parse_pattern_list
ws
literal(']') or expected("']' to close pattern")
# Return as a pattern node: [:pattern, ConstantName, contents]
return E[pos, :pattern, name] + pattern_contents
elsif literal('(')
# Parse pattern list inside parentheses
ws
pattern_contents = parse_pattern_list
ws
literal(')') or expected("')' to close pattern")
return E[pos, :pattern, name] + pattern_contents
elsif literal('=>')
# AS pattern: ConstantName => var (e.g., Integer => n)
# Matches the type and binds to the variable
ws
var = parse_name
if !var
expected("variable name after '=>' in AS pattern")
end
return E[pos, :as_pattern, name, var]
elsif @scanner.peek == ':'
# Bare hash pattern like a: 1, b: 2 (name already consumed)
return parse_hash_pattern_after_name(name)
else
# Bare name - variable binding pattern (e.g., "in a")
# Just return the name as-is
return name
end
end
# Check for bare hash splat pattern: in ** or in **rest
# This pattern matches any hash
if literal('**')
ws
rest_var = parse_name
if rest_var
return E[pos, :hash_splat, rest_var]
else
return E[pos, :hash_splat]
end
end
# Fall back to regular condition parsing for other patterns
parse_condition
end
# in ::= "in" ws* pattern (nolfws* (":" | "then" | ";"))? ws* defexp*
# Pattern matching branch for case statements (Ruby 3.0+)
def parse_in
pos = position
keyword(:in) or return
ws
pattern = parse_pattern or expected("pattern for 'in'")
nolfws
literal(COLON) || keyword(:then) || literal(SEMICOLON)
ws
# Use :in instead of :when to distinguish pattern matching branches
return E[:in, pattern, parse_opt_defexp]
end
# case ::= "case" ws* condition? (when|in)* ("else" ws* defexp*) "end"
# When condition is omitted, each when tests its expressions as booleans
# Ruby 3.0+ supports 'in' branches for pattern matching
def parse_case
pos = position
keyword(:case) or return
ws
cond = parse_condition # Condition is optional
nolfws; literal(";"); ws # Consume optional ; after condition
# Parse both 'when' and 'in' branches
branches = kleene { parse_when || parse_in }
ws
elses = nil # FIXME: Self-hosted compiler doesn't initialize local vars to nil.
# Without this, elses contains garbage when there's no else clause.
if keyword(:else)
ws
elses = parse_opt_defexp
end
ws
keyword(:end) or expected("'end' for open 'case'")
# Don't compact - cond can be nil for case-without-condition
# Only compact the elses if nil
result = E[pos, :case, cond, branches]
result << elses if elses
return result
end
# while ::= "while" ws* condition "do"? defexp* "end"
def parse_while_until_body(type)
pos = position
ws
cond = parse_condition or expected("condition for '#{type.to_s}' block")
nolfws; literal(SEMICOLON); nolfws; keyword(:do)
nolfws;
exps = parse_opt_defexp
keyword(:end) or expected("expression or 'end' for open '#{type.to_s}' block")
return E[pos, type, cond, [:do]+exps]
end
# for ::= "for" ws+ lvalue ws+ "in" ws+ expr ws* (SEMICOLON | ws+ "do") ws* defexp* "end"
# Supports: for x in array, for a,b in array (destructuring), for a, in array (trailing comma)
def parse_for
pos = position
keyword(:for) or return
ws
# Parse loop variable(s) - can be single var, method call, or destructured (a, b, c)
# Use shunting yard parser with 'in' as inhibit to stop at 'in' keyword
# This allows "for obj.attr in array" and "for a, b in array"
vars = []
# Parse first variable/lvalue - could be simple name or complex expression like obj.attr
first_var = @shunting.parse([:in, COMMA])
vars << (first_var or expected("variable name or expression after 'for'"))
ws
while literal(",")
ws
# Check if 'in' follows (trailing comma case: "for a, in array")
if keyword(:in)
# Put back 'in' and break - we'll parse it below
@scanner.unget("in")
break
end
# Check for splat (e.g., "for i, * in array" or "for i, *j in array")
if literal(ASTERISK)
ws
# Check if 'in' follows immediately (bare splat: "for i, * in array")
if keyword(:in)
@scanner.unget("in")
vars << :_
else
# Named splat: "for i, *j in array"
name = parse_name or expected("variable name or 'in' after splat in for loop")
vars << name
end
else
# Parse next variable - could be name or complex expression
next_var = @shunting.parse([:in, COMMA])
vars << (next_var or expected("variable name or expression in for loop"))
end
ws
end
# Expect 'in' keyword
keyword(:in) or expected("'in' keyword after for loop variable")
ws
# Parse the enumerable expression
enumerable = parse_condition or expected("expression after 'in'")
nolfws; literal(SEMICOLON); nolfws; keyword(:do)
nolfws;
# Parse body
exps = parse_opt_defexp
keyword(:end) or expected("expression or 'end' for open 'for' block")
# Return [:for, vars, enumerable, body]
# If single var, unwrap from array for simpler AST
var = vars.length == 1 ? vars[0] : [:destruct] + vars
return E[pos, :for, var, enumerable, [:do]+exps]
end
# parse_for_body - like parse_for but doesn't consume 'for' keyword
# Used when 'for' is treated as operator in shunting yard
def parse_for_body
pos = position
ws
# Parse loop variable(s) - can be single var, method call, or destructured (a, b, c)
vars = []
# Parse first variable/lvalue
first_var = @shunting.parse([:in, COMMA])
vars << (first_var or expected("variable name or expression after 'for'"))
ws
while literal(",")
ws
if keyword(:in)
@scanner.unget("in")
break
end
if literal(ASTERISK)
ws
if keyword(:in)
@scanner.unget("in")
vars << :_
else
name = parse_name or expected("variable name or 'in' after splat in for loop")
vars << name
end
else
next_var = @shunting.parse([:in, COMMA])
vars << (next_var or expected("variable name or expression in for loop"))
end
ws
end
keyword(:in) or expected("'in' keyword after for loop variable")
ws
enumerable = parse_condition or expected("expression after 'in'")
nolfws; literal(SEMICOLON); nolfws; keyword(:do)
nolfws;
exps = parse_opt_defexp
keyword(:end) or expected("expression or 'end' for open 'for' block")
var = vars.length == 1 ? vars[0] : [:destruct] + vars
return E[pos, :for, var, enumerable, [:do]+exps]
end
# rescue ::= "rescue" (nolfws* name nolfws* ("=>" ws* name)?)? ws defexp*
# Supports: rescue, rescue => e, rescue Error, rescue Error => e
def parse_rescue
pos = position
keyword(:rescue) or return
nolfws
c = parse_name # Optional exception class
nolfws
name = nil
# Check for => regardless of whether exception class was provided
if literal("=>")
# Parse assignable expression (allows self&.foo, @ivar, etc.)
# Similar to for loop variable parsing - allows complex lvalues
# Inhibit newline and statement keywords to stop at rescue body
name = @shunting.parse(["\n", :end, :rescue, :else, :elsif, :ensure]) or expected("variable to hold exception")
end
ws
body = parse_opt_defexp
return E[pos, :rescue, c, name, body]
end
# begin ::= "begin" ws* defexp* rescue? "end"
def parse_begin
pos = position
keyword(:begin) or return
ws
return parse_begin_body
end
# Shared method to parse rescue/else/ensure clauses
# Used by both begin...end and do...end blocks
# Returns [exps, rescue_, ensure_body]
def parse_rescue_else_ensure(exps = [])
pos = position
rescue_clauses = []
# Parse expressions until we hit rescue or a terminator
loop do
ws
if keyword(:rescue)
# Found rescue keyword - parse it properly
nolfws
c = parse_name # Optional exception class
nolfws
name = nil
if literal("=>")
# Parse assignable expression (allows self&.foo, @ivar, etc.)
# Similar to for loop variable parsing - allows complex lvalues
# Inhibit newline and statement keywords to stop at rescue body
name = @shunting.parse(["\n", :end, :rescue, :else, :elsif, :ensure]) or expected("variable to hold exception")
end
ws
body = parse_opt_defexp
rescue_clauses << E[pos, :rescue, c, name, body]
# Don't break - continue to parse more rescue clauses
else
# Use parse_exp to handle protected, class bodies, etc.
exp = parse_exp
break if !exp
exps << exp
end
end
# Parse additional rescue clauses if we already have some
while rescue_clauses.size > 0
ws
if keyword(:rescue)
pos = position
nolfws
c = parse_name
nolfws
name = nil
if literal("=>")
name = @shunting.parse(["\n", :end, :rescue, :else, :elsif, :ensure]) or expected("variable to hold exception")
end
ws
body = parse_opt_defexp
rescue_clauses << E[pos, :rescue, c, name, body]
else
break
end
end
# If we didn't parse any rescue in the loop, try parse_rescue
if rescue_clauses.empty?
single_rescue = parse_rescue
rescue_clauses << single_rescue if single_rescue
end
# Combine multiple rescue clauses into one if needed
rescue_ = rescue_clauses.empty? ? nil : (rescue_clauses.size == 1 ? rescue_clauses[0] : [:rescues] + rescue_clauses)
# Parse optional else clause (only valid if rescue exists)
else_body = nil
ws
if keyword(:else)
if !rescue_
expected("'rescue' before 'else' clause")
end
ws
else_body = parse_opt_defexp
end
# Parse optional ensure clause (can exist with or without rescue)
ensure_body = nil
ws
if keyword(:ensure)
ws
ensure_body = parse_opt_defexp
end
# If we have an else clause, append it to the rescue clause
if else_body && rescue_
rescue_ = E[rescue_.position, :rescue, rescue_[1], rescue_[2], rescue_[3], else_body]
end
return [exps, rescue_, ensure_body]
end
def parse_begin_body
pos = position
ws
exps, rescue_, ensure_body = parse_rescue_else_ensure([])
ws
keyword(:end) or expected("'end' for open 'begin' block")
# Return block with ensure as 5th element
# [:block, args, exps, rescue_clause, ensure_body]
return E[pos, :block, [], exps, rescue_, ensure_body]
end
# subexp ::= exp nolfws*
def parse_subexp
pos = position
# Inhibit ; and newline at statement level - they're separators, not :do operator
# Inside parentheses, ; and newline will still work as :do operator (see shunting.rb)
# Also inhibit case statement keywords (when, in, else, end) to prevent them from
# being consumed as operators when they should start new branches
ret = @shunting.parse([";", "\n", :when, :in, :else, :end, :elsif, :ensure, :rescue])
if ret.is_a?(Array)
ret = E[pos] + ret
end
nolfws
return ret
end
# parse_stabby_lambda ::= "->" *ws ("(" args ")")? *ws block
# | "->" *ws args *ws block
def parse_stabby_lambda
pos = position
keyword(:stabby_lambda) or return
ws
# Parse inline parameters: ->(x, y) { } or -> x, y { }
args = []
if literal("(")
# Parenthesized parameters: ->(x, y, z=1, *rest, &block) { }
# Use parse_arglist to handle defaults, splats, blocks, etc.
ws
args = parse_arglist([")"]) || []
ws
literal(")") or expected("')'")
ws
else
# Try to parse bare parameters: -> x, y { } or -> *a, b { }
# Only parse parameters if we don't see { or do
do_token = expect(:do)
if do_token
# Found 'do' - unget it for parse_block to consume
@scanner.unget(do_token)
elsif @scanner.peek != "{"
# No block start yet, parse bare parameters using parse_arglist
# This handles splat (*a), block (&b), keyword args, etc.
# Pass { and :do as stop tokens to prevent default values from consuming the lambda body
args = parse_arglist(["{", :do]) || []
end
ws
end
block = parse_block or expected("do .. end block")
# If we have inline args, create a proc with those args
# Otherwise use the block's args directly
if args.size > 0
block_body = block[2] || [] # block is [:proc, args, body]
return E[pos, :lambda, args, block_body]
end
return E[pos, :lambda, *block[1..-1]]
end
# lambda ::= "lambda" *ws block (no inline parameters allowed)
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_next
pos = position
return nil if !keyword(:next)
exps = parse_subexp
return E[pos, :next, exps] if exps
return E[pos, :next]
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
# Consume leading semicolons (empty statements)
while literal(";"); ws; end
ret = parse_def || parse_alias || parse_sexp ||
parse_subexp || parse_case || parse_require_relative || 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
kleene { parse_exp }
end
# block_body ::= ws * defexp*
def parse_block_exps
ws
kleene { parse_exp }
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
# Use parse_arglist to support default values, splats, and block parameters
# Pass [PIPE] as extra stop token so default values stop at the closing |
args = parse_arglist([PIPE]) || []
ws
literal(PIPE)
end
# Use shared rescue/else/ensure parsing
exps, rescue_, ensure_body = parse_rescue_else_ensure([])
ws
literal(close) or expected("'#{close.to_s}' for '#{start.to_s}'-block")
# Return proc node with rescue and ensure support
return E[pos, :proc] if args.size == 0 && exps.size == 0 && !rescue_ && !ensure_body
return E[pos, :proc, args, exps, rescue_, ensure_body]
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 ::= ("private"|"protected"|"public")? ws* "def" ws* name args? block_body
def parse_def
pos = position
saved_pos = @scanner.position
# Try to parse optional visibility modifier followed by def
# If we see a visibility keyword not followed by def, backtrack
vis = keyword(:private) || keyword(:protected) || keyword(:public)
if vis
saved_after_vis = @scanner.position
ws
if !keyword(:def)
# Visibility modifier not followed by def, so put it back with whitespace
# We need to include a space to separate from what follows
@scanner.unget(" ")
@scanner.unget(vis.to_s)
return nil
end
else
# No visibility modifier, just try to parse def
keyword(:def) or return nil
end
ws
name = parse_defname
args = parse_args || []
literal(";")
# Check for endless method definition: def name(args) = expr
# This is Ruby 3.0+ syntax for single-expression methods
nolfws
if literal("=")
ws
# Parse the expression (method body) - use parse_condition to get full expression
# parse_condition handles operators and complex expressions properly
expr = parse_condition or expected("expression for endless method definition")
# Endless methods can't have rescue/ensure, so return simple defm node
return E[pos, :defm, name, args, [expr]]
end
# Parse method body - parse_block_exps will naturally stop at keywords like rescue/ensure/end
exps = parse_block_exps
#STDERR.puts exps.inspect
# Parse optional rescue clause (similar to parse_begin)
rescue_ = nil
ws
if keyword(:rescue)
nolfws
c = parse_name # Optional exception class
nolfws
name_var = nil
if literal("=>")
# Parse assignable expression (allows self&.foo, @ivar, etc.)
# Similar to for loop variable parsing - allows complex lvalues
# Inhibit newline and statement keywords to stop at rescue body
name_var = @shunting.parse(["\n", :end, :rescue, :else, :elsif, :ensure]) or expected("variable to hold exception")
end
ws
# parse_opt_defexp will naturally stop at ensure/end keywords
rescue_body = parse_opt_defexp
rescue_ = E[pos, :rescue, c, name_var, rescue_body]
end
# Parse optional ensure clause (similar to parse_begin)
ensure_body = nil
ws
if keyword(:ensure)
ws
# parse_opt_defexp will naturally stop at end keyword
ensure_body = parse_opt_defexp
end
ws
#STDERR.puts @scanner.position.inspect
keyword(:end) or expected("expression or 'end' for open def '#{name.to_s}'")
# If we have rescue or ensure, wrap exps in a :block node
if rescue_ || ensure_body
body = E[pos, :block, [], exps, rescue_, ensure_body]
return E[pos, :defm, name, args, body]
else
return E[pos, :defm, name, args, exps]
end
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
# Check for global namespace (::ModuleName)
global_namespace = false
if literal('::')
ws
global_namespace = true
end
name = expect(Atom) || literal('<<') or expected("module name")
if name
# Check for namespaced module name (e.g., Foo::Bar::Baz)
# Build up [:deref, :Foo, :Bar, :Baz] for module Foo::Bar::Baz
while literal('::')
ws
next_part = expect(Atom) or expected("module name after ::")
name = [:deref, name, next_part]
end
# Mark as global namespace if :: prefix was present
if global_namespace
name = [:global, name]
end
end
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
# Like parse_module but for when 'module' keyword has already been consumed
def parse_module_body
pos = position
ws
# Check for global namespace (::ModuleName)
# In Ruby, ::A means "module A in the global namespace"
# We'll represent this as [:global, :A] so the compiler knows to emit at top level
global_namespace = false
if literal('::')
ws
global_namespace = true
end
name = expect(Atom) || literal('<<') or expected("module name")
if name
# Check for namespaced module name (e.g., Foo::Bar::Baz)