Parent

Racc::GrammarFileScanner

Constants

ReservedWord
LEFT_TO_RIGHT
CACHE

Attributes

epilogue[R]
debug[RW]

Public Class Methods

new(str, filename = '-') click to toggle source
     # File lib/racc/grammarfileparser.rb, line 297
297:     def initialize(str, filename = '-')
298:       @lines  = str.split(/\n|\r\n|\r/)
299:       @filename = filename
300:       @lineno = 1
301:       @line_head   = true
302:       @in_rule_blk = false
303:       @in_conv_blk = false
304:       @in_block = nil
305:       @epilogue = ''
306:       @debug = false
307:       next_line
308:     end

Public Instance Methods

lineno() click to toggle source
     # File lib/racc/grammarfileparser.rb, line 312
312:     def lineno
313:       @lineno + 1
314:     end
yylex(&block) click to toggle source
     # File lib/racc/grammarfileparser.rb, line 318
318:     def yylex(&block)
319:       unless @debug
320:         yylex0(&block)
321:       else
322:         yylex0 do |sym, tok|
323:           $stderr.printf "%7d %-10s %s\n", lineno(), sym.inspect, tok.inspect
324:           yield [sym, tok]
325:         end
326:       end
327:     end

Private Instance Methods

atom_symbol(token) click to toggle source
     # File lib/racc/grammarfileparser.rb, line 398
398:     def atom_symbol(token)
399:       if token == 'end'
400:         symbol = :END
401:         @in_conv_blk = false
402:         @in_rule_blk = false
403:       else
404:         if @line_head and not @in_conv_blk and not @in_rule_blk
405:           symbol = ReservedWord[token] || :SYMBOL
406:         else
407:           symbol = :SYMBOL
408:         end
409:         case symbol
410:         when :RULE then @in_rule_blk = true
411:         when :CONV then @in_conv_blk = true
412:         end
413:       end
414:       @line_head = false
415:       symbol
416:     end
get_quoted_re(left) click to toggle source
     # File lib/racc/grammarfileparser.rb, line 548
