Class: Regexp::Expression::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/regexp_parser/expression/base.rb,
lib/regexp_parser/expression/methods/match.rb,
lib/regexp_parser/expression/methods/tests.rb,
lib/regexp_parser/expression/methods/options.rb,
lib/regexp_parser/expression/methods/strfregexp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/regexp_parser/expression/base.rb', line 10

def initialize(token, options = {})
  self.type              = token.type
  self.token             = token.token
  self.text              = token.text
  self.ts                = token.ts
  self.level             = token.level
  self.set_level         = token.set_level
  self.conditional_level = token.conditional_level
  self.nesting_level     = 0
  self.quantifier        = nil
  self.options           = options
end

Instance Attribute Details

#conditional_levelObject

Returns the value of attribute conditional_level.



5
6
7
# File 'lib/regexp_parser/expression/base.rb', line 5

def conditional_level
  @conditional_level
end

#levelObject

Returns the value of attribute level.



5
6
7
# File 'lib/regexp_parser/expression/base.rb', line 5

def level
  @level
end

#nesting_levelObject

Returns the value of attribute nesting_level.



5
6
7
# File 'lib/regexp_parser/expression/base.rb', line 5

def nesting_level
  @nesting_level
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/regexp_parser/expression/base.rb', line 8

def options
  @options
end

#quantifierObject

Returns the value of attribute quantifier.



7
8
9
# File 'lib/regexp_parser/expression/base.rb', line 7

def quantifier
  @quantifier
end

#set_levelObject

Returns the value of attribute set_level.



5
6
7
# File 'lib/regexp_parser/expression/base.rb', line 5

def set_level
  @set_level
end

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/regexp_parser/expression/base.rb', line 4

def text
  @text
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/regexp_parser/expression/base.rb', line 3

def token
  @token
end

#tsObject Also known as: starts_at

Returns the value of attribute ts.



4
5
6
# File 'lib/regexp_parser/expression/base.rb', line 4

def ts
  @ts
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/regexp_parser/expression/base.rb', line 3

def type
  @type
end

Instance Method Details

#ascii_classes?Boolean Also known as: a?

Returns:

  • (Boolean)


25
26
27
# File 'lib/regexp_parser/expression/methods/options.rb', line 25

def ascii_classes?
  options[:a] == true
end

#attributesObject Also known as: to_h



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/regexp_parser/expression/base.rb', line 107

def attributes
  {
    type:              type,
    token:             token,
    text:              to_s(:base),
    starts_at:         ts,
    length:            full_length,
    level:             level,
    set_level:         set_level,
    conditional_level: conditional_level,
    options:           options,
    quantifier:        quantified? ? quantifier.to_h : nil,
  }
end

#base_lengthObject



36
37
38
# File 'lib/regexp_parser/expression/base.rb', line 36

def base_length
  to_s(:base).length
end

#case_insensitive?Boolean Also known as: i?, ignore_case?

Returns:

  • (Boolean)


8
9
10
# File 'lib/regexp_parser/expression/methods/options.rb', line 8

def case_insensitive?
  options[:i] == true
end

#coded_offsetObject



48
49
50
# File 'lib/regexp_parser/expression/base.rb', line 48

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

#default_classes?Boolean Also known as: d?

Returns:

  • (Boolean)


20
21
22
# File 'lib/regexp_parser/expression/methods/options.rb', line 20

def default_classes?
  options[:d] == true
end

#free_spacing?Boolean Also known as: x?, extended?

Returns:

  • (Boolean)


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

def free_spacing?
  options[:x] == true
end

#full_lengthObject



40
41
42
# File 'lib/regexp_parser/expression/base.rb', line 40

def full_length
  to_s.length
end

#greedy?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/regexp_parser/expression/base.rb', line 94

def greedy?
  quantified? and quantifier.greedy?
end

#initialize_copy(orig) ⇒ Object



23
24
25
26
27
28
# File 'lib/regexp_parser/expression/base.rb', line 23

def initialize_copy(orig)
  self.text       = (orig.text       ? orig.text.dup         : nil)
  self.options    = (orig.options    ? orig.options.dup      : nil)
  self.quantifier = (orig.quantifier ? orig.quantifier.clone : nil)
  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

#match(string, offset = 0) ⇒ Object Also known as: =~



8
9
10
# File 'lib/regexp_parser/expression/methods/match.rb', line 8

def match(string, offset = 0)
  Regexp.new(to_s).match(string, offset)
end

#match?(string) ⇒ Boolean Also known as: matches?

Returns:

  • (Boolean)


3
4
5
# File 'lib/regexp_parser/expression/methods/match.rb', line 3

def match?(string)
  !!match(string)
end

#multiline?Boolean Also known as: m?

Returns:

  • (Boolean)


3
4
5
# File 'lib/regexp_parser/expression/methods/options.rb', line 3

def multiline?
  options[:m] == true
end

#offsetObject



44
45
46
# File 'lib/regexp_parser/expression/base.rb', line 44

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

#possessive?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/regexp_parser/expression/base.rb', line 103

def possessive?
  quantified? and quantifier.possessive?
end

#quantified?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/regexp_parser/expression/base.rb', line 72

def quantified?
  !quantifier.nil?
