Class: Janeway::AST::Query

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(root_node, jsonpath) ⇒ Query

Returns a new instance of Query.

Parameters:

  • root_node (AST::Root)
  • jsonpath (String)

Raises:

  • (ArgumentError)


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

#jsonpathString (readonly)

The original jsonpath query, for use in error messages

Returns:

  • (String)


15
16
17
# File 'lib/janeway/ast/query.rb', line 15

def jsonpath
  @jsonpath
end

#rootAST::RootNode (readonly)

Returns:



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.

Parameters:

  • input (Object)

    ruby object to be searched

Returns:

  • (Array)

    all matched objects



31
32
33
# File 'lib/janeway/ast/query.rb', line 31

def find_all(input)
  Janeway::Interpreter.new(self).interpret(input)
end

#to_sObject



35
36
37
# File 'lib/janeway/ast/query.rb', line 35

def to_s
  @root.to_s
end

#treeObject

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