Sha256: 515a9c2f6796cd3f648998d2224515c2d60d986138b617315386c816085e2d8a

Contents?: true

Size: 781 Bytes

Versions: 3

Compression:

Stored size: 781 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.to_s
    @len = str.size
    @error_msgs = {
      :premature  => "Premature end of input", 
      :failed     => "Expected #{str.inspect}, but got "
    }
  end
  
  def try(source, context)
    return succ(source.consume(@len)) if source.matches?(str)
    
    # Failures: 
    return context.err(self, source, @error_msgs[:premature]) \
      if source.chars_left<@len
      
    error_pos = source.pos  
    return context.err_at(
      self, source, 
      [@error_msgs[:failed], source.consume(@len)], error_pos) 
  end
  
  def to_s_inner(prec)
    "'#{str}'"
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
ghazel-parslet-1.4.0.2 lib/parslet/atoms/str.rb
ghazel-parslet-1.4.0.1 lib/parslet/atoms/str.rb
parslet-1.4.0 lib/parslet/atoms/str.rb