Class: Racc::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc/rd/block_parser.rb,
lib/rdoc/rd/inline_parser.rb

Constant Summary collapse

Racc_Runtime_Version =
::Racc::VERSION
Racc_Runtime_Core_Version_R =
::Racc::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.racc_runtime_typeObject

:nodoc:



239
240
241
# File 'lib/rdoc/rd/block_parser.rb', line 239

def Parser.racc_runtime_type # :nodoc:
  Racc_Runtime_Type
end

Instance Method Details

#_racc_do_parse_rb(arg, in_debug) ⇒ Object



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
# File 'lib/rdoc/rd/block_parser.rb', line 302

def _racc_do_parse_rb(arg, in_debug)
  action_table, action_check, action_default, action_pointer,
  _,            _,            _,              _,
  _,            _,            token_table,    * = arg

  _racc_init_sysvars
  tok = act = i = nil

  catch(:racc_end_parse) {
    while true
      if i = action_pointer[@racc_state[-1]]
        if @racc_read_next
          if @racc_t != 0   # not EOF
            tok, @racc_val = next_token()
            unless tok      # EOF
              @racc_t = 0
            else
              @racc_t = (token_table[tok] or 1)   # error token
            end
            racc_read_token(@racc_t, tok, @racc_val) if @yydebug
            @racc_read_next = false
          end
        end
        i += @racc_t
        unless i >= 0 and
               act = action_table[i] and
               action_check[i] == @racc_state[-1]
          act = action_default[@racc_state[-1]]
        end
      else
        act = action_default[@racc_state[-1]]
      end
      while act = _racc_evalact(act, arg)
        ;
      end
    end
  }
end

#_racc_do_reduce(arg, act) ⇒ Object



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
# File 'lib/rdoc/rd/block_parser.rb', line 502

def _racc_do_reduce(arg, act)
  _,          _,            _,            _,
  goto_table, goto_check,   goto_default, goto_pointer,
  nt_base,    reduce_table, _,            _,
  _,          use_result,   * = arg

  state = @racc_state
  vstack = @racc_vstack
  tstack = @racc_tstack

  i = act * -3
  len       = reduce_table[i]
  reduce_to = reduce_table[i+1]
  method_id = reduce_table[i+2]
  void_array = []

  tmp_t = tstack[-len, len] if @yydebug
  tmp_v = vstack[-len, len]
  tstack[-len, len] = void_array if @yydebug
  vstack[-len, len] = void_array
  state[-len, len]  = void_array

  # tstack must be updated AFTER method call
  if use_result
    vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
  else
    vstack.push __send__(method_id, tmp_v, vstack)
  end
  tstack.push reduce_to

  racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug

  k1 = reduce_to - nt_base
  if i = goto_pointer[k1]
    i += state[-1]
    if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
      return curstate
    end
  end
  goto_default[k1]
end

#_racc_evalact(act, arg) ⇒ Object

common



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
# File 'lib/rdoc/rd/block_parser.rb', line 405

def _racc_evalact(act, arg)
  action_table, action_check, _, action_pointer,
  _,            _,            _, _,
  _,            _,            _, shift_n,
  reduce_n,     * = arg
  nerr = 0   # tmp

  if act > 0 and act < shift_n
    #
    # shift
    #
    if @racc_error_status > 0
      @racc_error_status -= 1 unless @racc_t <= 1 # error token or EOF
    end
    @racc_vstack.push @racc_val
    @racc_state.push act
    @racc_read_next = true
    if @yydebug
      @racc_tstack.push @racc_t
      racc_shift @racc_t, @racc_tstack, @racc_vstack
    end

  elsif act < 0 and act > -reduce_n
    #
    # reduce
    #
    code = catch(:racc_jump) {
      @racc_state.push _racc_do_reduce(arg, act)
      false
    }
    if code
      case code
      when 1 # yyerror
        @racc_user_yyerror = true   # user_yyerror
        return -reduce_n
      when 2 # yyaccept
        return shift_n
      else
        raise '[Racc Bug] unknown jump code'
      end
    end

  elsif act == shift_n
    #
    # accept
    #
    racc_accept if @yydebug
    throw :racc_end_parse, @racc_vstack[0]

  elsif act == -reduce_n
    #
    # error
    #
    case @racc_error_status
    when 0
      unless arg[21]    # user_yyerror
        nerr += 1
        on_error @racc_t, @racc_val, @racc_vstack
      end
    when 3
      if @racc_t == 0   # is $
        # We're at EOF, and another error occurred immediately after
        # attempting auto-recovery
        throw :racc_end_parse, nil
      end
      @racc_read_next = true
    end
    @racc_user_yyerror = false
    @racc_error_status = 3
    while true
      if i = action_pointer[@racc_state[-1]]
        i += 1   # error token
        if  i >= 0 and
            (act = action_table[i]) and
            action_check[i] == @racc_state[-1]
          break
        end
      end
      throw :racc_end_parse, nil if @racc_state.size <= 1
      @racc_state.pop
      @racc_vstack.pop
      if @yydebug
        @racc_tstack.pop
        racc_e_pop @racc_state, @racc_tstack, @racc_vstack
      end
    end
    return act

  else
    raise "[Racc Bug] unknown action #{act.inspect}"
  end

  racc_next_state(@racc_state[-1], @racc_state) if @yydebug

  nil
