Class: Regexgen::Ast::Literal

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

Overview

Represents a literal (e.g. a string)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Literal

Returns a new instance of Literal.



147
148
149
150
# File 'lib/regexgen/ast.rb', line 147

def initialize(value)
  @precedence = 2
  @value = value
end

Instance Attribute Details

#precedenceObject (readonly)

Returns the value of attribute precedence.



145
146
147
# File 'lib/regexgen/ast.rb', line 145

def precedence
  @precedence
end

#valueObject (readonly)

Returns the value of attribute value.



145
146
147
# File 'lib/regexgen/ast.rb', line 145

def value
  @value
end

Instance Method Details

#char_classObject



172
173
174
# File 'lib/regexgen/ast.rb', line 172

def char_class
  @value if single_codepoint?
end

#empty?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/regexgen/ast.rb', line 152

def empty?
  @value.empty?
end

#lengthObject



164
165
166
# File 'lib/regexgen/ast.rb', line 164

def length
  @value.length
end

#literal(_side = nil) ⇒ Object



176
177
178
# File 'lib/regexgen/ast.rb', line 176

def literal(_side = nil)
  @value
end

#remove_substring(side, len) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/regexgen/ast.rb', line 180

def remove_substring(side, len)
  case side
  when :start then Literal.new(@value[len...@value.length])
  when :end then Literal.new(@value[0...(@value.length - len)])
  else raise "Unknown side: #{side}"
  end
end

#single_character?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/regexgen/ast.rb', line 156

def single_character?
  length == 1
end

#single_codepoint?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/regexgen/ast.rb', line 160

def single_codepoint?
  @value.codepoints.length == 1
end

#to_sObject



168
169
170
# File 'lib/regexgen/ast.rb', line 168

def to_s
  Regexp.escape(@value)
end