Class: Rox::Core::RegularExpressionExtensions

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/roxx/regular_expression_extensions.rb

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ RegularExpressionExtensions

Returns a new instance of RegularExpressionExtensions.



4
5
6
# File 'lib/rox/core/roxx/regular_expression_extensions.rb', line 4

def initialize(parser)
  @parser = parser
end

Instance Method Details

#extendObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rox/core/roxx/regular_expression_extensions.rb', line 8

def extend
  @parser.add_operator('match') do |_parser, stack, _context|
    text = stack.pop
    pattern = stack.pop
    flags = stack.pop
    unless text.is_a?(String) && pattern.is_a?(String) && flags.is_a?(String)
      raise ArgumentError,
            'should be string'
    end

    options = 0
    flags.each_char do |flag|
      case flag
      when 'i'
        options |= Regexp::IGNORECASE
      when 'x'
        options |= Regexp::EXTENDED
      when 'm'
        options |= Regexp::MULTILINE
      end
    end

    matched = !Regexp.new(pattern, options).match(text).nil?
    stack.push(matched)
  end
end