Class: Rjq::AST::Slice

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

#invalid_path_message, #take, #with_source_span

Constructor Details

#initialize(base, start_node, finish_node, optional: false) ⇒ Slice

Returns a new instance of Slice.



493
494
495
496
497
498
# File 'lib/rjq/ast.rb', line 493

def initialize(base, start_node, finish_node, optional: false)
  @base = base
  @start_node = start_node
  @finish_node = finish_node
  @optional = optional
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



491
492
493
# File 'lib/rjq/ast.rb', line 491

def base
  @base
end

#finish_nodeObject (readonly)

Returns the value of attribute finish_node.



491
492
493
# File 'lib/rjq/ast.rb', line 491

def finish_node
  @finish_node
end

#start_nodeObject (readonly)

Returns the value of attribute start_node.



491
492
493
# File 'lib/rjq/ast.rb', line 491

def start_node
  @start_node
end

Instance Method Details

#assign_value(copy, input, context, value) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/rjq/ast.rb', line 518

def assign_value(copy, input, context, value)
  @base.paths(input, context).each do |path|
    starts = @start_node ? @start_node.eval(input, context) : [nil]
    finishes = @finish_node ? @finish_node.eval(input, context) : [nil]
    starts.each do |start|
      finishes.each do |finish|
        target = Path.get(copy, path)
        copy = Path.set(copy, path, replace_slice(target, start, finish, value))
      end
    end
  end
  copy
end

#eval(input, context) ⇒ Object



500
501
502
503
504
505
506
507
508
# File 'lib/rjq/ast.rb', line 500

def eval(input, context)
  @base.eval(input, context).flat_map do |value|
    starts = @start_node ? @start_node.eval(input, context) : [nil]
    finishes = @finish_node ? @finish_node.eval(input, context) : [nil]
    starts.flat_map { |start| finishes.map { |finish| slice(value, start, finish) } }
  rescue Rjq::RuntimeError
    @optional ? [] : raise
  end
end

#paths(input, context) ⇒ Object



510
511
512
513
514
515
516
# File 'lib/rjq/ast.rb', line 510

def paths(input, context)
  starts = @start_node ? @start_node.eval(input, context) : [nil]
  finishes = @finish_node ? @finish_node.eval(input, context) : [nil]
  @base.paths(input, context).flat_map do |path|
    starts.flat_map { |start| finishes.map { |finish| path + [{ 'start' => start, 'end' => finish }] } }
  end
end