Expand description
§pest. The Elegant Parser
pest is a general purpose parser written in Rust with a focus on accessibility, correctness, and performance. It uses parsing expression grammars (or PEG) as input, which are similar in spirit to regular expressions, but which offer the enhanced expressivity needed to parse complex languages.
§Getting started
The recommended way to start parsing with pest is to read the official book.
Other helpful resources:
- API reference on docs.rs
- play with grammars and share them on our fiddle
- leave feedback, ask questions, or greet us on Gitter
§Usage
The core of pest is the trait Parser, which provides an interface to the parsing
functionality.
The accompanying crate pest_derive can automatically generate a Parser from a PEG
grammar. Using pest_derive is highly encouraged, but it is also possible to implement
Parser manually if required.
§.pest files
Grammar definitions reside in custom .pest files located in the crate src directory.
Parsers are automatically generated from these files using #[derive(Parser)] and a special
#[grammar = "..."] attribute on a dummy struct.
#[derive(Parser)]
#[grammar = "path/to/my_grammar.pest"] // relative to src
struct MyParser;The syntax of .pest files is documented in the pest_derive crate.
§Inline grammars
Grammars can also be inlined by using the #[grammar_inline = "..."] attribute.
Modules§
- error
- Types for different kinds of parsing failures.
- iterators
- Types and iterators for parser output.
- prec_
climber - Constructs useful in infix operator parsing with the precedence climbing method.
Macros§
- fails_
with - Testing tool that compares produced errors.
- parses_
to - Testing tool that compares produced tokens.
Structs§
- Lines
- Line iterator for Spans, created by
Span::lines(). - Parser
State - The complete state of a
Parser. - Position
- A cursor position in a
&strwhich provides useful methods to manually parse that string. - Span
- A span over a
&str. It is created from either twoPositions or from aPair.
Enums§
- Atomicity
- The current atomicity of a
ParserState. - Lookahead
- The current lookahead status of a
ParserState. - Match
Dir - Match direction for the stack. Used in
PEEK[a..b]/stack_match_peek_slice. - Token
- A token generated by a
Parser.
Traits§
- Parser
- A trait with a single method that parses strings.
- Rule
Type - A trait which parser rules must implement.
Functions§
- state
- Creates a
ParserStatefrom a&str, supplying it to a closuref.
Type Aliases§
- Parse
Result - Type alias to simplify specifying the return value of chained closures.