Class: Regexp::Expression::Subexpression

Inherits:
Base
  • Object
show all
Defined in:
lib/regexp_parser/expression/subexpression.rb,
lib/regexp_parser/expression/methods/traverse.rb,
lib/regexp_parser/expression/methods/strfregexp.rb

Instance Attribute Summary collapse

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(token) ⇒ Subexpression

Returns a new instance of Subexpression.



6
7
8
9
10
# File 'lib/regexp_parser/expression/subexpression.rb', line 6

def initialize(token)
  super(token)

  @expressions = []
end

Instance Attribute Details

#expressionsObject

Returns the value of attribute expressions.



4
5
6
# File 'lib/regexp_parser/expression/subexpression.rb', line 4

def expressions
  @expressions
end

Instance Method Details

#<<(exp) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/regexp_parser/expression/subexpression.rb', line 19

def <<(exp)
  if exp.is_a?(WhiteSpace) and @expressions.last and
    @expressions.last.is_a?(WhiteSpace)
    @expressions.last.merge(exp)
  else
    @expressions << exp
  end
end

#[](index) ⇒ Object



48
49
50
# File 'lib/regexp_parser/expression/subexpression.rb', line 48

def [](index)
  @expressions[index]
end

#all?(&block) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/regexp_parser/expression/subexpression.rb', line 60

def all?(&block)
  @expressions.all? {|exp| yield(exp) }
end

#cloneObject

Override base method to clone the expressions as well.



13
14
15
16
17
# File 'lib/regexp_parser/expression/subexpression.rb', line 13

def clone
  copy = super
  copy.expressions = @expressions.map {|e| e.clone }
  copy
end

#each(&block) ⇒ Object



32
33
34
# File 'lib/regexp_parser/expression/subexpression.rb', line 32

def each(&block)
  @expressions.each {|e| yield e}
end

#each_expression(include_self = false, &block) ⇒ Object

Iterates over the expressions of this expression as an array, passing the expression and its index within its parent to the given block.



39
40
41
42
43
# File 'lib/regexp_parser/expression/methods/traverse.rb', line 39

def each_expression(include_self = false, &block)
  traverse(include_self) do |event, exp, index|
    yield(exp, index) unless event == :exit
  end
end

#each_with_index(&block) ⇒ Object



36
37
38
# File 'lib/regexp_parser/expression/subexpression.rb', line 36

def each_with_index(&block)
  @expressions.each_with_index {|e, i| yield e, i}
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/regexp_parser/expression/subexpression.rb', line 56

def empty?
  @expressions.empty?
end

#firstObject



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

def first
  @expressions.first
end

#insert(exp) ⇒ Object



28
29
30
# File 'lib/regexp_parser/expression/subexpression.rb', line 28

def insert(exp)
  @expressions.insert 0, exp
end

#lastObject



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

def last
  @expressions.last
end

#lengthObject



52
53
54
# File 'lib/regexp_parser/expression/subexpression.rb', line 52

def length
  @expressions.length
end

#map(include_self = false, &block) ⇒ Object

Returns a new array with the results of calling the given block once for every expression. If a block is not given, returns an array with each expression and its level index as an array.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/regexp_parser/expression/methods/traverse.rb', line 48

def map(include_self = false, &block)
  result = []

  each_expression(include_self) do |exp, index|
    if block_given?
      result << yield(exp, index)
    else
      result << [exp, index]
    end
  end

  result
end

#strfregexp_tree(format = '%a', include_self = true, separator = "\n") ⇒ Object Also known as: strfre_tree



101
102
103
104
105
106
107
108
109
# File 'lib/regexp_parser/expression/methods/strfregexp.rb', line 101

def strfregexp_tree(format = '%a', include_self = true, separator = "\n")
  output = include_self ? [self.strfregexp(format)] : []

  output += map {|exp, index|
    exp.strfregexp(format, (include_self ? 1 : 0), index)
  }

  output.join(separator)
end

#teObject



68
69
70
# File 'lib/regexp_parser/expression/subexpression.rb', line 68

def te
  ts + to_s.length
end

#to_hObject



89
90
91
92
93
94
# File 'lib/regexp_parser/expression/subexpression.rb', line 89

def to_h
  h = super
  h[:text] = to_s(:base)
  h[:expressions] = @expressions.map(&:to_h)
  h
end

#to_s(format = :full) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/regexp_parser/expression/subexpression.rb', line 72

def to_s(format = :full)
  s = ''

  # Note: the format does not get passed down to subexpressions.
  case format
  when :base
    s << @text.dup
    s << @expressions.map{|e| e.to_s}.join unless @expressions.empty?
  else
    s << @text.dup
    s << @expressions.map{|e| e.to_s}.join unless @expressions.empty?
    s << @quantifier if quantified?
  end

  s
end

#traverse(include_self = false, &block) ⇒ Object Also known as: walk

Traverses the subexpression (depth-first, pre-order) and calls the given block for each expression with three arguments; the traversal event, the expression, and the index of the expression within its parent.

The event argument is passed as follows:

  • For subexpressions, :enter upon entrering the subexpression, and :exit upon exiting it.

  • For terminal expressions, :visit is called once.

Returns self.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/regexp_parser/expression/methods/traverse.rb', line 16

def traverse(include_self = false, &block)
  raise 'traverse requires a block' unless block_given?

  block.call(:enter, self, 0) if include_self

  each_with_index do |exp, index|
    if exp.terminal?
      block.call(:visit, exp, index)
    else
      block.call(:enter, exp, index)
      exp.traverse(&block)
      block.call(:exit, exp, index)
    end
  end

  block.call(:exit, self, 0) if include_self

  self
end

#tsObject



64
65
66
# File 'lib/regexp_parser/expression/subexpression.rb', line 64

def ts
  starts_at
end