Class: Janeway::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/janeway/interpreter.rb

Overview

Tree-walk interpreter to apply the operations from the abstract syntax tree to the input

Defined Under Namespace

Classes: Error

Constant Summary collapse

FUNCTION_PARAMETER_TYPES =

Specify the parameter types that built-in JsonPath functions require

{
  length: [:value_type],
  count: [:nodes_type],
  match: i[value_type value_type],
  search: i[value_type value_type],
  value: [:nodes_type],
}.freeze
NOTHING =

Functions may accept or return this special value

:nothing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Interpreter

Returns a new instance of Interpreter.

Parameters:

  • query (AST::Query)

    abstract syntax tree of the jsonpath query

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/janeway/interpreter.rb', line 34

def initialize(query)
  raise ArgumentError, "expect AST::Query, got #{query.inspect}" unless query.is_a?(AST::Query)

  @query = query
  @jsonpath = query.jsonpath
  @input = nil
end

Instance Attribute Details

#call_stackObject (readonly)

Returns the value of attribute call_stack.



8
9
10
# File 'lib/janeway/interpreter.rb', line 8

def call_stack
  @call_stack
end

#envObject (readonly)

Returns the value of attribute env.



8
9
10
# File 'lib/janeway/interpreter.rb', line 8

def env
  @env
end

#jsonpathObject (readonly)

Returns the value of attribute jsonpath.



8
9
10
# File 'lib/janeway/interpreter.rb', line 8

def jsonpath
  @jsonpath
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/janeway/interpreter.rb', line 8

def output
  @output
end

Class Method Details

.interpret(input, query) ⇒ Object

Interpret a query on the given input, return result

Parameters:

  • input (Hash, Array)
  • query (String)


27
28
29
30
31
# File 'lib/janeway/interpreter.rb', line 27

def self.interpret(input, query)
  tokens = Lexer.lex(query)
  ast = Parser.new(tokens, query).parse
  new(ast).interpret(input)
end

Instance Method Details

#interpret(input) ⇒ Object

Parameters:

  • input (Array, Hash)

    object to be searched

Returns:

  • (Object)


44
45
46
47
48
49
50
51
# File 'lib/janeway/interpreter.rb', line 44

def interpret(input)
  @input = input
  unless @input.is_a?(Hash) || @input.is_a?(Array)
    return [] # can't query on any other types
  end

  interpret_node(@query.root, nil)
end