Class: Janeway::AST::Function

Inherits:
Expression show all
Defined in:
lib/janeway/ast/function.rb

Overview

Represents a JSONPath built-in function.

This is pure structure: name + parameters. The body / literal_return metadata lives in Functions::REGISTRY (looked up by name), so AST nodes don't drag closures over the parser's binding.

Instance Attribute Summary collapse

Attributes inherited from Expression

#next, #value

Instance Method Summary collapse

Methods inherited from Expression

#chain_of?, #indented, #type, type_name

Constructor Details

#initialize(name, parameters) ⇒ Function

Returns a new instance of Function.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/janeway/ast/function.rb', line 17

def initialize(name, parameters)
  raise ArgumentError, "expect string, got #{name.inspect}" unless name.is_a?(String)

  spec = Functions::REGISTRY[name]
  raise ArgumentError, "unknown function #{name.inspect}" unless spec
  unless spec[:arity] == parameters.size
    raise ArgumentError,
          "function #{name.inspect}: declared arity #{spec[:arity]} does not " \
          "match parameter count #{parameters.size}"
  end

  super(name)
  @parameters = parameters
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

Instance Method Details

#literal?Boolean

True if the function's return value is a literal

Returns:



51
52
53
# File 'lib/janeway/ast/function.rb', line 51

def literal?
  Functions::REGISTRY.fetch(name)[:literal_return]
end

#singular_query?Boolean

True if this is the root of a singular-query.



46
47
48
# File 'lib/janeway/ast/function.rb', line 46

def singular_query?
  true
end

#to_sObject



32
33
34
# File 'lib/janeway/ast/function.rb', line 32

def to_s
  "#{name}(#{@parameters.join(',')})"
end

#tree(level) ⇒ Array

Parameters:

  • level (Integer)

Returns:

  • (Array)


38
39
40
# File 'lib/janeway/ast/function.rb', line 38

def tree(level)
  [indented(level, to_s)]
end