Class: Janeway::AST::Query
- Inherits:
-
Object
- Object
- Janeway::AST::Query
- Defined in:
- lib/janeway/ast/query.rb
Overview
AST::Query holds the complete abstract syntax tree created by parsing the query.
This can be frozen and passed to multiple threads or ractors for simultaneous use. No instance members are modified during the interpretation stage.
Instance Attribute Summary collapse
-
#jsonpath ⇒ String
readonly
The original jsonpath query, for use in error messages.
- #root ⇒ AST::RootNode readonly
Instance Method Summary collapse
-
#==(other) ⇒ Object
Queries are considered equal if their ASTs evaluate to the same JSONPath string.
-
#find_all(input) ⇒ Array
Use this Query to search the input, and return the results.
-
#initialize(root_node, jsonpath) ⇒ Query
constructor
A new instance of Query.
- #to_s ⇒ Object
-
#tree ⇒ Object
Print AST in tree format Every AST class prints a 1-line representation of self, with children on separate lines.
Constructor Details
#initialize(root_node, jsonpath) ⇒ Query
Returns a new instance of Query.
19 20 21 22 23 24 25 |
# File 'lib/janeway/ast/query.rb', line 19 def initialize(root_node, jsonpath) raise ArgumentError, "expect root identifier, got #{root_node.inspect}" unless root_node.is_a?(RootNode) raise ArgumentError, "expect query string, got #{jsonpath.inspect}" unless jsonpath.is_a?(String) @root = root_node @jsonpath = jsonpath end |
Instance Attribute Details
#jsonpath ⇒ String (readonly)
The original jsonpath query, for use in error messages
15 16 17 |
# File 'lib/janeway/ast/query.rb', line 15 def jsonpath @jsonpath end |
#root ⇒ AST::RootNode (readonly)
11 12 13 |
# File 'lib/janeway/ast/query.rb', line 11 def root @root end |
Instance Method Details
#==(other) ⇒ Object
Queries are considered equal if their ASTs evaluate to the same JSONPath string.
The string output is generated by the AST and should be considered a “normalized” form of the query. It may have different whitespace and parentheses than the original input but will be semantically equivalent.
44 45 46 |
# File 'lib/janeway/ast/query.rb', line 44 def ==(other) to_s == other.to_s end |
#find_all(input) ⇒ Array
Use this Query to search the input, and return the results.
31 32 33 |
# File 'lib/janeway/ast/query.rb', line 31 def find_all(input) Janeway::Interpreter.new(self).interpret(input) end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/janeway/ast/query.rb', line 35 def to_s @root.to_s end |
#tree ⇒ Object
Print AST in tree format Every AST class prints a 1-line representation of self, with children on separate lines
50 51 52 53 54 |
# File 'lib/janeway/ast/query.rb', line 50 def tree result = @root.tree(0) result.flatten.join("\n") end |