Class: Janeway::Interpreter
- Inherits:
-
Object
- Object
- Janeway::Interpreter
- 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
-
#call_stack ⇒ Object
readonly
Returns the value of attribute call_stack.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#jsonpath ⇒ Object
readonly
Returns the value of attribute jsonpath.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
-
.interpret(input, query) ⇒ Object
Interpret a query on the given input, return result.
Instance Method Summary collapse
-
#initialize(query) ⇒ Interpreter
constructor
A new instance of Interpreter.
- #interpret(input) ⇒ Object
Constructor Details
#initialize(query) ⇒ Interpreter
Returns a new instance of Interpreter.
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_stack ⇒ Object (readonly)
Returns the value of attribute call_stack.
8 9 10 |
# File 'lib/janeway/interpreter.rb', line 8 def call_stack @call_stack end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
8 9 10 |
# File 'lib/janeway/interpreter.rb', line 8 def env @env end |
#jsonpath ⇒ Object (readonly)
Returns the value of attribute jsonpath.
8 9 10 |
# File 'lib/janeway/interpreter.rb', line 8 def jsonpath @jsonpath end |
#output ⇒ Object (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
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
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 |