Class: Fop::Compiler::Instructions::ExpressionMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/fop/compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ ExpressionMatch

Returns a new instance of ExpressionMatch.



42
43
44
45
46
47
48
49
# File 'lib/fop/compiler.rb', line 42

def initialize(node)
  @regex = node.regex&.regex
  @op = node.operator_token ? OPERATIONS.fetch(node.operator_token.val) : nil
  @regex_match = node.regex_match
  @args = node.args&.map { |arg|
    arg.has_captures ? arg.segments : arg.segments.join("")
  }
end

Instance Method Details

#call(input) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fop/compiler.rb', line 51

def call(input)
  if (match = @regex.match(input))
    val = match.to_s
    blank = val == BLANK
    input.sub!(val, BLANK) unless blank
    found_val = @regex_match || !blank
    if @op and @args and found_val
      args = @args.map { |arg|
        case arg
        when String then arg
        when Array then sub_caps(arg, match.captures)
        else raise "Unexpected arg type #{arg.class.name}"
        end
      }
      @op.proc.call(val, args)
    else
      val
    end
  end
end