class Regexp::Expression::Base

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