Class: DockerfileAst::Parser

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

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



4
5
6
7
# File 'lib/dockerfile_ast/parser.rb', line 4

def initialize
  Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'dockerfile_grammar.treetop')))
  @parser = DockerfileGrammarParser.new
end

Instance Method Details

#clean_tree(root_node) ⇒ Object



22
23
24
25
26
# File 'lib/dockerfile_ast/parser.rb', line 22

def clean_tree(root_node)
  return if(root_node.elements.nil?)
  root_node.elements.delete_if{|node| node.class.name == "Treetop::Runtime::SyntaxNode" }
  root_node.elements.each {|node| self.clean_tree(node) }
end

#parse(data, return_type = :string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dockerfile_ast/parser.rb', line 9

def parse(data, return_type = :string)
  tree = @parser.parse(data)

  if(tree.nil?)
    raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}"
  end

  # this edits the tree in place
  clean_tree(tree)

  return tree
end