Class: Zenlish::ZParser

Inherits:
Object
  • Object
show all
Defined in:
lib/zenlish/parser/zparser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZParser

Returns a new instance of ZParser.



11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



9
10
11
# File 'lib/zenlish/parser/zparser.rb', line 9

def engine
  @engine
end

Instance Method Details

#parse(tokenSeq) ⇒ Rley::PTree;;ParseTree Also known as: to_ptree

Parse the sequence of words into a parse tree.

Returns:

  • (Rley::PTree;;ParseTree)

    the resulting parse tree.

Raises:

  • (StandardError)

    Fails when the parse is ambiguous.



24
25
26
27
28
29
# 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

#to_pforest(tokenSeq) ⇒ Rley::SPPF::ParseForest

Parse the sequence of words into a parse forest. Parse forests are needed when dealing with ambiguous input.

Returns:

  • (Rley::SPPF::ParseForest)

    the resulting parse forest.



36
37
38
39
40
41
42
# 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