Class: Moxml::XPath::AST::Node Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/xpath/ast/node.rb

Overview

This class is abstract.

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :node, children = [], value = nil) ⇒ Node

Initialize a new AST node

Parameters:

  • type (Symbol) (defaults to: :node)

    Node type

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

    Child nodes

  • value (Object) (defaults to: nil)

    Optional value for leaf nodes



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

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/moxml/xpath/ast/node.rb', line 13

def children
  @children
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/moxml/xpath/ast/node.rb', line 13

def type
  @type
end

#valueObject (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

.currentObject

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

.parentObject

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

.wildcardObject

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

Returns:

  • (Boolean)

    true if node represents 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

Parameters:

Returns:

Raises:



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

#inspectString Also known as: to_s

String representation for debugging

Returns:

  • (String)

    Debug representation



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_typeSymbol

Get the result type of this node

Returns:

  • (Symbol)

    One of :node_set, :string, :number, :boolean



46
47
48
# File 'lib/moxml/xpath/ast/node.rb', line 46

def result_type
  :unknown
end