class Attentive::Tokens::Regexp

Attributes

regexp[R]

Public Class Methods

new(string, pos) click to toggle source
Calls superclass method Attentive::Token::new
# File lib/attentive/tokens/regexp.rb, line 8
def initialize(string, pos)
  @regexp = ::Regexp.compile("^#{string}")
  super pos
end

Public Instance Methods

==(other) click to toggle source
# File lib/attentive/tokens/regexp.rb, line 13
def ==(other)
  self.class == other.class && self.regexp == other.regexp
end
matches?(cursor) click to toggle source
# File lib/attentive/tokens/regexp.rb, line 17
def matches?(cursor)
  # Compare the original, untokenized, message to the regular expression
  match_data = regexp.match(cursor.to_s)
  return false unless match_data

  # Find the first token following the match
  new_character_index = cursor.offset + match_data.to_s.length
  cursor_pos = cursor.tokens.index { |token| token.begin >= new_character_index }
  cursor_pos = cursor.tokens.length unless cursor_pos

  # If the match ends in the middle of a token, treat it as a mismatch
  match_end_token = cursor.tokens[cursor_pos - 1]
  return false if match_end_token.begin + match_end_token.length > new_character_index

  # Advance the cursor to the first token after the regexp match
  cursor.advance cursor_pos - cursor.pos

  # Return the MatchData as a hash
  Hash[match_data.names.zip(match_data.captures)]
end
to_s() click to toggle source
# File lib/attentive/tokens/regexp.rb, line 38
def to_s
  regexp.inspect
end