Class: NagiosConfig::Parser

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

Constant Summary collapse

NEWLINE =
"\n".freeze
EQUALS =
"=".freeze
OPEN_BRACE =
"{".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ignore_comments = false) ⇒ Parser

Returns a new instance of Parser.



15
16
17
18
19
20
# File 'lib/nagios_config/parser.rb', line 15

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

Instance Attribute Details

#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

Instance Method Details

#<<(string) ⇒ Object



64
65
66
67
# File 'lib/nagios_config/parser.rb', line 64

def <<(string)
  @scanner << string
  @state = send(@state)
end

#parse(io) ⇒ Object



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
52
53
54
55
# File 'lib/nagios_config/parser.rb', line 22

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

#stream_parse(io) ⇒ Object



57
58
59
60
61
62
# File 'lib/nagios_config/parser.rb', line 57

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