# File lib/racc/grammarfileparser.rb, line 172
172: def initialize(debug_flags = DebugFlags.new)
173: @yydebug = debug_flags.parse
174: end
# File lib/racc/grammarfileparser.rb, line 176
176: def parse(src, filename = '-', lineno = 1)
177: @filename = filename
178: @lineno = lineno
179: @scanner = GrammarFileScanner.new(src, @filename)
180: @scanner.debug = @yydebug
181: @grammar = Grammar.new
182: @result = Result.new(@grammar)
183: @embedded_action_seq = 0
184: yyparse @scanner, :yylex
185: parse_user_code
186: @result.grammar.init
187: @result
188: end
# File lib/racc/grammarfileparser.rb, line 235
235: def add_rule(target, list, sprec)
236: if list.last.kind_of?(UserAction)
237: act = list.pop
238: else
239: act = UserAction.empty
240: end
241: list.map! {|s| s.kind_of?(UserAction) ? embedded_action(s) : s }
242: rule = Rule.new(target, list, act)
243: rule.specified_prec = sprec
244: @grammar.add rule
245: end
# File lib/racc/grammarfileparser.rb, line 211
211: def add_rule_block(list)
212: sprec = nil
213: target = list.shift
214: case target
215: when OrMark, UserAction, Prec
216: raise CompileError, "#{target.lineno}: unexpected symbol #{target.name}"
217: end
218: curr = []
219: list.each do |i|
220: case i
221: when OrMark
222: add_rule target, curr, sprec
223: curr = []
224: sprec = nil
225: when Prec
226: raise CompileError, "'=<prec>' used twice in one rule" if sprec
227: sprec = i.symbol
228: else
229: curr.push i
230: end
231: end
232: add_rule target, curr, sprec
233: end
# File lib/racc/grammarfileparser.rb, line 288
288: def add_user_code(label, src)
289: @result.params.send(USER_CODE_LABELS[label]).push src
290: end
# File lib/racc/grammarfileparser.rb, line 280
280: def canonical_label(src)
281: label = src.to_s.strip.downcase.slice(/\w+/)
282: unless USER_CODE_LABELS.key?(label)
283: raise CompileError, "unknown user code type: #{label.inspect}"
284: end
285: label
286: end
# File lib/racc/grammarfileparser.rb, line 247
247: def embedded_action(act)
248: sym = @grammar.intern("@#{@embedded_action_seq += 1}".intern, true)
249: @grammar.add Rule.new(sym, [], act)
250: sym
251: end
# File lib/racc/grammarfileparser.rb, line 207
207: def location
208: "#{@filename}:#{@lineno - 1 + @scanner.lineno}"
209: end
# File lib/racc/grammarfileparser.rb, line 192
192: def next_token
193: @scanner.scan
194: end
# File lib/racc/grammarfileparser.rb, line 196
196: def on_error(tok, val, _values)
197: if val.respond_to?(:id2name)
198: v = val.id2name
199: elsif val.kind_of?(String)
200: v = val
201: else
202: v = val.inspect
203: end
204: raise CompileError, "#{location()}: unexpected token '#{v}'"
205: end
User Code Block
# File lib/racc/grammarfileparser.rb, line 257
257: def parse_user_code
258: line = @scanner.lineno
259: _, *blocks = *@scanner.epilogue.split(/^----/)
260: blocks.each do |block|
261: header, *body = block.to_a
262: label0, pathes = *header.sub(/\A-+/, '').split('=', 2)
263: label = canonical_label(label0)
264: (pathes ? pathes.strip.split(' ') : []).each do |path|
265: add_user_code label, SourceText.new(File.read(path), path, 1)
266: end
267: add_user_code label, SourceText.new(body.join(''), @filename, line + 1)
268: line += (1 + body.size)
269: end
270: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.