class Cannonbol::Needle

Attributes

captures[RW]
cursor[R]
ignore_case[RW]
match_failed[RW]
string[R]

Public Class Methods

new(string) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 36
def initialize(string)
  @string = string
end

Public Instance Methods

capture(name, value) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 73
def capture(name, value)
  @captures[name.to_sym] = value if name
  value
end
pull(thread_state) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 91
def pull(thread_state)
  @starting_character, @cursor, @success_blocks, @ignore_case = thread_state if thread_state
  nil
end
push(length, &success_block) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 83
def push(length, &success_block)
  thread_state = [@starting_character, @cursor, @success_blocks.dup, @ignore_case]
  @starting_character ||= @cursor
  @cursor += length
  @success_blocks << success_block if success_block
  thread_state
end
remaining_string() click to toggle source
# File lib/cannonbol/cannonbol.rb, line 79
def remaining_string
  @string[@cursor..-1]
end
thread(pattern, opts = {}, &match_block) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 40
def thread(pattern, opts = {}, &match_block)
  @captures = {}
  anchor = opts[:anchor]
  raise_error = opts[:raise_error]
  replace_with = opts[:replace_match_with]
  ignore_case = opts[:ignore_case] | opts[:insensitive]
  @cursor = -1
  match = nil
  begin
    while !match and !match_failed and @cursor < @string.length-1
      @cursor += 1
      @starting_character = nil
      @success_blocks = []
      @ignore_case = ignore_case
      match = pattern._match?(self)
      break if !match and anchor
    end
  rescue MatchFailed
  end
  if match
    @success_blocks.each(&:call)
    match = MatchString.new(@string, @starting_character || @cursor, @cursor-1, @captures)
  else
    raise MatchFailed if raise_error
  end
  if match_block
    match = match_block.call(*([match] + (match_block.parameters[1..-1] || []).collect { |param| @captures[param[1].to_sym] }))
  elsif replace_with
    match = match.replace_match_with(replace_with)
  end
  match
end