Class: Moxml::XPath::AST::Node Abstract
- Inherits:
-
Object
- Object
- Moxml::XPath::AST::Node
- Defined in:
- lib/moxml/xpath/ast/node.rb
Overview
Subclass and override #evaluate to implement
Abstract base class for all XPath AST nodes
All AST nodes must implement the #evaluate method which takes a context and returns a result (NodeSet, String, Number, or Boolean).
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.absolute_path(descendant_or_self, *steps) ⇒ Object
Create an absolute path node (starts with / or //).
-
.attribute(name) ⇒ Object
Create an attribute node.
-
.axis(axis_name, node_test, *predicates) ⇒ Object
Create an axis node.
-
.binary_op(operator, left, right) ⇒ Object
Create a binary operator node.
-
.current ⇒ Object
Create a current node (.).
-
.function(name, *args) ⇒ Object
Create a function call node.
-
.node_type(type_name) ⇒ Object
Create a node type test (text(), comment(), etc.).
-
.number(value) ⇒ Object
Create a literal number node.
-
.parent ⇒ Object
Create a parent node (..).
-
.path(*steps) ⇒ Object
Create a path node.
-
.predicate(condition) ⇒ Object
Create a predicate node.
-
.relative_path(*steps) ⇒ Object
Create a relative path node.
-
.string(value) ⇒ Object
Create a literal string node.
-
.test(namespace, name) ⇒ Object
Create a node test.
-
.unary_op(operator, operand) ⇒ Object
Create a unary operator node.
-
.union(*expressions) ⇒ Object
Create a union node (|).
-
.variable(name) ⇒ Object
Create a variable reference node.
-
.wildcard ⇒ Object
Create a wildcard test.
Instance Method Summary collapse
-
#constant? ⇒ Boolean
Check if this node is a constant value.
-
#evaluate(context) ⇒ Moxml::NodeSet, ...
Evaluate this AST node in the given context.
-
#initialize(type = :node, children = [], value = nil) ⇒ Node
constructor
Initialize a new AST node.
-
#inspect ⇒ String
(also: #to_s)
String representation for debugging.
-
#result_type ⇒ Symbol
Get the result type of this node.
Constructor Details
#initialize(type = :node, children = [], value = nil) ⇒ Node
Initialize a new AST node
20 21 22 23 24 |
# File 'lib/moxml/xpath/ast/node.rb', line 20 def initialize(type = :node, children = [], value = nil) @type = type @children = Array(children) @value = value end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
13 14 15 |
# File 'lib/moxml/xpath/ast/node.rb', line 13 def children @children end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/moxml/xpath/ast/node.rb', line 13 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
13 14 15 |
# File 'lib/moxml/xpath/ast/node.rb', line 13 def value @value end |
Class Method Details
.absolute_path(descendant_or_self, *steps) ⇒ Object
Create an absolute path node (starts with / or //)
68 69 70 |
# File 'lib/moxml/xpath/ast/node.rb', line 68 def self.absolute_path(descendant_or_self, *steps) new(:absolute_path, [descendant_or_self] + steps) end |
.attribute(name) ⇒ Object
Create an attribute node
138 139 140 |
# File 'lib/moxml/xpath/ast/node.rb', line 138 def self.attribute(name) new(:attribute, [], name) end |
.axis(axis_name, node_test, *predicates) ⇒ Object
Create an axis node
83 84 85 |
# File 'lib/moxml/xpath/ast/node.rb', line 83 def self.axis(axis_name, node_test, *predicates) new(:axis, [axis_name, node_test] + predicates) end |
.binary_op(operator, left, right) ⇒ Object
Create a binary operator node
123 124 125 |
# File 'lib/moxml/xpath/ast/node.rb', line 123 def self.binary_op(operator, left, right) new(:binary_op, [left, right], operator) end |
.current ⇒ Object
Create a current node (.)
143 144 145 |
# File 'lib/moxml/xpath/ast/node.rb', line 143 def self.current new(:current) end |
.function(name, *args) ⇒ Object
Create a function call node
103 104 105 |
# File 'lib/moxml/xpath/ast/node.rb', line 103 def self.function(name, *args) new(:function, args, name) end |
.node_type(type_name) ⇒ Object
Create a node type test (text(), comment(), etc.)
153 154 155 |
# File 'lib/moxml/xpath/ast/node.rb', line 153 def self.node_type(type_name) new(:node_type, [], type_name) end |
.number(value) ⇒ Object
Create a literal number node
118 119 120 |
# File 'lib/moxml/xpath/ast/node.rb', line 118 def self.number(value) new(:number, [], value.to_f) end |
.parent ⇒ Object
Create a parent node (..)
148 149 150 |
# File 'lib/moxml/xpath/ast/node.rb', line 148 def self.parent new(:parent) end |
.path(*steps) ⇒ Object
Create a path node
78 79 80 |
# File 'lib/moxml/xpath/ast/node.rb', line 78 def self.path(*steps) new(:path, steps) end |
.predicate(condition) ⇒ Object
Create a predicate node
98 99 100 |
# File 'lib/moxml/xpath/ast/node.rb', line 98 def self.predicate(condition) new(:predicate, [condition]) end |
.relative_path(*steps) ⇒ Object
Create a relative path node
73 74 75 |
# File 'lib/moxml/xpath/ast/node.rb', line 73 def self.relative_path(*steps) new(:relative_path, steps) end |
.string(value) ⇒ Object
Create a literal string node
113 114 115 |
# File 'lib/moxml/xpath/ast/node.rb', line 113 def self.string(value) new(:string, [], value) end |
.test(namespace, name) ⇒ Object
Create a node test
88 89 90 |
# File 'lib/moxml/xpath/ast/node.rb', line 88 def self.test(namespace, name) new(:test, [], { namespace: namespace, name: name }) end |
.unary_op(operator, operand) ⇒ Object
Create a unary operator node
128 129 130 |
# File 'lib/moxml/xpath/ast/node.rb', line 128 def self.unary_op(operator, operand) new(:unary_op, [operand], operator) end |
.union(*expressions) ⇒ Object
Create a union node (|)
133 134 135 |
# File 'lib/moxml/xpath/ast/node.rb', line 133 def self.union(*expressions) new(:union, expressions) end |
.variable(name) ⇒ Object
Create a variable reference node
108 109 110 |
# File 'lib/moxml/xpath/ast/node.rb', line 108 def self.variable(name) new(:variable, [], name) end |
.wildcard ⇒ Object
Create a wildcard test
93 94 95 |
# File 'lib/moxml/xpath/ast/node.rb', line 93 def self.wildcard new(:wildcard) end |
Instance Method Details
#constant? ⇒ Boolean
Check if this node is a constant value
39 40 41 |
# File 'lib/moxml/xpath/ast/node.rb', line 39 def constant? false end |
#evaluate(context) ⇒ Moxml::NodeSet, ...
Evaluate this AST node in the given context
31 32 33 34 |
# File 'lib/moxml/xpath/ast/node.rb', line 31 def evaluate(context) raise ::NotImplementedError, "#{self.class}#evaluate must be implemented by subclass" end |
#inspect ⇒ String Also known as: to_s
String representation for debugging
53 54 55 56 57 58 59 60 61 |
# File 'lib/moxml/xpath/ast/node.rb', line 53 def inspect if @value "#<#{self.class.name} @type=#{@type} @value=#{@value.inspect}>" elsif @children.any? "#<#{self.class.name} @type=#{@type} children=#{@children.size}>" else "#<#{self.class.name} @type=#{@type}>" end end |
#result_type ⇒ Symbol
Get the result type of this node
46 47 48 |
# File 'lib/moxml/xpath/ast/node.rb', line 46 def result_type :unknown end |