Class: Dhaka::ParserState

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/parser_state.rb

Overview

:nodoc:

Constant Summary collapse

@@state_id =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, items, id = nil) ⇒ ParserState

Returns a new instance of ParserState.



16
17
18
19
20
21
# File 'lib/parser/parser_state.rb', line 16

def initialize(parser, items, id=nil)
  @parser = parser
  @items = items
  @actions = {}
  @id =  id ? id : ParserState.next_state_id
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



6
7
8
# File 'lib/parser/parser_state.rb', line 6

def actions
  @actions
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/parser/parser_state.rb', line 6

def id
  @id
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/parser/parser_state.rb', line 6

def items
  @items
end

Class Method Details

.next_state_idObject



10
11
12
13
14
# File 'lib/parser/parser_state.rb', line 10

def self.next_state_id
  result = @@state_id
  @@state_id += 1
  result
end

Instance Method Details

#compile_to_ruby_sourceObject



39
40
41
42
43
44
45
46
# File 'lib/parser/parser_state.rb', line 39

def compile_to_ruby_source
  result = "  at_state(#{@id}) {\n"
  actions.each do |symbol_name, action|
    result << "    for_symbol('#{symbol_name}') { #{action.compile_to_ruby_source} }\n"
  end
  result << "  }"
  result
end

#dot_nameObject



31
32
33
# File 'lib/parser/parser_state.rb', line 31

def dot_name
  self.to_s
end

#for_symbol(symbol_name, &blk) ⇒ Object



48
49
50
# File 'lib/parser/parser_state.rb', line 48

def for_symbol symbol_name, &blk
  actions[symbol_name] = @parser.instance_eval(&blk)
end

#to_dot(options = {}) ⇒ Object



35
36
37
38
# File 'lib/parser/parser_state.rb', line 35

def to_dot(options = {})
  label = self.items.values.collect{|item| item.to_s(options)}.join('\n')
  "#{dot_name} [label=\"#{label}\"]"
end

#to_sObject



52
53
54
# File 'lib/parser/parser_state.rb', line 52

def to_s
  "State#{id}"
end

#transition_itemsObject



23
24
25
26
27
28
29
# File 'lib/parser/parser_state.rb', line 23

def transition_items
  result = Hash.new {|h, k| h[k] = ItemSet.new()}
  for item in @items.values
    (result[item.next_symbol] << item) if item.next_symbol
  end
  result
end