Class: Resyma::Core::ParseTree

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

Overview

Parse tree with fields used by the matching algorithm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, children, parent, index, is_leaf, ast = nil) ⇒ ParseTree

Create an instance of parse tree

Parameters:

  • symbol (Symbol)

    Symbol associating to the node

  • children (Array)

    Subtrees of current node, or an array with a single element if it is a leaf

  • parent (Resyma::Core::ParseTree, nil)

    Parent tree, or nil if the current node is the root

  • index (Integer)

    There are ‘index` brother preceding to the current node

  • is_leaf (true, false)

    Whether or not the current node is a leaf

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

    Its corresponding abstract syntax tree



51
52
53
54
55
56
57
58
59
60
# File 'lib/resyma/core/parsetree/definition.rb', line 51

def initialize(symbol, children, parent, index, is_leaf, ast = nil)
  @symbol = symbol
  @children = children
  @parent = parent
  @index = index
  @field = Field.clean_field
  @is_leaf = is_leaf
  @ast = ast
  @cache = {}
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



35
36
37
# File 'lib/resyma/core/parsetree/definition.rb', line 35

def ast
  @ast
end

#cacheObject

Returns the value of attribute cache.



35
36
37
# File 'lib/resyma/core/parsetree/definition.rb', line 35

def cache
  @cache
end

#childrenObject

Returns the value of attribute children.



35
36
37
# File 'lib/resyma/core/parsetree/definition.rb', line 35

def children
  @children
end

#fieldObject

Returns the value of attribute field.



35
36
37
# File 'lib/resyma/core/parsetree/definition.rb', line 35

def field
  @field
end

#indexObject

Returns the value of attribute index.



35
36
37
# File 'lib/resyma/core/parsetree/definition.rb', line 35

def index
  @index
end

#parentObject

Returns the value of attribute parent.



35
36
37
# File 'lib/resyma/core/parsetree/definition.rb', line 35

def parent
  @parent
end

#symbolObject

Returns the value of attribute symbol.



35
36
37
# File 'lib/resyma/core/parsetree/definition.rb', line 35

def symbol
  @symbol
end

Instance Method Details

#build(parent = nil) ⇒ Object



6
7
8
9
# File 'lib/resyma/core/parsetree/builder.rb', line 6

def build(parent = nil)
  @parent = parent
  self
end

#clear!Object



62
63
64
65
66
# File 'lib/resyma/core/parsetree/definition.rb', line 62

def clear!
  @field = Field.clean_field
  @cache = {}
  @children.each(&:clear!) unless leaf?
end

#depth_first_each {|A| ... } ⇒ nil

Depth-firstly traverse the tree

Yield Parameters:

Returns:

  • (nil)

    Nothing



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

def depth_first_each(&block)
  yield self

  return if leaf?

  @children.each do |child|
    child.depth_first_each(&block)
  end

  nil
end

#leaf?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/resyma/core/parsetree/definition.rb', line 72

def leaf?
  @is_leaf
end

#root?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/resyma/core/parsetree/definition.rb', line 68

def root?
  @parent.nil?
end

#to_literalObject



154
155
156
157
158
159
160
# File 'lib/resyma/parsetree.rb', line 154

def to_literal
  unless leaf?
    raise TypeError,
          "Cannot convert a non-leaf node(i.e. non-token) to literal"
  end
  children.first
end

#to_ruby(bd = binding, filename = "(resyma)", lino = 1) ⇒ Object

Evaluate current parse tree using default evaluator

Parameters:

  • bd (Binding) (defaults to: binding)

    Environment

  • filename (String) (defaults to: "(resyma)")

    Source location

  • lino (Integer) (defaults to: 1)

    Source location

Returns:

  • (Object)

    Evaluation result



150
151
152
# File 'lib/resyma/parsetree.rb', line 150

def to_ruby(bd = binding, filename = "(resyma)", lino = 1)
  Evaluator.new.evaluate(self, bd, filename, lino)
end