Module: Regexp::Expression::Shared::ClassMethods

Defined in:
lib/regexp_parser/expression/shared.rb,
lib/regexp_parser/expression/methods/construct.rb

Overview

filled in ./methods/*.rb

Instance Method Summary collapse

Instance Method Details

#construct(params = {}) ⇒ Object

Convenience method to init a valid Expression without a Regexp::Token

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
# File 'lib/regexp_parser/expression/methods/construct.rb', line 5

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

#construct_defaultsObject



15
16
17
18
19
20
21
22
23
# File 'lib/regexp_parser/expression/methods/construct.rb', line 15

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

#token_classObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/regexp_parser/expression/methods/construct.rb', line 25

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