Class: NagiosConfig::Parser

Inherits:
Object
  • Object
show all
Includes:
Events::Emitter
Defined in:
lib/nagios_config/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



11
12
13
14
15
# File 'lib/nagios_config/parser.rb', line 11

def initialize
  @state = :body
  self.scanner = StringScanner.new("")
  self.value_buffer = ""
end

Instance Attribute Details

#in_defineObject

Returns the value of attribute in_define.



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

def in_define
  @in_define
end

#scannerObject

Returns the value of attribute scanner.



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

def scanner
  @scanner
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

#value_bufferObject

Returns the value of attribute value_buffer.



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

def value_buffer
  @value_buffer
end

Instance Method Details

#<<(string) ⇒ Object



60
61
62
63
64
65
# File 'lib/nagios_config/parser.rb', line 60

def <<(string)
  scanner.string.replace(scanner.rest)
  scanner.reset
  scanner << string
  self.state = send(state)
end

#parse(io) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nagios_config/parser.rb', line 17

def parse(io)
  root = NagiosConfig::Config.new
  current_define = nil
  current_variable = nil
  on(:comment) do |comment|
    (current_define || root).add_node(NagiosConfig::Comment.new(comment))
  end
  on(:whitespace) do |whitespace|
    (current_define || root).add_node(NagiosConfig::Whitespace.new(whitespace))
  end
  on(:trailing_comment) do |comment|
    (current_define || root).nodes.last.add_node(NagiosConfig::TrailingComment.new(comment))
  end
  on(:name) do |name|
    current_variable = NagiosConfig::Variable.new
    current_variable.add_node(NagiosConfig::Name.new(name))
    (current_define || root).add_node(current_variable)
  end
  on(:value) do |value|
    current_variable.add_node(NagiosConfig::Value.new(value))
    current_variable = nil
  end
  on(:begin_define) do
    current_define = NagiosConfig::Define.new
    root.add_node(current_define)
  end
  on(:type) do |type|
    current_define.add_node(NagiosConfig::Type.new(type))
  end
  on(:finish_define) do
    current_define = nil
  end
  stream_parse(io)
  root
end

#stream_parse(io) ⇒ Object



53
54
55
56
57
58
# File 'lib/nagios_config/parser.rb', line 53

def stream_parse(io)
  io = StringIO.new(io) unless io.respond_to?(:read)
  until io.eof?
    self << io.read(1024)
  end
end