Module: Regexp::Expression::Shared

Included in:
Base, Quantifier
Defined in:
lib/regexp_parser/expression/shared.rb,
lib/regexp_parser/expression/methods/tests.rb,
lib/regexp_parser/expression/methods/construct.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



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

def self.included(mod)
  mod.class_eval do
    extend Shared::ClassMethods

    attr_accessor :type, :token, :text, :ts, :te,
                  :level, :set_level, :conditional_level,
                  :options, :quantifier

    attr_reader   :nesting_level
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: ===, eql?

Deep-compare two expressions for equality.



98
99
100
101
102
# File 'lib/regexp_parser/expression/methods/tests.rb', line 98

def ==(other)
  other.class == self.class &&
    other.to_s == to_s &&
    other.options == options
end

#base_lengthObject



42
43
44
# File 'lib/regexp_parser/expression/shared.rb', line 42

def base_length
  to_s(:base).length
end

#coded_offsetObject



71
72
73
# File 'lib/regexp_parser/expression/shared.rb', line 71

def coded_offset
  '@%d+%d' % offset
end

#full_lengthObject



46
47
48
# File 'lib/regexp_parser/expression/shared.rb', line 46

def full_length
  to_s.length
end

#initialize_copy(orig) ⇒ Object



31
32
33
34
35
36
# File 'lib/regexp_parser/expression/shared.rb', line 31

def initialize_copy(orig)
  self.text       = orig.text.dup         if orig.text
  self.options    = orig.options.dup      if orig.options
  self.quantifier = orig.quantifier.clone if orig.quantifier
  super
end

#is?(test_token, test_type = nil) ⇒ Boolean

Test if this expression has the given test_token, and optionally a given test_type.

# Any expressions
exp.is? :*  # always returns true

# is it a :capture
exp.is? :capture

# is it a :character and a :set
exp.is? :character, :set

# is it a :meta :dot
exp.is? :dot, :meta

# is it a :meta or :escape :dot
exp.is? :dot, [:meta, :escape]

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/regexp_parser/expression/methods/tests.rb', line 36

def is?(test_token, test_type = nil)
  return true if test_token === :*
  token == test_token and (test_type ? type?(test_type) : true)
end

#nesting_level=(lvl) ⇒ Object



79
80
81
82
83
# File 'lib/regexp_parser/expression/shared.rb', line 79

def nesting_level=(lvl)
  @nesting_level = lvl
  quantifier && quantifier.nesting_level = lvl
  terminal? || each { |subexp| subexp.nesting_level = lvl + 1 }
end

#offsetObject



67
68
69
# File 'lib/regexp_parser/expression/shared.rb', line 67

def offset
  [starts_at, full_length]
end

#one_of?(scope, top = true) ⇒ Boolean

Test if this expression matches an entry in the given scope spec.

A scope spec can be one of:

. An array: Interpreted as a set of tokens, tested for inclusion
            of the expression's token.

. A hash:   Where the key is interpreted as the expression type
            and the value is either a symbol or an array. In this
            case, when the scope is a hash, one_of? calls itself to
            evaluate the key's value.

. A symbol: matches the expression's token or type, depending on
            the level of the call. If one_of? is called directly with
            a symbol then it will always be checked against the
            type of the expression. If it's being called for a value
            from a hash, it will be checked against the token of the
            expression.

# any expression
exp.one_of?(:*) # always true

# like exp.type?(:group)
exp.one_of?(:group)

# any expression of type meta
exp.one_of?(:meta => :*)

# meta dots and alternations
exp.one_of?(:meta => [:dot, :alternation])

# meta dots and any set tokens
exp.one_of?({meta: [:dot], set: :*})

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/regexp_parser/expression/methods/tests.rb', line 75

def one_of?(scope, top = true)
  case scope
  when Array
    scope.include?(:*) || scope.include?(token)

  when Hash
    if scope.has_key?(:*)
      test_type = scope.has_key?(type) ? type : :*
      one_of?(scope[test_type], false)
    else
      scope.has_key?(type) && one_of?(scope[type], false)
    end

  when Symbol
    scope.equal?(:*) || (top ? type?(scope) : is?(scope))

  else
    raise ArgumentError,
          "Array, Hash, or Symbol expected, #{scope.class.name} given"
  end
end

#partsObject



55
56
57
# File 'lib/regexp_parser/expression/shared.rb', line 55

def parts
  [text.dup]
end

#quantified?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/regexp_parser/expression/shared.rb', line 63

def quantified?
  !quantifier.nil?
end

#quantifier_affix(expression_format) ⇒ Object



59
60
61
# File 'lib/regexp_parser/expression/shared.rb', line 59

def quantifier_affix(expression_format)
  quantifier.to_s if quantified? && expression_format != :base
end

#starts_atObject



38
39
40
# File 'lib/regexp_parser/expression/shared.rb', line 38

def starts_at
  ts
end

#terminal?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/regexp_parser/expression/shared.rb', line 75

def terminal?
  !respond_to?(:expressions)
end

#to_s(format = :full) ⇒ Object Also known as: to_str



50
51
52
# File 'lib/regexp_parser/expression/shared.rb', line 50

def to_s(format = :full)
  "#{parts.join}#{quantifier_affix(format)}"
end

#token_classObject



39
40
41
# File 'lib/regexp_parser/expression/methods/construct.rb', line 39

def token_class
  self.class.token_class
end

#type?(test_type) ⇒ Boolean

Test if this expression has the given test_type, which can be either a symbol or an array of symbols to check against the expression’s type.

# is it a :group expression
exp.type? :group

# is it a :set, or :meta
exp.type? [:set, :meta]

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/regexp_parser/expression/methods/tests.rb', line 13

def type?(test_type)
  test_types = Array(test_type).map(&:to_sym)
  test_types.include?(:*) || test_types.include?(type)
end