Class: IcAgent::Ast::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



8
9
10
11
# File 'lib/ic_agent/ast/parser.rb', line 8

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

Instance Attribute Details

#parserObject

Returns the value of attribute parser.



6
7
8
# File 'lib/ic_agent/ast/parser.rb', line 6

def parser
  @parser
end

#treeObject

Returns the value of attribute tree.



6
7
8
# File 'lib/ic_agent/ast/parser.rb', line 6

def tree
  @tree
end

Instance Method Details

#clean_tree(root_node) ⇒ Object



25
26
27
28
29
30
# File 'lib/ic_agent/ast/parser.rb', line 25

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

#ic_serviceObject



32
33
34
35
36
37
# File 'lib/ic_agent/ast/parser.rb', line 32

def ic_service
  tree.elements.each do |ele|
    return ele if ele.title == :ic_service
  end
  nil
end

#ic_service_methodsObject



55
56
57
58
59
60
61
62
63
# File 'lib/ic_agent/ast/parser.rb', line 55

def ic_service_methods
  ic_service_tree = ic_service
  unless ic_service_tree.empty?
    ic_service_tree.elements.each do |ele|
      return ele if ele.title == :ic_service_methods
    end
  end
  nil
end

#ic_type_by_name(type_name) ⇒ Object



77
78
79
80
81
82
# File 'lib/ic_agent/ast/parser.rb', line 77

def ic_type_by_name(type_name)
  ic_types.each do |ic_type|
    return ic_type if type_name == ic_type_name(ic_type)
  end
  nil
end

#ic_type_name(ic_type) ⇒ Object



65
66
67
# File 'lib/ic_agent/ast/parser.rb', line 65

def ic_type_name(ic_type)
  ic_type.type_param_name
end

#ic_type_namesObject



69
70
71
72
73
74
75
# File 'lib/ic_agent/ast/parser.rb', line 69

def ic_type_names
  names_arr = []
  ic_types.each do |ic_type|
    names_arr << ic_type_name(ic_type)
  end
  names_arr
end

#ic_typesObject



39
40
41
42
43
44
45
# File 'lib/ic_agent/ast/parser.rb', line 39

def ic_types
  type_arr = []
  tree.elements.each do |ele|
    type_arr << ele if ele.title == :type_declaration
  end
  type_arr
end

#ic_types_objObject



47
48
49
50
51
52
53
# File 'lib/ic_agent/ast/parser.rb', line 47

def ic_types_obj
  obj_arr = []
  ic_types.each do |ic_type|
    obj_arr << ic_type.to_obj
  end
  obj_arr
end

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

Raises:

  • (Exception)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ic_agent/ast/parser.rb', line 13

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

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

  # this edits the tree in place
  clean_tree(tree)

  @tree = tree
  tree
end