parser.rb

Path: lib/parslet/parser.rb
Last Update: Thu Apr 26 16:22:26 -0400 2012

The base class for all your parsers. Use as follows:

  require 'parslet'

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

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

Parslet::Parser is also a grammar atom. This means that you can mix full fledged parsers freely with small parts of a different parser.

Example:

  class ParserA < Parslet::Parser
    root :aaa
    rule(:aaa) { str('a').repeat(3,3) }
  end
  class ParserB < Parslet::Parser
    root :expression
    rule(:expression) { str('b') >> ParserA.new >> str('b') }
  end

In the above example, ParserB would parse something like ‘baaab’.

[Validate]