end

#quantifier_affix(expression_format) ⇒ Object



56
57
58
# File 'lib/regexp_parser/expression/base.rb', line 56

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

#quantify(token, text, min = nil, max = nil, mode = :greedy) ⇒ Object



64
65
66
# File 'lib/regexp_parser/expression/base.rb', line 64

def quantify(token, text, min = nil, max = nil, mode = :greedy)
  self.quantifier = Quantifier.new(token, text, min, max, mode)
end

#quantityObject

Deprecated. Prefer `#repetitions` which has a more uniform interface.



77
78
79
80
# File 'lib/regexp_parser/expression/base.rb', line 77

def quantity
  return [nil,nil] unless quantified?
  [quantifier.min, quantifier.max]
end

#reluctant?Boolean Also known as: lazy?

Returns:

  • (Boolean)


98
99
100
# File 'lib/regexp_parser/expression/base.rb', line 98

def reluctant?
  quantified? and quantifier.reluctant?
end

#repetitionsObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/regexp_parser/expression/base.rb', line 82

def repetitions
  return 1..1 unless quantified?
  min = quantifier.min
  max = quantifier.max < 0 ? Float::INFINITY : quantifier.max
  range = min..max
  # fix Range#minmax on old Rubies - https://bugs.ruby-lang.org/issues/15807
  if RUBY_VERSION.to_f < 2.7
    range.define_singleton_method(:minmax) { [min, max] }
  end
  range
end

#strfregexp(format = '%a', indent_offset = 0, index = nil) ⇒ Object Also known as: strfre

%l Level (depth) of the expression. Returns 'root' for the root

    expression, returns zero or higher for all others.

%>  Indentation at expression's level.

%x  Index of the expression at its depth. Available when using
    the sprintf_tree method only.

%s  Start offset within the whole expression.
%e  End offset within the whole expression.
%S  Length of expression.

%o  Coded offset and length, same as '@%s+%S'

%y  Type of expression.
%k  Token of expression.
%i  ID, same as '%y:%k'
%c  Class name

%q  Quantifier info, as {m[,M]}
%Q  Quantifier text

%z  Quantifier min
%Z  Quantifier max

%t  Base text of the expression (excludes quantifier, if any)
%~t Full text if the expression is terminal, otherwise %i
%T  Full text of the expression (includes quantifier, if any)

%b  Basic info, same as '%o %i'
%m  Most info, same as '%b %q'
%a  All info, same as '%m %t'


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/regexp_parser/expression/methods/strfregexp.rb', line 37

def strfregexp(format = '%a', indent_offset = 0, index = nil)
  have_index    = index ? true : false

  part = {}

  print_level = nesting_level > 0 ? nesting_level - 1 : nil

  # Order is important! Fields that use other fields in their
  # definition must appear before the fields they use.
  part_keys = %w[a m b o i l x s e S y k c q Q z Z t ~t T >]
  part.keys.each {|k| part[k] = "<?#{k}?>"}

  part['>'] = print_level ? ('  ' * (print_level + indent_offset)) : ''

  part['l'] = print_level ? "#{'%d' % print_level}" : 'root'
  part['x'] = "#{'%d' % index}" if have_index

  part['s'] = starts_at
  part['S'] = full_length
  part['e'] = starts_at + full_length
  part['o'] = coded_offset

  part['k'] = token
  part['y'] = type
  part['i'] = '%y:%k'
  part['c'] = self.class.name

  if quantified?
    if quantifier.max == -1
      part['q'] = "{#{quantifier.min}, or-more}"
    else
      part['q'] = "{#{quantifier.min}, #{quantifier.max}}"
    end

    part['Q'] = quantifier.text
    part['z'] = quantifier.min
    part['Z'] = quantifier.max
  else
    part['q'] = '{1}'
    part['Q'] = ''
    part['z'] = '1'
    part['Z'] = '1'
  end

  part['t'] = to_s(:base)
  part['~t'] = terminal? ? to_s : "#{type}:#{token}"
  part['T'] = to_s(:full)

  part['b'] = '%o %i'
  part['m'] = '%b %q'
  part['a'] = '%m %t'

  out = format.dup

  part_keys.each do |k|
    out.gsub!(/%#{k}/, part[k].to_s)
  end

  out
end

#terminal?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/regexp_parser/expression/base.rb', line 60

def terminal?
  !respond_to?(:expressions)
end

#to_re(format = :full) ⇒ Object



30
31
32
# File 'lib/regexp_parser/expression/base.rb', line 30

def to_re(format = :full)
  ::Regexp.new(to_s(format))
end

#to_s(format = :full) ⇒ Object



52
53
54
# File 'lib/regexp_parser/expression/base.rb', line 52

def to_s(format = :full)
  "#{text}#{quantifier_affix(format)}"
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

#unicode_classes?Boolean Also known as: u?

Returns:

  • (Boolean)


30
31
32
# File 'lib/regexp_parser/expression/methods/options.rb', line 30

def unicode_classes?
  options[:u] == true
end

#unquantified_cloneObject



68
69
70
# File 'lib/regexp_parser/expression/base.rb', line 68

def unquantified_clone
  clone.tap { |exp| exp.quantifier = nil }
end