class Parslet::Atoms::Str

Matches a string of characters.

Example:

str('foo') # matches 'foo'

Attributes

str[R]

Public Class Methods

new(str) click to toggle source
# File lib/parslet/atoms/str.rb, line 9
def initialize(str)
  super()

  @str = str.to_s
  @pat = Regexp.new(Regexp.escape(str))
  @len = str.size
  @error_msgs = {
    :premature  => "Premature end of input", 
    :failed     => "Expected #{str.inspect}, but got "
  }
end

Public Instance Methods

accept(visitor) click to toggle source

Call back visitors visit_str method. See parslet/export for an example.

# File lib/parslet/atoms/visitor.rb, line 15
def accept(visitor)
  visitor.visit_str(str)
end
to_s_inner(prec) click to toggle source
# File lib/parslet/atoms/str.rb, line 35
def to_s_inner(prec)
  "'#{str}'"
end
try(source, context, consume_all) click to toggle source
# File lib/parslet/atoms/str.rb, line 21
def try(source, context, consume_all)
  return succ(source.consume(@len)) if source.matches?(@pat)
  
  # Input ending early:
  return context.err(self, source, @error_msgs[:premature])        if source.chars_left<@len
  
  # Expected something, but got something else instead:  
  error_pos = source.pos  
  return context.err_at(
    self, source, 
    [@error_msgs[:failed], source.consume(@len)], error_pos) 
end