Class: ScopedSearch::QueryLanguage::AST::OperatorNode
- Defined in:
- lib/scoped_search/query_language/ast.rb
Overview
AST class for representing operators in the query. An operator node has an operator and operands that are represented as AST child nodes. Usually, operator nodes have one or two children. For logical operators, a distinct subclass exists to implement some tree simplification rules.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
Instance Method Summary collapse
-
#[](child_nr) ⇒ Object
Returns a child node by index, starting with 0.
- #empty? ⇒ Boolean
-
#eql?(node) ⇒ Boolean
:nodoc.
-
#infix? ⇒ Boolean
Returns true if this is an infix operator.
-
#initialize(operator, children, root_node = false) ⇒ OperatorNode
constructor
:nodoc.
-
#lhs ⇒ Object
Return the left-hand side (LHS) operand for this operator.
-
#prefix? ⇒ Boolean
Returns true if this is a prefix operator.
-
#rhs ⇒ Object
Return the right-hand side (RHS) operand for this operator.
-
#simplify ⇒ Object
Tree simplicication: returns itself after simpifying its children.
-
#to_a ⇒ Object
Return an array representation for the node.
Methods inherited from Node
Constructor Details
#initialize(operator, children, root_node = false) ⇒ OperatorNode
:nodoc
71 72 73 74 75 76 |
# File 'lib/scoped_search/query_language/ast.rb', line 71 def initialize(operator, children, root_node = false) # :nodoc @operator = operator @children = children raise ScopedSearch::QueryNotSupported, "Empty list of operands" if @children.empty? && !root_node end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
69 70 71 |
# File 'lib/scoped_search/query_language/ast.rb', line 69 def children @children end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
68 69 70 |
# File 'lib/scoped_search/query_language/ast.rb', line 68 def operator @operator end |
Instance Method Details
#[](child_nr) ⇒ Object
Returns a child node by index, starting with 0.
121 122 123 |
# File 'lib/scoped_search/query_language/ast.rb', line 121 def [](child_nr) children[child_nr] end |
#empty? ⇒ Boolean
106 107 108 |
# File 'lib/scoped_search/query_language/ast.rb', line 106 def empty? children.length == 0 end |
#eql?(node) ⇒ Boolean
:nodoc
89 90 91 |
# File 'lib/scoped_search/query_language/ast.rb', line 89 def eql?(node) # :nodoc node.kind_of?(OperatorNode) && node.operator == operator && node.children.eql?(children) end |
#infix? ⇒ Boolean
Returns true if this is an infix operator
111 112 113 |
# File 'lib/scoped_search/query_language/ast.rb', line 111 def infix? children.length > 1 end |
#lhs ⇒ Object
Return the left-hand side (LHS) operand for this operator.
94 95 96 97 98 |
# File 'lib/scoped_search/query_language/ast.rb', line 94 def lhs raise ScopedSearch::Exception, "Operator does not have a LHS" if prefix? raise ScopedSearch::Exception, "Operators with more than 2 children do not have LHS/RHS" if children.length > 2 children[0] end |
#prefix? ⇒ Boolean
Returns true if this is a prefix operator
116 117 118 |
# File 'lib/scoped_search/query_language/ast.rb', line 116 def prefix? children.length == 1 end |
#rhs ⇒ Object
Return the right-hand side (RHS) operand for this operator.
101 102 103 104 |
# File 'lib/scoped_search/query_language/ast.rb', line 101 def rhs raise ScopedSearch::Exception, "Operators with more than 2 children do not have LHS/RHS" if children.length > 2 children.length == 1 ? children[0] : children[1] end |
#simplify ⇒ Object
Tree simplicication: returns itself after simpifying its children
79 80 81 82 |
# File 'lib/scoped_search/query_language/ast.rb', line 79 def simplify @children = children.map { |c| c.simplify } return self end |
#to_a ⇒ Object
Return an array representation for the node
85 86 87 |
# File 'lib/scoped_search/query_language/ast.rb', line 85 def to_a [@operator] + @children.map { |c| c.to_a } end |