rust !N#rustc 1.81.0 (eeb90cda1 2024-09-04) œm̫Ų޳<-0f9bda72675979e42vKfw-d8cea91d43634f65Yi1-b1f27438890f45b3rustc_std_workspace_core谼n&,5wtEC-a22427de72ab3630j{!1e'-3d4e98832b983b89A~mf\e`B-4484e05b7089b5ef]XX2_jr-85a6d450f72e1aabcfg_ifмɊra-cf81a5498b42604d miniz_oxideu/#y, -a1543c9c7e6c02bbadlerѹQpC@-7efc4608b0bc96f7 hashbrownb]Ұ.1zPd~-44fc4705e85ac7carustc_std_workspace_alloca+^SΗ%B&-f11d6ba5d61218f4 std_detect"d&+Olb-bd30e1a2296ebf34rustc_demangle{ `*2{-6e14ac007075e94c addr2lineNc:bYǛ-12c2628c83917178gimli}:O^-34000eb5b4402af9objectLL7# 7-79130ad3ad7802b2memchr} wC,-81f43cdd1f7d7095 w9͟IBj6N-51773ba94f264ce4 peg_macros ;Cog-abf8ef9d3ed7fa96 peg_runtimeN V 7]uO-7705872aee73b827 runtime TvParse-Y ParseElemMa ParseLiterale e ParseSliceU g RuleResultUvK `rust-peg` is a simple yet flexible parser generator that makes it easy toN7 write robust parsers. Based on the [Parsing ExpressionO:J Grammar][wikipedia-peg] formalism, it provides a Rust macro that builds aMC recursive descent parser from a concise definition of the grammar.FJ [wikipedia-peg]: https://en.wikipedia.org/wiki/Parsing_expression_grammarM ## Features|H * Parse input from `&str`, `&[u8]`, `&[T]` or custom types implementingK traitsd) * Customizable reporting of parse errors,? * Rules can accept arguments to create reusable rule templatesB; * Precedence climbing for prefix/postfix/infix expressions>M * Helpful `rustc` error messages for errors in the grammar definition or theP Rust code embedded within it"' * Rule-level tracing to debug grammars* ## Overview|K The `peg::parser!{}` macro encloses a `grammar NAME() for INPUT_TYPE { ...NG }` definition containing a set of rules which match components of yourJ language.lJ Rules are defined with `rule NAME(PARAMETERS) -> RETURN_TYPE = PEG_EXPR`.MM The body of the rule, following the `=`, is a PEG expression, definining howP) the input is matched to produce a value. , M PEG expressions are evaluated at a particular position of the input. When an PM expression matches, it advances the position and optionally returns a value. P2 The expression syntax and behavior is [documented 5 below](#expression-reference). " M The macro expands to a Rust `mod` containing a function for each rule marked PD `pub` in the grammar. To parse an input sequence, call one of these GJ functions. The call returns a `Result` carrying either the MM successfully parsed value returned by the rule, or a `ParseError` containing P; the failure position and the set of tokens expected there. > ## ExampletR Parse a comma-separated list of numbers surrounded by brackets into a `Vec`:U ```rust\ peg::parser!{" grammar list_parser() for str {% rule number() -> u328 = n:$(['0'..='9']+) {? n.parse().or(Err("u32")) }; pub rule list() -> Vec#* = "[" l:(number() ** ",") "]" { l }- }< }, pub fn main() {P assert_eq!(list_parser::list("[1,1,2,3,5,8]"), Ok(vec![1, 1, 2, 3, 5, 8]));S, ```< ## Expression Referenceܪ ### Atomsl5 * `"keyword"` - _Literal:_ match a literal string.8Y * `['0'..='9']` - _Pattern:_ match a single element that matches a Rust `match`-style\/ pattern. [(details)](#pattern-expressions)2k * `[^ '0'..='9']` - _Inverted pattern:_ match a single element that does not match a Rust `match`-stylen!2Y * `some_rule()` - _Rule:_ match a rule defined elsewhere in the grammar and return its\? result. Arguments in the parentheses are Rust expressions.BN * `_` or `__` or `___` - _Rule (underscore):_ As a special case, rule namesQX consisting of underscores can be defined and invoked without parentheses. These are[< conventionally used to match whitespace between tokens.?G * `(e)` - _Parentheses:_ wrap an expression into a group to overrideJL normal precedence. Returns the same value as the inner expression. (UseO? an _Action_ block to set the return value for a sequence).B ### Combining^ * `e1 e2 e3` - _Sequence:_ match expressions in sequence (`e1` followed by `e2` followed bya' `e3`), ignoring the return values.*M * `a:e1 e2 b:e3 c:e4 { rust }` - _Action:_ match `e1`, `e2`, `e3`, `e4` inPK sequence, like above. If they match successfully, run the Rust code inNI the block and return its return value. The variable names before theL; colons in the sequence are bound to the results of the>K corresponding expressions. It is important that the Rust code embeddedNK in the grammar is deterministic and free of side effects, as it may beN called multiple times.K * `a:e1 b:e2 c:e3 {? rust }` - _Conditional action:_ Like above, but theNK Rust block returns a `Result` instead of a value directly. On NM `Ok(v)`, it matches successfully and returns `v`. On `Err(e)`, the match PJ of the entire expression fails and it tries alternatives or reports a!M' parse failure with the `&str` `e`."*\ * `e1 / e2 / e3` - _Ordered choice:_ try to match `e1`. If the match succeeds, return its"_+ result, otherwise try `e2`, and so on.#.# ### Repetition#Z * `expression?` - _Optional:_ match zero or one repetitions of `expression`. Returns an#] `Option`.$\ * `expression*` - _Repeat:_ match zero or more repetitions of `expression` and return the$_ results as a `Vec`.ܡ%` * `expression+` - _One-or-more:_ match one or more repetitions of `expression` and return the%c4ܡ&_ * `expression*` - _Range repeat:_ match between `n` and `m` repetitions of `expression`&b? return the results as a `Vec`. [(details)](#repeat-ranges)'B_ * `expression ** delim` - _Delimited repeat:_ match zero or more repetitions of `expression`'b> delimited with `delim` and return the results as a `Vec`.(As * `expression ** delim` - _Delimited repeat (range):_ match between `n` and `m` repetitions of `expression`)vZ delimited with `delim` and return the results as a `Vec`. [(details)](#repeat-ranges))]l * `expression ++ delim` - _Delimited repeat (one or more):_ match one or more repetitions of `expression`*o7+A, ### Special,R * `$(e)` - _Slice:_ match the expression `e`, and return the slice of the input,U corresponding to the match.,#M * `&e` - _Positive lookahead:_ Match only if `e` matches at this position,-P& without consuming any characters.-)J * `!e` - _Negative lookahead:_ Match only if `e` does not match at this.M0 position, without consuming any characters..3J * `position!()` - return a `usize` representing the current offset into/M* the input without consuming anything./-c * `quiet!{ e }` - match the expression `e`, but don't report literals within it as "expected" in0f error messages.0Z * `expected!("something")` - fail to match, and report the specified string as expected1] at the current location.1 _ * `precedence!{ ... }` - Parse infix, prefix, or postfix expressions by precedence climbing.2b& [(details)](#precedence-climbing)2)3 ## Expression detailș33 ### Pattern expressions330 The `[pat]` syntax expands into a [Rust `match`33` pattern](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html) against the next character4c (or element) of the input.4 $5E When the pattern begins with `^`, the matching behavior is inverted:5H> the expression succeeds only if the pattern does *not* match.5A3 `[^' ']` matches any character other than a space.666F To match sets of characters, use Rust's `..=` inclusive range pattern6I\ syntax and `|` to match multiple patterns. For example `['a'..='z' | 'A'..='Z']` matches an7_. upper or lower case ASCII alphabet character.818Z If your input type is a slice of an enum type, a pattern could match an enum variant like8] `[Token::Operator('+')]`.99H Variables captured by the pattern are accessible in a subsequent action9K$ block: `[Token::Integer(i)] { i }`.:'E$:K The pattern expression also evaluates to the matched element, which can be:NO captured into a variable or used as the return value of a rule: `c:['+'|'-']`.;RE$;B Like Rust `match`, pattern expressions support guard expressions:;E `[c if c.is_ascii_digit()]`.< <] `[_]` matches any single element. As this always matches except at end-of-file, combining it<`H with negative lookahead as `![_]` is the idiom for matching EOF in PEG.=KE$= ### Repeat ranges>>\ The repeat operators `*` and `**` can be followed by an optional range specification of the>_~ form `` (exact), `` (min-inclusive), `<,m>` (max-inclusive) or `` (range-inclusive), where `n` and `m` are either>9 integers, or a Rust `usize` expression enclosed in `{}`.?<@ ### Precedence climbing@@X `precedence!{ rules... }` provides a convenient way to parse infix, prefix, and postfix@[ operators using the [precedenceA#^ climbing](http://eli.thegreenplace.net/2012/08/02/parsing-expressions-by-precedence-climbing)Aa algorithm.tBB ```rust,no_runB' # peg::parser!{grammar doc() for str {B*) # pub rule number() -> i64 = "..." { 0 }C,, pub rule arithmetic() -> i64 = precedence!{C/ x:(@) "+" y:@ { x + y }C x:(@) "-" y:@ { x - y }D --DD x:(@) "*" y:@ { x * y }D x:(@) "/" y:@ { x / y }DVDD$ x:@ "^" y:(@) { x.pow(y as u32) }D'VDE n:number() { n }E "(" e:arithmetic() ")" { e }E",E # }}DE # fn main() {}EI ## Input typesIIH The first line of the grammar declares an input type. This is normallyIKL `str`, but `rust-peg` handles input types through a series of traits. TheJOM library comes with implementations for `str`, `[u8]`, and `[T]`. Define theKP@ traits below to use your own types as input to `peg` grammars:KCLa * [`Parse`] is the base trait required for all inputs. The others are only required to use theLd corresponding expressions.M"b * [`ParseElem`] implements the `[_]` pattern operator, with a method returning the next item ofMe the input to match.ܢNG * [`ParseLiteral`] implements matching against a `"string"` literal.NJ\ * [`ParseSlice`] implements the `$()` operator, returning a slice from a span of indexes.O_OL As a more complex example, the body of the `peg::parser!{}` macro itself isOOL parsed with `peg`, using a [definition of these traits][gh-flat-token-tree]PO* for a type that wraps Rust's `TokenTree`.Q-Q_ [gh-flat-token-tree]: https://github.com/kevinmehall/rust-peg/blob/master/peg-macros/tokens.rsQbR ## End-of-file handlingܦRRa Normally, parsers report an error if the top-level rule matches without consuming all the input.RdZ To allow matching a prefix of the input, add the `#[no_eof]` attribute before `pub rule`.S]Z Take care to not miss a malformed `x` at the last position if the rule ends with a `x()*`T] repeat expression.TT ## Rule parametersUUX Rules can be parameterized with types, lifetimes, and values, just like Rust functions.U[Ua In addition to Rust values, rules can also accept PEG expression fragments as arguments by usingUda `rule` as a parameter type. When calling such a rule, use `<>` around a PEG expression in theVdA argument list to capture the expression and pass it to the rule.WDX For example:XXTXUX*" rule num_radix(radix: u32) -> u32X%K = n:$(['0'..='9']+) {? u32::from_str_radix(n, radix).or(Err("number")) }YNYE rule list(x: rule) -> Vec = "[" v:(x() ** ",") ","? "]" {v}YHZ9 pub rule octal_list() -> Vec = list()Z<XDZXZ<[[ ## Failure reportingĎ[[V When a match fails, position information is automatically recorded to report a set of[YI "expected" tokens that would have allowed the parser to advance further.\L\Z Some rules should never appear in error messages, and can be suppressed with `quiet!{e}`:\]T]U]*1 rule whitespace() = quiet!{[' ' | '\n' | '\t']+}]4XD^X^<^^_ If you want the "expected" set to contain a more helpful string instead of character sets, you^b/ can use `quiet!{}` and `expected!()` together:_2_T_U_* rule identifier()`J = quiet!{[ 'a'..='z' | 'A'..='Z']['a'..='z' | 'A'..='Z' | '0'..='9' ]*}`M / expected!("identifier")aXDaXa Vec = "[...]" { vec![] }g5XDgXg Y a e !g <WWWugv#yvLPGBFfj SaJk!'R$kKJJJJKODHT 9ҢVxi`[EhPI'יs9K$Rv0VG~1 \_q`E;|gq`E;|g]/Users/filozopdasha/.cargo/registry/src/index.crates.io-6f17d22bba15001f/peg-0.8.4/src/lib.rs v`ecmo-5>vO;NGNL -C?Q#+OKNQ-QQ6#QHNQ?V &<$.T9]3o3]CR\@KPCb+QOM?OOOOQN+`/^`dcCcBw^pBV$Q*N4N.g^!c*4dIB7J`2^L(OSF!aL`=\$b+-0  ( # a\`a?LPQDe#fK`PP.ce^^\eeE+&OI= ZM^+5 c3+N )\cCc+#6 b"PQ@HPNQP;U8S,OL %)784% CR E