Sha256: 06684b5cdc989149f9ad7e0770bf534992fdd3da325564c4fbaf746074158064

Contents?: true

Size: 1.05 KB

Versions: 14

Compression:

Stored size: 1.05 KB

Contents

module EBNF
  module PEG
    autoload :Parser, 'ebnf/peg/parser'
    autoload :Rule, 'ebnf/peg/rule'

    ##
    # 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
    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

    ##
    # Output Ruby parser files for PEG parsing
    #
    # @param [IO, StringIO] output
    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
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ebnf-2.5.0 lib/ebnf/peg.rb
ebnf-2.4.0 lib/ebnf/peg.rb
ebnf-2.3.5 lib/ebnf/peg.rb
ebnf-2.3.4 lib/ebnf/peg.rb
ebnf-2.3.3 lib/ebnf/peg.rb
ebnf-2.3.2 lib/ebnf/peg.rb
ebnf-2.3.1 lib/ebnf/peg.rb
ebnf-2.3.0 lib/ebnf/peg.rb
ebnf-2.2.1 lib/ebnf/peg.rb
ebnf-2.2.0 lib/ebnf/peg.rb
ebnf-2.1.3 lib/ebnf/peg.rb
ebnf-2.1.2 lib/ebnf/peg.rb
ebnf-2.1.1 lib/ebnf/peg.rb
ebnf-2.1.0 lib/ebnf/peg.rb