Class: Racc::GrammarFileParser
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | lib/racc/grammarfileparser.rb |
Overview
reopen
Constant Summary
-
USER_CODE_LABELS =
# File 'lib/racc/grammarfileparser.rb', line 272{ 'header' => :header, 'prepare' => :header, # obsolete 'inner' => :inner, 'footer' => :, 'driver' => : # obsolete }
Class Method Summary
Instance Method Summary
- #parse(src, filename = '-', lineno = 1)
- #add_rule(target, list, sprec) private
- #add_rule_block(list) private
- #add_user_code(label, src) private
- #canonical_label(src) private
- #embedded_action(act) private
- #location private
- #next_token private
- #on_error(tok, val, _values) private
-
#parse_user_code
private
User Code Block.
Constructor Details
.new(debug_flags = DebugFlags.new) ⇒ GrammarFileParser
# File 'lib/racc/grammarfileparser.rb', line 172
def initialize(debug_flags = DebugFlags.new) @yydebug = debug_flags.parse end
Class Method Details
.parse(src, filename = '-', lineno = 1)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 168
def GrammarFileParser.parse(src, filename = '-', lineno = 1) new().parse(src, filename, lineno) end
.parse_file(filename)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 164
def GrammarFileParser.parse_file(filename) parse(File.read(filename), filename, 1) end
Instance Method Details
#add_rule(target, list, sprec) (private)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 235
def add_rule(target, list, sprec) if list.last.kind_of?(UserAction) act = list.pop else act = UserAction.empty end list.map! {|s| s.kind_of?(UserAction) ? (s) : s } rule = Rule.new(target, list, act) rule.specified_prec = sprec @grammar.add rule end
#add_rule_block(list) (private)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 211
def add_rule_block(list) sprec = nil target = list.shift case target when OrMark, UserAction, Prec raise CompileError, "#{target.lineno}: unexpected symbol #{target.name}" end curr = [] list.each do |i| case i when OrMark add_rule target, curr, sprec curr = [] sprec = nil when Prec raise CompileError, "'=<prec>' used twice in one rule" if sprec sprec = i.symbol else curr.push i end end add_rule target, curr, sprec end
#add_user_code(label, src) (private)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 288
def add_user_code(label, src) @result.params.send(USER_CODE_LABELS[label]).push src end
#canonical_label(src) (private)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 280
def canonical_label(src) label = src.to_s.strip.downcase.slice(/\w+/) unless USER_CODE_LABELS.key?(label) raise CompileError, "unknown user code type: #{label.inspect}" end label end
#embedded_action(act) (private)
[ GitHub ]#location (private)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 207
def location "#{@filename}:#{@lineno - 1 + @scanner.lineno}" end
#next_token (private)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 192
def next_token @scanner.scan end
#on_error(tok, val, _values) (private)
# File 'lib/racc/grammarfileparser.rb', line 196
def on_error(tok, val, _values) if val.respond_to?(:id2name) v = val.id2name elsif val.kind_of?(String) v = val else v = val.inspect end raise CompileError, "#{location()}: unexpected token '#{v}'" end
#parse(src, filename = '-', lineno = 1)
[ GitHub ]# File 'lib/racc/grammarfileparser.rb', line 176
def parse(src, filename = '-', lineno = 1) @filename = filename @lineno = lineno @scanner = GrammarFileScanner.new(src, @filename) @scanner.debug = @yydebug @grammar = Grammar.new @result = Result.new(@grammar) @embedded_action_seq = 0 yyparse @scanner, :yylex parse_user_code @result.grammar.init @result end
#parse_user_code (private)
User Code Block
# File 'lib/racc/grammarfileparser.rb', line 257
def parse_user_code line = @scanner.lineno _, *blocks = *@scanner.epilogue.split(/^----/) blocks.each do |block| header, *body = block.lines.to_a label0, pathes = *header.sub(/\A-+/, '').split('=', 2) label = canonical_label(label0) (pathes ? pathes.strip.split(' ') : []).each do |path| add_user_code label, SourceText.new(File.read(path), path, 1) end add_user_code label, SourceText.new(body.join(''), @filename, line + 1) line += (1 + body.size) end end