Class: Regexgen::Ast::Concatenation

Inherits:
Object
  • Object
show all
Defined in:
lib/regexgen/ast.rb

Overview

Represents a concatenation (e.g. ‘foo`)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Concatenation

Returns a new instance of Concatenation.



91
92
93
94
95
# File 'lib/regexgen/ast.rb', line 91

def initialize(a, b)
  @precedence = 2
  @a = a
  @b = b
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



89
90
91
# File 'lib/regexgen/ast.rb', line 89

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



89
90
91
# File 'lib/regexgen/ast.rb', line 89

def b
  @b
end

#precedenceObject (readonly)

Returns the value of attribute precedence.



89
90
91
# File 'lib/regexgen/ast.rb', line 89

def precedence
  @precedence
end

Instance Method Details

#lengthObject



97
98
99
# File 'lib/regexgen/ast.rb', line 97

def length
  @a.length + @b.length
end

#literal(side) ⇒ Object



105
106
107
108
109
# File 'lib/regexgen/ast.rb', line 105

def literal(side)
  return @a.literal(side) if side == :start && @a.respond_to?(:literal)

  @b.literal(side) if side == :end && @b.respond_to?(:literal)
end

#remove_substring(side, len) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/regexgen/ast.rb', line 111

def remove_substring(side, len)
  a = @a
  b = @b
  a = @a.remove_substring(side, len) if side == :start && @a.respond_to?(:remove_substring)
  b = @b.remove_substring(side, len) if side == :end && @b.respond_to?(:remove_substring)

  return b if a.respond_to?(:empty?) && a.empty?
  return a if b.respond_to?(:empty?) && b.empty?

  Concatenation.new(a, b)
end

#to_sObject



101
102
103
# File 'lib/regexgen/ast.rb', line 101

def to_s
  Ast.parens(@a, self) + Ast.parens(@b, self)
end