Module: DrgDSL::Visitor

Included in:
ParenCleaner, PrettyPrinter
Defined in:
lib/drgdsl/visitor.rb

Overview

Mixin for Ast::Node visitors. Visits all child nodes and returns the visited node by default.

Defined Under Namespace

Modules: Cache, ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(visitor) ⇒ Object



8
9
10
11
# File 'lib/drgdsl/visitor.rb', line 8

def self.included(visitor)
  visitor.prepend Cache
  visitor.extend ClassMethods
end

Instance Method Details

#cache?Boolean

Whether this visitor should cache visit results. Note that one might need to override #cache_key(n) if a visit is not deterministic, e.g. when it depends on some instance variables.

Returns:

  • (Boolean)

See Also:



39
40
41
# File 'lib/drgdsl/visitor.rb', line 39

def cache?
  false
end

#cache_key(n) ⇒ Integer

Returns ideally an object hash.

Parameters:

Returns:

  • (Integer)

    ideally an object hash



45
46
47
# File 'lib/drgdsl/visitor.rb', line 45

def cache_key(n)
  n.hash
end

#visit(n) ⇒ Object

Parameters:



50
51
52
53
# File 'lib/drgdsl/visitor.rb', line 50

def visit(n)
  return visit_nil if n.nil?
  send("visit_#{n.type}", n)
end

#visit_AndExpression(n) ⇒ Object



68
69
70
71
# File 'lib/drgdsl/visitor.rb', line 68

def visit_AndExpression(n)
  n.expressions.map { |e| visit(e) }
  n
end

#visit_BasicExpression(n) ⇒ Object



92
93
94
95
96
# File 'lib/drgdsl/visitor.rb', line 92

def visit_BasicExpression(n)
  visit(n.variable)
  visit(n.condition)
  n
end

#visit_Comparison(n) ⇒ Object



98
99
100
101
102
# File 'lib/drgdsl/visitor.rb', line 98

def visit_Comparison(n)
  visit(n.value)
  visit(n.table_condition)
  n
end

#visit_Constant(n) ⇒ Object



136
137
138
# File 'lib/drgdsl/visitor.rb', line 136

def visit_Constant(n)
  n
end

#visit_DateExpression(n) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/drgdsl/visitor.rb', line 123

def visit_DateExpression(n)
  visit(n.left_variable)
  visit(n.left_condition)
  visit(n.right_variable)
  visit(n.right_condition)
  visit(n.comparison)
  n
end


87
88
89
90
# File 'lib/drgdsl/visitor.rb', line 87

def visit_DrgLink(n)
  visit(n.variable)
  n
end

#visit_Empty(n) ⇒ Object



109
110
111
# File 'lib/drgdsl/visitor.rb', line 109

def visit_Empty(n)
  n
end

#visit_Expression(n) ⇒ Object



63
64
65
66
# File 'lib/drgdsl/visitor.rb', line 63

def visit_Expression(n)
  n.expressions.map { |e| visit(e) }
  n
end

#visit_FunctionCall(n) ⇒ Object



83
84
85
# File 'lib/drgdsl/visitor.rb', line 83

def visit_FunctionCall(n)
  n
end

#visit_nilObject

Override to define what should happen when one calls #visit with nil (can occur on optional AST branches).

Does nothing by default. A string visitor might want to return an empty string here.



60
61
# File 'lib/drgdsl/visitor.rb', line 60

def visit_nil
end

#visit_NotExpression(n) ⇒ Object



78
79
80
81
# File 'lib/drgdsl/visitor.rb', line 78

def visit_NotExpression(n)
  visit(n.expression)
  n
end

#visit_ParenExpression(n) ⇒ Object



73
74
75
76
# File 'lib/drgdsl/visitor.rb', line 73

def visit_ParenExpression(n)
  visit(n.expression)
  n
end

#visit_SrglrbTableCondition(n) ⇒ Object



118
119
120
121
# File 'lib/drgdsl/visitor.rb', line 118

def visit_SrglrbTableCondition(n)
  visit(n.condition)
  n
end

#visit_TableCondition(n) ⇒ Object



113
114
115
116
# File 'lib/drgdsl/visitor.rb', line 113

def visit_TableCondition(n)
  visit(n.comparison)
  n
end

#visit_UnaryCondition(n) ⇒ Object



104
105
106
107
# File 'lib/drgdsl/visitor.rb', line 104

def visit_UnaryCondition(n)
  visit(n.condition)
  n
end

#visit_Variable(n) ⇒ Object



132
133
134
# File 'lib/drgdsl/visitor.rb', line 132

def visit_Variable(n)
  n
end