class FBO::Parser

Public Class Methods

new(file = nil) click to toggle source
# File lib/fbo/parser.rb, line 9
def initialize(file = nil)
  @file = file
end

Public Instance Methods

parse(data = nil) click to toggle source
# File lib/fbo/parser.rb, line 13
def parse(data = nil)
  data ||= @file.contents

  if data.respond_to? :each
    @tree = parse_collection(data)
  else
    @tree = parse_string(data)
  end
  @tree
end

Private Instance Methods

clean_tree(node) click to toggle source
# File lib/fbo/parser.rb, line 56
def clean_tree(node)
  return if node.elements.nil?
  node.elements.delete_if { |el| el.class.name == "Treetop::Runtime::SyntaxNode" }
  node.elements.each { |el| clean_tree(el) }
  node
end
parse_collection(data) click to toggle source
# File lib/fbo/parser.rb, line 42
def parse_collection(data)
  super_tree = nil
  data.each do |string|
    tree = parse_string(string)
    if super_tree
      super_tree = FBO::Dump::DumpNode.new(super_tree.input, super_tree.interval,
                                           super_tree.elements + tree.elements)
    else
      super_tree = tree
    end
  end
  super_tree
end
parse_string(data) click to toggle source
# File lib/fbo/parser.rb, line 30
def parse_string(data)
  tree = parser.parse(data)
  if tree.nil?
    line = parser.failure_line
    column = parser.failure_column
    reason = parser.failure_reason
    raise FBO::ParserError.new("Could not parse data: line #{ line }, column #{ column }, reason '#{ reason }'",
                               data:data)
  end
  clean_tree(tree)
end
parser() click to toggle source
# File lib/fbo/parser.rb, line 26
def parser
  @parser ||= FBO::DumpParser.new
end