API Reference
pest.Parser
A pest parser.
This class provides methods to parse text using a pest grammar, generate parser source code, and inspect the grammar tree.
PARAMETER | DESCRIPTION |
---|---|
rules
|
Mapping of rule names to
TYPE:
|
doc
|
Optional list of grammar documentation lines.
TYPE:
|
optimizer
|
Optional optimizer to apply to the rules.
TYPE:
|
debug
|
If True, enables debug output during optimization.
TYPE:
|
ATTRIBUTE | DESCRIPTION |
---|---|
rules |
A mapping of rule names to
TYPE:
|
doc |
An optional list of grammar documentation lines.
|
from_grammar
classmethod
from_grammar(
grammar: str,
*,
optimizer: Optimizer | None = DEFAULT_OPTIMIZER,
debug: bool = False,
) -> Parser
Parse a grammar definition and return a new Parser
for it.
PARAMETER | DESCRIPTION |
---|---|
grammar
|
The grammar definition as a string.
TYPE:
|
optimizer
|
Optional optimizer to apply to the rules.
TYPE:
|
debug
|
If True, enables debug output during optimization.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Parser
|
A new parser instance for the given grammar.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
PestGrammarSyntaxError
|
If |
generate
Return a generated parser as Python module source code.
RETURNS | DESCRIPTION |
---|---|
str
|
The generated Python source code for the parser.
TYPE:
|
parse
Parse text
starting from the specified start_rule
.
PARAMETER | DESCRIPTION |
---|---|
start_rule
|
The name of the rule to start parsing from.
TYPE:
|
text
|
The input string to parse.
TYPE:
|
start_pos
|
The position in the input string to start parsing from (default: 0).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Pairs
|
The parse tree as a
TYPE:
|
RAISES | DESCRIPTION |
---|---|
KeyError
|
If |
PestParsingError
|
If the input |
pest.Pair
A matching pair of Tokens and everything between them.
Represents a node in the parse tree, corresponding to a matched rule and its children.
PARAMETER | DESCRIPTION |
---|---|
input_
|
The input string.
TYPE:
|
start
|
Start position in the input.
TYPE:
|
end
|
End position in the input.
TYPE:
|
rule
|
The rule or rule frame this pair represents. |
children
|
List of child pairs (subrules).
TYPE:
|
tag
|
Optional tag for this node.
TYPE:
|
inner_texts
property
The list of substrings pointed to by this pair's children.
dumps
Return a string representation of this token pair and all its children.
Translated from the format_pair
function found in the source for pest.rs.
https://github.com/pest-parser/site/blob/master/src/lib.rs.
line_col
Return the line and column number of this pair's start position.
pest.Pairs
Bases: Sequence[Pair]
A sequence of token pairs.
Provides sequence and utility methods for working with lists of Pair objects.
PARAMETER | DESCRIPTION |
---|---|
pairs
|
List of Pair objects.
TYPE:
|
dumps
Return a JSON formatted string representation of this node.
PARAMETER | DESCRIPTION |
---|---|
compact
|
If True, returns a compact string; otherwise, pretty-prints JSON.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
A string representation of the pairs. |
find_first_tagged
Finds the first pair that has its node tagged with label
.
PARAMETER | DESCRIPTION |
---|---|
label
|
The tag to search for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Pair | None
|
The first Pair with the given tag, or None if not found. |
find_tagged
Iterate over pairs tagged with label
.
PARAMETER | DESCRIPTION |
---|---|
label
|
The tag to search for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Iterator[Pair]
|
An iterator over all Pairs with the given tag. |
first
Return the single root pair.
RETURNS | DESCRIPTION |
---|---|
Pair
|
The first Pair in the sequence. |
pest.PrattParser
Bases: ABC
, Generic[ExprT]
Generic Pratt parser base class operating on a pest Stream
of Pair
s.
Subclasses define how to construct AST nodes by overriding the four abstract parse_* methods and providing operator-precedence tables.
INFIX_OPS
class-attribute
Mapping of infix operator rule names to (precedence, right_associative).
LEFT_ASSOC
class-attribute
instance-attribute
An alias for False
.
Use it as the second item in INFIX_OPS
values, right_associative, for
improved readability.
POSTFIX_OPS
class-attribute
Mapping of postfix operator rule names to precedence levels.
PREFIX_OPS
class-attribute
Mapping of prefix operator rule names to precedence levels.
RIGHT_ASSOC
class-attribute
instance-attribute
An alias for True
.
Use it as the second item in INFIX_OPS
values, right_associative, for
improved readability.
parse_expr
Parse an expression from a pest Stream
using Pratt precedence rules.
parse_infix
abstractmethod
Build a node for an infix operator expression.
parse_postfix
abstractmethod
Build a node for a postfix operator expression.
parse_prefix
abstractmethod
Build a node for a prefix operator expression.
pest.Stream
Step through pairs of tokens.
Provides a simple interface for sequential access to a list of Pair objects.
pest.Token
pest.Start
pest.End
pest.Position
Bases: NamedTuple
A position in a string as a Unicode codepoint offset.
Provides utilities for determining line and column numbers.
pest.Span
Bases: NamedTuple
A half-open interval [start, end) into the input string.
Represents a substring of the input, along with its start and end positions.
lines
Return a list of lines covered by this span.
Includes lines that are partially covered.
pest.Rule
Bases: Expression
Base class for all rules.
generate
Emit Python source code that implements this grammar expression.
parse
Attempt to match this expression against the input at start
.
pest.RuleFrame
Rule meta data for the generated rule stack.
Generated parser don't have access to complete Rule
object. RuleFrame
is a lightweight stand in.
ATTRIBUTE | DESCRIPTION |
---|---|
name |
The rule's name.
TYPE:
|
modifier |
A bit mask encoding modifiers applied to the rule.
TYPE:
|
pest.PestParsingError
Bases: Exception
An exception raised when an input string can't be passed by a pest grammar.
detailed_message
Return an error message formatted with extra context info.
expected
Return the expected/unexpected part of a detailed error message.