Class: Rley::Parser::ParseEntrySet
- Inherits:
-
Object
- Object
- Rley::Parser::ParseEntrySet
- Extended by:
- Forwardable
- Defined in:
- lib/rley/parser/parse_entry_set.rb
Overview
Responsibilities:
- To know all the parse entries in the set
Instance Attribute Summary collapse
-
#entries ⇒ Array<ParseEntry>
readonly
The set of parse entries.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Access the entry at given position.
-
#ambiguities ⇒ Object
Return an Array of Arrays of ambiguous parse entries.
-
#entries4n_term(aNonTerminal) ⇒ Object
Returns a Hash with pairs of the form: non terminal symbol => [ parse entry expecting the non-terminal ].
-
#entries4term(aTerminal) ⇒ Object
Returns a Hash with pairs of the form: terminal symbol => [ parse entry expecting the terminal ].
-
#expected_terminals ⇒ Object
The list of distinct expected terminal symbols.
-
#initialize ⇒ ParseEntrySet
constructor
Constructor.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of the set of parse entries.
-
#push_entry(anEntry) ⇒ ParseEntry
Append the given entry (if it isn't yet in the set) to the list of parse entries.
Constructor Details
#initialize ⇒ ParseEntrySet
Constructor.
18 19 20 21 22 |
# File 'lib/rley/parser/parse_entry_set.rb', line 18 def initialize() @entries = [] @entries4term = Hash.new { |hash, key| hash[key] = [] } @entries4n_term = Hash.new { |hash, key| hash[key] = [] } end |
Instance Attribute Details
#entries ⇒ Array<ParseEntry> (readonly)
Returns The set of parse entries.
15 16 17 |
# File 'lib/rley/parser/parse_entry_set.rb', line 15 def entries @entries end |
Instance Method Details
#[](index) ⇒ Object
Access the entry at given position
36 37 38 |
# File 'lib/rley/parser/parse_entry_set.rb', line 36 def [](index) return entries[index] end |
#ambiguities ⇒ Object
Return an Array of Arrays of ambiguous parse entries.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rley/parser/parse_entry_set.rb', line 72 def ambiguities() complete_entries = entries.select(&:exit_entry?) return [] if complete_entries.size <= 1 # Group parse entries by lhs symbol and origin groupings = complete_entries.group_by do |entry| entry.vertex.dotted_rule.lhs.object_id.to_s end # Retain the groups having more than one element. ambiguous_groups = [] groupings.each_value do |a_group| ambiguous_groups << a_group if a_group.size > 1 end return ambiguous_groups end |
#entries4n_term(aNonTerminal) ⇒ Object
Returns a Hash with pairs of the form: non terminal symbol => [ parse entry expecting the non-terminal ]
48 49 50 |
# File 'lib/rley/parser/parse_entry_set.rb', line 48 def entries4n_term(aNonTerminal) return @entries4n_term.fetch(aNonTerminal, []) end |
#entries4term(aTerminal) ⇒ Object
Returns a Hash with pairs of the form: terminal symbol => [ parse entry expecting the terminal ]
42 43 44 |
# File 'lib/rley/parser/parse_entry_set.rb', line 42 def entries4term(aTerminal) return @entries4term.fetch(aTerminal, []) end |
#expected_terminals ⇒ Object
The list of distinct expected terminal symbols. An expected symbol is on the left of a dot in a parse state of the parse set.
92 93 94 |
# File 'lib/rley/parser/parse_entry_set.rb', line 92 def expected_terminals() return @entries4term.keys end |
#inspect ⇒ String
Returns a string containing a human-readable representation of the set of parse entries.
27 28 29 30 31 32 33 |
# File 'lib/rley/parser/parse_entry_set.rb', line 27 def inspect() result = "#<#{self.class.name}:#{object_id}" result << ' @entries=[' entries.each { |e| result << e.inspect } result << ']>' return result end |
#push_entry(anEntry) ⇒ ParseEntry
Append the given entry (if it isn't yet in the set) to the list of parse entries
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rley/parser/parse_entry_set.rb', line 56 def push_entry(anEntry) # TODO: control overhead next line match = entries.find { |entry| entry == anEntry } if match result = match else @entries << anEntry expecting = anEntry.next_symbol add_lookup4symbol(anEntry) if expecting result = anEntry end return result end |