Class: Rjq::AST::Binding

Inherits:
Node
  • Object
show all
Defined in:
lib/rjq/ast.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#source_span

Instance Method Summary collapse

Methods inherited from Node

#assign_value, #invalid_path_message, #take, #with_source_span

Constructor Details

#initialize(source, pattern, body) ⇒ Binding

Returns a new instance of Binding.



362
363
364
365
366
# File 'lib/rjq/ast.rb', line 362

def initialize(source, pattern, body)
  @source = source
  @pattern = pattern
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



360
361
362
# File 'lib/rjq/ast.rb', line 360

def body
  @body
end

#patternObject (readonly)

Returns the value of attribute pattern.



360
361
362
# File 'lib/rjq/ast.rb', line 360

def pattern
  @pattern
end

#sourceObject (readonly)

Returns the value of attribute source.



360
361
362
# File 'lib/rjq/ast.rb', line 360

def source
  @source
end

Instance Method Details

#eval(input, context) ⇒ Object



368
369
370
371
372
373
# File 'lib/rjq/ast.rb', line 368

def eval(input, context)
  @source.eval(input, context).flat_map do |value|
    bound = AST.bind_pattern(context, @pattern, value)
    bound ? @body.eval(input, bound) : []
  end
end

#paths(input, context) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/rjq/ast.rb', line 375

def paths(input, context)
  values = @source.eval(input, context)
  values.flat_map do |value|
    bound, pattern_path, relative_variables = AST.bind_pattern_with_path(context, @pattern, value)
    base_path = context.current_path + pattern_path
    variable_paths = context.binding_variable_paths.to_h { |name, _path| [name, base_path] }
    variable_paths.merge!(relative_variables.transform_values { |path| context.current_path + path })
    AST.validate_pattern_path(input, base_path)
    scoped = bound.with_path_state(current_path: base_path, variables: variable_paths)
    AST.validate_binding_results(input, @body.eval(input, scoped), @body.paths(input, scoped),
                                 alternative_base: base_path,
                                 variable_paths: variable_paths.values,
                                 alternative_paths: AST.alternative_paths(@pattern))
  end
end