end

#_racc_init_sysvarsObject



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rdoc/rd/block_parser.rb', line 255

def _racc_init_sysvars
  @racc_state  = [0]
  @racc_tstack = []
  @racc_vstack = []

  @racc_t = nil
  @racc_val = nil

  @racc_read_next = true

  @racc_user_yyerror = false
  @racc_error_status = 0
end

#_racc_setupObject



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rdoc/rd/block_parser.rb', line 243

def _racc_setup
  @yydebug = false unless self.class::Racc_debug_parser
  @yydebug = false unless defined?(@yydebug)
  if @yydebug
    @racc_debug_out = $stderr unless defined?(@racc_debug_out)
    @racc_debug_out ||= $stderr
  end
  arg = self.class::Racc_arg
  arg[13] = true if arg.size < 14
  arg
end

#_racc_yyparse_rb(recv, mid, arg, c_debug) ⇒ Object



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
# File 'lib/rdoc/rd/block_parser.rb', line 352

def _racc_yyparse_rb(recv, mid, arg, c_debug)
  action_table, action_check, action_default, action_pointer,
  _,            _,            _,              _,
  _,            _,            token_table,    * = arg

  _racc_init_sysvars

  catch(:racc_end_parse) {
    until i = action_pointer[@racc_state[-1]]
      while act = _racc_evalact(action_default[@racc_state[-1]], arg)
        ;
      end
    end
    recv.__send__(mid) do |tok, val|
      unless tok
        @racc_t = 0
      else
        @racc_t = (token_table[tok] or 1)   # error token
      end
      @racc_val = val
      @racc_read_next = false

      i += @racc_t
      unless i >= 0 and
             act = action_table[i] and
             action_check[i] == @racc_state[-1]
        act = action_default[@racc_state[-1]]
      end
      while act = _racc_evalact(act, arg)
        ;
      end

      while !(i = action_pointer[@racc_state[-1]]) ||
            ! @racc_read_next ||
            @racc_t == 0  # $
        unless i and i += @racc_t and
               i >= 0 and
               act = action_table[i] and
               action_check[i] == @racc_state[-1]
          act = action_default[@racc_state[-1]]
        end
        while act = _racc_evalact(act, arg)
          ;
        end
      end
    end
  }
end

#next_tokenObject

The method to fetch next token. If you use #do_parse method, you must implement #next_token.

The format of return value is [TOKEN_SYMBOL, VALUE]. token-symbol is represented by Ruby’s symbol by default, e.g. :IDENT for ‘IDENT’. “;” (String) for ‘;’.

The final symbol (End of file) must be false.

Raises:

  • (NotImplementedError)


298
299
300
# File 'lib/rdoc/rd/block_parser.rb', line 298

def next_token
  raise NotImplementedError, "#{self.class}\#next_token is not defined"
end

#on_error(t, val, vstack) ⇒ Object

This method is called when a parse error is found.

ERROR_TOKEN_ID is an internal ID of token which caused error. You can get string representation of this ID by calling #token_to_str.

ERROR_VALUE is a value of error token.

value_stack is a stack of symbol values. DO NOT MODIFY this object.

This method raises ParseError by default.

If this method returns, parsers enter “error recovering mode”.

