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 array of parse entries.
-
#membership ⇒ Hash
readonly
A Hash with pairs { hash of ParseEntry => ParseEntry }.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Access the entry at given position.
-
#ambiguities ⇒ Object
Return an Array of Arrays of ambiguous parse entries.
- #count_edges ⇒ Object
-
#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.
24 25 26 27 28 29 |
# File 'lib/rley/parser/parse_entry_set.rb', line 24 def initialize @entries = [] @membership = {} @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 array of parse entries.
18 19 20 |
# File 'lib/rley/parser/parse_entry_set.rb', line 18 def entries @entries end |
#membership ⇒ Hash (readonly)
Returns A Hash with pairs { hash of ParseEntry => ParseEntry }.
21 22 23 |
# File 'lib/rley/parser/parse_entry_set.rb', line 21 def membership @membership end |
Instance Method Details
#[](index) ⇒ Object
Access the entry at given position
44 45 46 |
# File 'lib/rley/parser/parse_entry_set.rb', line 44 def [](index) entries[index] end |
#ambiguities ⇒ Object
Return an Array of Arrays of ambiguous parse entries.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rley/parser/parse_entry_set.rb', line 79 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_item.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 ambiguous_groups end |
#count_edges ⇒ Object
103 104 105 106 107 108 |
# File 'lib/rley/parser/parse_entry_set.rb', line 103 def count_edges # rubocop: disable Lint/UselessAssignment entries.reduce(0) do |sub_result, entry| sub_result += entry.vertex.edges.size end end |
#entries4n_term(aNonTerminal) ⇒ Object
Returns a Hash with pairs of the form: non terminal symbol => [ parse entry expecting the non-terminal ]
56 57 58 |
# File 'lib/rley/parser/parse_entry_set.rb', line 56 def entries4n_term(aNonTerminal) @entries4n_term.fetch(aNonTerminal, []) end |
#entries4term(aTerminal) ⇒ Object
Returns a Hash with pairs of the form: terminal symbol => [ parse entry expecting the terminal ]
50 51 52 |
# File 'lib/rley/parser/parse_entry_set.rb', line 50 def entries4term(aTerminal) @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.
99 100 101 |
# File 'lib/rley/parser/parse_entry_set.rb', line 99 def expected_terminals return @entries4term.keys end |
#inspect ⇒ String
Returns a string containing a human-readable representation of the set of parse entries.
34 35 36 37 38 39 40 41 |
# File 'lib/rley/parser/parse_entry_set.rb', line 34 def inspect result = +"#<#{self.class.name}:#{object_id}" result << ' @entries=[' entries.each { |e| result << e.inspect } result << ']>' result end |
#push_entry(anEntry) ⇒ ParseEntry
Append the given entry (if it isn't yet in the set) to the list of parse entries
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rley/parser/parse_entry_set.rb', line 64 def push_entry(anEntry) entry_key = anEntry.hash result = membership.fetch(entry_key, false) unless result @entries << anEntry membership[entry_key] = anEntry expecting = anEntry.next_symbol add_lookup4symbol(anEntry) if expecting result = anEntry end result end |