class Regexp::Expression::Base
def ascii_classes?
def ascii_classes? (@options and @options[:a]) ? true : false end
def case_insensitive?
def case_insensitive? (@options and @options[:i]) ? true : false end
def clone
def clone copy = self.dup copy.text = (self.text ? self.text.dup : nil) copy.options = (self.options ? self.options.dup : nil) copy.quantifier = (self.quantifier ? self.quantifier.clone : nil) copy end
def coded_offset
def coded_offset '@%d+%d' % offset end
def default_classes?
def default_classes? (@options and @options[:d]) ? true : false end
def free_spacing?
def free_spacing? (@options and @options[:x]) ? true : false end
def full_length
def full_length to_s.length end
def greedy?
def greedy? quantified? and @quantifier.mode == :greedy end
def initialize(token)
def initialize(token) @type = token.type @token = token.token @text = token.text @ts = token.ts @level = token.level @set_level = token.set_level @conditional_level = token.conditional_level @quantifier = nil @options = nil end
def is?(test_token, test_type = nil)
exp.is? :dot, [:meta, :escape]
# is it a :meta or :escape :dot
exp.is? :dot, :meta
# is it a :meta :dot
exp.is? :character, :set
# is it a :character and a :set
exp.is? :capture
# is it a :capture
exp.is? :* # always returns true
# Any expressions
test_type.
Test if this expression has the given test_token, and optionally a given
def is?(test_token, test_type = nil) return true if test_token === :* token == test_token and (test_type ? type?(test_type) : true) end
def match(string, offset)
def match(string, offset) Regexp.new(to_s).match(string, offset) end
def matches?(string)
def matches?(string) Regexp.new(to_s) =~ string ? true : false end
def multiline?
def multiline? (@options and @options[:m]) ? true : false end
def offset
def offset [starts_at, full_length] end
def one_of?(scope, top = true)
exp.one_of?({meta: [:dot], set: :*})
# meta dots and any set tokens
exp.one_of?(:meta => [:dot, :alternation])
# meta dots and alternations
exp.one_of?(:meta => :*)
# any expression of type meta
exp.one_of?(:group)
# like exp.type?(:group)
exp.one_of?(:*) # always true
# any expression
expression.
from a hash, it will be checked against the token of the
type of the expression. If it's being called for a value
a symbol then it will always be checked against the
the level of the call. If one_of? is called directly with
. A symbol: matches the expression's token or type, depending on
evaluate the key's value.
case, when the scope is a hash, one_of? calls itself to
and the value is either a symbol or an array. In this
. A hash: Where the key is interpreted as the expression type
of the expression's token.
. An array: Interpreted as a set of tokens, tested for inclusion
A scope spec can be one of:
Test if this expression matches an entry in the given scope spec.
def one_of?(scope, top = true) case scope when Array if scope.include?(:*) return (scope.include?(token) or scope.include?(:*)) else return scope.include?(token) end when Hash if scope.has_key?(:*) test_type = scope.has_key?(type) ? type : :* return one_of?(scope[test_type], false) else return (scope.has_key?(type) and one_of?(scope[type], false)) end when Symbol return true if scope == :* return is?(scope) unless top return type?(scope) if top else raise "Array, Hash, or Symbol expected, #{scope.class.name} given" end false end
def possessive?
def possessive? quantified? and @quantifier.mode == :possessive end
def quantified?
def quantified? not @quantifier.nil? end
def quantify(token, text, min = nil, max = nil, mode = :greedy)
def quantify(token, text, min = nil, max = nil, mode = :greedy) @quantifier = Quantifier.new(token, text, min, max, mode) end
def quantity
def quantity return [nil,nil] unless quantified? [@quantifier.min, @quantifier.max] end
def reluctant?
def reluctant? quantified? and @quantifier.mode == :reluctant end
def starts_at
def starts_at @ts end
def strfregexp(format = '%a', indent_offset = 0, index = nil)
%a All info, same as '%m %t'
%m Most info, same as '%b %q'
%b Basic info, same as '%o %i'
%T Full text of the expression (includes quantifier, if any)
%~t Full text if the expression is terminal, otherwise %i
%t Base text of the expression (excludes quantifier, if any)
%Z Quantifier max
%z Quantifier min
%Q Quantifier text
%q Quantifier info, as {m[,M]}
%c Class name
%i ID, same as '%y:%k'
%k Token of expression.
%y Type of expression.
%o Coded offset and length, same as '@%s+%S'
%S Length of expression.
%e End offset within the whole expression.
%s Start offset within the whole expression.
the sprintf_tree method only.
%x Index of the expression at its depth. Available when using
%> Indentation at expression's level.
expression, returns zero or higher for all others.
%l Level (depth) of the expression. Returns 'root' for the root
def strfregexp(format = '%a', indent_offset = 0, index = nil) have_index = index ? true : false part = {} # 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['>'] = level ? (' ' * (level + indent_offset)) : '' part['l'] = level ? "#{'%d' % 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
def terminal?
def terminal? !respond_to?(:expressions) end
def to_h
def to_h { :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
def to_re(format = :full)
def to_re(format = :full) ::Regexp.new(to_s(format)) end
def to_s(format = :full)
def to_s(format = :full) s = '' case format when :base s << @text.dup else s << @text.dup s << @quantifier if quantified? end s end
def type?(test_type)
exp.type? [:set, :subset, :meta]
# is it a :set, :subset, or :meta
exp.type? :group
# is it a :group expression
a symbol or an array of symbols to check against the expression's type.
Test if this expression has the given test_type, which can be either
def type?(test_type) case test_type when Array if test_type.include?(:*) return (test_type.include?(type) or test_type.include?(:*)) else return test_type.include?(type) end when Symbol return (type == test_type or test_type == :*) else raise "Array or Symbol expected, #{test_type.class.name} given" end end
def unicode_classes?
def unicode_classes? (@options and @options[:u]) ? true : false end