class Regexp::Expression::Base

Attributes

conditional_level[RW]
level[RW]
nesting_level[RW]
options[RW]
quantifier[RW]
set_level[RW]
starts_at[RW]
text[RW]
token[RW]
ts[RW]
type[RW]

Public Class Methods

new(token, options = {}) click to toggle source
# File lib/regexp_parser/expression.rb, line 11
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

Public Instance Methods

=~(string, offset = 0)
Alias for: match
a?()
Alias for: ascii_classes?
ascii_classes?() click to toggle source
# File lib/regexp_parser/expression/methods/options.rb, line 25
def ascii_classes?
  options[:a] == true
end
Also aliased as: a?
attributes() click to toggle source
# File lib/regexp_parser/expression.rb, line 108
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
Also aliased as: to_h
base_length() click to toggle source
# File lib/regexp_parser/expression.rb, line 37
def base_length
  to_s(:base).length
end
case_insensitive?() click to toggle source
# File lib/regexp_parser/expression/methods/options.rb, line 8
def case_insensitive?
  options[:i] == true
end
Also aliased as: i?, ignore_case?
coded_offset() click to toggle source
# File lib/regexp_parser/expression.rb, line 49
def coded_offset
  '@%d+%d' % offset
end
d?()
Alias for: default_classes?
default_classes?() click to toggle source
# File lib/regexp_parser/expression/methods/options.rb, line 20
def default_classes?
  options[:d] == true
end
Also aliased as: d?
extended?()
Alias for: free_spacing?
free_spacing?() click to toggle source
# File lib/regexp_parser/expression/methods/options.rb, line 14
def free_spacing?
  options[:x] == true
end
Also aliased as: x?, extended?
full_length() click to toggle source
# File lib/regexp_parser/expression.rb, line 41
def full_length
  to_s.length
end
greedy?() click to toggle source
# File lib/regexp_parser/expression.rb, line 95
def greedy?
  quantified? and quantifier.greedy?
end
i?()
Alias for: case_insensitive?
ignore_case?()
Alias for: case_insensitive?
initialize_clone(orig) click to toggle source
Calls superclass method
# File lib/regexp_parser/expression.rb, line 24
def initialize_clone(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) click to toggle source

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]
# 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
lazy?()
Alias for: reluctant?
m?()
Alias for: multiline?
match(string, offset = 0) click to toggle source
# File lib/regexp_parser/expression/methods/match.rb, line 8
def match(string, offset = 0)
  Regexp.new(to_s).match(string, offset)
end
Also aliased as: =~
match?(string) click to toggle source
# File lib/regexp_parser/expression/methods/match.rb, line 3
def match?(string)
  !!match(string)
end
Also aliased as: matches?
matches?(string)
Alias for: match?
multiline?() click to toggle source
# File lib/regexp_parser/expression/methods/options.rb, line 3
def multiline?
  options[:m] == true
end
Also aliased as: m?
offset() click to toggle source
# File lib/regexp_parser/expression.rb, line 45
def offset
  [starts_at, full_length]
end
one_of?(scope, top = true) click to toggle source

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: :*})
# 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?() click to toggle source
# File lib/regexp_parser/expression.rb, line 104
def possessive?
  quantified? and quantifier.possessive?
end
quantified?() click to toggle source
# File lib/regexp_parser/expression.rb, line 73
def quantified?
  !quantifier.nil?
end
quantifier_affix(expression_format) click to toggle source
# File lib/regexp_parser/expression.rb, line 57
def quantifier_affix(expression_format)
  quantifier.to_s if quantified? && expression_format != :base
end
quantify(token, text, min = nil, max = nil, mode = :greedy) click to toggle source
# File lib/regexp_parser/expression.rb, line 65
def quantify(token, text, min = nil, max = nil, mode = :greedy)
  self.quantifier = Quantifier.new(token, text, min, max, mode)
end
quantity() click to toggle source

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

# File lib/regexp_parser/expression.rb, line 78
def quantity
  return [nil,nil] unless quantified?
  [quantifier.min, quantifier.max]
end
reluctant?() click to toggle source
# File lib/regexp_parser/expression.rb, line 99
def reluctant?
  quantified? and quantifier.reluctant?
end
Also aliased as: lazy?
repetitions() click to toggle source
# File lib/regexp_parser/expression.rb, line 83
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
strfre(format = '%a', indent_offset = 0, index = nil)
Alias for: strfregexp
strfregexp(format = '%a', indent_offset = 0, index = nil) click to toggle source

%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} %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'

# 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
Also aliased as: strfre
terminal?() click to toggle source
# File lib/regexp_parser/expression.rb, line 61
def terminal?
  !respond_to?(:expressions)
end
to_h()
Alias for: attributes
to_re(format = :full) click to toggle source
# File lib/regexp_parser/expression.rb, line 31
def to_re(format = :full)
  ::Regexp.new(to_s(format))
end
to_s(format = :full) click to toggle source
# File lib/regexp_parser/expression.rb, line 53
def to_s(format = :full)
  "#{text}#{quantifier_affix(format)}"
end
type?(test_type) click to toggle source

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]
# 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
u?()
Alias for: unicode_classes?
unicode_classes?() click to toggle source
# File lib/regexp_parser/expression/methods/options.rb, line 30
def unicode_classes?
  options[:u] == true
end
Also aliased as: u?
unquantified_clone() click to toggle source
# File lib/regexp_parser/expression.rb, line 69
def unquantified_clone
  clone.tap { |exp| exp.quantifier = nil }
end
x?()
Alias for: free_spacing?