Sha256: f16da792aa188d486fb9819585a241fdf443250a6d93429d7089f25a1ff38291

Contents?: true

Size: 1.37 KB

Versions: 15

Compression:

Stored size: 1.37 KB

Contents

module Regexp::Expression
  module Shared
    module ClassMethods
      # Convenience method to init a valid Expression without a Regexp::Token
      def construct(params = {})
        attrs = construct_defaults.merge(params)
        options = attrs.delete(:options)
        token_args = Regexp::TOKEN_KEYS.map { |k| attrs.delete(k) }
        token = Regexp::Token.new(*token_args)
        raise ArgumentError, "unsupported attribute(s): #{attrs}" if attrs.any?

        new(token, options)
      end

      def construct_defaults
        if self == Root
          { type: :expression, token: :root, ts: 0 }
        elsif self < Sequence
          { type: :expression, token: :sequence }
        else
          { type: token_class::Type }
        end.merge(level: 0, set_level: 0, conditional_level: 0, text: '')
      end

      def token_class
        if self == Root || self < Sequence
          nil # no token class because these objects are Parser-generated
        # TODO: synch exp & token class names for alt., dot, escapes in v3.0.0
        elsif self == Alternation || self == CharacterType::Any
          Regexp::Syntax::Token::Meta
        elsif self <= EscapeSequence::Base
          Regexp::Syntax::Token::Escape
        else
          Regexp::Syntax::Token.const_get(name.split('::')[2])
        end
      end
    end

    def token_class
      self.class.token_class
    end
  end
end

Version data entries

15 entries across 15 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.6.1/lib/regexp_parser/expression/methods/construct.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.6.1/lib/regexp_parser/expression/methods/construct.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/regexp_parser-2.6.1/lib/regexp_parser/expression/methods/construct.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/methods/construct.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/methods/construct.rb
call_your_name-0.1.0 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.7.0/lib/regexp_parser/expression/methods/construct.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.7.0/lib/regexp_parser/expression/methods/construct.rb
regexp_parser-2.7.0 lib/regexp_parser/expression/methods/construct.rb
regexp_parser-2.6.2 lib/regexp_parser/expression/methods/construct.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/methods/construct.rb
regexp_parser-2.6.1 lib/regexp_parser/expression/methods/construct.rb
regexp_parser-2.6.0 lib/regexp_parser/expression/methods/construct.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/methods/construct.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/regexp_parser-2.5.0/lib/regexp_parser/expression/methods/construct.rb
regexp_parser-2.5.0 lib/regexp_parser/expression/methods/construct.rb