Module: EBNF::PEG

Included in:
Base
Defined in:
lib/ebnf/peg.rb,
lib/ebnf/peg/rule.rb,
lib/ebnf/peg/parser.rb

Defined Under Namespace

Modules: Parser, Rule

Instance Method Summary collapse

Instance Method Details

#make_pegENBF

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.

Returns:

  • (ENBF)

    self


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object

Output Ruby parser files for PEG parsing

Parameters:

  • output (IO, StringIO)

31
32
33
34
35
36
37
# 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