Class: Dhaka::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParserMethods

#parse

Constructor Details

#initialize(grammar, logger = nil) ⇒ Parser

Creates a new parser from the given grammar. Messages are logged by default to STDOUT and the log level is WARN. Shift-reduce conflicts are reported at WARN and reduce-reduce conflicts at ERROR. You may pass in your own logger. Logging at DEBUG shows a lot of progress output.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/parser/parser.rb', line 13

def initialize(grammar, logger = nil)
  if logger
    @logger = logger
  else
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::WARN
  end
  @transitions = Hash.new {|hash, state| hash[state] = {}}
  @grammar = grammar
  @channels = []
  @states = Hash.new do |hash, kernel|
      channels, closure = @grammar.closure(kernel)
      @channels += channels.to_a
      new_state = ParserState.new(self, closure)
      hash[kernel] = new_state
      @logger.debug("Created #{new_state}.")
      new_state.transition_items.each do |symbol, items|
        destination_kernel = ItemSet.new(items.collect{|item| item.next_item})
        destination_state = hash[destination_kernel]
        items.each { |item| @channels << @grammar.passive_channel(item, destination_state.items[item.next_item]) }
        @transitions[new_state][symbol] = destination_state
      end
      new_state
  end
  initialize_states
end

Instance Attribute Details

#grammarObject (readonly)

Returns the value of attribute grammar.



8
9
10
# File 'lib/parser/parser.rb', line 8

def grammar
  @grammar
end

Instance Method Details

#compile_to_ruby_source_as(parser_class_name) ⇒ Object

Returns the Ruby source of the generated parser compiled as parser_class_name. This can be written out to a file.



41
42
43
44
45
46
47
48
49
50
# File 'lib/parser/parser.rb', line 41

def compile_to_ruby_source_as parser_class_name
  result = "class #{parser_class_name} < Dhaka::CompiledParser\n\n"
  result << "  self.grammar = #{@grammar.name}\n\n"
  result << "  start_with #{start_state.id}\n\n"
  states.each do |state|
    result << "#{state.compile_to_ruby_source}\n\n"
  end
  result << "end"
  result
end

#to_dot(options = {}) ⇒ Object

Returns the dot representation of the parser. If :hide_lookaheads is set to true in the options hash, lookaheads are not written out to the parser states, which is helpful when there are dozens of lookahead symbols for every item in every state.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/parser/parser.rb', line 55

def to_dot(options = {})
  result = ["digraph x {", "node [fontsize=\"10\" shape=box size=\"5\"]"]
  result += states.collect { |state| state.to_dot(options) }
  states.each { |state| 
      @transitions[state].each { |symbol, dest_state| 
          result << "#{state.dot_name} -> #{dest_state.dot_name} [label=\"#{symbol.name}\"]"
         }
     }
  result << ['}']
  result.join("\n")
end