Class: Parslet::Cause

### Home Classes Methods

### In Files

- lib/parslet/cause.rb

### Parent

Object

### Methods

- ::format
- ::new
- #ascii_tree
- #children
- #raise
- #to_s

### 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::Cause

Represents a cause why a parse did fail. A lot of these objects are constructed - not all of the causes turn out to be failures for the whole parse.

### Attributes

message[R]

@return [String, Array] A string or an array of message pieces that

    provide failure information. Use #to_s to get a formatted string.

pos[R]

Location of the error.

@return [Fixnum] Position where the error happened. (character offset)

source[R]

@return [Parslet::Source] Source that was parsed when this error

    happend. Mainly used for line number information.

### Public Class Methods

format(source, pos, str, children=[]) click to toggle source

Appends 'at line LINE char CHAR' to the string given. Use `pos` to override the position of the `source`. This method returns an object that can be turned into a string using to_s.

@param source [Parslet::Source] source that was parsed when this error

    happened

@param pos [Fixnum] position of error @param str [String, Array<String>] message parts @param children [Array<Parslet::Cause>] child nodes for this error tree @return [Parslet::Cause] a new instance of {Parslet::Cause}

    # File lib/parslet/cause.rb, line 44
    def self.format(source, pos, str, children=[])
      self.new(str, source, pos, children)
    end

new(message, source, pos, children) click to toggle source

    # File lib/parslet/cause.rb, line 7
    def initialize(message, source, pos, children)
      @message, @source, @pos, @children =
        message, source, pos, children
    end

### Public Instance Methods

ascii_tree() click to toggle source

Returns an ascii tree representation of the causes of this node and its children.

    # File lib/parslet/cause.rb, line 69
    def ascii_tree
      StringIO.new.tap { |io|
        recursive_ascii_tree(self, io, [true]) }.
        string
    end

children() click to toggle source

When this cause is part of a tree of error causes: child nodes for this node. Very often carries the reasons for this cause.

@return [Array<Parslet::Cause>] A list of reasons for this cause.

    # File lib/parslet/cause.rb, line 29
    def children
      @children ||= []
    end

raise(exception_klass=Parslet::ParseFailed) click to toggle source

Signals to the outside that the parse has failed. Use this in conjunction with .format for nice error messages.

    # File lib/parslet/cause.rb, line 61
    def raise(exception_klass=Parslet::ParseFailed)
      exception = exception_klass.new(self.to_s, self)
      Kernel.raise exception
    end

to_s() click to toggle source

    # File lib/parslet/cause.rb, line 48
    def to_s
      line, column = source.line_and_column(pos)
      # Allow message to be a list of objects. Join them here, since we now
      # really need it.
      Array(message).map { |o|
        o.respond_to?(:to_slice) ?
          o.str.inspect :
          o.to_s }.join + " at line #{line} char #{column}."
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.