548:     def get_quoted_re(left)
549:       term = Regexp.quote(LEFT_TO_RIGHT[left] || left)
550:       CACHE[left] ||= /\A[^#{term}\\]*(?:\\.[^\\#{term}]*)*#{term}/
551:     end
literal_head?(pre, post) click to toggle source
     # File lib/racc/grammarfileparser.rb, line 504
504:     def literal_head?(pre, post)
505:       (!pre || /[a-zA-Z_0-9]/ !~ pre[1,1]) &&
506:           !post.empty? && /\A[\s\=]/ !~ post
507:     end
next_line() click to toggle source
     # File lib/racc/grammarfileparser.rb, line 363
363:     def next_line
364:       @lineno += 1
365:       @line = @lines[@lineno]
366:       if not @line or /\A----/ =~ @line
367:         @epilogue = @lines.join("\n")
368:         @lines.clear
369:         @line = nil
370:         if @in_block
371:           @lineno -= 1
372:           scan_error! sprintf('unterminated %s', @in_block)
373:         end
374:         false
375:       else
376:         @line.sub!(/(?:\n|\r\n|\r)\z/, '')
377:         @line_head = true
378:         true
379:       end
380:     end
read(len) click to toggle source
     # File lib/racc/grammarfileparser.rb, line 509
509:     def read(len)
510:       s = @line[0, len]
511:       @line = @line[len .. 1]
512:       s
513:     end
reads(re) click to toggle source
     # File lib/racc/grammarfileparser.rb, line 515
515:     def reads(re)
516:       m = re.match(@line) or return nil
517:       @line = m.post_match
518:       m[0]
519:     end
scan_action() click to toggle source
     # File lib/racc/grammarfileparser.rb, line 429
429:     def scan_action
430:       buf = ''
431:       nest = 1
432:       pre = nil
433:       @in_block = 'action'
434:       begin
435:         pre = nil
436:         if s = reads(/\A\s+/)
437:           # does not set 'pre'
438:           buf << s
439:         end
440:         until @line.empty?
441:           if s = reads(/\A[^'"`{}%#\/\$]+/)
442:             buf << (pre = s)
443:             next
444:           end
445:           case ch = read(1)
446:           when '{'
447:             nest += 1
448:             buf << (pre = ch)
449:           when '}'
450:             nest -= 1
451:             if nest == 0
452:               @in_block = nil
453:               return buf
454:             end
455:             buf << (pre = ch)
456:           when '#'   # comment
457:             buf << ch << @line
458:             break
459:           when "'", '"', '`'
460:             buf << (pre = scan_quoted(ch))
461:           when '%'
462:             if literal_head? pre, @line
463:               # % string, regexp, array
464:               buf << ch
465:               case ch = read(1)
466:               when /[qQx]/
467:                 buf << ch << (pre = scan_quoted(read(1), '%string'))
468:               when /wW/
469:                 buf << ch << (pre = scan_quoted(read(1), '%array'))
470:               when /s/
471:                 buf << ch << (pre = scan_quoted(read(1), '%symbol'))
472:               when /r/
473:                 buf << ch << (pre = scan_quoted(read(1), '%regexp'))
474:               when /[a-zA-Z0-9= ]/   # does not include "_"
475:                 scan_error! "unknown type of % literal '%#{ch}'"
476:               else
477:                 buf << (pre = scan_quoted(ch, '%string'))
478:               end
479:             else
480:               # operator
481:               buf << '||op->' if $raccs_print_type
482:               buf << (pre = ch)
483:             end
484:           when '/'
485:             if literal_head? pre, @line
486:               # regexp
487:               buf << (pre = scan_quoted(ch, 'regexp'))
488:             else
489:               # operator
490:               buf << '||op->' if $raccs_print_type
491:               buf << (pre = ch)
492:             end
493:           when '$'   # gvar
494:             buf << ch << (pre = read(1))
495:           else
496:             raise 'racc: fatal: must not happen'
497:           end
498:         end
499:         buf << "\n"
500:       end while next_line()
501:       raise 'racc: fatal: scan finished before parser finished'
502:     end
scan_error!(msg) click to toggle source
     # File lib/racc/grammarfileparser.rb, line 553
553:     def scan_error!(msg)
554:       raise CompileError, "#{lineno()}: #{msg}"
555:     end
scan_quoted(left, tag = 'string') click to toggle source
     # File lib/racc/grammarfileparser.rb, line 521
521:     def scan_quoted(left, tag = 'string')
522:       buf = left.dup
523:       buf = "||#{tag}->" + buf if $raccs_print_type
524:       re = get_quoted_re(left)
525:       sv, @in_block = @in_block, tag
526:       begin
527:         if s = reads(re)
528:           buf << s
529:           break
530:         else
531:           buf << @line
532:         end
533:       end while next_line()
534:       @in_block = sv
535:       buf << "<-#{tag}||" if $raccs_print_type
536:       buf
537:     end
skip_comment() click to toggle source
     # File lib/racc/grammarfileparser.rb, line 418
418:     def skip_comment
419:       @in_block = 'comment'
420:       until m = /\*\//.match(@line)
421:         next_line
422:       end
423:       @line = m.post_match
424:       @in_block = nil
425:     end
yylex0() click to toggle source
     # File lib/racc/grammarfileparser.rb, line 331
331:     def yylex0
332:       begin
333:         until @line.empty?
334:           @line.sub!(/\A\s+/, '')
335:           if /\A\#/ =~ @line
336:             break
337:           elsif /\A\/\*/ =~ @line
338:             skip_comment
339:           elsif s = reads(/\A[a-zA-Z_]\w*/)
340:             yield [atom_symbol(s), s.intern]
341:           elsif s = reads(/\A\d+/)
342:             yield [:DIGIT, s.to_i]
343:           elsif ch = reads(/\A./)
344:             case ch
345:             when '"', "'"
346:               yield [:STRING, eval(scan_quoted(ch))]
347:             when '{'
348:               lineno = lineno()
349:               yield [:ACTION, SourceText.new(scan_action(), @filename, lineno)]
350:             else
351:               if ch == '|'
352:                 @line_head = false
353:               end
354:               yield [ch, ch]
355:             end
356:           else
357:           end
358:         end
359:       end while next_line()
360:       yield nil
361:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.