Module Parslet
In: lib/parslet.rb
lib/parslet/source.rb
lib/parslet/cause.rb

A simple parser generator library. Typical usage would look like this:

  require 'parslet'

  class MyParser < Parslet::Parser
    rule(:a) { str('a').repeat }
    root(:a)
  end

  pp MyParser.new.parse('aaaa')   # => 'aaaa'@0
  pp MyParser.new.parse('bbbb')   # => Parslet::Atoms::ParseFailed:
                                  #    Don't know what to do with bbbb at line 1 char 1.

The simple DSL allows you to define grammars in PEG-style. This kind of grammar construction does away with the ambiguities that usually comes with parsers; instead, it allows you to construct grammars that are easier to debug, since less magic is involved.

Parslet is typically used in stages:

The first stage is traditionally intermingled with the second stage; output from the second stage is usually called the ‘Abstract Syntax Tree’ or AST.

The stages are completely decoupled; You can change your grammar around and use the second stage to isolate the rest of your code from the changes you‘ve effected.

Further reading

All parslet atoms are subclasses of Parslet::Atoms::Base. You might want to look at all of those: Parslet::Atoms::Re, Parslet::Atoms::Str, Parslet::Atoms::Repetition, Parslet::Atoms::Sequence, Parslet::Atoms::Alternative.

When things go wrong

A parse that fails will raise Parslet::ParseFailed. A detailed explanation of what went wrong can be obtained from the parslet involved or the root of the parser instance.

Methods

any   match   sequence   simple   str   subtree  

Classes and Modules

Module Parslet::Atoms
Module Parslet::Bytecode
Module Parslet::ClassMethods
Module Parslet::Transform
Class Parslet::ErrorTree
Class Parslet::Expression
Class Parslet::InvalidSliceOperation
Class Parslet::ParseFailed
Class Parslet::Parser
Class Parslet::Pattern
Class Parslet::Slice
Class Parslet::Source
Class Parslet::Transform
Class Parslet::UnconsumedInput

Public Instance methods

Returns an atom matching any character. It acts like the ’.’ (dot) character in regular expressions.

Example:

  any.parse('a')    # => 'a'

Returns an atom matching a character class. All regular expressions can be used, as long as they match only a single character at a time.

Example:

  match('[ab]')     # will match either 'a' or 'b'
  match('[\n\s]')   # will match newlines and spaces

There is also another (convenience) form of this method:

  match['a-z']      # synonymous to match('[a-z]')
  match['\n']       # synonymous to match('[\n]')

Returns a placeholder for a tree transformation that will only match a sequence of elements. The symbol you specify will be the key for the matched sequence in the returned dictionary.

Example:

  # This would match a body element that contains several declarations.
  { :body => sequence(:declarations) }

The above example would match :body => [‘a’, ‘b’], but not :body => ‘a’.

Returns a placeholder for a tree transformation that will only match simple elements. This matches everything that sequence doesn‘t match.

Example:

  # Matches a single header.
  { :header => simple(:header) }

Returns an atom matching the str given.

Example:

  str('class')      # will match 'class'

Returns a placeholder for tree transformation patterns that will match any kind of subtree.

Example:

  { :expression => subtree(:exp) }

[Validate]