Method: JSONSchemer::EcmaRegexp.ruby_equivalent

Defined in:
lib/json_schemer/ecma_regexp.rb

.ruby_equivalent(pattern) ⇒ Object


39
40
41
42
43
44
45
46
47
48
# File 'lib/json_schemer/ecma_regexp.rb', line 39

def ruby_equivalent(pattern)
  Regexp::Scanner.scan(pattern).map do |type, token, text|
    Syntax::SYNTAX.check!(*Syntax::SYNTAX.normalize(type, token))
    RUBY_EQUIVALENTS.dig(type, token) || text
  rescue Regexp::Syntax::NotImplementedError
    raise InvalidEcmaRegexp, "invalid token #{text.inspect} (#{type}:#{token}) in #{pattern.inspect}"
  end.join
rescue Regexp::Scanner::ScannerError
  raise InvalidEcmaRegexp, "invalid pattern #{pattern.inspect}"
end