Sha256: e2140f83367f152e0e83359b091ebd01acd7613be44ab02d2938514e25a00394

Contents?: true

Size: 734 Bytes

Versions: 2

Compression:

Stored size: 734 Bytes

Contents

# Matches a string of characters. 
#
# Example: 
# 
#   str('foo') # matches 'foo'
#
class Parslet::Atoms::Str < Parslet::Atoms::Base
  attr_reader :str
  def initialize(str)
    super()

    @str = str
    @error_msgs = {
      :premature  => "Premature end of input", 
      :failed     => "Expected #{str.inspect}, but got "
    }
  end
  
  def try(source, context) # :nodoc:
    error_pos = source.pos
    s = source.read(str.size)

    return success(s) if s == str
    
    # assert: s != str

    # Failures: 
    return error(source, @error_msgs[:premature]) unless s && s.size==str.size
    return error(source, @error_msgs[:failed]+s.inspect, error_pos) 
  end
  
  def to_s_inner(prec) # :nodoc:
    "'#{str}'"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
parslet-1.1.1 lib/parslet/atoms/str.rb
parslet-1.1.0 lib/parslet/atoms/str.rb