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

Returns a new instance of ParseEntrySet.



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

def initialize()
  @entries = []
  @entries4term = Hash.new { |hash, key| hash[key] = [] } 
  @entries4n_term = Hash.new { |hash, key| hash[key] = [] }
end

Instance Attribute Details

#entriesObject (readonly)

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



24
25
26
# File 'lib/rley/parser/parse_entry_set.rb', line 24

def [](index)
  return entries[index]
end

#ambiguitiesObject

Return an Array of Arrays of ambiguous parse entries.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rley/parser/parse_entry_set.rb', line 60

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 ]



36
37
38
# File 'lib/rley/parser/parse_entry_set.rb', line 36

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 ]



30
31
32
# File 'lib/rley/parser/parse_entry_set.rb', line 30

def entries4term(aTerminal)
  return @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.



80
81
82
# File 'lib/rley/parser/parse_entry_set.rb', line 80

def expected_terminals()
  return @entries4term.keys
end

#push_entry(anEntry) ⇒ ParseEntry

Append the given entry (if it isn't yet in the set) to the list of parse entries

Parameters:

  • aState (ParseEntry)

    the parse entry to push.

Returns:

  • (ParseEntry)

    the passed parse entry it doesn't added



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rley/parser/parse_entry_set.rb', line 44

def push_entry(anEntry)
  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