Expand description
fpeg compiles a parsing expression grammar, given as text at runtime, to bytecode for a parsing machine and builds a lossless parse tree with exact source spans.
A Grammar is read from text or built with the builder, and a Parser compiles it
for the machine and parses inputs into a Tree. A Source shows the errors of a parse
with the lines they stand in. Behind the reference feature, the reference interpreter
defines what a grammar means, and the tests compare the machine with it.
The documentation describes the grammar language, its diagnostics and the command line, which are the same from Rust and from Python.
use fpeg::{Grammar, Parser};
let text = "start: first=N (\"+\" N)*\nN: [0-9]+\n%ignore \" \"+";
let grammar = Grammar::from_text(text).expect("the grammar reads");
let parser = Parser::new(&grammar).expect("the grammar compiles");
let input = "1 + 23";
let tree = parser.parse(input, "start").expect("the input parses");
assert_eq!(tree.to_text(input, false), "(start first=N\"1\" ~\"+\" N\"23\")");Modules§
- builder
- Build grammars in code.
Structs§
- Alternative
- A top-level alternative of a rule or a level, with its alias and value builder.
- Builder
Arg - One argument of a builder call.
- Class
- A character class: listed code points and ranges, or a Unicode property.
- Count
- A number written in the grammar, such as a repetition bound or a tab width.
- Diagnostic
- A problem that makes a grammar unusable, or a warning about one that works.
- Expr
- An expression with its source span.
- FieldId
- Identifies a field in a grammar’s field table. Field 0 means no field.
- Grammar
- A whole grammar: its rules and directives.
- Import
- An
%importdirective. - Kind
- What a compiled grammar knows about one kind of node or token.
- KindId
- Identifies a kind of node or token in a grammar’s kind table.
- Level
- One level of a precedence block.
- Limits
- How far a parse may go before it stops with
ParseErrorKind::Limit. - Line
Cursor - Positions of byte offsets that a scan meets in increasing order, each counted on from the one before, so that a scan over a whole text counts each byte once however long its lines are.
- Line
Index - The start offsets of the lines of a text. Line breaks are
\r\n,\nand\r. - Module
Files - The grammar files of a folder: the module
mis the filem.fpeg, UTF-8 with or without a byte order mark. A name that is not plain names none, so that a grammar cannot reach out of the folder. Only a regular file of at most the text that joining reads is read. - Name
- A name as written, with where it was written.
- Names
- The names behind kind and field ids, shared by a grammar and its trees.
- NodeRef
- A node or token of a tree.
- Options
- How a grammar is compiled. The default suits every use but tests and measurements, which turn single parts of the engine off or on. No setting changes what a parse returns.
- Parse
Error - Why a parse did not produce a tree.
- Parser
- A grammar compiled for the parsing machine.
- Passes
- The passes of the optimizer, one switch each, all on by default. A parse gives the same tree or error with any of them off, only its time changes.
- Position
- A place in a text as people count it.
- Profile
- What a parse did. See
crate::Parser::profile. - Profile
Row - What a parse did in the rules of one name.
- Recover
- A
%recover label: edirective. - Recovery
- A labeled error that a parse in recovering mode recovered from: the error item that stands in the tree in place of what failed.
- Rule
- One rule of a grammar.
- Source
- A text with its lines indexed and the path it was read from, to show errors in.
- Span
- A byte range in the grammar’s source text. Grammars built in code have empty spans.
- Text
- A string written in the grammar, after its escapes are decoded.
- Tree
- A parse tree. See the module documentation for the layout.
Enums§
- Builder
Value - The value of a builder argument.
- Class
Item - One entry of a character class.
- Constant
- A constant in a value builder.
- Diagnostic
Code - The kinds of problems a grammar can have. Each has a stable code, and its message says what exactly is wrong.
- Error
Label - The error of an error label.
- Expr
Kind - The forms an expression can take.
- Field
Count - How many items a field holds in one node. The variants are ordered from the fewest cases to the most, so that the larger of two counts covers both.
- Limit
- The limits a parse can hit.
- Memo
- Which rules remember their results, so that a second call of a rule at a position is answered at once.
- Parse
Error Kind - The kinds of parse errors.
- Property
- The Unicode properties a class may use.
- Rule
Body - The body of a rule.
- Rule
Kind - What a rule produces, decided by the case of its name and its attributes.
- Severity
- How serious a diagnostic is.
- Value
Builder - A value builder written after
=>.
Constants§
- BUILT_
IN_ BUILDERS - The value builders the language defines. Any other name after
=>is a function given with the grammar. - DEFAULT_
MAX_ DEPTH - Rule activations that may be nested before a parse stops. The Python grammar takes the deepest nesting that CPython takes, 200 brackets, within it, which a test holds.
- DEFAULT_
MAX_ MEMORY_ BYTES - The most memory in bytes a parse may hold by default, whatever its input.
- DEFAULT_
MAX_ STEPS - Steps a parse may take before it stops. A step is an instruction of the parsing machine, and work beyond one counts too, so that the limit bounds the time a parse takes: closing a node counts its items, comparing a long literal, taking a run of a class or looking up a reserved word counts its length, and testing a class beyond ASCII counts its items. A tree that repeats the nodes of a remembered match or of a left recursion counts what laying out the repeats takes. A parse that fails runs a second time to find what was expected, which takes as long again.
- DEFAULT_
MEMORY_ BYTES_ PER_ INPUT_ BYTE - Bytes of memory a parse may hold by default for each byte of its input.
- DEFAULT_
MIN_ MEMORY_ BYTES - The least memory in bytes a parse may hold by default, whatever its input.
- MAX_
EXPRESSION_ DEPTH - How deeply the expressions of a grammar may nest. Passes over an expression recurse, so a deeper grammar is rejected before they run.
- VERSION
- The version of this crate, which the command line and, in Python’s spelling, the Python package report.
Traits§
- Modules
- Where the grammars that a grammar extends and imports come from, before those that come with the library.
Functions§
- check
- Everything to say about a grammar. If it does not resolve, the errors of that. Otherwise a grammar too large to compile, which is an error too, and the warnings. Sorted by where they stand in the grammar.
- error_
title - The first line of an error as
Source::rendershows it, all that an error without a place in a text shows. - visible
- A text as
Source::rendershows it, with tabs as spaces and control and bidirectional characters as visible marks, so that a terminal acts on none of it: for messages that a grammar wrote, wherever they are printed.