class Zenlish::ZParser

Attributes

engine[R]

@!attribute [r] engine

@return [Rley::Engine] Facade object of Rley library

Public Class Methods

new() click to toggle source
# File lib/zenlish/parser/zparser.rb, line 11
def initialize
  # Create a Rley facade object
  @engine = Rley::Engine.new do |config|
    config.diagnose = true
  end

  # Step 1. Load Zenlish grammar
  @engine.use_grammar(ZenlishGrammar)
end

Public Instance Methods

parse(tokenSeq) click to toggle source

Parse the sequence of words into a parse tree. @raise [StandardError] Fails when the parse is ambiguous. @return [Rley::PTree;;ParseTree] the resulting parse tree.

# File lib/zenlish/parser/zparser.rb, line 24
def parse(tokenSeq)
  result = earley_parse(tokenSeq)

  # Convert into a parse tree
  engine.to_ptree(result)
end
Also aliased as: to_ptree
to_pforest(tokenSeq) click to toggle source

Parse the sequence of words into a parse forest. Parse forests are needed when dealing with ambiguous input. @return [Rley::SPPF::ParseForest] the resulting parse forest.

# File lib/zenlish/parser/zparser.rb, line 36
def to_pforest(tokenSeq)
  result = earley_parse(tokenSeq)
  # puts result

  # Convert into a parse forest
  engine.to_pforest(result)
end
to_ptree(tokenSeq)
Alias for: parse

Private Instance Methods

earley_parse(tokenSeq) click to toggle source
# File lib/zenlish/parser/zparser.rb, line 46
def earley_parse(tokenSeq)
  result = engine.parse(tokenSeq)

  unless result.success?
    # Stop if the parse failed...
    line1 = "Parsing failed\n"
    line2 = "Reason: #{result.failure_reason.message}"
    raise StandardError, line1 + line2
  end

  result
end