chore: checkpoint before Python removal

This commit is contained in:
2026-03-26 22:33:59 +00:00
parent 683cec9307
commit e568ddf82a
29972 changed files with 11269302 additions and 2 deletions

1
vendor/pest_meta/.cargo-checksum.json vendored Normal file
View File

@@ -0,0 +1 @@
{"files":{".cargo_vcs_info.json":"b19dccd327f9dc8a6be4ff326d266d38f90dd201ab127e743b9e6020da987f15","Cargo.lock":"c656829b4503b3293a9d93501ed822a0f4e2a598c1651843d87871e12fd6a493","Cargo.toml":"c88afc43565ef8c974ab08c3da68cc8882794044f1a2d726f1088582a7ac7db8","Cargo.toml.orig":"63649c71da8e3e29fa0ab5b4be7e3f0798f62bb71340d1675816944d2dd92abe","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","_README.md":"214e60d12802a3c5990520ff21e6e8a788b800cb49b16663e45ebdc417202ac4","src/ast.rs":"dff8556744346d303167daf99797e924b07e68eaa55fc2bb1a4a6e7fe5ee0fd3","src/grammar.pest":"95d16d89b032c47271cfd62fb51e65eb86713bb812d28386c8f2debe02301ac9","src/grammar.rs":"cf1f54c85a8a007b8cbb359486857f2c1d8893a89babf63d9dcb990c0ebc7682","src/lib.rs":"7474c24022985fde808fe026486bd6681f79cd7e855779c23a43d1418eb00d97","src/optimizer/concatenator.rs":"ab613bedc7001ab2057a8afee295ee72fc5ba7fc1017f6806477b8caa37af3c8","src/optimizer/factorizer.rs":"b79591db09a2066315f30398a5a2911db19c982ce49bbc7aa6acce894b4e3ede","src/optimizer/lister.rs":"01acfdcadd197c6a1d9dcf313ea9d0bf13983ef34a1c8638e5b439036b7ee924","src/optimizer/mod.rs":"40868d597b7147c9bf6c96dc8778038d8ae18bfbb35bf9a45d6ea4d1dc9df8b1","src/optimizer/restorer.rs":"04c966b5add5b4b48eb7ffe5d4e3c05729fb09478609e6c77c6b723ec17dbd64","src/optimizer/rotator.rs":"08da3716296a6bd7d1872b3dacd961814b87854cdd0d9c714974507fe7aca307","src/optimizer/skipper.rs":"d786647fa1fdd541015bc195f9ff18ea5618ccca9dcb70ee13638813a89c5936","src/optimizer/unroller.rs":"f032acb6b13cfd12aaada312209db8c23e54d5159d9e7283c59887fe5b03aba0","src/parser.rs":"4a9370cd3b5f0cc8bcb8e1be7a37cc87790aa033d69d035b3e295246e16f7442","src/validator.rs":"55352aacdb173fdf8dcd076794e76cc7f9634c95d69855556910db7c298077fd"},"package":"89815c69d36021a140146f26659a81d6c2afa33d216d736dd4be5381a7362220"}

7
vendor/pest_meta/.cargo_vcs_info.json vendored Normal file
View File

@@ -0,0 +1,7 @@
{
"git": {
"sha1": "79dd30d11aab6f0fba3cd79bd48f456209b966b3",
"dirty": true
},
"path_in_vcs": "meta"
}

3957
vendor/pest_meta/Cargo.lock generated vendored Normal file

File diff suppressed because it is too large Load Diff

64
vendor/pest_meta/Cargo.toml vendored Normal file
View File

