Class: Rley::Parser::ParseEntrySet

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeParseEntrySet

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

#entriesArray<ParseEntry> (readonly)

Returns The array of parse entries.

Returns:

  • (Array<ParseEntry>)

    The array of parse entries



18
19
20
# File 'lib/rley/parser/parse_entry_set.rb', line 18

def entries
  @entries
end

#membershipHash (readonly)

Returns A Hash with pairs { hash of ParseEntry => ParseEntry }.

Returns:

  • (Hash)

    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

#ambiguitiesObject

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_edgesObject



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_terminalsObject

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

#inspectString

Returns a string containing a human-readable representation of the set of parse entries.

Returns:

  • (String)


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

Parameters:

  • anEntry (ParseEntry)

    the parse entry to push.

Returns:

  • (ParseEntry)

    the passed parse entry if it pushes it



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