Class: Resyma::Core::ParseTreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/resyma/core/parsetree/builder.rb

Overview

Builder of Resyma::Core::ParseTree

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, index = 0, is_leaf = false, children = [], ast = nil) ⇒ ParseTreeBuilder

Returns a new instance of ParseTreeBuilder.



16
17
18
19
20
21
22
23
# File 'lib/resyma/core/parsetree/builder.rb', line 16

def initialize(symbol, index = 0, is_leaf = false,
               children = [], ast = nil)
  @symbol = symbol
  @children = children
  @index = index
  @is_leaf = is_leaf
  @ast = ast
end

Class Method Details

.root(symbol, value = nil, index = 0, ast = nil, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/resyma/core/parsetree/builder.rb', line 78

def self.root(symbol, value = nil, index = 0, ast = nil, &block)
  if block.nil?
    ParseTreeBuilder.new(symbol, index, true, [value], ast)
  else
    ptb = ParseTreeBuilder.new(symbol, index, false, [], ast)
    ptb.instance_eval(&block) unless block.nil?
    ptb
  end
end

Instance Method Details

#add_child!(symbol, ast = nil, is_leaf = false, value = []) ⇒ Resyma::Core::ParseTreeBuilder

Define and add a node to current tree as a child

Parameters:

  • symbol (Symbol)

    Type of the node

  • ast (Parser::AST::Node) (defaults to: nil)

    Abstract syntax tree of the new node

  • is_leaf (true, false) (defaults to: false)

    Is a leaf node?

  • value (Array) (defaults to: [])

    Should only be used when ‘is_leaf`, meaning that this node is a token node. Pass an array with a single value as the value of the token

Returns:



37
38
39
40
41
42
# File 'lib/resyma/core/parsetree/builder.rb', line 37

def add_child!(symbol, ast = nil, is_leaf = false, value = [])
  ptb = ParseTreeBuilder.new(symbol, @children.length, is_leaf, value,
                             ast)
  @children.push ptb
  ptb
end

#add_parsetree_child!(tree, ast = nil) ⇒ nil

Add a node to current tree as a child

Parameters:

Returns:

  • (nil)

    Nothing



51
52
53
54
55
56
# File 'lib/resyma/core/parsetree/builder.rb', line 51

def add_parsetree_child!(tree, ast = nil)
  tree.index = @children.length
  tree.ast = ast
  @children.push tree
  nil
end

#build(parent = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/resyma/core/parsetree/builder.rb', line 58

def build(parent = nil)
  pt = ParseTree.new(@symbol, nil, parent, @index, @is_leaf, @ast)
  pt.children = if @is_leaf
                  @children
                else
                  @children.map { |c| c.build(pt) }
                end
  pt
end

#leaf(symbol, value, ast = nil) ⇒ Object



74
75
76
# File 'lib/resyma/core/parsetree/builder.rb', line 74

def leaf(symbol, value, ast = nil)
  add_child! symbol, ast, true, [value]
end

#node(symbol, ast = nil, &block) ⇒ Object



68
69
70
71
72
# File 'lib/resyma/core/parsetree/builder.rb', line 68

def node(symbol, ast = nil, &block)
  ptb = add_child! symbol, ast
  ptb.instance_eval(&block) unless block.nil?
  ptb
end