Class: Parslet::Atoms::Base

### Home Classes Methods

### In Files

- lib/parslet/atoms/base.rb
- lib/parslet/atoms/visitor.rb
- lib/parslet/convenience.rb

### Parent

Object

### Methods

- ::precedence
- #accept
- #apply
- #inspect
- #parse
- #parse_with_debug
- #setup_and_apply
- #to_s
- #try

### Included Modules

- Parslet::Atoms::Precedence
- Parslet::Atoms::DSL
- Parslet::Atoms::CanFlatten

### Files

- README

### Class/Module Index

Quicksearch

- Parslet
- Parslet::Atoms
- Parslet::Atoms::Alternative
- Parslet::Atoms::Base
- Parslet::Atoms::CanFlatten
- Parslet::Atoms::Context
- Parslet::Atoms::DSL
- Parslet::Atoms::Entity
- Parslet::Atoms::Lookahead
- Parslet::Atoms::Named
- Parslet::Atoms::Precedence
- Parslet::Atoms::Re
- Parslet::Atoms::Repetition
- 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::Slice
- Parslet::Source
- Parslet::Source::LineCache
- Parslet::Source::RangeSearch
- Parslet::Transform
- Parslet::Transform::Context
- Parslet::UnconsumedInput

No matching classes.

# Parslet::Atoms::Base

Base class for all parslets, handles orchestration of calls and implements a lot of the operator and chaining methods.

Also see Parslet::Atoms::DSL chaining parslet atoms together.

### Public Class Methods

precedence(prec) click to toggle source

Debug printing - in Treetop syntax.

    # File lib/parslet/atoms/base.rb, line 101
    def self.precedence(prec)
      define_method(:precedence) { prec }
    end

### Public Instance Methods

accept(visitor) click to toggle source

    # File lib/parslet/atoms/visitor.rb, line 7
    def accept(visitor)
      raise NotImplementedError, "No #accept method on #{self.class.name}."
    end

apply(source, context) click to toggle source

+

    # File lib/parslet/atoms/base.rb, line 79
    def apply(source, context)
      old_pos = source.pos

      success, value = result = context.try_with_cache(self, source)

      return result if success

      # We only reach this point if the parse has failed. Rewind the input.
      source.pos = old_pos
      return result
    end

inspect() click to toggle source

    # File lib/parslet/atoms/base.rb, line 112
    def inspect
      to_s(OUTER)
    end

parse(io, options={}) click to toggle source

Given a string or an IO object, this will attempt a parse of its contents and return a result. If the parse fails, a Parslet::ParseFailed exception will be thrown.

@param io [String, Source] input for the parse process @option options [Parslet::ErrorReporter] :reporter error reporter to use,

    defaults to Parslet::ErrorReporter::Tree

@option options [Boolean] :prefix Should a prefix match be accepted?

    (default: false)

@return [Hash, Array, Parslet::Slice] PORO (Plain old Ruby object) result

    tree

    # File lib/parslet/atoms/base.rb, line 23
    def parse(io, options={})
      source = io.respond_to?(:line_and_column) ?
        io :
        Parslet::Source.new(io)

      # Try to cheat. Assuming that we'll be able to parse the input, don't
      # run error reporting code.
      success, value = setup_and_apply(source, nil)

      # If we didn't succeed the parse, raise an exception for the user.
      # Stack trace will be off, but the error tree should explain the reason
      # it failed.
      unless success
        # Cheating has not paid off. Now pay the cost: Rerun the parse,
        # gathering error information in the process.
        reporter = options[:reporter] || Parslet::ErrorReporter::Tree.new
        success, value = setup_and_apply(source, reporter)

        fail "Assertion failed: success was true when parsing with reporter"          if success

        # Value is a Parslet::Cause, which can be turned into an exception:
        value.raise

        fail "NEVER REACHED"
      end

      # assert: success is true

      # If we haven't consumed the input, then the pattern doesn't match. Try
      # to provide a good error message (even asking down below)
      if !options[:prefix] && !source.eof?
        old_pos = source.pos
        Parslet::Cause.format(
          source, old_pos,
          "Don't know what to do with #{source.consume(10).to_s.inspect}").
          raise(Parslet::UnconsumedInput)
      end

      return flatten(value)
    end

parse_with_debug(str, opts={}) click to toggle source

Packages the common idiom

    begin
      tree = parser.parse('something')
    rescue Parslet::ParseFailed => error
      puts parser.cause.ascii_tree
    end

into a convenient method.

Usage:

    require 'parslet'
    require 'parslet/convenience'

    class FooParser < Parslet::Parser
      rule(:foo) { str('foo') }
      root(:foo)
    end

    FooParser.new.parse_with_debug('bar')

@see Parslet::Atoms::Base#parse

    # File lib/parslet/convenience.rb, line 27
    def parse_with_debug str, opts={}
      parse str, opts
    rescue Parslet::UnconsumedInput => error
      puts error
    rescue Parslet::ParseFailed => error
      puts error.cause.ascii_tree
    end

setup_and_apply(source, error_reporter) click to toggle source

Creates a context for parsing and applies the current atom to the input. Returns the parse result.

@return [<Boolean, Object>] Result of the parse. If the first member is

    true, the parse has succeeded.

    # File lib/parslet/atoms/base.rb, line 70
    def setup_and_apply(source, error_reporter)
      context = Parslet::Atoms::Context.new(error_reporter)
      apply(source, context)
    end

to_s(outer_prec=OUTER) click to toggle source

    # File lib/parslet/atoms/base.rb, line 105
    def to_s(outer_prec=OUTER)
      if outer_prec < precedence
        "("+to_s_inner(precedence)+")"
      else
        to_s_inner(precedence)
      end
    end

try(source, context) click to toggle source

Override this in your Atoms::Base subclasses to implement parsing behaviour.

    # File lib/parslet/atoms/base.rb, line 94
    def try(source, context)
      raise NotImplementedError,        "Atoms::Base doesn't have behaviour, please implement #try(source, context)."
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.