Class: Dhaka::ParserState

Inherits:
Object
  • Object
show all
Defined in:
lib/dhaka/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.



13
14
15
16
17
18
# File 'lib/dhaka/parser/parser_state.rb', line 13

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

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



3
4
5
# File 'lib/dhaka/parser/parser_state.rb', line 3

def actions
  @actions
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/dhaka/parser/parser_state.rb', line 3

def id
  @id
end

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/dhaka/parser/parser_state.rb', line 3

def items
  @items
end

Class Method Details

.next_state_idObject



7
8
9
10
11
# File 'lib/dhaka/parser/parser_state.rb', line 7

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

Instance Method Details

#compile_to_ruby_sourceObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dhaka/parser/parser_state.rb', line 32

def compile_to_ruby_source
  result = "  at_state(#{id}) {\n"

  symbol_names_by_action = Hash.new {|hash, key| hash[key] = []}
  actions.each do |symbol_name, action|
    symbol_names_by_action[action] << symbol_name
  end
  
  symbol_names_by_action.keys.each do |action|
    symbol_names = symbol_names_by_action[action].collect {|symbol_name| "#{symbol_name.inspect}"}.join(', ')
    result << "    for_symbols(#{symbol_names}) { #{action.compile_to_ruby_source} }\n"
  end
  
  result << "  }"
  result
end

#for_symbols(*symbol_names, &blk) ⇒ Object Also known as: for_symbol



49
50
51
52
53
# File 'lib/dhaka/parser/parser_state.rb', line 49

def for_symbols *symbol_names, &blk
  symbol_names.each do |symbol_name|
    actions[symbol_name] = @parser.instance_eval(&blk)
  end
end

#to_s(options = {}) ⇒ Object



57
58
59
# File 'lib/dhaka/parser/parser_state.rb', line 57

def to_s(options = {})
  items.values.collect{|item| item.to_s(options)}.join("\n")
end

#transition_itemsObject



20
21
22
23
24
25
26
# File 'lib/dhaka/parser/parser_state.rb', line 20

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

#unique_nameObject



28
29
30
# File 'lib/dhaka/parser/parser_state.rb', line 28

def unique_name
  "State#{id}"
end