class Gullah::Parse

A parse is the collection of root nodes produced by parsing a text.

class Example
  extend Gullah

  rule :S, 'NP VP'
  rule :NP, 'D N'
  rule :VP, 'V'

  leaf :D, /the/
  leaf :N, /cat/
  leaf :V, /sat/
end

parses = Example.parse 'the cat sat', n: 1

# this is a Parse
parse = parses.first
puts parse.length  # => 1
puts parse.size    # => 8
puts parse.summary # => S[NP[D,_ws,N],_ws,VP[V]]

Attributes

roots[R]

The root nodes of all subtrees found in this parse in sequence. This is an array.

summary[R]

A concise stringification of the syntactic structure of this parse. For a given string and grammar all the parses will have a unique summary.

text[R]

The text parsed by this parse.

Public Instance Methods

dbg(so: false) click to toggle source

a simplified representation for debugging “so” = “significant only”

# File lib/gullah/parse.rb, line 116
def dbg(so: false)
  roots.map { |n| n.dbg so: so }
end
end() click to toggle source

The end offset of the last leaf in the parse.

# File lib/gullah/parse.rb, line 146
def end
  roots.last.end
end
errors?() click to toggle source

Are there any nodes in the parse that are erroneous, either because some test failed or because they correspond to “trash” – characters that matched no leaf rule?

# File lib/gullah/parse.rb, line 98
def errors?
  incorrectness_count.positive?
end
failure?() click to toggle source

Not a success?

# File lib/gullah/parse.rb, line 110
def failure?
  !success?
end
incorrectness_count() click to toggle source

The count of nodes that failed some test. Structure tests mark both the child and the ancestor node where the test was run as erroneous, so they will increase the incorrectness_count by 2.

# File lib/gullah/parse.rb, line 83
def incorrectness_count
  @incorrectness_count ||= roots.select(&:failed?).count
end
length() click to toggle source

The number of root nodes in this parse. This is not the same as size.

# File lib/gullah/parse.rb, line 69
def length
  roots.length
end
nodes() click to toggle source

return an enumeration of all the nodes in the parse.

parses = Grammar.parse "this grammar uses the usual whitespace rule"

parses.first.nodes.select { |n| n.name == :_ws }.count  # => 6
# File lib/gullah/parse.rb, line 126
def nodes
  NodeIterator.new self
end
pending_count() click to toggle source

The count of nodes which have some structure test which was never successfully run.

# File lib/gullah/parse.rb, line 90
def pending_count
  @pending_count ||= roots.select(&:pending_tests?).count
end
size() click to toggle source

The total number of nodes in this parse. This is not the same as length.

# File lib/gullah/parse.rb, line 75
def size
  @size ||= roots.sum(&:size)
end
start() click to toggle source

The start offset of the first leaf in the parse.

# File lib/gullah/parse.rb, line 140
def start
  roots.first.start
end
success?() click to toggle source

Are all leaves accounted for without errors and have all tests passed?

# File lib/gullah/parse.rb, line 104
def success?
  !errors? && roots.all? { |n| n.ignorable? || n.nonterminal? && !n.pending_tests? }
end