Class: JSONSchemer::EcmaRegexp

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schemer/ecma_regexp.rb

Defined Under Namespace

Classes: Syntax

Constant Summary collapse

RUBY_EQUIVALENTS =
{
  :anchor => {
    :bol => '\A',
    :eol => '\z'
  },
  :type => {
    :space => '[\t\r\n\f\v\uFEFF\u2029\p{Zs}]',
    :nonspace => '[^\t\r\n\f\v\uFEFF\u2029\p{Zs}]'
  }
}.freeze

Class Method Summary collapse

Class Method Details

.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