Class: Regexp::Expression::Conditional::Expression

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

Instance Attribute Summary

Attributes inherited from Subexpression

#expressions

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Subexpression

#[], #all?, #clone, #each, #each_expression, #each_with_index, #empty?, #first, #insert, #last, #length, #map, #strfregexp_tree, #te, #to_h, #traverse, #ts

Methods inherited from Base

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

Constructor Details

#initialize(token) ⇒ Expression

Returns a new instance of Expression.



14
15
16
17
18
19
# File 'lib/regexp_parser/expression/classes/conditional.rb', line 14

def initialize(token)
  super(token)

  @condition = nil
  @branches  = []
end

Instance Method Details

#<<(exp) ⇒ Object



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

def <<(exp)
  @expressions.last << exp
end

#branch(exp = nil) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
# File 'lib/regexp_parser/expression/classes/conditional.rb', line 31

def branch(exp = nil)
  raise TooManyBranches.new if @branches.length == 2

  sequence = Branch.new(level, set_level, conditional_level + 1)

  @expressions << sequence
  @branches << @expressions.last
end

#branchesObject



40
41
42
# File 'lib/regexp_parser/expression/classes/conditional.rb', line 40

def branches
  @branches
end

#condition(exp = nil) ⇒ Object



21
22
23
24
25
# File 'lib/regexp_parser/expression/classes/conditional.rb', line 21

def condition(exp = nil)
  return @condition unless exp
  @condition = exp
  @expressions << exp
end

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



44
45
46
# File 'lib/regexp_parser/expression/classes/conditional.rb', line 44

def quantify(token, text, min = nil, max = nil, mode = :greedy)
  branches.last.last.quantify(token, text, min, max, mode)
end

#to_sObject



48
49
50
51
52
53
# File 'lib/regexp_parser/expression/classes/conditional.rb', line 48

def to_s
  s = @text.dup
  s << @condition.text
  s << branches.map{|e| e.to_s}.join('|')
  s << ')'
end