Sha256: 51119f5d26e12fb3aab4fb01b05d5ee6bba51fdacde880d5ef28003cf74e1b2a

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

begin
  require 'awesome_print'
  require 'pry'
rescue LoadError; end

require 'regexp_parser'

module EcmaReValidator

  # JS doesn't have Unicode matching
  UNICODE_CHARACTERS = Regexp::Syntax::Token::UnicodeProperty::All

  INVALID_REGEXP = [
    # JS doesn't have \A or \Z
    :bos, :eos_ob_eol,
    # JS doesn't have lookbehinds
    :lookbehind, :nlookbehind,
    # JS doesn't have atomic grouping
    :atomic,
    # JS doesn't have possesive quantifiers
    :zero_or_one_possessive, :zero_or_more_possessive, :one_or_more_possessive,
    # JS doesn't have named capture groups
    :named_ab, :named_sq,
    # JS doesn't support modifying options
    :options,
    # JS doesn't support conditionals
    :condition_open,
    # JS doesn't support comments
    :comment
  ]

  INVALID_TOKENS = INVALID_REGEXP + UNICODE_CHARACTERS

  def self.valid?(input)
    if input.is_a? String
      begin
        input = Regexp.new(input)
      rescue RegexpError
        return false
      end
    elsif !input.is_a? Regexp
      return false
    end

    Regexp::Scanner.scan(input).none? { |t| INVALID_TOKENS.include?(t[1]) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecma-re-validator-0.1.0 lib/ecma-re-validator.rb