Sha256: 4d6b2aa04a0ed56766b24e9c80a1b527484ae753895ad622756fccfa22a5adc7

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

class NagiosParser::Status::Parser
  token
    OPEN CLOSE KEY VALUE

  rule
    types
      : type
      | types type
      ;
    type
      : type_names OPEN assignments CLOSE {
        @result[val[0]] ||= []
	@result[val[0]] << val[2]
      }
      ;
    type_names
      : 'hostcomment' | 'servicestatus' | 'info' | 'programstatus'
      | 'hoststatus' | 'contactstatus' | 'servicecomment'
      ;
    assignments
      : assignment
      | assignments assignment {
        val[1].each do |key, value|
          val[0][key] = value
        end
        result = val[0]
      }
      ;
    assignment
      : KEY { result = {val[0] => nil}}
      | KEY VALUE { result = {val[0] => val[1]} }
      ;
end

---- header
require 'strscan'

---- inner

def create_token(string)
  result = []
  inside = false
  scanner = StringScanner.new(string)

  until scanner.empty?
    case
    when scanner.scan(/\s+/)
      # ignore whitespace
    when scanner.scan(/#[^\n]*/)
      # ignore comments
    when (!inside and match = scanner.scan(/\w+/))
      result << [match, match]
    when match = scanner.scan(/\{/)
      inside = true
      result << [:OPEN, nil]
    when match = scanner.scan(/\}/)
      inside = false
      result << [:CLOSE, nil]
    when match = scanner.scan(/\w+\s*\=/)
      result << [:KEY, match.chop.gsub(/\s+$/, '')]
    when (inside and match = scanner.scan(/\d+$/))
      result << [:VALUE, match.to_i]
    when (inside and match = scanner.scan(/[^\n\}]+/))
      result << [:VALUE, match.gsub(/\s+$/, '')]
    else
      raise "Can't tokenize <#{scanner.peek(10)}>"
    end
  end

  result << [false, false]
end

attr_accessor :token

def self.parse(string)
  new.parse(string)
end

def parse(string)
  @result = {}
  @token = create_token(string)
  do_parse
  @result
end

def next_token
  @token.shift
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nagios_parser-1.0.0 lib/nagios_parser/status/parser.y