Class: Janeway::Interpreters::Base

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

Overview

Base class for interpreters.

Constant Summary collapse

NOTHING =

The special result NOTHING represents the absence of a JSON value and is distinct from any JSON value, including null. It represents:

* object keys referred to by name that do not exist in the input
* array values referred to by index that are out of range
* return value of function calls with an invalid input data type
:nothing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Base

Returns a new instance of Base.



26
27
28
29
30
31
32
# File 'lib/janeway/interpreters/base.rb', line 26

def initialize(node)
  @node = node

  return unless node.next

  @next = TreeConstructor.ast_node_to_interpreter(node.next)
end

Instance Attribute Details

#nextInterpreters::Base?

Subsequent expression interpreter that filters the output of this one

Returns:



21
22
23
# File 'lib/janeway/interpreters/base.rb', line 21

def next
  @next
end

#nodeAST::Expression (readonly)

Returns:



24
25
26
# File 'lib/janeway/interpreters/base.rb', line 24

def node
  @node
end

Instance Method Details

#as_jsonHash

Return hash representation of this selector interpreter

Returns:

  • (Hash)


49
50
51
52
53
54
55
# File 'lib/janeway/interpreters/base.rb', line 49

def as_json
  if node
    { type: type, value: node&.value, next: @next&.as_json }.compact
  else
    { type: type, next: @next&.as_json }.compact
  end
end

#interpret(_input, _root) ⇒ Object

Interpret the input, return result or forward to next node.

Parameters:

  • _input (Array, Hash)

    the results of processing so far

  • _root (Array, Hash)

    the entire input

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/janeway/interpreters/base.rb', line 38

def interpret(_input, _root)
  raise NotImplementedError, 'subclass must implement #interpret'
end

#selectorAST::Selector

Returns AST node containing this interpreter’s data.

Returns:

  • (AST::Selector)

    AST node containing this interpreter’s data



58
59
60
# File 'lib/janeway/interpreters/base.rb', line 58

def selector
  nil # subclass should implement
end

#to_sString

Returns:

  • (String)


43
44
45
# File 'lib/janeway/interpreters/base.rb', line 43

def to_s
  @node.to_s
end

#typeObject



62
63
64
# File 'lib/janeway/interpreters/base.rb', line 62

def type
  self.class.to_s.split('::').last # eg. Janeway::AST::FunctionCall => "FunctionCall"
end