Raises:



558
559
560
561
# File 'lib/rdoc/rd/block_parser.rb', line 558

def on_error(t, val, vstack)
  raise ParseError, sprintf("parse error on value %s (%s)",
                            val.inspect, token_to_str(t) || '?')
end

#racc_acceptObject



607
608
609
610
# File 'lib/rdoc/rd/block_parser.rb', line 607

def racc_accept
  @racc_debug_out.puts 'accept'
  @racc_debug_out.puts
end

#racc_e_pop(state, tstack, vstack) ⇒ Object



612
613
614
615
616
617
# File 'lib/rdoc/rd/block_parser.rb', line 612

def racc_e_pop(state, tstack, vstack)
  @racc_debug_out.puts 'error recovering mode: pop token'
  racc_print_states state
  racc_print_stacks tstack, vstack
  @racc_debug_out.puts
end

#racc_next_state(curstate, state) ⇒ Object



619
620
621
622
623
# File 'lib/rdoc/rd/block_parser.rb', line 619

def racc_next_state(curstate, state)
  @racc_debug_out.puts  "goto    #{curstate}"
  racc_print_states state
  @racc_debug_out.puts
end

#racc_print_stacks(t, v) ⇒ Object



625
626
627
628
629
630
631
632
# File 'lib/rdoc/rd/block_parser.rb', line 625

def racc_print_stacks(t, v)
  out = @racc_debug_out
  out.print '        ['
  t.each_index do |i|
    out.print ' (', racc_token2str(t[i]), ' ', v[i].inspect, ')'
  end
  out.puts ' ]'
end

#racc_print_states(s) ⇒ Object



634
635
636
637
638
639
# File 'lib/rdoc/rd/block_parser.rb', line 634

def racc_print_states(s)
  out = @racc_debug_out
  out.print '        ['
  s.each {|st| out.print ' ', st }
  out.puts ' ]'
end

#racc_read_token(t, tok, val) ⇒ Object

For debugging output



581
582
583
584
585
586
# File 'lib/rdoc/rd/block_parser.rb', line 581

def racc_read_token(t, tok, val)
  @racc_debug_out.print 'read    '
  @racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
  @racc_debug_out.puts val.inspect
  @racc_debug_out.puts
end

#racc_reduce(toks, sim, tstack, vstack) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/rdoc/rd/block_parser.rb', line 594

def racc_reduce(toks, sim, tstack, vstack)
  out = @racc_debug_out
  out.print 'reduce '
  if toks.empty?
    out.print ' <none>'
  else
    toks.each {|t| out.print ' ', racc_token2str(t) }
  end
  out.puts " --> #{racc_token2str(sim)}"
  racc_print_stacks tstack, vstack
  @racc_debug_out.puts
end

#racc_shift(tok, tstack, vstack) ⇒ Object



588
589
590
591
592
# File 'lib/rdoc/rd/block_parser.rb', line 588

def racc_shift(tok, tstack, vstack)
  @racc_debug_out.puts "shift   #{racc_token2str tok}"
  racc_print_stacks tstack, vstack
  @racc_debug_out.puts
end

#racc_token2str(tok) ⇒ Object



641
642
643
644
# File 'lib/rdoc/rd/block_parser.rb', line 641

def racc_token2str(tok)
  self.class::Racc_token_to_s_table[tok] or
      raise "[Racc Bug] can't convert token #{tok} to string"
end

#token_to_str(t) ⇒ Object

Convert internal ID of token symbol to the string.



647
648
649
# File 'lib/rdoc/rd/block_parser.rb', line 647

def token_to_str(t)
  self.class::Racc_token_to_s_table[t]
end

#yyacceptObject

Exit parser. Return value is Symbol_Value_Stack[0].



571
572
573
# File 'lib/rdoc/rd/block_parser.rb', line 571

def yyaccept
  throw :racc_jump, 2
end

#yyerrokObject

Leave error recovering mode.



576
577
578
# File 'lib/rdoc/rd/block_parser.rb', line 576

def yyerrok
  @racc_error_status = 0
end

#yyerrorObject

Enter error recovering mode. This method does not call #on_error.



565
566
567
# File 'lib/rdoc/rd/block_parser.rb', line 565

def yyerror
  throw :racc_jump, 1
end