Module: Peruby::RegexpCompiler

Defined in:
lib/peruby/runtime/regexp_compiler.rb

Overview

Converts the Perl regexp differences that Ruby's Onigmo does not share.

Class Method Summary collapse

Class Method Details

.compile(pattern, modifiers = '') ⇒ Object



8
9
10
11
12
13
# File 'lib/peruby/runtime/regexp_compiler.rb', line 8

def compile(pattern, modifiers = '')
  @cache ||= {}
  @cache[[pattern, modifiers]] ||= Regexp.new(*transform(pattern, modifiers))
rescue RegexpError => e
  raise CompileError, e.message
end

.transform(pattern, modifiers = '') ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/peruby/runtime/regexp_compiler.rb', line 15

def transform(pattern, modifiers = '')
  raise CompileError, 'Regexp code execution is not supported' if pattern.match?(/\(\?\??\{/)

  pattern = pattern.gsub(/\(\?\^([isx]*):/) { "(?#{Regexp.last_match(1).tr('s', 'm')}:" }
  source = rewrite(pattern, multiline: modifiers.include?('m'))
  source = extended_character_classes(source) if modifiers.include?('xx')
  source = ascii_classes(source) if modifiers.include?('a')
  options = 0
  options |= Regexp::IGNORECASE if modifiers.include?('i')
  options |= Regexp::EXTENDED if modifiers.include?('x')
  options |= Regexp::MULTILINE if modifiers.include?('s')
  [source, options]
end