Class: Parslet::Atoms::Str

### Home Classes Methods

### In Files

- lib/parslet/atoms/str.rb
- lib/parslet/atoms/visitor.rb

### Parent

Parslet::Atoms::Base

### Methods

- ::new
- #accept
- #to_s_inner
- #try

### Files

- README

### Class/Module Index

Quicksearch

- Parslet
- Parslet::Atoms
- Parslet::Atoms::Alternative
- Parslet::Atoms::Base
- Parslet::Atoms::CanFlatten
- Parslet::Atoms::Capture
- Parslet::Atoms::Context
- Parslet::Atoms::DSL
- Parslet::Atoms::Dynamic
- Parslet::Atoms::Entity
- Parslet::Atoms::Lookahead
- Parslet::Atoms::Named
- Parslet::Atoms::Precedence
- Parslet::Atoms::Re
- Parslet::Atoms::Repetition
- Parslet::Atoms::Scope
- Parslet::Atoms::Sequence
- Parslet::Atoms::Str
- Parslet::Cause
- Parslet::ClassMethods
- Parslet::DelayedMatchConstructor
- Parslet::ErrorReporter
- Parslet::ErrorReporter::Deepest
- Parslet::ErrorReporter::Tree
- Parslet::Expression
- Parslet::Expression::Treetop
- Parslet::Expression::Treetop::Parser
- Parslet::Expression::Treetop::Transform
- Parslet::ParseFailed
- Parslet::Parser
- Parslet::Parser::PrettyPrinter
- Parslet::Parser::Visitors
- Parslet::Parser::Visitors::Citrus
- Parslet::Parser::Visitors::Treetop
- Parslet::Pattern
- Parslet::Pattern::SequenceBind
- Parslet::Pattern::SimpleBind
- Parslet::Pattern::SubtreeBind
- Parslet::Scope
- Parslet::Scope::Binding
- Parslet::Scope::NotFound
- Parslet::Slice
- Parslet::Source
- Parslet::Source::LineCache
- Parslet::Source::RangeSearch
- Parslet::Transform
- Parslet::Transform::Context

No matching classes.

# 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
      @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 34
    def to_s_inner(prec)
      "'#{str}'"
    end

try(source, context, consume_all) click to toggle source

    # File lib/parslet/atoms/str.rb, line 20
    def try(source, context, consume_all)
      return succ(source.consume(@len)) if source.matches?(str)

      # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.