Class: Arugula::MetacharacterPart

Inherits:
Part
  • Object
show all
Defined in:
lib/arugula/parts.rb

Constant Summary collapse

MATCHERS =
{
  A: ->(_str, index) { index == 0 },
  d: ->(str, index) { ('0'..'9').member?(str[index]) },
  s: ->(str, index) { [' ', "\t"].include?(str[index]) },
  S: ->(str, index) { ![' ', "\t"].include?(str[index]) },
  z: ->(str, index) { index == str.size },
  Z: ->(str, index) { str[index..-1] == "\n" || index == str.size },
}.freeze
OFFSETS =
begin
  offsets = {
    A: ->(_str, _index) { 0 },
    Z: ->(_str, _index) { 0 },
    z: ->(_str, _index) { 0 },
  }
  offsets.default = ->(_str, _index) { 1 }
  offsets.freeze
end

Instance Method Summary collapse

Methods inherited from Part

all, inherited, type

Constructor Details

#initialize(metachar) ⇒ MetacharacterPart

Returns a new instance of MetacharacterPart.



132
133
134
# File 'lib/arugula/parts.rb', line 132

def initialize(metachar)
  @metachar = metachar.to_sym
end

Instance Method Details

#match(str, index, _match_data) ⇒ Object



136
137
138
139
# File 'lib/arugula/parts.rb', line 136

def match(str, index, _match_data)
  matches = MATCHERS[@metachar][str, index]
  [matches, index + (matches ? OFFSETS[@metachar][str, index] : 0)]
end

#to_sObject



141
142
143
# File 'lib/arugula/parts.rb', line 141

def to_s
  "\\#{@metachar}"
end