ruby_parser-legacy 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ruby_parser-legacy might be problematic. Click here for more details.

@@ -0,0 +1,1412 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+
4
+ $DEBUG = true if ENV["DEBUG"]
5
+
6
+ class RubyParser::Legacy::RubyLexer
7
+
8
+ # :stopdoc:
9
+ HAS_ENC = "".respond_to? :encoding
10
+
11
+ IDENT_CHAR = if HAS_ENC then
12
+ /[\w\u0080-\u{10ffff}]/u
13
+ else
14
+ /[\w\x80-\xFF]/n
15
+ end
16
+
17
+ EOF = :eof_haha!
18
+
19
+ # ruby constants for strings (should this be moved somewhere else?)
20
+
21
+ STR_FUNC_BORING = 0x00
22
+ STR_FUNC_ESCAPE = 0x01 # TODO: remove and replace with REGEXP
23
+ STR_FUNC_EXPAND = 0x02
24
+ STR_FUNC_REGEXP = 0x04
25
+ STR_FUNC_QWORDS = 0x08
26
+ STR_FUNC_SYMBOL = 0x10
27
+ STR_FUNC_INDENT = 0x20 # <<-HEREDOC
28
+ STR_FUNC_ICNTNT = 0x40 # <<~HEREDOC
29
+
30
+ STR_SQUOTE = STR_FUNC_BORING
31
+ STR_DQUOTE = STR_FUNC_BORING | STR_FUNC_EXPAND
32
+ STR_XQUOTE = STR_FUNC_BORING | STR_FUNC_EXPAND
33
+ STR_REGEXP = STR_FUNC_REGEXP | STR_FUNC_ESCAPE | STR_FUNC_EXPAND
34
+ STR_SSYM = STR_FUNC_SYMBOL
35
+ STR_DSYM = STR_FUNC_SYMBOL | STR_FUNC_EXPAND
36
+
37
+ EXPR_BEG_ANY = [:expr_beg, :expr_mid, :expr_class ]
38
+ EXPR_ARG_ANY = [:expr_arg, :expr_cmdarg, ]
39
+ EXPR_END_ANY = [:expr_end, :expr_endarg, :expr_endfn]
40
+
41
+ ESCAPES = {
42
+ "a" => "\007",
43
+ "b" => "\010",
44
+ "e" => "\033",
45
+ "f" => "\f",
46
+ "n" => "\n",
47
+ "r" => "\r",
48
+ "s" => " ",
49
+ "t" => "\t",
50
+ "v" => "\13",
51
+ "\\" => '\\',
52
+ "\n" => "",
53
+ "C-\?" => 127.chr,
54
+ "c\?" => 127.chr,
55
+ }
56
+
57
+ TOKENS = {
58
+ "!" => :tBANG,
59
+ "!=" => :tNEQ,
60
+ # "!@" => :tUBANG,
61
+ "!~" => :tNMATCH,
62
+ "," => :tCOMMA,
63
+ ".." => :tDOT2,
64
+ "..." => :tDOT3,
65
+ "=" => :tEQL,
66
+ "==" => :tEQ,
67
+ "===" => :tEQQ,
68
+ "=>" => :tASSOC,
69
+ "=~" => :tMATCH,
70
+ "->" => :tLAMBDA,
71
+ }
72
+
73
+ TAB_WIDTH = 8
74
+
75
+ @@regexp_cache = Hash.new { |h,k| h[k] = Regexp.new(Regexp.escape(k)) }
76
+ @@regexp_cache[nil] = nil
77
+
78
+ # :startdoc:
79
+
80
+ attr_accessor :lineno # we're bypassing oedipus' lineno handling.
81
+ attr_accessor :brace_nest
82
+ attr_accessor :cmdarg
83
+ attr_accessor :command_start
84
+ attr_accessor :cmd_state # temporary--ivar to avoid passing everywhere
85
+ attr_accessor :last_state
86
+ attr_accessor :cond
87
+ attr_accessor :extra_lineno
88
+
89
+ ##
90
+ # Additional context surrounding tokens that both the lexer and
91
+ # grammar use.
92
+
93
+ attr_accessor :lex_state
94
+ attr_accessor :lex_strterm
95
+ attr_accessor :lpar_beg
96
+ attr_accessor :paren_nest
97
+ attr_accessor :parser # HACK for very end of lexer... *sigh*
98
+ attr_accessor :space_seen
99
+ attr_accessor :string_buffer
100
+ attr_accessor :string_nest
101
+
102
+ if $DEBUG then
103
+ alias lex_state= lex_state=
104
+ def lex_state=o
105
+ return if @lex_state == o
106
+ c = caller.first
107
+ c = caller[1] if c =~ /\bresult\b/
108
+ warn "lex_state: %p -> %p from %s" % [@lex_state, o, c.clean_caller]
109
+ @lex_state = o
110
+ end
111
+ end
112
+
113
+ # Last token read via next_token.
114
+ attr_accessor :token
115
+
116
+ ##
117
+ # What version of ruby to parse. 18 and 19 are the only valid values
118
+ # currently supported.
119
+
120
+ attr_accessor :version
121
+
122
+ attr_writer :comments
123
+
124
+ def initialize v = 18
125
+ self.version = v
126
+ @lex_state = :expr_none
127
+
128
+ self.cond = RubyParser::Legacy::RubyParserStuff::StackState.new(:cond, $DEBUG)
129
+ self.cmdarg = RubyParser::Legacy::RubyParserStuff::StackState.new(:cmdarg, $DEBUG)
130
+
131
+ reset
132
+ end
133
+
134
+ def arg_ambiguous
135
+ self.warning("Ambiguous first argument. make sure.")
136
+ end
137
+
138
+ def arg_state
139
+ in_arg_state? ? :expr_arg : :expr_beg
140
+ end
141
+
142
+ def beginning_of_line?
143
+ ss.bol?
144
+ end
145
+ alias :bol? :beginning_of_line? # to make .rex file more readable
146
+
147
+ def comments # TODO: remove this... maybe comment_string + attr_accessor
148
+ c = @comments.join
149
+ @comments.clear
150
+ c
151
+ end
152
+
153
+ def end_of_stream?
154
+ ss.eos?
155
+ end
156
+
157
+ def expr_dot?
158
+ lex_state == :expr_dot
159
+ end
160
+
161
+ def expr_fname?
162
+ lex_state == :expr_fname
163
+ end
164
+
165
+ def expr_result token, text
166
+ cond.push false
167
+ cmdarg.push false
168
+ result :expr_beg, token, text
169
+ end
170
+
171
+ def heredoc here # TODO: rewrite / remove
172
+ _, eos, func, last_line = here
173
+
174
+ indent = (func & STR_FUNC_INDENT) != 0 ? "[ \t]*" : nil
175
+ content_indent = (func & STR_FUNC_ICNTNT) != 0
176
+ expand = (func & STR_FUNC_EXPAND) != 0
177
+ eos_re = /#{indent}#{Regexp.escape eos}(\r*\n|\z)/
178
+ err_msg = "can't match #{eos_re.inspect} anywhere in "
179
+
180
+ rb_compile_error err_msg if end_of_stream?
181
+
182
+ if beginning_of_line? && scan(eos_re) then
183
+ self.lineno += 1
184
+ ss.unread_many last_line # TODO: figure out how to remove this
185
+ return :tSTRING_END, eos
186
+ end
187
+
188
+ self.string_buffer = []
189
+
190
+ if expand then
191
+ case
192
+ when scan(/#[$@]/) then
193
+ ss.pos -= 1 # FIX omg stupid
194
+ return :tSTRING_DVAR, matched
195
+ when scan(/#[{]/) then
196
+ return :tSTRING_DBEG, matched
197
+ when scan(/#/) then
198
+ string_buffer << '#'
199
+ end
200
+
201
+ begin
202
+ c = tokadd_string func, "\n", nil
203
+
204
+ rb_compile_error err_msg if
205
+ c == RubyLexer::EOF
206
+
207
+ if c != "\n" then
208
+ return :tSTRING_CONTENT, string_buffer.join.delete("\r")
209
+ else
210
+ string_buffer << scan(/\n/)
211
+ end
212
+
213
+ rb_compile_error err_msg if end_of_stream?
214
+ end until check(eos_re)
215
+ else
216
+ until check(eos_re) do
217
+ string_buffer << scan(/.*(\n|\z)/)
218
+ rb_compile_error err_msg if end_of_stream?
219
+ end
220
+ end
221
+
222
+ self.lex_strterm = [:heredoc, eos, func, last_line]
223
+
224
+ string_content = string_buffer.join.delete("\r")
225
+
226
+ string_content = heredoc_dedent(string_content) if content_indent && ruby23plus?
227
+
228
+ return :tSTRING_CONTENT, string_content
229
+ end
230
+
231
+ def heredoc_dedent(string_content)
232
+ width = string_content.scan(/^[ \t]*(?=\S)/).map do |whitespace|
233
+ heredoc_whitespace_indent_size whitespace
234
+ end.min || 0
235
+
236
+ string_content.split("\n", -1).map do |line|
237
+ dedent_string line, width
238
+ end.join "\n"
239
+ end
240
+
241
+ def dedent_string(string, width)
242
+ characters_skipped = 0
243
+ indentation_skipped = 0
244
+
245
+ string.chars.each do |char|
246
+ break if indentation_skipped >= width
247
+ if char == ' '
248
+ characters_skipped += 1
249
+ indentation_skipped += 1
250
+ elsif char == "\t"
251
+ proposed = TAB_WIDTH * (indentation_skipped / TAB_WIDTH + 1)
252
+ break if (proposed > width)
253
+ characters_skipped += 1
254
+ indentation_skipped = proposed
255
+ end
256
+ end
257
+ string[characters_skipped..-1]
258
+ end
259
+
260
+ def heredoc_whitespace_indent_size(whitespace)
261
+ whitespace.chars.inject 0 do |size, char|
262
+ if char == "\t"
263
+ size + TAB_WIDTH
264
+ else
265
+ size + 1
266
+ end
267
+ end
268
+ end
269
+
270
+ def heredoc_identifier # TODO: remove / rewrite
271
+ term, func = nil, STR_FUNC_BORING
272
+ self.string_buffer = []
273
+
274
+ heredoc_indent_mods = '-'
275
+ heredoc_indent_mods += '\~' if ruby23plus?
276
+
277
+ case
278
+ when scan(/([#{heredoc_indent_mods}]?)([\'\"\`])(.*?)\2/) then
279
+ term = ss[2]
280
+ func |= STR_FUNC_INDENT unless ss[1].empty?
281
+ func |= STR_FUNC_ICNTNT if ss[1] == '~'
282
+ func |= case term
283
+ when "\'" then
284
+ STR_SQUOTE
285
+ when '"' then
286
+ STR_DQUOTE
287
+ else
288
+ STR_XQUOTE
289
+ end
290
+ string_buffer << ss[3]
291
+ when scan(/[#{heredoc_indent_mods}]?([\'\"\`])(?!\1*\Z)/) then
292
+ rb_compile_error "unterminated here document identifier"
293
+ when scan(/([#{heredoc_indent_mods}]?)(#{IDENT_CHAR}+)/) then
294
+ term = '"'
295
+ func |= STR_DQUOTE
296
+ unless ss[1].empty? then
297
+ func |= STR_FUNC_INDENT
298
+ func |= STR_FUNC_ICNTNT if ss[1] == '~'
299
+ end
300
+ string_buffer << ss[2]
301
+ else
302
+ return nil
303
+ end
304
+
305
+ if scan(/.*\n/) then
306
+ # TODO: think about storing off the char range instead
307
+ line = matched
308
+ else
309
+ line = nil
310
+ end
311
+
312
+ self.lex_strterm = [:heredoc, string_buffer.join, func, line]
313
+
314
+ if term == '`' then
315
+ result nil, :tXSTRING_BEG, "`"
316
+ else
317
+ result nil, :tSTRING_BEG, "\""
318
+ end
319
+ end
320
+
321
+ def in_fname?
322
+ in_lex_state? :expr_fname
323
+ end
324
+
325
+ def in_arg_state? # TODO: rename is_after_operator?
326
+ in_lex_state? :expr_fname, :expr_dot
327
+ end
328
+
329
+ def in_lex_state?(*states)
330
+ states.include? lex_state
331
+ end
332
+
333
+ def int_with_base base
334
+ rb_compile_error "Invalid numeric format" if matched =~ /__/
335
+
336
+ text = matched
337
+ case
338
+ when text.end_with?('ri')
339
+ return result(:expr_end, :tIMAGINARY, Complex(0, Rational(text.chop.chop.to_i(base))))
340
+ when text.end_with?('r')
341
+ return result(:expr_end, :tRATIONAL, Rational(text.chop.to_i(base)))
342
+ when text.end_with?('i')
343
+ return result(:expr_end, :tIMAGINARY, Complex(0, text.chop.to_i(base)))
344
+ else
345
+ return result(:expr_end, :tINTEGER, text.to_i(base))
346
+ end
347
+ end
348
+
349
+ def is_arg?
350
+ in_lex_state?(*EXPR_ARG_ANY)
351
+ end
352
+
353
+ def is_beg?
354
+ # TODO: in_lex_state?(*EXPR_BEG_ANY) || lex_state == [:expr_arg, :expr_labeled]
355
+ in_lex_state?(*EXPR_BEG_ANY, :expr_value, :expr_labeled)
356
+ end
357
+
358
+ def is_end?
359
+ in_lex_state?(*EXPR_END_ANY)
360
+ end
361
+
362
+ def lvar_defined? id
363
+ # TODO: (dyna_in_block? && dvar_defined?(id)) || local_id?(id)
364
+ self.parser.env[id.to_sym] == :lvar
365
+ end
366
+
367
+
368
+ def ruby22_label?
369
+ ruby22plus? and is_label_possible?
370
+ end
371
+
372
+ def is_label_possible?
373
+ (in_lex_state?(:expr_beg, :expr_endfn) && !cmd_state) || is_arg?
374
+ end
375
+
376
+ def is_label_suffix?
377
+ check(/:(?!:)/)
378
+ end
379
+
380
+ def is_space_arg? c = "x"
381
+ is_arg? and space_seen and c !~ /\s/
382
+ end
383
+
384
+ def lambda_beginning?
385
+ lpar_beg && lpar_beg == paren_nest
386
+ end
387
+
388
+ def matched
389
+ ss.matched
390
+ end
391
+
392
+ def not_end?
393
+ not is_end?
394
+ end
395
+
396
+ def process_amper text
397
+ token = if is_arg? && space_seen && !check(/\s/) then
398
+ warning("`&' interpreted as argument prefix")
399
+ :tAMPER
400
+ elsif in_lex_state? :expr_beg, :expr_mid then
401
+ :tAMPER
402
+ else
403
+ :tAMPER2
404
+ end
405
+
406
+ return result(:arg_state, token, "&")
407
+ end
408
+
409
+ def process_backref text
410
+ token = ss[1].to_sym
411
+ # TODO: can't do lineno hack w/ symbol
412
+ result :expr_end, :tBACK_REF, token
413
+ end
414
+
415
+ def process_begin text
416
+ @comments << matched
417
+
418
+ unless scan(/.*?\n=end( |\t|\f)*[^\n]*(\n|\z)/m) then
419
+ @comments.clear
420
+ rb_compile_error("embedded document meets end of file")
421
+ end
422
+
423
+ @comments << matched
424
+ self.lineno += matched.count("\n")
425
+
426
+ nil # TODO
427
+ end
428
+
429
+ def process_brace_close text
430
+ # matching compare/parse23.y:8561
431
+ cond.lexpop
432
+ cmdarg.lexpop
433
+
434
+ case matched
435
+ when "}" then
436
+ self.brace_nest -= 1
437
+ self.lex_state = :expr_endarg # TODO: :expr_end ? Look at 2.6
438
+
439
+ return :tSTRING_DEND, matched if brace_nest < 0 unless ruby18 || ruby19
440
+ return :tRCURLY, matched
441
+ when "]" then
442
+ self.paren_nest -= 1
443
+ self.lex_state = :expr_endarg
444
+ return :tRBRACK, matched
445
+ when ")" then
446
+ self.paren_nest -= 1
447
+ self.lex_state = :expr_endfn
448
+ return :tRPAREN, matched
449
+ else
450
+ raise "Unknown bracing: #{matched.inspect}"
451
+ end
452
+ end
453
+
454
+ def process_colon1 text
455
+ # ?: / then / when
456
+ if is_end? || check(/\s/) then
457
+ return result :expr_beg, :tCOLON, text
458
+ end
459
+
460
+ case
461
+ when scan(/\'/) then
462
+ string STR_SSYM
463
+ when scan(/\"/) then
464
+ string STR_DSYM
465
+ end
466
+
467
+ result :expr_fname, :tSYMBEG, text
468
+ end
469
+
470
+ def process_colon2 text
471
+ if is_beg? || in_lex_state?(:expr_class) || is_space_arg? then
472
+ result :expr_beg, :tCOLON3, text
473
+ else
474
+ result :expr_dot, :tCOLON2, text
475
+ end
476
+ end
477
+
478
+ def process_brace_open text
479
+ # matching compare/parse23.y:8694
480
+ self.brace_nest += 1
481
+
482
+ if lambda_beginning? then
483
+ self.lpar_beg = nil
484
+ self.paren_nest -= 1 # close arg list when lambda opens body
485
+
486
+ return expr_result(:tLAMBEG, "{")
487
+ end
488
+
489
+ token = case lex_state
490
+ when :expr_labeled then
491
+ :tLBRACE # hash
492
+ when *EXPR_ARG_ANY, :expr_end, :expr_endfn then
493
+ :tLCURLY # block (primary)
494
+ when :expr_endarg
495
+ :tLBRACE_ARG # block (expr)
496
+ else
497
+ :tLBRACE # hash
498
+ end
499
+
500
+ # TODO: self.lex_state |= :expr_label if token != :tLBRACE_ARG
501
+ self.command_start = true if token != :tLBRACE
502
+
503
+ return expr_result(token, "{")
504
+ end
505
+
506
+ def process_float text
507
+ rb_compile_error "Invalid numeric format" if text =~ /__/
508
+
509
+ case
510
+ when text.end_with?('ri')
511
+ return result(:expr_end, :tIMAGINARY, Complex(0, Rational(text.chop.chop)))
512
+ when text.end_with?('r')
513
+ return result(:expr_end, :tRATIONAL, Rational(text.chop))
514
+ when text.end_with?('i')
515
+ return result(:expr_end, :tIMAGINARY, Complex(0, text.chop.to_f))
516
+ else
517
+ return result(:expr_end, :tFLOAT, text.to_f)
518
+ end
519
+ end
520
+
521
+ def process_gvar text
522
+ text.lineno = self.lineno
523
+ result(:expr_end, :tGVAR, text)
524
+ end
525
+
526
+ def process_gvar_oddity text
527
+ return result :expr_end, "$", "$" if text == "$" # TODO: wtf is this?
528
+ rb_compile_error "#{text.inspect} is not allowed as a global variable name"
529
+ end
530
+
531
+ def process_ivar text
532
+ tok_id = text =~ /^@@/ ? :tCVAR : :tIVAR
533
+ text.lineno = self.lineno
534
+ return result(:expr_end, tok_id, text)
535
+ end
536
+
537
+ def process_lchevron text
538
+ if (!in_lex_state?(:expr_dot, :expr_class) &&
539
+ !is_end? &&
540
+ (!is_arg? || space_seen)) then # TODO: || in_state(:expr_labeled)
541
+ tok = self.heredoc_identifier
542
+ return tok if tok
543
+ end
544
+
545
+ if in_arg_state? then
546
+ self.lex_state = :expr_arg
547
+ else
548
+ self.command_start = true if lex_state == :expr_class
549
+ self.lex_state = :expr_beg
550
+ end
551
+
552
+ return result(lex_state, :tLSHFT, "\<\<")
553
+ end
554
+
555
+ def process_newline_or_comment text
556
+ c = matched
557
+ hit = false
558
+
559
+ if c == '#' then
560
+ ss.pos -= 1
561
+
562
+ # TODO: handle magic comments
563
+ while scan(/\s*\#.*(\n+|\z)/) do
564
+ hit = true
565
+ self.lineno += matched.lines.to_a.size
566
+ @comments << matched.gsub(/^ +#/, '#').gsub(/^ +$/, '')
567
+ end
568
+
569
+ return nil if end_of_stream?
570
+ end
571
+
572
+ self.lineno += 1 unless hit
573
+
574
+ # Replace a string of newlines with a single one
575
+ self.lineno += matched.lines.to_a.size if scan(/\n+/)
576
+
577
+ # TODO: remove :expr_value -- audit all uses of it
578
+ c = in_lex_state?(:expr_beg, :expr_value, :expr_class,
579
+ :expr_fname, :expr_dot) && !in_lex_state?(:expr_labeled)
580
+
581
+ # TODO: figure out what token_seen is for
582
+ # TODO: if c || self.lex_state == [:expr_beg, :expr_labeled] then
583
+ if c || self.lex_state == :expr_labeled then
584
+ # ignore if !fallthrough?
585
+ if !c && parser.in_kwarg then
586
+ # normal newline
587
+ return result(:expr_beg, :tNL, nil)
588
+ else
589
+ return # skip
590
+ end
591
+ end
592
+
593
+ if scan(/([\ \t\r\f\v]*)(\.|&)/) then
594
+ self.space_seen = true unless ss[1].empty?
595
+
596
+ ss.pos -= 1
597
+ return unless check(/\.\./)
598
+ end
599
+
600
+ self.command_start = true
601
+
602
+ return result(:expr_beg, :tNL, nil)
603
+ end
604
+
605
+ def process_nthref text
606
+ # TODO: can't do lineno hack w/ number
607
+ result :expr_end, :tNTH_REF, ss[1].to_i
608
+ end
609
+
610
+ def process_paren text
611
+ token = if ruby18 then
612
+ process_paren18
613
+ else
614
+ process_paren19
615
+ end
616
+
617
+ self.paren_nest += 1
618
+
619
+ # TODO: add :expr_label to :expr_beg (set in expr_result below)
620
+ return expr_result(token, "(")
621
+ end
622
+
623
+ def process_paren18
624
+ self.command_start = true
625
+ token = :tLPAREN2
626
+
627
+ if in_lex_state? :expr_beg, :expr_mid then
628
+ token = :tLPAREN
629
+ elsif space_seen then
630
+ if in_lex_state? :expr_cmdarg then
631
+ token = :tLPAREN_ARG
632
+ elsif in_lex_state? :expr_arg then
633
+ warning "don't put space before argument parentheses"
634
+ end
635
+ else
636
+ # not a ternary -- do nothing?
637
+ end
638
+
639
+ token
640
+ end
641
+
642
+ def process_paren19
643
+ if is_beg? then
644
+ :tLPAREN
645
+ elsif is_space_arg? then
646
+ :tLPAREN_ARG
647
+ else
648
+ :tLPAREN2 # plain '(' in parse.y
649
+ end
650
+ end
651
+
652
+ def process_percent text
653
+ return parse_quote if is_beg?
654
+
655
+ return result(:expr_beg, :tOP_ASGN, "%") if scan(/\=/)
656
+
657
+ return parse_quote if is_arg? && space_seen && ! check(/\s/)
658
+
659
+ return result(:arg_state, :tPERCENT, "%")
660
+ end
661
+
662
+ def process_plus_minus text
663
+ sign = matched
664
+ utype, type = if sign == "+" then
665
+ [:tUPLUS, :tPLUS]
666
+ else
667
+ [:tUMINUS, :tMINUS]
668
+ end
669
+
670
+ if in_arg_state? then
671
+ if scan(/@/) then
672
+ return result(:expr_arg, utype, "#{sign}@")
673
+ else
674
+ return result(:expr_arg, type, sign)
675
+ end
676
+ end
677
+
678
+ return result(:expr_beg, :tOP_ASGN, sign) if scan(/\=/)
679
+
680
+ if (is_beg? || (is_arg? && space_seen && !check(/\s/))) then
681
+ arg_ambiguous if is_arg?
682
+
683
+ if check(/\d/) then
684
+ return nil if utype == :tUPLUS
685
+ return result(:expr_beg, :tUMINUS_NUM, sign)
686
+ end
687
+
688
+ return result(:expr_beg, utype, sign)
689
+ end
690
+
691
+ return result(:expr_beg, type, sign)
692
+ end
693
+
694
+ def process_questionmark text
695
+ if is_end? then
696
+ state = ruby18 ? :expr_beg : :expr_value # HACK?
697
+ return result(state, :tEH, "?")
698
+ end
699
+
700
+ if end_of_stream? then
701
+ rb_compile_error "incomplete character syntax: parsed #{text.inspect}"
702
+ end
703
+
704
+ if check(/\s|\v/) then
705
+ unless is_arg? then
706
+ c2 = { " " => 's',
707
+ "\n" => 'n',
708
+ "\t" => 't',
709
+ "\v" => 'v',
710
+ "\r" => 'r',
711
+ "\f" => 'f' }[matched]
712
+
713
+ if c2 then
714
+ warning("invalid character syntax; use ?\\" + c2)
715
+ end
716
+ end
717
+
718
+ # ternary
719
+ state = ruby18 ? :expr_beg : :expr_value # HACK?
720
+ return result(state, :tEH, "?")
721
+ elsif check(/\w(?=\w)/) then # ternary, also
722
+ return result(:expr_beg, :tEH, "?")
723
+ end
724
+
725
+ c = if scan(/\\/) then
726
+ self.read_escape
727
+ else
728
+ ss.getch
729
+ end
730
+
731
+ if version == 18 then
732
+ return result(:expr_end, :tINTEGER, c[0].ord & 0xff)
733
+ else
734
+ return result(:expr_end, :tSTRING, c)
735
+ end
736
+ end
737
+
738
+ def process_slash text
739
+ if is_beg? then
740
+ string STR_REGEXP
741
+
742
+ return result(nil, :tREGEXP_BEG, "/")
743
+ end
744
+
745
+ if scan(/\=/) then
746
+ return result(:expr_beg, :tOP_ASGN, "/")
747
+ end
748
+
749
+ if is_arg? && space_seen then
750
+ unless scan(/\s/) then
751
+ arg_ambiguous
752
+ string STR_REGEXP, "/"
753
+ return result(nil, :tREGEXP_BEG, "/")
754
+ end
755
+ end
756
+
757
+ return result(:arg_state, :tDIVIDE, "/")
758
+ end
759
+
760
+ def process_square_bracket text
761
+ self.paren_nest += 1
762
+
763
+ token = nil
764
+
765
+ if in_arg_state? then
766
+ case
767
+ when scan(/\]\=/) then
768
+ self.paren_nest -= 1 # HACK? I dunno, or bug in MRI
769
+ return result(:expr_arg, :tASET, "[]=")
770
+ when scan(/\]/) then
771
+ self.paren_nest -= 1 # HACK? I dunno, or bug in MRI
772
+ return result(:expr_arg, :tAREF, "[]")
773
+ else
774
+ rb_compile_error "unexpected '['"
775
+ end
776
+ elsif is_beg? then
777
+ token = :tLBRACK
778
+ elsif is_arg? && space_seen then
779
+ token = :tLBRACK
780
+ else
781
+ token = :tLBRACK2
782
+ end
783
+
784
+ # TODO: this is done by expr_result except "|EXPR_LABEL")
785
+ # SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
786
+ expr_result token, "["
787
+ end
788
+
789
+ def possibly_escape_string text, check
790
+ content = match[1]
791
+
792
+ if text =~ check then
793
+ content.gsub(ESC) { unescape $1 }
794
+ else
795
+ content.gsub(/\\\\/, "\\").gsub(/\\'/, "'")
796
+ end
797
+ end
798
+
799
+ def process_symbol text
800
+ symbol = possibly_escape_string text, /^:"/
801
+
802
+ rb_compile_error "symbol cannot contain '\\0'" if
803
+ ruby18 && symbol =~ /\0/
804
+
805
+ return result(:expr_end, :tSYMBOL, symbol)
806
+ end
807
+
808
+ def was_label?
809
+ @was_label = ruby22_label?
810
+ true
811
+ end
812
+
813
+ def process_label_or_string text
814
+ if @was_label && text =~ /:\Z/ then
815
+ @was_label = nil
816
+ return process_label text
817
+ elsif text =~ /:\Z/ then
818
+ ss.pos -= 1 # put back ":"
819
+ text = text[0..-2]
820
+ end
821
+
822
+ result :expr_end, :tSTRING, text[1..-2].gsub(/\\\\/, "\\").gsub(/\\'/, "'")
823
+ end
824
+
825
+ def process_label text
826
+ symbol = possibly_escape_string text, /^"/
827
+
828
+ result(:expr_labeled, :tLABEL, [symbol, self.lineno]) # TODO: expr_arg|expr_labeled
829
+ end
830
+
831
+ def process_token text
832
+ # matching: parse_ident in compare/parse23.y:7989
833
+ # TODO: make this always return [token, lineno]
834
+ self.last_state = lex_state
835
+
836
+ token = self.token = text
837
+ token << matched if scan(/[\!\?](?!=)/)
838
+
839
+ tok_id =
840
+ case
841
+ when token =~ /[!?]$/ then
842
+ :tFID
843
+ when in_lex_state?(:expr_fname) && scan(/=(?:(?![~>=])|(?==>))/) then
844
+ # ident=, not =~ => == or followed by =>
845
+ # TODO test lexing of a=>b vs a==>b
846
+ token << matched
847
+ :tIDENTIFIER
848
+ when token =~ /^[A-Z]/ then
849
+ :tCONSTANT
850
+ else
851
+ :tIDENTIFIER
852
+ end
853
+
854
+ if !ruby18 and is_label_possible? and is_label_suffix? then
855
+ scan(/:/)
856
+ # TODO: :expr_arg|:expr_labeled
857
+ return result :expr_labeled, :tLABEL, [token, self.lineno]
858
+ end
859
+
860
+ # TODO: mb == ENC_CODERANGE_7BIT && !in_lex_state?(:expr_dot)
861
+ unless in_lex_state? :expr_dot then
862
+ # See if it is a reserved word.
863
+ keyword = if ruby18 then # REFACTOR need 18/19 lexer subclasses
864
+ RubyParser::Legacy::RubyParserStuff::Keyword.keyword18 token
865
+ else
866
+ RubyParser::Legacy::RubyParserStuff::Keyword.keyword19 token
867
+ end
868
+
869
+ return process_token_keyword keyword if keyword
870
+ end # unless in_lex_state? :expr_dot
871
+
872
+ # matching: compare/parse23.y:8079
873
+ state = if is_beg? or is_arg? or in_lex_state? :expr_dot then
874
+ cmd_state ? :expr_cmdarg : :expr_arg
875
+ elsif not ruby18 and in_lex_state? :expr_fname then
876
+ :expr_endfn
877
+ else
878
+ :expr_end
879
+ end
880
+
881
+ if not [:expr_dot, :expr_fname].include? last_state and
882
+ (tok_id == :tIDENTIFIER) and # not :expr_fname, not attrasgn
883
+ lvar_defined?(token) then
884
+ state = :expr_end # TODO: EXPR_END|EXPR_LABEL
885
+ end
886
+
887
+ token.lineno = self.lineno # yes, on a string. I know... I know...
888
+
889
+ return result(state, tok_id, token)
890
+ end
891
+
892
+ def process_token_keyword keyword
893
+ # matching MIDDLE of parse_ident in compare/parse23.y:8046
894
+ state = lex_state
895
+ self.lex_state = keyword.state
896
+
897
+ value = [token, self.lineno]
898
+
899
+ return result(lex_state, keyword.id0, value) if state == :expr_fname
900
+
901
+ self.command_start = true if lex_state == :expr_beg
902
+
903
+ case
904
+ when keyword.id0 == :kDO then
905
+ case
906
+ when lambda_beginning? then
907
+ self.lpar_beg = nil # lambda_beginning? == FALSE in the body of "-> do ... end"
908
+ self.paren_nest -= 1
909
+ result(lex_state, :kDO_LAMBDA, value)
910
+ when cond.is_in_state then
911
+ result(lex_state, :kDO_COND, value)
912
+ when cmdarg.is_in_state && state != :expr_cmdarg then
913
+ result(lex_state, :kDO_BLOCK, value)
914
+ when [:expr_beg, :expr_endarg].include?(state) then
915
+ result(lex_state, :kDO_BLOCK, value)
916
+ else
917
+ result(lex_state, :kDO, value)
918
+ end
919
+ when [:expr_beg, :expr_labeled].include?(state) then
920
+ result(lex_state, keyword.id0, value)
921
+ when keyword.id0 != keyword.id1 then
922
+ result(:expr_beg, keyword.id1, value) # TODO: :expr_beg|:expr_label
923
+ else
924
+ result(lex_state, keyword.id1, value)
925
+ end
926
+ end
927
+
928
+ def process_underscore text
929
+ ss.unscan # put back "_"
930
+
931
+ if beginning_of_line? && scan(/\__END__(\r?\n|\Z)/) then
932
+ return [RubyLexer::EOF, RubyLexer::EOF]
933
+ elsif scan(/\_\w*/) then
934
+ return process_token matched
935
+ end
936
+ end
937
+
938
+ def rb_compile_error msg
939
+ msg += ". near line #{self.lineno}: #{ss.rest[/^.*/].inspect}"
940
+ raise RubyParser::SyntaxError, msg
941
+ end
942
+
943
+ def read_escape # TODO: remove / rewrite
944
+ case
945
+ when scan(/\\/) then # Backslash
946
+ '\\'
947
+ when scan(/n/) then # newline
948
+ self.extra_lineno -= 1
949
+ "\n"
950
+ when scan(/t/) then # horizontal tab
951
+ "\t"
952
+ when scan(/r/) then # carriage-return
953
+ "\r"
954
+ when scan(/f/) then # form-feed
955
+ "\f"
956
+ when scan(/v/) then # vertical tab
957
+ "\13"
958
+ when scan(/a/) then # alarm(bell)
959
+ "\007"
960
+ when scan(/e/) then # escape
961
+ "\033"
962
+ when scan(/b/) then # backspace
963
+ "\010"
964
+ when scan(/s/) then # space
965
+ " "
966
+ when scan(/[0-7]{1,3}/) then # octal constant
967
+ (matched.to_i(8) & 0xFF).chr
968
+ when scan(/x([0-9a-fA-F]{1,2})/) then # hex constant
969
+ ss[1].to_i(16).chr
970
+ when check(/M-\\[\\MCc]/) then
971
+ scan(/M-\\/) # eat it
972
+ c = self.read_escape
973
+ c[0] = (c[0].ord | 0x80).chr
974
+ c
975
+ when scan(/M-(.)/) then
976
+ c = ss[1]
977
+ c[0] = (c[0].ord | 0x80).chr
978
+ c
979
+ when check(/(C-|c)\\[\\MCc]/) then
980
+ scan(/(C-|c)\\/) # eat it
981
+ c = self.read_escape
982
+ c[0] = (c[0].ord & 0x9f).chr
983
+ c
984
+ when scan(/C-\?|c\?/) then
985
+ 127.chr
986
+ when scan(/(C-|c)(.)/) then
987
+ c = ss[2]
988
+ c[0] = (c[0].ord & 0x9f).chr
989
+ c
990
+ when scan(/^[89]/i) then # bad octal or hex... MRI ignores them :(
991
+ matched
992
+ when scan(/u([0-9a-fA-F]{2,4}|\{[0-9a-fA-F]{2,6}\})/) then
993
+ [ss[1].delete("{}").to_i(16)].pack("U")
994
+ when scan(/[McCx0-9]/) || end_of_stream? then
995
+ rb_compile_error("Invalid escape character syntax")
996
+ else
997
+ ss.getch
998
+ end.dup
999
+ end
1000
+
1001
+ def regx_options # TODO: rewrite / remove
1002
+ good, bad = [], []
1003
+
1004
+ if scan(/[a-z]+/) then
1005
+ good, bad = matched.split(//).partition { |s| s =~ /^[ixmonesu]$/ }
1006
+ end
1007
+
1008
+ unless bad.empty? then
1009
+ rb_compile_error("unknown regexp option%s - %s" %
1010
+ [(bad.size > 1 ? "s" : ""), bad.join.inspect])
1011
+ end
1012
+
1013
+ return good.join
1014
+ end
1015
+
1016
+ def reset
1017
+ self.brace_nest = 0
1018
+ self.command_start = true
1019
+ self.comments = []
1020
+ self.lex_state = :expr_none
1021
+ self.lex_strterm = nil
1022
+ self.lineno = 1
1023
+ self.lpar_beg = nil
1024
+ self.paren_nest = 0
1025
+ self.space_seen = false
1026
+ self.string_nest = 0
1027
+ self.token = nil
1028
+ self.extra_lineno = 0
1029
+
1030
+ self.cond.reset
1031
+ self.cmdarg.reset
1032
+ end
1033
+
1034
+ def result lex_state, token, text # :nodoc:
1035
+ lex_state = self.arg_state if lex_state == :arg_state
1036
+ self.lex_state = lex_state if lex_state
1037
+ [token, text]
1038
+ end
1039
+
1040
+ def ruby18
1041
+ RubyParser::V18 === parser
1042
+ end
1043
+
1044
+ def ruby19
1045
+ RubyParser::V19 === parser
1046
+ end
1047
+
1048
+ def scan re
1049
+ ss.scan re
1050
+ end
1051
+
1052
+ def check re
1053
+ ss.check re
1054
+ end
1055
+
1056
+ def eat_whitespace
1057
+ r = scan(/\s+/)
1058
+ self.extra_lineno += r.count("\n") if r
1059
+ r
1060
+ end
1061
+
1062
+ def fixup_lineno extra = 0
1063
+ self.lineno += self.extra_lineno + extra
1064
+ self.extra_lineno = 0
1065
+ end
1066
+
1067
+ def scanner_class # TODO: design this out of oedipus_lex. or something.
1068
+ RPStringScanner
1069
+ end
1070
+
1071
+ def space_vs_beginning space_type, beg_type, fallback
1072
+ if is_space_arg? check(/./m) then
1073
+ warning "`**' interpreted as argument prefix"
1074
+ space_type
1075
+ elsif is_beg? then
1076
+ beg_type
1077
+ else
1078
+ # TODO: warn_balanced("**", "argument prefix");
1079
+ fallback
1080
+ end
1081
+ end
1082
+
1083
+ def string type, beg = matched, nnd = "\0"
1084
+ self.lex_strterm = [:strterm, type, beg, nnd]
1085
+ end
1086
+
1087
+ # TODO: consider
1088
+ # def src= src
1089
+ # raise "bad src: #{src.inspect}" unless String === src
1090
+ # @src = RPStringScanner.new(src)
1091
+ # end
1092
+
1093
+ def tokadd_escape term # TODO: rewrite / remove
1094
+ case
1095
+ when scan(/\\\n/) then
1096
+ # just ignore
1097
+ when scan(/\\([0-7]{1,3}|x[0-9a-fA-F]{1,2})/) then
1098
+ self.string_buffer << matched
1099
+ when scan(/\\([MC]-|c)(?=\\)/) then
1100
+ self.string_buffer << matched
1101
+ self.tokadd_escape term
1102
+ when scan(/\\([MC]-|c)(.)/) then
1103
+ self.string_buffer << matched
1104
+ when scan(/\\[McCx]/) then
1105
+ rb_compile_error "Invalid escape character syntax"
1106
+ when scan(/\\(.)/m) then
1107
+ chr = ss[1]
1108
+ prev = self.string_buffer.last
1109
+ if term == chr && prev && prev.end_with?("(?") then
1110
+ self.string_buffer << chr
1111
+ else
1112
+ self.string_buffer << matched
1113
+ end
1114
+ else
1115
+ rb_compile_error "Invalid escape character syntax"
1116
+ end
1117
+ end
1118
+
1119
+ def tokadd_string(func, term, paren) # TODO: rewrite / remove
1120
+ qwords = (func & STR_FUNC_QWORDS) != 0
1121
+ escape = (func & STR_FUNC_ESCAPE) != 0
1122
+ expand = (func & STR_FUNC_EXPAND) != 0
1123
+ regexp = (func & STR_FUNC_REGEXP) != 0
1124
+ symbol = (func & STR_FUNC_SYMBOL) != 0
1125
+
1126
+ paren_re = @@regexp_cache[paren]
1127
+ term_re = @@regexp_cache[term]
1128
+
1129
+ until end_of_stream? do
1130
+ c = nil
1131
+ handled = true
1132
+
1133
+ case
1134
+ when paren_re && scan(paren_re) then
1135
+ self.string_nest += 1
1136
+ when scan(term_re) then
1137
+ if self.string_nest == 0 then
1138
+ ss.pos -= 1
1139
+ break
1140
+ else
1141
+ self.string_nest -= 1
1142
+ end
1143
+ when expand && scan(/#(?=[\$\@\{])/) then
1144
+ ss.pos -= 1
1145
+ break
1146
+ when qwords && scan(/\s/) then
1147
+ ss.pos -= 1
1148
+ break
1149
+ when expand && scan(/#(?!\n)/) then
1150
+ # do nothing
1151
+ when check(/\\/) then
1152
+ case
1153
+ when qwords && scan(/\\\n/) then
1154
+ string_buffer << "\n"
1155
+ next
1156
+ when qwords && scan(/\\\s/) then
1157
+ c = ' '
1158
+ when expand && scan(/\\\n/) then
1159
+ next
1160
+ when regexp && check(/\\/) then
1161
+ self.tokadd_escape term
1162
+ next
1163
+ when expand && scan(/\\/) then
1164
+ c = self.read_escape
1165
+ when scan(/\\\n/) then
1166
+ # do nothing
1167
+ when scan(/\\\\/) then
1168
+ string_buffer << '\\' if escape
1169
+ c = '\\'
1170
+ when scan(/\\/) then
1171
+ unless scan(term_re) || paren.nil? || scan(paren_re) then
1172
+ string_buffer << "\\"
1173
+ end
1174
+ else
1175
+ handled = false
1176
+ end # inner /\\/ case
1177
+ else
1178
+ handled = false
1179
+ end # top case
1180
+
1181
+ unless handled then
1182
+ t = Regexp.escape term
1183
+ x = Regexp.escape(paren) if paren && paren != "\000"
1184
+ re = if qwords then
1185
+ if HAS_ENC then
1186
+ /[^#{t}#{x}\#\0\\\s]+|./ # |. to pick up whatever
1187
+ else
1188
+ /[^#{t}#{x}\#\0\\\s\v]+|./ # argh. 1.8's \s doesn't pick up \v
1189
+ end
1190
+ else
1191
+ /[^#{t}#{x}\#\0\\]+|./
1192
+ end
1193
+
1194
+ scan re
1195
+ c = matched
1196
+
1197
+ rb_compile_error "symbol cannot contain '\\0'" if symbol && c =~ /\0/
1198
+ end # unless handled
1199
+
1200
+ c ||= matched
1201
+ string_buffer << c
1202
+ end # until
1203
+
1204
+ c ||= matched
1205
+ c = RubyLexer::EOF if end_of_stream?
1206
+
1207
+ return c
1208
+ end
1209
+
1210
+ def unescape s
1211
+ r = ESCAPES[s]
1212
+
1213
+ self.extra_lineno += 1 if s == "\n" # eg backslash newline strings
1214
+ self.extra_lineno -= 1 if r && s == "n" # literal \n, not newline
1215
+
1216
+ return r if r
1217
+
1218
+ x = case s
1219
+ when /^[0-7]{1,3}/ then
1220
+ ($&.to_i(8) & 0xFF).chr
1221
+ when /^x([0-9a-fA-F]{1,2})/ then
1222
+ $1.to_i(16).chr
1223
+ when /^M-(.)/ then
1224
+ ($1[0].ord | 0x80).chr
1225
+ when /^(C-|c)(.)/ then
1226
+ ($2[0].ord & 0x9f).chr
1227
+ when /^[89a-f]/i then # bad octal or hex... ignore? that's what MRI does :(
1228
+ s
1229
+ when /^[McCx0-9]/ then
1230
+ rb_compile_error("Invalid escape character syntax")
1231
+ when /u([0-9a-fA-F]{2,4}|\{[0-9a-fA-F]{2,6}\})/ then
1232
+ [$1.delete("{}").to_i(16)].pack("U")
1233
+ else
1234
+ s
1235
+ end
1236
+ x.force_encoding "UTF-8" if HAS_ENC
1237
+ x
1238
+ end
1239
+
1240
+ def warning s
1241
+ # do nothing for now
1242
+ end
1243
+
1244
+ def ruby22plus?
1245
+ parser.class.version >= 22
1246
+ end
1247
+
1248
+ def ruby23plus?
1249
+ parser.class.version >= 23
1250
+ end
1251
+
1252
+ def process_string # TODO: rewrite / remove
1253
+ # matches top of parser_yylex in compare/parse23.y:8113
1254
+ token = if lex_strterm[0] == :heredoc then
1255
+ self.heredoc lex_strterm
1256
+ else
1257
+ self.parse_string lex_strterm
1258
+ end
1259
+
1260
+ token_type, c = token
1261
+
1262
+ # matches parser_string_term
1263
+ if ruby22plus? && token_type == :tSTRING_END && ["'", '"'].include?(c) then
1264
+ if (([:expr_beg, :expr_endfn].include?(lex_state) &&
1265
+ !cond.is_in_state) || is_arg?) &&
1266
+ is_label_suffix? then
1267
+ scan(/:/)
1268
+ token_type = token[0] = :tLABEL_END
1269
+ end
1270
+ end
1271
+
1272
+ if [:tSTRING_END, :tREGEXP_END, :tLABEL_END].include? token_type then
1273
+ self.lex_strterm = nil
1274
+ # TODO: :expr_beg|:expr_label
1275
+ self.lex_state = (token_type == :tLABEL_END) ? :expr_label : :expr_end
1276
+ end
1277
+
1278
+ return token
1279
+ end
1280
+
1281
+ def parse_quote # TODO: remove / rewrite
1282
+ beg, nnd, short_hand, c = nil, nil, false, nil
1283
+
1284
+ if scan(/[a-z0-9]{1,2}/i) then # Long-hand (e.g. %Q{}).
1285
+ rb_compile_error "unknown type of %string" if ss.matched_size == 2
1286
+ c, beg, short_hand = matched, ss.getch, false
1287
+ else # Short-hand (e.g. %{, %., %!, etc)
1288
+ c, beg, short_hand = 'Q', ss.getch, true
1289
+ end
1290
+
1291
+ if end_of_stream? or c == RubyLexer::EOF or beg == RubyLexer::EOF then
1292
+ rb_compile_error "unterminated quoted string meets end of file"
1293
+ end
1294
+
1295
+ # Figure nnd-char. "\0" is special to indicate beg=nnd and that no nesting?
1296
+ nnd = { "(" => ")", "[" => "]", "{" => "}", "<" => ">" }[beg]
1297
+ nnd, beg = beg, "\0" if nnd.nil?
1298
+
1299
+ token_type, text = nil, "%#{c}#{beg}"
1300
+ token_type, string_type = case c
1301
+ when 'Q' then
1302
+ ch = short_hand ? nnd : c + beg
1303
+ text = "%#{ch}"
1304
+ [:tSTRING_BEG, STR_DQUOTE]
1305
+ when 'q' then
1306
+ [:tSTRING_BEG, STR_SQUOTE]
1307
+ when 'W' then
1308
+ eat_whitespace
1309
+ [:tWORDS_BEG, STR_DQUOTE | STR_FUNC_QWORDS]
1310
+ when 'w' then
1311
+ eat_whitespace
1312
+ [:tQWORDS_BEG, STR_SQUOTE | STR_FUNC_QWORDS]
1313
+ when 'x' then
1314
+ [:tXSTRING_BEG, STR_XQUOTE]
1315
+ when 'r' then
1316
+ [:tREGEXP_BEG, STR_REGEXP]
1317
+ when 's' then
1318
+ self.lex_state = :expr_fname
1319
+ [:tSYMBEG, STR_SSYM]
1320
+ when 'I' then
1321
+ eat_whitespace
1322
+ [:tSYMBOLS_BEG, STR_DQUOTE | STR_FUNC_QWORDS]
1323
+ when 'i' then
1324
+ eat_whitespace
1325
+ [:tQSYMBOLS_BEG, STR_SQUOTE | STR_FUNC_QWORDS]
1326
+ end
1327
+
1328
+ rb_compile_error "Bad %string type. Expected [QqWwIixrs], found '#{c}'." if
1329
+ token_type.nil?
1330
+
1331
+ raise "huh" unless string_type
1332
+
1333
+ string string_type, nnd, beg
1334
+
1335
+ return token_type, text
1336
+ end
1337
+
1338
+ def parse_string quote # TODO: rewrite / remove
1339
+ _, string_type, term, open = quote
1340
+
1341
+ space = false # FIX: remove these
1342
+ func = string_type
1343
+ paren = open
1344
+ term_re = @@regexp_cache[term]
1345
+
1346
+ qwords = (func & STR_FUNC_QWORDS) != 0
1347
+ regexp = (func & STR_FUNC_REGEXP) != 0
1348
+ expand = (func & STR_FUNC_EXPAND) != 0
1349
+
1350
+ unless func then # nil'ed from qwords below. *sigh*
1351
+ return :tSTRING_END, nil
1352
+ end
1353
+
1354
+ space = true if qwords and eat_whitespace
1355
+
1356
+ if self.string_nest == 0 && scan(/#{term_re}/) then
1357
+ if qwords then
1358
+ quote[1] = nil
1359
+ return :tSPACE, nil
1360
+ elsif regexp then
1361
+ return :tREGEXP_END, self.regx_options
1362
+ else
1363
+ return :tSTRING_END, term
1364
+ end
1365
+ end
1366
+
1367
+ return :tSPACE, nil if space
1368
+
1369
+ self.string_buffer = []
1370
+
1371
+ if expand
1372
+ case
1373
+ when scan(/#(?=\$(-.|[a-zA-Z_0-9~\*\$\?!@\/\\;,\.=:<>\"\&\`\'+]))/) then
1374
+ # TODO: !ISASCII
1375
+ # ?! see parser_peek_variable_name
1376
+ return :tSTRING_DVAR, nil
1377
+ when scan(/#(?=\@\@?[a-zA-Z_])/) then
1378
+ # TODO: !ISASCII
1379
+ return :tSTRING_DVAR, nil
1380
+ when scan(/#[{]/) then
1381
+ self.command_start = true
1382
+ return :tSTRING_DBEG, nil
1383
+ when scan(/#/) then
1384
+ string_buffer << '#'
1385
+ end
1386
+ end
1387
+
1388
+ if tokadd_string(func, term, paren) == RubyLexer::EOF then
1389
+ rb_compile_error "unterminated string meets end of file"
1390
+ end
1391
+
1392
+ return :tSTRING_CONTENT, string_buffer.join
1393
+ end
1394
+ end
1395
+
1396
+ require "ruby_parser/legacy/ruby_lexer.rex"
1397
+
1398
+ if ENV["RP_LINENO_DEBUG"] then
1399
+ class RubyParser::Legacy::RubyLexer
1400
+ alias :old_lineno= :lineno=
1401
+
1402
+ def d o
1403
+ $stderr.puts o.inspect
1404
+ end
1405
+
1406
+ def lineno= n
1407
+ self.old_lineno= n
1408
+ where = caller.first.split(/:/).first(2).join(":")
1409
+ d :lineno => [n, where, ss && ss.rest[0,40]]
1410
+ end
1411
+ end
1412
+ end