Class: Regexp::Expression::Sequence

Inherits:
Subexpression show all
Defined in:
lib/regexp_parser/expression/sequence.rb

Overview

A sequence of expressions. Differs from a Subexpressions by how it handles quantifiers, as it applies them to its last element instead of itself as a whole subexpression.

Used as the base class for the Alternation alternatives, Conditional branches, and CharacterSet::Intersection intersected sequences.

Instance Attribute Summary

Attributes inherited from Subexpression

#expressions

Attributes inherited from Base

#conditional_level, #level, #nesting_level, #options, #quantifier, #set_level, #text, #token, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Subexpression

#<<, #dig, #each_expression, #flat_map, #initialize, #initialize_copy, #inner_match_length, #match_length, #strfregexp_tree, #te, #to_h, #to_s, #traverse

Methods inherited from Base

#ascii_classes?, #attributes, #base_length, #case_insensitive?, #coded_offset, #default_classes?, #free_spacing?, #full_length, #greedy?, #initialize, #initialize_copy, #is?, #match, #match?, #multiline?, #offset, #one_of?, #possessive?, #quantified?, #quantifier_affix, #quantity, #reluctant?, #repetitions, #strfregexp, #terminal?, #to_re, #to_s, #type?, #unicode_classes?, #unquantified_clone

Constructor Details

This class inherits a constructor from Regexp::Expression::Subexpression

Class Method Details

.add_to(subexpression, params = {}, active_opts = {}) ⇒ Object



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

def add_to(subexpression, params = {}, active_opts = {})
  sequence = at_levels(
    subexpression.level,
    subexpression.set_level,
    params[:conditional_level] || subexpression.conditional_level
  )
  sequence.nesting_level = subexpression.nesting_level + 1
  sequence.options = active_opts
  subexpression.expressions << sequence
  sequence
end

.at_levels(level, set_level, conditional_level) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/regexp_parser/expression/sequence.rb', line 22

def at_levels(level, set_level, conditional_level)
  token = Regexp::Token.new(
    :expression,
    :sequence,
    '',
    nil, # ts
    nil, # te
    level,
    set_level,
    conditional_level
  )
  new(token)
end

Instance Method Details

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



42
43
44
45
46
47
48
# File 'lib/regexp_parser/expression/sequence.rb', line 42

def quantify(token, text, min = nil, max = nil, mode = :greedy)
  target = expressions.reverse.find { |exp| !exp.is_a?(FreeSpace) }
  target or raise Regexp::Parser::Error,
    "No valid target found for '#{text}' quantifier"

  target.quantify(token, text, min, max, mode)
end

#starts_atObject Also known as: ts



37
38
39
# File 'lib/regexp_parser/expression/sequence.rb', line 37

def starts_at
  expressions.first.starts_at
end