lib/ebnf/base.rb in ebnf-0.3.6 vs lib/ebnf/base.rb in ebnf-0.3.7

- old
+ new

@@ -188,9 +188,38 @@ # @return [String] def to_html Writer.html(*ast) end + ## + # Output Ruby parser files + # + # @param [IO, StringIO] output + # @param [Hash{Symbol => Object}] options + # @option options [String] :grammarFile + def to_ruby(output = STDOUT, options = {}) + self.build_tables + unless output == STDOUT + output.puts "# This file is automatically generated by #{__FILE__}" + output.puts "# BRANCH derived from #{options[:grammarFile]}" if options[:grammarFile] + unless self.errors.empty? + output.puts "# Note, tables completed with errors, may need to be resolved manually:" + #output.puts "# #{pp.conflicts.map{|c| c.join("\n# ")}.join("\n# ")}" + end + output.puts "module #{options.fetch(:mod_name, 'Branch')}" + output.puts " START = #{self.start.inspect}" + output.puts + end + self.outputTable(output, 'BRANCH', self.branch, 1) + self.outputTable(output, 'TERMINALS', self.terminals, 1) + self.outputTable(output, 'FIRST', self.first, 1) + self.outputTable(output, 'FOLLOW', self.follow, 1) + self.outputTable(output, 'PASS', [self.pass], 1) if self.pass + unless output == STDOUT + output.puts "end" + end + end + def dup new_obj = super new_obj.instance_variable_set(:@ast, @ast.dup) new_obj end \ No newline at end of file