module EBNF::PEG

Public Instance Methods

make_peg() click to toggle source

Transform EBNF Rule set for PEG parsing:

* Transform each rule into a set of sub-rules extracting unnamed sequences into new rules, using {Rule#to_peg}.

@return [ENBF] self

# File lib/ebnf/peg.rb, line 11
def make_peg
  progress("make_peg") {"Start: #{@ast.length} rules"}
  new_ast = []

  ast.each do |rule|
    debug("make_peg") {"expand from: #{rule.inspect}"}
    new_rules = rule.to_peg
    debug(" => ") {new_rules.map(&:sym).join(', ')}
    new_ast += new_rules
  end

  @ast = new_ast
  progress("make_peg") {"End: #{@ast.length} rules"}
  self
end
to_ruby_peg(output, **options) click to toggle source

Output Ruby parser files for PEG parsing

@param [IO, StringIO] output

# File lib/ebnf/peg.rb, line 31
def to_ruby_peg(output, **options)
  output.puts "  RULES = ["
  ast.each do |rule|
    output.puts "    " + rule.to_ruby + (rule.is_a?(EBNF::PEG::Rule) ? '.extend(EBNF::PEG::Rule)' : '') + ','
  end
  output.puts "  ]"
end