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 and Conditional branches.

Instance Attribute Summary

Attributes inherited from Subexpression

#expressions

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Subexpression

#<<, #each_expression, #flat_map, #initialize_clone, #strfregexp_tree, #te, #to_h, #to_s, #traverse

Methods inherited from Base

#ascii_classes?, #case_insensitive?, #coded_offset, #default_classes?, #free_spacing?, #full_length, #greedy?, #initialize_clone, #is?, #match, #matches?, #multiline?, #offset, #one_of?, #possessive?, #quantified?, #quantifier_affix, #quantity, #reluctant?, #strfregexp, #terminal?, #to_h, #to_re, #to_s, #type?, #unicode_classes?

Constructor Details

#initialize(level, set_level, conditional_level) ⇒ Sequence

Returns a new instance of Sequence.



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

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

Instance Method Details

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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/regexp_parser/expression/sequence.rb', line 32

def quantify(token, text, min = nil, max = nil, mode = :greedy)
  offset = -1
  target = expressions[offset]
  while target.is_a?(FreeSpace)
    target = expressions[offset -= 1]
  end

  target || raise(ArgumentError, "No valid target found for '#{text}' "\
                                 'quantifier')

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

#starts_atObject Also known as: ts



27
28
29
# File 'lib/regexp_parser/expression/sequence.rb', line 27

def starts_at
  expressions.first.starts_at
end

#textObject



23
24
25
# File 'lib/regexp_parser/expression/sequence.rb', line 23

def text
  to_s
end