@@ -0,0 +1,64 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
rust-version = "1.83"
name = "pest_meta"
version = "2.8.6"
authors = ["Dragoș Tiselice <dragostiselice@gmail.com>"]
build = false
exclude = ["src/grammar.pest"]
include = [
"Cargo.toml",
"src/**/*",
"src/grammar.rs",
"_README.md",
"LICENSE-*",
]
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "pest meta language parser and validator"
homepage = "https://pest.rs/"
documentation = "https://docs.rs/pest"
readme = "_README.md"
keywords = [
"pest",
"parser",
"meta",
"optimizer",
]
categories = ["parsing"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/pest-parser/pest"
[features]
default = []
grammar-extras = []
not-bootstrap-in-src = ["dep:cargo"]
[lib]
name = "pest_meta"
path = "src/lib.rs"
[dependencies.pest]
version = "2.8.6"
[build-dependencies.cargo]
version = "0.81.0"
optional = true
[build-dependencies.sha2]
version = "0.10"
default-features = false

201
vendor/pest_meta/LICENSE-APACHE vendored Normal file
View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

23
vendor/pest_meta/LICENSE-MIT vendored Normal file
View File

@@ -0,0 +1,23 @@
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

233
vendor/pest_meta/_README.md vendored Normal file
View File

@@ -0,0 +1,233 @@
<p align="center">
<img src="https://raw.github.com/pest-parser/pest/master/pest-logo.svg?sanitize=true" width="80%"/>
</p>
# pest. The Elegant Parser
[![Join the chat at https://gitter.im/pest-parser/pest](https://badges.gitter.im/dragostis/pest.svg)](https://gitter.im/pest-parser/pest?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Book](https://img.shields.io/badge/book-WIP-4d76ae.svg)](https://pest.rs/book)
[![Docs](https://docs.rs/pest/badge.svg)](https://docs.rs/pest)
[![pest Continuous Integration](https://github.com/pest-parser/pest/actions/workflows/ci.yml/badge.svg)](https://github.com/pest-parser/pest/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/pest-parser/pest/branch/master/graph/badge.svg)](https://codecov.io/gh/pest-parser/pest)
<a href="https://blog.rust-lang.org/2024/11/28/Rust-1.83.0/"><img alt="Rustc Version 1.83.0+" src="https://img.shields.io/badge/rustc-1.83.0%2B-lightgrey.svg"/></a>
[![Crates.io](https://img.shields.io/crates/d/pest.svg)](https://crates.io/crates/pest)
[![Crates.io](https://img.shields.io/crates/v/pest.svg)](https://crates.io/crates/pest)
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.
[PEG]: https://en.wikipedia.org/wiki/Parsing_expression_grammar
## 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]
* find previous common questions answered or ask questions on [GitHub Discussions]
* leave feedback, ask questions, or greet us on [Gitter] or [Discord]
[book]: https://pest.rs/book
[docs.rs]: https://docs.rs/pest
[fiddle]: https://pest.rs/#editor
[Gitter]: https://gitter.im/pest-parser/pest
[Discord]: https://discord.gg/XEGACtWpT2
[GitHub Discussions]: https://github.com/pest-parser/pest/discussions
## Example
The following is an example of a grammar for a list of alphanumeric identifiers
where all identifiers don't start with a digit:
```rust
alpha = { 'a'..'z' | 'A'..'Z' }
digit = { '0'..'9' }
ident = { !digit ~ (alpha | digit)+ }
ident_list = _{ ident ~ (" " ~ ident)* }
// ^
// ident_list rule is silent which means it produces no tokens
```
Grammars are saved in separate .pest files which are never mixed with procedural
code. This results in an always up-to-date formalization of a language that is
easy to read and maintain.
## Meaningful error reporting
Based on the grammar definition, the parser also includes automatic error
reporting. For the example above, the input `"123"` will result in:
```
thread 'main' panicked at ' --> 1:1
|
1 | 123
| ^---
|
= unexpected digit', src/main.rs:12
```
while `"ab *"` will result in:
```
thread 'main' panicked at ' --> 1:1
|
1 | ab *
| ^---
|
= expected ident', src/main.rs:12
```
These error messages can be obtained from their default `Display` implementation,
e.g. `panic!("{}", parser_result.unwrap_err())` or `println!("{}", e)`.
## Pairs API
The grammar can be used to derive a `Parser` implementation automatically.
Parsing returns an iterator of nested token pairs:
```rust
use pest_derive::Parser;
use pest::Parser;
#[derive(Parser)]
#[grammar = "ident.pest"]
struct IdentParser;
fn main() {
let pairs = IdentParser::parse(Rule::ident_list, "a1 b2").unwrap_or_else(|e| panic!("{}", e));
// Because ident_list is silent, the iterator will contain idents
for pair in pairs {
// A pair is a combination of the rule which matched and a span of input
println!("Rule: {:?}", pair.as_rule());
println!("Span: {:?}", pair.as_span());
println!("Text: {}", pair.as_str());
// A pair can be converted to an iterator of the tokens which make it up:
for inner_pair in pair.into_inner() {
match inner_pair.as_rule() {
Rule::alpha => println!("Letter: {}", inner_pair.as_str()),
Rule::digit => println!("Digit: {}", inner_pair.as_str()),
_ => unreachable!()
};
}
}
}
```
This produces the following output:
```
Rule: ident
Span: Span { start: 0, end: 2 }
Text: a1
Letter: a
Digit: 1
Rule: ident
Span: Span { start: 3, end: 5 }
Text: b2
Letter: b
Digit: 2
```
### Defining multiple parsers in a single file
The current automatic `Parser` derivation will produce the `Rule` enum
which would have name conflicts if one tried to define multiple such structs
that automatically derive `Parser`. One possible way around it is to put each
parser struct in a separate namespace:
```rust
mod a {
#[derive(Parser)]
#[grammar = "a.pest"]
pub struct ParserA;
}
mod b {
#[derive(Parser)]
#[grammar = "b.pest"]
pub struct ParserB;
}
```
## Other features
* Precedence climbing
* Input handling
* Custom errors
* Runs on stable Rust
## Projects using pest
You can find more projects and ecosystem tools in the [awesome-pest](https://github.com/pest-parser/awesome-pest) repo.
* [pest_meta](https://github.com/pest-parser/pest/blob/master/meta/src/grammar.pest) (bootstrapped)
* [AshPaper](https://github.com/shnewto/ashpaper)
* [brain](https://github.com/brain-lang/brain)
* [cicada](https://github.com/mitnk/cicada)
* [comrak](https://github.com/kivikakk/comrak)
* [elastic-rs](https://github.com/cch123/elastic-rs)
* [graphql-parser](https://github.com/Keats/graphql-parser)
* [handlebars-rust](https://github.com/sunng87/handlebars-rust)
* [hexdino](https://github.com/Luz/hexdino)
* [Huia](https://gitlab.com/jimsy/huia/)
* [insta](https://github.com/mitsuhiko/insta)
* [jql](https://github.com/yamafaktory/jql)
* [json5-rs](https://github.com/callum-oakley/json5-rs)
* [mt940](https://github.com/svenstaro/mt940-rs)
* [Myoxine](https://github.com/d3bate/myoxine)
* [py_literal](https://github.com/jturner314/py_literal)
* [rouler](https://github.com/jarcane/rouler)
* [RuSh](https://github.com/lwandrebeck/RuSh)
* [rs_pbrt](https://github.com/wahn/rs_pbrt)
* [stache](https://github.com/dgraham/stache)
* [tera](https://github.com/Keats/tera)
* [ui_gen](https://github.com/emoon/ui_gen)
* [ukhasnet-parser](https://github.com/adamgreig/ukhasnet-parser)
* [ZoKrates](https://github.com/ZoKrates/ZoKrates)
* [Vector](https://github.com/timberio/vector)
* [AutoCorrect](https://github.com/huacnlee/autocorrect)
* [yaml-peg](https://github.com/aofdev/yaml-peg)
* [qubit](https://github.com/abhimanyu003/qubit)
* [caith](https://github.com/Geobert/caith) (a dice roller crate)
* [Melody](https://github.com/yoav-lavi/melody)
* [json5-nodes](https://github.com/jlyonsmith/json5-nodes)
* [prisma](https://github.com/prisma/prisma)
* [ws2markdown](https://code.rosaelefanten.org/ws2markdown) (a WordStar to Markdown converter)
## Minimum Supported Rust Version (MSRV)
This library should always compile with default features on **Rust 1.83.0**.
## no_std support
The `pest` and `pest_derive` crates can be built without the Rust standard
library and target embedded environments. To do so, you need to disable
their default features. In your `Cargo.toml`, you can specify it as follows:
```toml
[dependencies]
# ...
pest = { version = "2", default-features = false }
pest_derive = { version = "2", default-features = false }
```
If you want to build these crates in the pest repository's workspace, you can
pass the `--no-default-features` flag to `cargo` and specify these crates using
the `--package` (`-p`) flag. For example:
```bash
$ cargo build --target thumbv7em-none-eabihf --no-default-features -p pest
$ cargo bootstrap
$ cargo build --target thumbv7em-none-eabihf --no-default-features -p pest_derive
```
## Special thanks
A special round of applause goes to prof. Marius Minea for his guidance and all
pest contributors, some of which being none other than my friends.

708
vendor/pest_meta/src/ast.rs vendored Normal file
View File

@@ -0,0 +1,708 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
//! Types for the pest's abstract syntax tree.
/// A grammar rule
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rule {
/// The name of the rule
pub name: String,
/// The rule's type (silent, atomic, ...)
pub ty: RuleType,
/// The rule's expression
pub expr: Expr,
}
/// All possible rule types
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RuleType {
/// The normal rule type
Normal,
/// Silent rules are just like normal rules
/// — when run, they function the same way —
/// except they do not produce pairs or tokens.
/// If a rule is silent, it will never appear in a parse result.
/// (their syntax is `_{ ... }`)
Silent,
/// atomic rule prevent implicit whitespace: inside an atomic rule,
/// the tilde ~ means "immediately followed by",
/// and repetition operators (asterisk * and plus sign +)
/// have no implicit separation. In addition, all other rules
/// called from an atomic rule are also treated as atomic.
/// In an atomic rule, interior matching rules are silent.
/// (their syntax is `@{ ... }`)
Atomic,
/// Compound atomic rules are similar to atomic rules,
/// but they produce inner tokens as normal.
/// (their syntax is `${ ... }`)
CompoundAtomic,
/// Non-atomic rules cancel the effect of atomic rules.
/// (their syntax is `!{ ... }`)
NonAtomic,
}
/// All possible rule expressions
///
/// # Warning: Semantic Versioning
/// There may be non-breaking changes to the meta-grammar
/// between minor versions. Those non-breaking changes, however,
/// may translate into semver-breaking changes due to the additional variants
/// propagated from the `Rule` enum. This is a known issue and will be fixed in the
/// future (e.g. by increasing MSRV and non_exhaustive annotations).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Expr {
/// Matches an exact string, e.g. `"a"`
Str(String),
/// Matches an exact string, case insensitively (ASCII only), e.g. `^"a"`
Insens(String),
/// Matches one character in the range, e.g. `'a'..'z'`
Range(String, String),
/// Matches the rule with the given name, e.g. `a`
Ident(String),
/// Matches a custom part of the stack, e.g. `PEEK[..]`
PeekSlice(i32, Option<i32>),
/// Positive lookahead; matches expression without making progress, e.g. `&e`
PosPred(Box<Expr>),
/// Negative lookahead; matches if expression doesn't match, without making progress, e.g. `!e`
NegPred(Box<Expr>),
/// Matches a sequence of two expressions, e.g. `e1 ~ e2`
Seq(Box<Expr>, Box<Expr>),
/// Matches either of two expressions, e.g. `e1 | e2`
Choice(Box<Expr>, Box<Expr>),
/// Optionally matches an expression, e.g. `e?`
Opt(Box<Expr>),
/// Matches an expression zero or more times, e.g. `e*`
Rep(Box<Expr>),
/// Matches an expression one or more times, e.g. `e+`
RepOnce(Box<Expr>),
/// Matches an expression an exact number of times, e.g. `e{n}`
RepExact(Box<Expr>, u32),
/// Matches an expression at least a number of times, e.g. `e{n,}`
RepMin(Box<Expr>, u32),
/// Matches an expression at most a number of times, e.g. `e{,n}`
RepMax(Box<Expr>, u32),
/// Matches an expression a number of times within a range, e.g. `e{m, n}`
RepMinMax(Box<Expr>, u32, u32),
/// Continues to match expressions until one of the strings in the `Vec` is found
Skip(Vec<String>),
/// Matches an expression and pushes it to the stack, e.g. `push(e)`
Push(Box<Expr>),
/// Pushes a literal string to the stack, e.g. `push_literal("a")`
#[cfg(feature = "grammar-extras")]
PushLiteral(String),
/// Matches an expression and assigns a label to it, e.g. #label = exp
#[cfg(feature = "grammar-extras")]
NodeTag(Box<Expr>, String),
}
impl Expr {
/// Returns the iterator that steps the expression from top to bottom.
pub fn iter_top_down(&self) -> ExprTopDownIterator {
ExprTopDownIterator::new(self)
}
/// Applies `f` to the expression and all its children (top to bottom).
pub fn map_top_down<F>(self, mut f: F) -> Expr
where
F: FnMut(Expr) -> Expr,
{
fn map_internal<F>(expr: Expr, f: &mut F) -> Expr
where
F: FnMut(Expr) -> Expr,
{
let expr = f(expr);
match expr {
Expr::PosPred(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::PosPred(mapped)
}
Expr::NegPred(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::NegPred(mapped)
}
Expr::Seq(lhs, rhs) => {
let mapped_lhs = Box::new(map_internal(*lhs, f));
let mapped_rhs = Box::new(map_internal(*rhs, f));
Expr::Seq(mapped_lhs, mapped_rhs)
}
Expr::Choice(lhs, rhs) => {
let mapped_lhs = Box::new(map_internal(*lhs, f));
let mapped_rhs = Box::new(map_internal(*rhs, f));
Expr::Choice(mapped_lhs, mapped_rhs)
}
Expr::Rep(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::Rep(mapped)
}
Expr::RepOnce(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepOnce(mapped)
}
Expr::RepExact(expr, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepExact(mapped, max)
}
Expr::RepMin(expr, num) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMin(mapped, num)
}
Expr::RepMax(expr, num) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMax(mapped, num)
}
Expr::RepMinMax(expr, min, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMinMax(mapped, min, max)
}
Expr::Opt(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::Opt(mapped)
}
Expr::Push(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::Push(mapped)
}
#[cfg(feature = "grammar-extras")]
Expr::NodeTag(expr, tag) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::NodeTag(mapped, tag)
}
expr => expr,
}
}
map_internal(self, &mut f)
}
/// Applies `f` to the expression and all its children (bottom up).
pub fn map_bottom_up<F>(self, mut f: F) -> Expr
where
F: FnMut(Expr) -> Expr,
{
fn map_internal<F>(expr: Expr, f: &mut F) -> Expr
where
F: FnMut(Expr) -> Expr,
{
let mapped = match expr {
Expr::PosPred(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::PosPred(mapped)
}
Expr::NegPred(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::NegPred(mapped)
}
Expr::Seq(lhs, rhs) => {
let mapped_lhs = Box::new(map_internal(*lhs, f));
let mapped_rhs = Box::new(map_internal(*rhs, f));
Expr::Seq(mapped_lhs, mapped_rhs)
}
Expr::Choice(lhs, rhs) => {
let mapped_lhs = Box::new(map_internal(*lhs, f));
let mapped_rhs = Box::new(map_internal(*rhs, f));
Expr::Choice(mapped_lhs, mapped_rhs)
}
Expr::Rep(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::Rep(mapped)
}
Expr::RepOnce(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepOnce(mapped)
}
Expr::RepExact(expr, num) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepExact(mapped, num)
}
Expr::RepMin(expr, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMin(mapped, max)
}
Expr::RepMax(expr, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMax(mapped, max)
}
Expr::RepMinMax(expr, min, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMinMax(mapped, min, max)
}
Expr::Opt(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::Opt(mapped)
}
Expr::Push(expr) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::Push(mapped)
}
#[cfg(feature = "grammar-extras")]
Expr::NodeTag(expr, tag) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::NodeTag(mapped, tag)
}
expr => expr,
};
f(mapped)
}
map_internal(self, &mut f)
}
}
impl core::fmt::Display for Expr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Expr::Str(s) => write!(f, "{:?}", s),
Expr::Insens(s) => write!(f, "^{:?}", s),
Expr::Range(start, end) => {
let start = start.chars().next().expect("Empty range start.");
let end = end.chars().next().expect("Empty range end.");
write!(f, "({:?}..{:?})", start, end)
}
Expr::Ident(id) => write!(f, "{}", id),
Expr::PeekSlice(start, end) => match end {
Some(end) => write!(f, "PEEK[{}..{}]", start, end),
None => write!(f, "PEEK[{}..]", start),
},
Expr::PosPred(expr) => write!(f, "&{}", expr.as_ref()),
Expr::NegPred(expr) => write!(f, "!{}", expr.as_ref()),
Expr::Seq(lhs, rhs) => {
let mut nodes = Vec::new();
nodes.push(lhs);
let mut current = rhs;
while let Expr::Seq(lhs, rhs) = current.as_ref() {
nodes.push(lhs);
current = rhs;
}
nodes.push(current);
let sequence = nodes
.iter()
.map(|node| format!("{}", node))
.collect::<Vec<_>>()
.join(" ~ ");
write!(f, "({})", sequence)
}
Expr::Choice(lhs, rhs) => {
let mut nodes = Vec::new();
nodes.push(lhs);
let mut current = rhs;
while let Expr::Choice(lhs, rhs) = current.as_ref() {
nodes.push(lhs);
current = rhs;
}
nodes.push(current);
let sequence = nodes
.iter()
.map(|node| format!("{}", node))
.collect::<Vec<_>>()
.join(" | ");
write!(f, "({})", sequence)
}
Expr::Opt(expr) => write!(f, "{}?", expr),
Expr::Rep(expr) => write!(f, "{}*", expr),
Expr::RepOnce(expr) => write!(f, "{}+", expr),
Expr::RepExact(expr, n) => write!(f, "{}{{{}}}", expr, n),
Expr::RepMin(expr, min) => write!(f, "{}{{{},}}", expr, min),
Expr::RepMax(expr, max) => write!(f, "{}{{,{}}}", expr, max),
Expr::RepMinMax(expr, min, max) => write!(f, "{}{{{}, {}}}", expr, min, max),
Expr::Skip(strings) => {
let strings = strings
.iter()
.map(|s| format!("{:?}", s))
.collect::<Vec<_>>()
.join(" | ");
write!(f, "(!({}) ~ ANY)*", strings)
}
Expr::Push(expr) => write!(f, "PUSH({})", expr),
#[cfg(feature = "grammar-extras")]
Expr::PushLiteral(s) => write!(f, "PUSH_LITERAL({:?})", s),
#[cfg(feature = "grammar-extras")]
Expr::NodeTag(expr, tag) => {
write!(f, "(#{} = {})", tag, expr)
}
}
}
}
/// The top down iterator for an expression.
pub struct ExprTopDownIterator {
current: Option<Expr>,
next: Option<Expr>,
right_branches: Vec<Expr>,
}
impl ExprTopDownIterator {
/// Constructs a top-down iterator from the expression.
pub fn new(expr: &Expr) -> Self {
let mut iter = ExprTopDownIterator {
current: None,
next: None,
right_branches: vec![],
};
iter.iterate_expr(expr.clone());
iter
}
fn iterate_expr(&mut self, expr: Expr) {
self.current = Some(expr.clone());
match expr {
Expr::Seq(lhs, rhs) => {
self.right_branches.push(*rhs);
self.next = Some(*lhs);
}
Expr::Choice(lhs, rhs) => {
self.right_branches.push(*rhs);
self.next = Some(*lhs);
}
Expr::PosPred(expr)
| Expr::NegPred(expr)
| Expr::Rep(expr)
| Expr::RepOnce(expr)
| Expr::RepExact(expr, _)
| Expr::RepMin(expr, _)
| Expr::RepMax(expr, _)
| Expr::RepMinMax(expr, ..)
| Expr::Opt(expr)
| Expr::Push(expr) => {
self.next = Some(*expr);
}
#[cfg(feature = "grammar-extras")]
Expr::NodeTag(expr, _) => {
self.next = Some(*expr);
}
_ => {
self.next = None;
}
}
}
}
impl Iterator for ExprTopDownIterator {
type Item = Expr;
fn next(&mut self) -> Option<Self::Item> {
let result = self.current.take();
if let Some(expr) = self.next.take() {
self.iterate_expr(expr);
} else if let Some(expr) = self.right_branches.pop() {
self.iterate_expr(expr);
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn top_down_iterator() {
let expr = Expr::Choice(
Box::new(Expr::Str(String::from("a"))),
Box::new(Expr::Str(String::from("b"))),
);
let mut top_down = expr.iter_top_down();
assert_eq!(top_down.next(), Some(expr));
assert_eq!(top_down.next(), Some(Expr::Str(String::from("a"))));
assert_eq!(top_down.next(), Some(Expr::Str(String::from("b"))));
assert_eq!(top_down.next(), None);
}
#[test]
fn identity() {
let expr = Expr::Choice(
Box::new(Expr::Seq(
Box::new(Expr::Ident("a".to_owned())),
Box::new(Expr::Str("b".to_owned())),
)),
Box::new(Expr::PosPred(Box::new(Expr::NegPred(Box::new(Expr::Rep(
Box::new(Expr::RepOnce(Box::new(Expr::Opt(Box::new(Expr::Choice(
Box::new(Expr::Insens("c".to_owned())),
Box::new(Expr::Push(Box::new(Expr::Range(
"'d'".to_owned(),
"'e'".to_owned(),
)))),
)))))),
)))))),
);
assert_eq!(
expr.clone()
.map_bottom_up(|expr| expr)
.map_top_down(|expr| expr),
expr,
);
}
mod display {
use super::super::*;
#[test]
fn string() {
assert_eq!(Expr::Str("a".to_owned()).to_string(), r#""a""#);
}
#[test]
fn insens() {
assert_eq!(Expr::Insens("a".to_owned()).to_string(), r#"^"a""#);
}
#[test]
fn range() {
assert_eq!(
Expr::Range("a".to_owned(), "z".to_owned()).to_string(),
r#"('a'..'z')"#,
);
}
#[test]
fn ident() {
assert_eq!(Expr::Ident("a".to_owned()).to_string(), "a");
}
#[test]
fn peek_slice() {
assert_eq!(Expr::PeekSlice(0, None).to_string(), "PEEK[0..]");
assert_eq!(Expr::PeekSlice(0, Some(-1)).to_string(), "PEEK[0..-1]");
}
#[test]
fn pos_pred() {
assert_eq!(
Expr::PosPred(Box::new(Expr::Ident("e".to_owned()))).to_string(),
"&e",
);
}
#[test]
fn neg_pred() {
assert_eq!(
Expr::NegPred(Box::new(Expr::Ident("e".to_owned()))).to_string(),
"!e",
);
}
#[test]
fn seq() {
assert_eq!(
Expr::Seq(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Ident("e2".to_owned())),
)
.to_string(),
"(e1 ~ e2)",
);
assert_eq!(
Expr::Seq(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Ident("e2".to_owned())),
Box::new(Expr::Ident("e3".to_owned())),
)),
)
.to_string(),
"(e1 ~ e2 ~ e3)",
);
assert_eq!(
Expr::Seq(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Ident("e2".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Ident("e3".to_owned())),
Box::new(Expr::Ident("e4".to_owned())),
)),
)),
)
.to_string(),
"(e1 ~ e2 ~ e3 ~ e4)",
);
assert_eq!(
Expr::Seq(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Choice(
Box::new(Expr::Ident("e2".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Ident("e3".to_owned())),
Box::new(Expr::Ident("e4".to_owned())),
)),
)),
)
.to_string(),
"(e1 ~ (e2 | (e3 ~ e4)))",
);
assert_eq!(
Expr::Seq(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Ident("e2".to_owned())),
Box::new(Expr::Choice(
Box::new(Expr::Ident("e3".to_owned())),
Box::new(Expr::Ident("e4".to_owned())),
)),
)),
)
.to_string(),
"(e1 ~ e2 ~ (e3 | e4))",
);
}
#[test]
fn choice() {
assert_eq!(
Expr::Choice(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Ident("e2".to_owned())),
)
.to_string(),
"(e1 | e2)",
);
assert_eq!(
Expr::Choice(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Choice(
Box::new(Expr::Ident("e2".to_owned())),
Box::new(Expr::Ident("e3".to_owned())),
)),
)
.to_string(),
"(e1 | e2 | e3)",
);
assert_eq!(
Expr::Choice(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Choice(
Box::new(Expr::Ident("e2".to_owned())),
Box::new(Expr::Choice(
Box::new(Expr::Ident("e3".to_owned())),
Box::new(Expr::Ident("e4".to_owned())),
)),
)),
)
.to_string(),
"(e1 | e2 | e3 | e4)",
);
assert_eq!(
Expr::Choice(
Box::new(Expr::Ident("e1".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Ident("e2".to_owned())),
Box::new(Expr::Choice(
Box::new(Expr::Ident("e3".to_owned())),
Box::new(Expr::Ident("e4".to_owned())),
)),
)),
)
.to_string(),
"(e1 | (e2 ~ (e3 | e4)))",
);
}
#[test]
fn opt() {
assert_eq!(
Expr::Opt(Box::new(Expr::Ident("e".to_owned()))).to_string(),
"e?",
);
}
#[test]
fn rep() {
assert_eq!(
Expr::Rep(Box::new(Expr::Ident("e".to_owned()))).to_string(),
"e*",
);
}
#[test]
fn rep_once() {
assert_eq!(
Expr::RepOnce(Box::new(Expr::Ident("e".to_owned()))).to_string(),
"e+",
);
}
#[test]
fn rep_exact() {
assert_eq!(
Expr::RepExact(Box::new(Expr::Ident("e".to_owned())), 1).to_string(),
"e{1}",
);
}
#[test]
fn rep_min() {
assert_eq!(
Expr::RepMin(Box::new(Expr::Ident("e".to_owned())), 1).to_string(),
"e{1,}",
);
}
#[test]
fn rep_max() {
assert_eq!(
Expr::RepMax(Box::new(Expr::Ident("e".to_owned())), 1).to_string(),
"e{,1}",
);
}
#[test]
fn rep_min_max() {
assert_eq!(
Expr::RepMinMax(Box::new(Expr::Ident("e".to_owned())), 1, 2).to_string(),
"e{1, 2}",
);
}
#[test]
fn skip() {
assert_eq!(
Expr::Skip(
["a", "bc"]
.into_iter()
.map(|s| s.to_owned())
.collect::<Vec<_>>(),
)
.to_string(),
r#"(!("a" | "bc") ~ ANY)*"#,
);
}
#[test]
fn push() {
assert_eq!(
Expr::Push(Box::new(Expr::Ident("e".to_owned()))).to_string(),
"PUSH(e)",
);
}
#[test]
#[cfg(feature = "grammar-extras")]
fn push_literal() {
assert_eq!(
Expr::PushLiteral("one \" ' two".to_string()).to_string(),
r#"PUSH_LITERAL("one \" ' two")"#
)
}
#[test]
#[cfg(feature = "grammar-extras")]
fn node_tag() {
assert_eq!(
Expr::NodeTag(Box::new(Expr::Ident("expr".to_owned())), "label".to_owned())
.to_string(),
"(#label = expr)",
);
}
}
}

228
vendor/pest_meta/src/grammar.pest vendored Normal file
View File

@@ -0,0 +1,228 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
//! Pest meta-grammar
//!
//! # Warning: Semantic Versioning
//! There may be non-breaking changes to the meta-grammar
//! between minor versions. Those non-breaking changes, however,
//! may translate into semver-breaking changes due to the additional variants
//! added to the `Rule` enum. This is a known issue and will be fixed in the
//! future (e.g. by increasing MSRV and non_exhaustive annotations).
/// The top-level rule of a grammar.
grammar_rules = _{ SOI ~ grammar_doc* ~ grammar_rule* ~ EOI }
/// A rule of a grammar.
grammar_rule = {
identifier ~ assignment_operator ~ modifier? ~ opening_brace ~ expression ~ closing_brace
| line_doc
}
/// Assignment operator.
assignment_operator = { "=" }
/// Opening brace for a rule.
opening_brace = { "{" }
/// Closing brace for a rule.
closing_brace = { "}" }
/// Opening parenthesis for a branch, PUSH, etc.
opening_paren = { "(" }
/// Closing parenthesis for a branch, PUSH, etc.
closing_paren = { ")" }
/// Opening bracket for PEEK (slice inside).
opening_brack = { "[" }
/// Closing bracket for PEEK (slice inside).
closing_brack = { "]" }
/// A rule modifier.
modifier = _{
silent_modifier
| atomic_modifier
| compound_atomic_modifier
| non_atomic_modifier
}
/// Silent rule prefix.
silent_modifier = { "_" }
/// Atomic rule prefix.
atomic_modifier = { "@" }
/// Compound atomic rule prefix.
compound_atomic_modifier = { "$" }
/// Non-atomic rule prefix.
non_atomic_modifier = { "!" }
/// A tag label.
tag_id = @{ "#" ~ ("_" | alpha) ~ ("_" | alpha_num)* }
/// For assigning labels to nodes.
node_tag = _{ tag_id ~ assignment_operator }
/// A rule expression.
expression = { choice_operator? ~ term ~ (infix_operator ~ term)* }
/// A rule term.
term = { node_tag? ~ prefix_operator* ~ node ~ postfix_operator* }
/// A rule node (inside terms).
node = _{ opening_paren ~ expression ~ closing_paren | terminal }
/// A terminal expression.
terminal = _{ _push_literal | _push | peek_slice | identifier | string | insensitive_string | range }
/// Possible predicates for a rule.
prefix_operator = _{ positive_predicate_operator | negative_predicate_operator }
/// Branches or sequences.
infix_operator = _{ sequence_operator | choice_operator }
/// Possible modifiers for a rule.
postfix_operator = _{
optional_operator
| repeat_operator
| repeat_once_operator
| repeat_exact
| repeat_min
| repeat_max
| repeat_min_max
}
/// A positive predicate.
positive_predicate_operator = { "&" }
/// A negative predicate.
negative_predicate_operator = { "!" }
/// A sequence operator.
sequence_operator = { "~" }
/// A choice operator.
choice_operator = { "|" }
/// An optional operator.
optional_operator = { "?" }
/// A repeat operator.
repeat_operator = { "*" }
/// A repeat at least once operator.
repeat_once_operator = { "+" }
/// A repeat exact times.
repeat_exact = { opening_brace ~ number ~ closing_brace }
/// A repeat at least times.
repeat_min = { opening_brace ~ number ~ comma ~ closing_brace }
/// A repeat at most times.
repeat_max = { opening_brace ~ comma ~ number ~ closing_brace }
/// A repeat in a range.
repeat_min_max = { opening_brace ~ number ~ comma ~ number ~ closing_brace }
/// A number.
number = @{ '0'..'9'+ }
/// An integer number (positive or negative).
integer = @{ number | "-" ~ "0"* ~ '1'..'9' ~ number? }
/// A comma terminal.
comma = { "," }
/// A PUSH expression.
_push = { "PUSH" ~ opening_paren ~ expression ~ closing_paren }
/// A PUSH_LITERAL expression with one argument, which must be a literal string.
_push_literal = { "PUSH_LITERAL" ~ opening_paren ~ string ~ closing_paren }
/// A PEEK expression.
peek_slice = { "PEEK" ~ opening_brack ~ integer? ~ range_operator ~ integer? ~ closing_brack }
/// An identifier.
identifier = @{ !"PUSH" ~ ("_" | alpha) ~ ("_" | alpha_num)* }
/// An alpha character.
alpha = _{ 'a'..'z' | 'A'..'Z' }
/// An alphanumeric character.
alpha_num = _{ alpha | '0'..'9' }
/// A string.
string = ${ quote ~ inner_str ~ quote }
/// An insensitive string.
insensitive_string = { "^" ~ string }
/// A character range.
range = { character ~ range_operator ~ character }
/// A single quoted character
character = ${ single_quote ~ inner_chr ~ single_quote }
/// A quoted string.
inner_str = @{ (!("\"" | "\\") ~ ANY)* ~ (escape ~ inner_str)? }
/// An escaped or any character.
inner_chr = @{ escape | ANY }
/// An escape sequence.
escape = @{ "\\" ~ ("\"" | "\\" | "r" | "n" | "t" | "0" | "'" | code | unicode) }
/// A hexadecimal code.
code = @{ "x" ~ hex_digit{2} }
/// A unicode code.
unicode = @{ "u" ~ opening_brace ~ hex_digit{2, 6} ~ closing_brace }
/// A hexadecimal digit.
hex_digit = @{ '0'..'9' | 'a'..'f' | 'A'..'F' }
/// A double quote.
quote = { "\"" }
/// A single quote.
single_quote = { "'" }
/// A range operator.
range_operator = { ".." }
/// A newline character.
newline = _{ "\n" | "\r\n" }
/// A whitespace character.
WHITESPACE = _{ " " | "\t" | newline }
/// A single line comment.
line_comment = _{ ("//" ~ !("/" | "!") ~ (!newline ~ ANY)*) }
/// A multi-line comment.
block_comment = _{ "/*" ~ (block_comment | !"*/" ~ ANY)* ~ "*/" }
/// A grammar comment.
COMMENT = _{ block_comment | line_comment }
// ref: https://doc.rust-lang.org/reference/comments.html
/// A space character.
space = _{ " " | "\t" }
/// A top-level comment.
grammar_doc = ${ "//!" ~ space? ~ inner_doc }
/// A rule comment.
line_doc = ${ "///" ~ space? ~ inner_doc }
/// A comment content.
inner_doc = @{ (!newline ~ ANY)* }

2
vendor/pest_meta/src/grammar.rs vendored Normal file

File diff suppressed because one or more lines are too long

76
vendor/pest_meta/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,76 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
//! # pest meta
//!
//! This crate parses, validates, optimizes, and converts pest's own grammars to ASTs.
#![doc(
html_logo_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
#[cfg(test)]
#[macro_use]
extern crate pest;
use std::fmt::Display;
use std::sync::LazyLock;
use pest::error::Error;
use pest::unicode::unicode_property_names;
pub mod ast;
pub mod optimizer;
pub mod parser;
pub mod validator;
/// A helper that will unwrap the result or panic
/// with the nicely formatted error message.
pub fn unwrap_or_report<T, E>(result: Result<T, E>) -> T
where
E: IntoIterator,
E::Item: Display,
{
result.unwrap_or_else(|e| {
panic!(
"{}{}",
"grammar error\n\n".to_owned(),
&e.into_iter()
.map(|error| format!("{}", error))
.collect::<Vec<_>>()
.join("\n\n")
)
})
}
/// A tuple returned by the validation and processing of the parsed grammar.
/// The first element is the vector of used builtin rule names,
/// the second element is the vector of optimized rules.
type UsedBuiltinAndOptimized<'i> = (Vec<&'i str>, Vec<optimizer::OptimizedRule>);
/// Parses, validates, processes and optimizes the provided grammar.
pub fn parse_and_optimize(
grammar: &str,
) -> Result<UsedBuiltinAndOptimized<'_>, Vec<Error<parser::Rule>>> {
let pairs = match parser::parse(parser::Rule::grammar_rules, grammar) {
Ok(pairs) => Ok(pairs),
Err(error) => Err(vec![error]),
}?;
let defaults = validator::validate_pairs(pairs.clone())?;
let ast = parser::consume_rules(pairs)?;
Ok((defaults, optimizer::optimize(ast)))
}
#[doc(hidden)]
#[deprecated(note = "use `pest::unicode::unicode_property_names` instead")]
pub static UNICODE_PROPERTY_NAMES: LazyLock<Vec<&str>> =
LazyLock::new(|| unicode_property_names().collect::<Vec<_>>());

View File

@@ -0,0 +1,32 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
use crate::ast::*;
pub fn concatenate(rule: Rule) -> Rule {
let Rule { name, ty, expr } = rule;
Rule {
name,
ty,
expr: expr.map_bottom_up(|expr| {
if ty == RuleType::Atomic {
match expr {
Expr::Seq(lhs, rhs) => match (*lhs, *rhs) {
(Expr::Str(lhs), Expr::Str(rhs)) => Expr::Str(lhs + &rhs),
(Expr::Insens(lhs), Expr::Insens(rhs)) => Expr::Insens(lhs + &rhs),
(lhs, rhs) => Expr::Seq(Box::new(lhs), Box::new(rhs)),
},
expr => expr,
}
} else {
expr
}
}),
}
}

View File

@@ -0,0 +1,54 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
use crate::ast::*;
pub fn factor(rule: Rule) -> Rule {
let Rule { name, ty, expr } = rule;
Rule {
name,
ty,
expr: expr.map_top_down(|expr| {
match expr {
Expr::Choice(lhs, rhs) => match (*lhs, *rhs) {
(Expr::Seq(l1, r1), Expr::Seq(l2, r2)) => {
if l1 == l2 {
Expr::Seq(l1, Box::new(Expr::Choice(r1, r2)))
} else {
Expr::Choice(Box::new(Expr::Seq(l1, r1)), Box::new(Expr::Seq(l2, r2)))
}
}
// Converts `(rule ~ rest) | rule` to `rule ~ rest?`, avoiding trying to match `rule` twice.
// This is only done for atomic rules, because other rule types have implicit whitespaces.
// FIXME: "desugar" implicit whitespace rules before applying any optimizations
(Expr::Seq(l1, l2), r)
if matches!(ty, RuleType::Atomic | RuleType::CompoundAtomic) =>
{
if *l1 == r {
Expr::Seq(l1, Box::new(Expr::Opt(l2)))
} else {
Expr::Choice(Box::new(Expr::Seq(l1, l2)), Box::new(r))
}
}
// Converts `rule | (rule ~ rest)` to `rule` since `(rule ~ rest)`
// will never match if `rule` didn't.
(l, Expr::Seq(r1, r2)) => {
if l == *r1 {
l
} else {
Expr::Choice(Box::new(l), Box::new(Expr::Seq(r1, r2)))
}
}
(lhs, rhs) => Expr::Choice(Box::new(lhs), Box::new(rhs)),
},
expr => expr,
}
}),
}
}

View File

@@ -0,0 +1,41 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
use crate::ast::*;
pub fn list(rule: Rule) -> Rule {
let Rule { name, ty, expr } = rule;
Rule {
name,
ty,
expr: expr.map_bottom_up(|expr| {
match expr {
Expr::Seq(l, r) => match *l {
Expr::Rep(l) => {
let l = *l;
match l {
Expr::Seq(l1, l2) => {
// Converts `(rule ~ rest)* ~ rule` to `rule ~ (rest ~ rule)*`,
// avoiding matching the last `rule` twice.
if l1 == r {
Expr::Seq(l1, Box::new(Expr::Rep(Box::new(Expr::Seq(l2, r)))))
} else {
Expr::Seq(Box::new(Expr::Rep(Box::new(Expr::Seq(l1, l2)))), r)
}
}
expr => Expr::Seq(Box::new(Expr::Rep(Box::new(expr))), r),
}
}
expr => Expr::Seq(Box::new(expr), r),
},
expr => expr,
}
}),
}
}

1146
vendor/pest_meta/src/optimizer/mod.rs vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,153 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
use std::collections::HashMap;
use crate::optimizer::*;
pub fn restore_on_err(
rule: OptimizedRule,
rules: &HashMap<String, OptimizedExpr>,
) -> OptimizedRule {
let OptimizedRule { name, ty, expr } = rule;
let expr = expr.map_bottom_up(|expr| wrap_branching_exprs(expr, rules));
OptimizedRule { name, ty, expr }
}
fn wrap_branching_exprs(
expr: OptimizedExpr,
rules: &HashMap<String, OptimizedExpr>,
) -> OptimizedExpr {
match expr {
OptimizedExpr::Opt(expr) => {
if child_modifies_state(&expr, rules, &mut HashMap::new()) {
OptimizedExpr::Opt(Box::new(OptimizedExpr::RestoreOnErr(expr)))
} else {
OptimizedExpr::Opt(expr)
}
}
OptimizedExpr::Choice(lhs, rhs) => {
let wrapped_lhs = if child_modifies_state(&lhs, rules, &mut HashMap::new()) {
Box::new(OptimizedExpr::RestoreOnErr(lhs))
} else {
lhs
};
let wrapped_rhs = if child_modifies_state(&rhs, rules, &mut HashMap::new()) {
Box::new(OptimizedExpr::RestoreOnErr(rhs))
} else {
rhs
};
OptimizedExpr::Choice(wrapped_lhs, wrapped_rhs)
}
OptimizedExpr::Rep(expr) => {
if child_modifies_state(&expr, rules, &mut HashMap::new()) {
OptimizedExpr::Rep(Box::new(OptimizedExpr::RestoreOnErr(expr)))
} else {
OptimizedExpr::Rep(expr)
}
}
_ => expr,
}
}
fn child_modifies_state(
expr: &OptimizedExpr,
rules: &HashMap<String, OptimizedExpr>,
cache: &mut HashMap<String, Option<bool>>,
) -> bool {
expr.iter_top_down().any(|expr| match expr {
OptimizedExpr::Push(_) => true,
OptimizedExpr::Ident(ref name) if name == "DROP" => true,
OptimizedExpr::Ident(ref name) if name == "POP" => true,
OptimizedExpr::Ident(ref name) => match cache.get(name).cloned() {
Some(option) => match option {
Some(cached) => cached,
None => {
cache.insert(name.to_owned(), Some(false));
false
}
},
None => {
cache.insert(name.to_owned(), None);
let result = match rules.get(name) {
Some(expr) => child_modifies_state(expr, rules, cache),
None => false,
};
cache.insert(name.to_owned(), Some(result));
result
}
},
_ => false,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::optimizer::OptimizedExpr::*;
#[test]
fn restore_no_stack_children() {
let rules = vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Opt(Str("a".to_string()))),
}];
assert_eq!(
restore_on_err(rules[0].clone(), &to_optimized_hash_map(&rules)),
rules[0].clone()
);
}
#[test]
fn restore_with_child_stack_ops() {
let rules = vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Rep(Push(Str("a".to_string())))),
}];
let restored = OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Rep(RestoreOnErr(Push(Str("a".to_string()))))),
};
assert_eq!(
restore_on_err(rules[0].clone(), &to_optimized_hash_map(&rules)),
restored
);
}
#[test]
fn restore_choice_branch_with_and_branch_without() {
let rules = vec![OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Choice(Push(Str("a".to_string())), Str("a".to_string()))),
}];
let restored = OptimizedRule {
name: "rule".to_owned(),
ty: RuleType::Normal,
expr: box_tree!(Choice(
RestoreOnErr(Push(Str("a".to_string()))),
Str("a".to_string())
)),
};
assert_eq!(
restore_on_err(rules[0].clone(), &to_optimized_hash_map(&rules)),
restored
);
}
}

View File

@@ -0,0 +1,43 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
use crate::ast::*;
pub fn rotate(rule: Rule) -> Rule {
fn rotate_internal(expr: Expr) -> Expr {
match expr {
Expr::Seq(lhs, rhs) => {
let lhs = *lhs;
match lhs {
Expr::Seq(ll, lr) => {
rotate_internal(Expr::Seq(ll, Box::new(Expr::Seq(lr, rhs))))
}
lhs => Expr::Seq(Box::new(lhs), rhs),
}
}
Expr::Choice(lhs, rhs) => {
let lhs = *lhs;
match lhs {
Expr::Choice(ll, lr) => {
rotate_internal(Expr::Choice(ll, Box::new(Expr::Choice(lr, rhs))))
}
lhs => Expr::Choice(Box::new(lhs), rhs),
}
}
expr => expr,
}
}
let Rule { name, ty, expr } = rule;
Rule {
name,
ty,
expr: expr.map_top_down(rotate_internal),
}
}

View File

@@ -0,0 +1,76 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
use std::collections::HashMap;
use crate::ast::*;
pub fn skip(rule: Rule, map: &HashMap<String, Expr>) -> Rule {
fn populate_choices(
expr: Expr,
map: &HashMap<String, Expr>,
mut choices: Vec<String>,
) -> Option<Expr> {
match expr {
Expr::Choice(lhs, rhs) => {
if let Expr::Str(string) = *lhs {
choices.push(string);
populate_choices(*rhs, map, choices)
} else if let Expr::Ident(name) = *lhs {
// Try inlining rule in choices
if let Some(Expr::Skip(mut inlined_choices)) = map
.get(&name)
.and_then(|expr| populate_choices(expr.clone(), map, vec![]))
{
choices.append(&mut inlined_choices);
populate_choices(*rhs, map, choices)
} else {
None
}
} else {
None
}
}
Expr::Str(string) => {
choices.push(string);
Some(Expr::Skip(choices))
}
// Try inlining single rule
Expr::Ident(name) => map
.get(&name)
.and_then(|expr| populate_choices(expr.clone(), map, choices)),
_ => None,
}
}
let Rule { name, ty, expr } = rule;
Rule {
name,
ty,
expr: if ty == RuleType::Atomic {
expr.map_top_down(|expr| {
if let Expr::Rep(expr) = expr.clone() {
if let Expr::Seq(lhs, rhs) = *expr {
if let (Expr::NegPred(expr), Expr::Ident(ident)) = (*lhs, *rhs) {
if ident == "ANY" {
if let Some(expr) = populate_choices(*expr, map, vec![]) {
return expr;
}
}
}
}
};
expr
})
} else {
expr
},
}
}

View File

@@ -0,0 +1,67 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
use crate::ast::*;
pub fn unroll(rule: Rule) -> Rule {
let Rule { name, ty, expr } = rule;
Rule {
name,
ty,
expr: expr.map_bottom_up(|expr| match expr {
#[cfg(not(feature = "grammar-extras"))]
Expr::RepOnce(expr) => Expr::Seq(expr.clone(), Box::new(Expr::Rep(expr))),
Expr::RepExact(expr, num) => (1..num + 1)
.map(|_| *expr.clone())
.rev()
.fold(None, |rep, expr| match rep {
None => Some(expr),
Some(rep) => Some(Expr::Seq(Box::new(expr), Box::new(rep))),
})
.unwrap(),
Expr::RepMin(expr, min) => (1..min + 2)
.map(|i| {
if i <= min {
*expr.clone()
} else {
Expr::Rep(expr.clone())
}
})
.rev()
.fold(None, |rep, expr| match rep {
None => Some(expr),
Some(rep) => Some(Expr::Seq(Box::new(expr), Box::new(rep))),
})
.unwrap(),
Expr::RepMax(expr, max) => (1..max + 1)
.map(|_| Expr::Opt(expr.clone()))
.rev()
.fold(None, |rep, expr| match rep {
None => Some(expr),
Some(rep) => Some(Expr::Seq(Box::new(expr), Box::new(rep))),
})
.unwrap(),
Expr::RepMinMax(expr, min, max) => (1..max + 1)
.map(|i| {
if i <= min {
*expr.clone()
} else {
Expr::Opt(expr.clone())
}
})
.rev()
.fold(None, |rep, expr| match rep {
None => Some(expr),
Some(rep) => Some(Expr::Seq(Box::new(expr), Box::new(rep))),
})
.unwrap(),
expr => expr,
}),
}
}

1847
vendor/pest_meta/src/parser.rs vendored Normal file

File diff suppressed because it is too large Load Diff

1967
vendor/pest_meta/src/validator.rs vendored Normal file

File diff suppressed because it is too large Load Diff