pest_derive-2.1.0/_README.md 0100644 0000765 0000024 00000012436 13373263000 0013663 0 ustar 00 0000000 0000000
# pest. The Elegant Parser
[](https://gitter.im/dragostis/pest?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://pest-parser.github.io/book)
[](https://docs.rs/pest)
[](https://travis-ci.org/pest-parser/pest)
[](https://codecov.io/gh/pest-parser/pest)
[](https://crates.io/crates/pest)
[](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]
* leave feedback, ask questions, or greet us on [Gitter]
[book]: https://pest-parser.github.io/book
[docs.rs]: https://docs.rs/pest
[fiddle]: https://pest-parser.github.io/#editor
[Gitter]: https://gitter.im/dragostis/pest
## Example
The following is an example of a grammar for a list of alpha-numeric identifiers
where the first identifier does not start with a digit:
```rust
alpha = { 'a'..'z' | 'A'..'Z' }
digit = { '0'..'9' }
ident = { (alpha | digit)+ }
ident_list = _{ !digit ~ 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
```
## Pairs API
The grammar can be used to derive a `Parser` implementation automatically.
Parsing returns an iterator of nested token pairs:
```rust
extern crate pest;
#[macro_use]
extern crate pest_derive;
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 {
let span = pair.clone().into_span();
// A pair is a combination of the rule which matched and a span of input
println!("Rule: {:?}", pair.as_rule());
println!("Span: {:?}", span);
println!("Text: {}", span.as_str());
// A pair can be converted to an iterator of the tokens which make it up:
for inner_pair in pair.into_inner() {
let inner_span = inner_pair.clone().into_span();
match inner_pair.as_rule() {
Rule::alpha => println!("Letter: {}", inner_span.as_str()),
Rule::digit => println!("Digit: {}", inner_span.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
```
## Other features
* Precedence climbing
* Input handling
* Custom errors
* Runs on stable Rust
## Projects using pest
* [pest_meta](https://github.com/pest-parser/pest/blob/master/meta/src/grammar.pest) (bootstrapped)
* [brain](https://github.com/brain-lang/brain)
* [Chelone](https://github.com/Aaronepower/chelone)
* [comrak](https://github.com/kivikakk/comrak)
* [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/)
* [json5-rs](https://github.com/callum-oakley/json5-rs)
* [mt940](https://github.com/svenstaro/mt940-rs)
* [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)
## 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.
pest_derive-2.1.0/Cargo.toml.orig 0100644 0000765 0000024 00000001374 13407232453 0015141 0 ustar 00 0000000 0000000 [package]
name = "pest_derive"
description = "pest's derive macro"
version = "2.1.0"
authors = ["Dragoș Tiselice "]
homepage = "https://pest-parser.github.io/"
repository = "https://github.com/pest-parser/pest"
documentation = "https://docs.rs/pest"
keywords = ["pest", "parser", "peg", "grammar"]
categories = ["parsing"]
license = "MIT/Apache-2.0"
readme = "_README.md"
[lib]
name = "pest_derive"
proc-macro = true
[dependencies]
# for tests, included transitively anyway
pest = { path = "../pest", version = "2.1.0" }
pest_generator = { path = "../generator", version = "2.1.0" }
[badges]
codecov = { repository = "pest-parser/pest" }
maintenance = { status = "actively-developed" }
travis-ci = { repository = "pest-parser/pest" }
pest_derive-2.1.0/Cargo.toml 0000644 00000002271 00000000000 0011404 0 ustar 00 # 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 believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
name = "pest_derive"
version = "2.1.0"
authors = ["Dragoș Tiselice "]
description = "pest's derive macro"
homepage = "https://pest-parser.github.io/"
documentation = "https://docs.rs/pest"
readme = "_README.md"
keywords = ["pest", "parser", "peg", "grammar"]
categories = ["parsing"]
license = "MIT/Apache-2.0"
repository = "https://github.com/pest-parser/pest"
[lib]
name = "pest_derive"
proc-macro = true
[dependencies.pest]
version = "2.1.0"
[dependencies.pest_generator]
version = "2.1.0"
[badges.codecov]
repository = "pest-parser/pest"
[badges.maintenance]
status = "actively-developed"
[badges.travis-ci]
repository = "pest-parser/pest"
pest_derive-2.1.0/LICENSE-APACHE 0100644 0000765 0000024 00000025137 13327737546 0014217 0 ustar 00 0000000 0000000 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.
pest_derive-2.1.0/LICENSE-MIT 0100644 0000765 0000024 00000001777 13327737546 0013733 0 ustar 00 0000000 0000000 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.
pest_derive-2.1.0/src/lib.rs 0100644 0000765 0000024 00000027562 13407232764 0014171 0 ustar 00 0000000 0000000 // pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
//! # 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.
//!
//! [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]
//! * leave feedback, ask questions, or greet us on [Gitter]
//!
//! [book]: https://pest-parser.github.io/book
//! [docs.rs]: https://docs.rs/pest
//! [fiddle]: https://pest-parser.github.io/#editor
//! [Gitter]: https://gitter.im/dragostis/pest
//!
//! ## `.pest` files
//!
//! Grammar definitions reside in custom `.pest` files located in the `src` directory. Their path is
//! relative to `src` and is specified between the `derive` attribute and empty `struct` that
//! `Parser` will be derived on.
//!
//! ```ignore
//! #[derive(Parser)]
//! #[grammar = "path/to/my_grammar.pest"] // relative to src
//! struct MyParser;
//! ```
//!
//! ## Inline grammars
//!
//! Grammars can also be inlined by using the `#[grammar_inline = "..."]` attribute.
//!
//! ## Grammar
//!
//! A grammar is a series of rules separated by whitespace, possibly containing comments.
//!
//! ### Comments
//!
//! Comments start with `//` and end at the end of the line.
//!
//! ```ignore
//! // a comment
//! ```
//!
//! ### Rules
//!
//! Rules have the following form:
//!
//! ```ignore
//! name = optional_modifier { expression }
//! ```
//!
//! The name of the rule is formed from alphanumeric characters or `_` with the condition that the
//! first character is not a digit and is used to create token pairs. When the rule starts being
//! parsed, the starting part of the token is being produced, with the ending part being produced
//! when the rule finishes parsing.
//!
//! The following token pair notation `a(b(), c())` denotes the tokens: start `a`, start `b`, end
//! `b`, start `c`, end `c`, end `a`.
//!
//! #### Modifiers
//!
//! Modifiers are optional and can be one of `_`, `@`, `$`, or `!`. These modifiers change the
//! behavior of the rules.
//!
//! 1. Silent (`_`)
//!
//! Silent rules do not create token pairs during parsing, nor are they error-reported.
//!
//! ```ignore
//! a = _{ "a" }
//! b = { a ~ "b" }
//! ```
//!
//! Parsing `"ab"` produces the token pair `b()`.
//!
//! 2. Atomic (`@`)
//!
//! Atomic rules do not accept whitespace or comments within their expressions and have a
//! cascading effect on any rule they call. I.e. rules that are not atomic but are called by atomic
//! rules behave atomically.
//!
//! Any rules called by atomic rules do not generate token pairs.
//!
//! ```ignore
//! a = { "a" }
//! b = @{ a ~ "b" }
//!
//! WHITESPACE = _{ " " }
//! ```
//!
//! Parsing `"ab"` produces the token pair `b()`, while `"a b"` produces an error.
//!
//! 3. Compound-atomic (`$`)
//!
//! Compound-atomic are identical to atomic rules with the exception that rules called by them are
//! not forbidden from generating token pairs.
//!
//! ```ignore
//! a = { "a" }
//! b = ${ a ~ "b" }
//!
//! WHITESPACE = _{ " " }
//! ```
//!
//! Parsing `"ab"` produces the token pairs `b(a())`, while `"a b"` produces an error.
//!
//! 4. Non-atomic (`!`)
//!
//! Non-atomic are identical to normal rules with the exception that they stop the cascading effect
//! of atomic and compound-atomic rules.
//!
//! ```ignore
//! a = { "a" }
//! b = !{ a ~ "b" }
//! c = @{ b }
//!
//! WHITESPACE = _{ " " }
//! ```
//!
//! Parsing both `"ab"` and `"a b"` produce the token pairs `c(a())`.
//!
//! #### Expressions
//!
//! Expressions can be either terminals or non-terminals.
//!
//! 1. Terminals
//!
//! | Terminal | Usage |
//! |------------|----------------------------------------------------------------|
//! | `"a"` | matches the exact string `"a"` |
//! | `^"a"` | matches the exact string `"a"` case insensitively (ASCII only) |
//! | `'a'..'z'` | matches one character between `'a'` and `'z'` |
//! | `a` | matches rule `a` |
//!
//! Strings and characters follow
//! [Rust's escape mechanisms](https://doc.rust-lang.org/reference/tokens.html#byte-escapes), while
//! identifiers can contain alpha-numeric characters and underscores (`_`), as long as they do not
//! start with a digit.
//!
//! 2. Non-terminals
//!
//! | Non-terminal | Usage |
//! |-----------------------|------------------------------------------------------------|
//! | `(e)` | matches `e` |
//! | `e1 ~ e2` | matches the sequence `e1` `e2` |
//! | e1 \| e2
| matches either `e1` or `e2` |
//! | `e*` | matches `e` zero or more times |
//! | `e+` | matches `e` one or more times |
//! | `e{n}` | matches `e` exactly `n` times |
//! | `e{, n}` | matches `e` at most `n` times |
//! | `e{n,} ` | matches `e` at least `n` times |
//! | `e{m, n}` | matches `e` between `m` and `n` times inclusively |
//! | `e?` | optionally matches `e` |
//! | `&e` | matches `e` without making progress |
//! | `!e` | matches if `e` doesn't match without making progress |
//! | `PUSH(e)` | matches `e` and pushes it's captured string down the stack |
//!
//! where `e`, `e1`, and `e2` are expressions.
//!
//! Expressions can modify the stack only if they match the input. For example,
//! if `e1` in the compound expression `e1 | e2` does not match the input, then
//! it does not modify the stack, so `e2` sees the stack in the same state as
//! `e1` did. Repetitions and optionals (`e*`, `e+`, `e{, n}`, `e{n,}`,
//! `e{m,n}`, `e?`) can modify the stack each time `e` matches. The `!e` and `&e`
//! expressions are a special case; they never modify the stack.
//!
//! ## Special rules
//!
//! Special rules can be called within the grammar. They are:
//!
//! * `WHITESPACE` - runs between rules and sub-rules
//! * `COMMENT` - runs between rules and sub-rules
//! * `ANY` - matches exactly one `char`
//! * `SOI` - (start-of-input) matches only when a `Parser` is still at the starting position
//! * `EOI` - (end-of-input) matches only when a `Parser` has reached its end
//! * `POP` - pops a string from the stack and matches it
//! * `POP_ALL` - pops the entire state of the stack and matches it
//! * `PEEK` - peeks a string from the stack and matches it
//! * `PEEK[a..b]` - peeks part of the stack and matches it
//! * `PEEK_ALL` - peeks the entire state of the stack and matches it
//! * `DROP` - drops the top of the stack (fails to match if the stack is empty)
//!
//! `WHITESPACE` and `COMMENT` should be defined manually if needed. All other rules cannot be
//! overridden.
//!
//! ## `WHITESPACE` and `COMMENT`
//!
//! When defined, these rules get matched automatically in sequences (`~`) and repetitions
//! (`*`, `+`) between expressions. Atomic rules and those rules called by atomic rules are exempt
//! from this behavior.
//!
//! These rules should be defined so as to match one whitespace character and one comment only since
//! they are run in repetitions.
//!
//! If both `WHITESPACE` and `COMMENT` are defined, this grammar:
//!
//! ```ignore
//! a = { b ~ c }
//! ```
//!
//! is effectively transformed into this one behind the scenes:
//!
//! ```ignore
//! a = { b ~ WHITESPACE* ~ (COMMENT ~ WHITESPACE*)* ~ c }
//! ```
//!
//! ## `PUSH`, `POP`, `DROP`, and `PEEK`
//!
//! `PUSH(e)` simply pushes the captured string of the expression `e` down a stack. This stack can
//! then later be used to match grammar based on its content with `POP` and `PEEK`.
//!
//! `PEEK` always matches the string at the top of stack. So, if the stack contains `["b", "a"]`
//! (`"a"` being on top), this grammar:
//!
//! ```ignore
//! a = { PEEK }
//! ```
//!
//! is effectively transformed into at parse time:
//!
//! ```ignore
//! a = { "a" }
//! ```
//!
//! `POP` works the same way with the exception that it pops the string off of the stack if the
//! match worked. With the stack from above, if `POP` matches `"a"`, the stack will be mutated
//! to `["b"]`.
//!
//! `DROP` makes it possible to remove the string at the top of the stack
//! without matching it. If the stack is nonempty, `DROP` drops the top of the
//! stack. If the stack is empty, then `DROP` fails to match.
//!
//! ### Advanced peeking
//!
//! `PEEK[start..end]` and `PEEK_ALL` allow to peek deeper into the stack. The syntax works exactly
//! like Rust’s exclusive slice syntax. Additionally, negative indices can be used to indicate an
//! offset from the top. If the end lies before or at the start, the expression matches (as does
//! a `PEEK_ALL` on an empty stack). With the stack `["c", "b", "a"]` (`"a"` on top):
//!
//! ```ignore
//! fill = PUSH("c") ~ PUSH("b") ~ PUSH("a")
//! v = { PEEK_ALL } = { "a" ~ "b" ~ "c" } // top to bottom
//! w = { PEEK[..] } = { "c" ~ "b" ~ "a" } // bottom to top
//! x = { PEEK[1..2] } = { PEEK[1..-1] } = { "b" }
//! y = { PEEK[..-2] } = { PEEK[0..1] } = { "a" }
//! z = { PEEK[1..] } = { PEEK[-2..3] } = { "c" ~ "b" }
//! n = { PEEK[2..-2] } = { PEEK[2..1] } = { "" }
//! ```
//!
//! For historical reasons, `PEEK_ALL` matches from top to bottom, while `PEEK[start..end]` matches
//! from bottom to top. There is currectly no syntax to match a slice of the stack top to bottom.
//!
//! ## `Rule`
//!
//! All rules defined or used in the grammar populate a generated `enum` called `Rule`. This
//! implements `pest`'s `RuleType` and can be used throughout the API.
//!
//! ## `Built-in rules`
//!
//! Pest also comes with a number of built-in rules for convenience. They are:
//!
//! * `ASCII_DIGIT` - matches a numeric character from 0..9
//! * `ASCII_NONZERO_DIGIT` - matches a numeric character from 1..9
//! * `ASCII_BIN_DIGIT` - matches a numeric character from 0..1
//! * `ASCII_OCT_DIGIT` - matches a numeric character from 0..7
//! * `ASCII_HEX_DIGIT` - matches a numeric character from 0..9 or a..f or A..F
//! * `ASCII_ALPHA_LOWER` - matches a character from a..z
//! * `ASCII_ALPHA_UPPER` - matches a character from A..Z
//! * `ASCII_ALPHA` - matches a character from a..z or A..Z
//! * `ASCII_ALPHANUMERIC` - matches a character from a..z or A..Z or 0..9
//! * `ASCII` - matches a character from \x00..\x7f
//! * `NEWLINE` - matches either "\n" or "\r\n" or "\r"
#![doc(html_root_url = "https://docs.rs/pest_derive")]
extern crate pest_generator;
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro_derive(Parser, attributes(grammar, grammar_inline))]
pub fn derive_parser(input: TokenStream) -> TokenStream {
pest_generator::derive_parser(input.into(), true).into()
}
pest_derive-2.1.0/tests/grammar.pest 0100644 0000765 0000024 00000004775 13376530424 0015753 0 ustar 00 0000000 0000000 // pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
string = { "abc" }
insensitive = { ^"abc" }
range = { '0'..'9' }
ident = { string }
pos_pred = { &string }
neg_pred = { !string }
double_neg_pred = { !!string }
sequence = !{ string ~ string }
sequence_compound = ${ string ~ string }
sequence_atomic = @{ string ~ string }
sequence_non_atomic = @{ sequence }
sequence_atomic_compound = @{ sequence_compound }
sequence_nested = { string ~ string }
sequence_compound_nested = ${ sequence_nested }
choice = { string | range }
optional = { string? }
repeat = { string* }
repeat_atomic = @{ string* }
repeat_once = { string+ }
repeat_once_atomic = @{ string+ }
repeat_min_max = { string{2, 3} }
repeat_min_max_atomic = @{ string{2, 3} }
repeat_exact = { string{2} }
repeat_min = { string{2,} }
repeat_min_atomic = @{ string{2,} }
repeat_max = { string{, 2} }
repeat_max_atomic = @{ string{, 2} }
soi_at_start = { SOI ~ string }
repeat_mutate_stack = { (PUSH('a'..'c') ~ ",")* ~ POP ~ POP ~ POP }
peek_ = { PUSH(range) ~ PUSH(range) ~ PEEK ~ PEEK }
peek_all = { PUSH(range) ~ PUSH(range) ~ PEEK_ALL }
peek_slice_23 = { PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PEEK[1..-2] }
pop_ = { PUSH(range) ~ PUSH(range) ~ POP ~ POP }
pop_all = { PUSH(range) ~ PUSH(range) ~ POP_ALL }
pop_fail = { PUSH(range) ~ !POP ~ range ~ POP }
checkpoint_restore = ${
PUSH("") ~ (PUSH("a") ~ "b" ~ POP | DROP ~ "b" | POP ~ "a") ~ EOI
}
ascii_digits = { ASCII_DIGIT+ }
ascii_nonzero_digits = { ASCII_NONZERO_DIGIT+ }
ascii_bin_digits = { ASCII_BIN_DIGIT+ }
ascii_oct_digits = { ASCII_OCT_DIGIT+ }
ascii_hex_digits = { ASCII_HEX_DIGIT+ }
ascii_alpha_lowers = { ASCII_ALPHA_LOWER+ }
ascii_alpha_uppers = { ASCII_ALPHA_UPPER+ }
ascii_alphas = { ASCII_ALPHA+ }
ascii_alphanumerics = { ASCII_ALPHANUMERIC+ }
asciis = { ASCII+ }
newline = { NEWLINE+ }
unicode = { XID_START ~ XID_CONTINUE* }
SYMBOL = { "shadows builtin" }
WHITESPACE = _{ " " }
COMMENT = _{ "$"+ }
// Line comment
/* 1-line multiline comment */
/*
N-line multiline comment
*/
/*
// Line comment inside multiline
/*
(Multiline inside) multiline
*/
Invalid segment of grammar below (repeated rule)
WHITESPACE = _{ "hi" }
*/
pest_derive-2.1.0/tests/grammar.rs 0100644 0000765 0000024 00000041770 13407230676 0015421 0 ustar 00 0000000 0000000 // pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
#[macro_use]
extern crate pest;
#[macro_use]
extern crate pest_derive;
#[derive(Parser)]
#[grammar = "../tests/grammar.pest"]
struct GrammarParser;
#[test]
fn string() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::string,
tokens: [
string(0, 3)
]
};
}
#[test]
fn insensitive() {
parses_to! {
parser: GrammarParser,
input: "aBC",
rule: Rule::insensitive,
tokens: [
insensitive(0, 3)
]
};
}
#[test]
fn range() {
parses_to! {
parser: GrammarParser,
input: "6",
rule: Rule::range,
tokens: [
range(0, 1)
]
};
}
#[test]
fn ident() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::ident,
tokens: [
ident(0, 3, [
string(0, 3)
])
]
};
}
#[test]
fn pos_pred() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::pos_pred,
tokens: [
pos_pred(0, 0)
]
};
}
#[test]
fn neg_pred() {
parses_to! {
parser: GrammarParser,
input: "",
rule: Rule::neg_pred,
tokens: [
neg_pred(0, 0)
]
};
}
#[test]
fn double_neg_pred() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::double_neg_pred,
tokens: [
double_neg_pred(0, 0)
]
};
}
#[test]
fn sequence() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::sequence,
tokens: [
sequence(0, 9, [
string(0, 3),
string(6, 9)
])
]
};
}
#[test]
fn sequence_compound() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::sequence_compound,
tokens: [
sequence_compound(0, 6, [
string(0, 3),
string(3, 6)
])
]
};
}
#[test]
fn sequence_atomic() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::sequence_atomic,
tokens: [
sequence_atomic(0, 6)
]
};
}
#[test]
fn sequence_non_atomic() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::sequence_non_atomic,
tokens: [
sequence_non_atomic(0, 9, [
sequence(0, 9, [
string(0, 3),
string(6, 9)
])
])
]
};
}
#[test]
#[should_panic]
fn sequence_atomic_space() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::sequence_atomic,
tokens: []
};
}
#[test]
fn sequence_atomic_compound() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::sequence_atomic_compound,
tokens: [
sequence_atomic_compound(0, 6, [
sequence_compound(0, 6, [
string(0, 3),
string(3, 6)
])
])
]
};
}
#[test]
fn sequence_compound_nested() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::sequence_compound_nested,
tokens: [
sequence_compound_nested(0, 6, [
sequence_nested(0, 6, [
string(0, 3),
string(3, 6)
])
])
]
};
}
#[test]
#[should_panic]
fn sequence_compound_nested_space() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::sequence_compound_nested,
tokens: []
};
}
#[test]
fn choice_string() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::choice,
tokens: [
choice(0, 3, [
string(0, 3)
])
]
};
}
#[test]
fn choice_range() {
parses_to! {
parser: GrammarParser,
input: "0",
rule: Rule::choice,
tokens: [
choice(0, 1, [
range(0, 1)
])
]
};
}
#[test]
fn optional_string() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::optional,
tokens: [
optional(0, 3, [
string(0, 3)
])
]
};
}
#[test]
fn optional_empty() {
parses_to! {
parser: GrammarParser,
input: "",
rule: Rule::optional,
tokens: [
optional(0, 0)
]
};
}
#[test]
fn repeat_empty() {
parses_to! {
parser: GrammarParser,
input: "",
rule: Rule::repeat,
tokens: [
repeat(0, 0)
]
};
}
#[test]
fn repeat_strings() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat,
tokens: [
repeat(0, 9, [
string(0, 3),
string(6, 9)
])
]
};
}
#[test]
fn repeat_atomic_empty() {
parses_to! {
parser: GrammarParser,
input: "",
rule: Rule::repeat_atomic,
tokens: [
repeat_atomic(0, 0)
]
};
}
#[test]
fn repeat_atomic_strings() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::repeat_atomic,
tokens: [
repeat_atomic(0, 6)
]
};
}
#[test]
#[should_panic]
fn repeat_atomic_space() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_atomic,
tokens: []
};
}
#[test]
#[should_panic]
fn repeat_once_empty() {
parses_to! {
parser: GrammarParser,
input: "",
rule: Rule::repeat_once,
tokens: []
};
}
#[test]
fn repeat_once_strings() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_once,
tokens: [
repeat_once(0, 9, [
string(0, 3),
string(6, 9)
])
]
};
}
#[test]
#[should_panic]
fn repeat_once_atomic_empty() {
parses_to! {
parser: GrammarParser,
input: "",
rule: Rule::repeat_once_atomic,
tokens: []
};
}
#[test]
fn repeat_once_atomic_strings() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::repeat_once_atomic,
tokens: [
repeat_once_atomic(0, 6)
]
};
}
#[test]
#[should_panic]
fn repeat_once_atomic_space() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_once_atomic,
tokens: []
};
}
#[test]
fn repeat_min_max_twice() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_min_max,
tokens: [
repeat_min_max(0, 7, [
string(0, 3),
string(4, 7)
])
]
};
}
#[test]
fn repeat_min_max_thrice() {
parses_to! {
parser: GrammarParser,
input: "abc abc abc",
rule: Rule::repeat_min_max,
tokens: [
repeat_min_max(0, 11, [
string(0, 3),
string(4, 7),
string(8, 11)
])
]
};
}
#[test]
fn repeat_min_max_atomic_twice() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::repeat_min_max_atomic,
tokens: [
repeat_min_max_atomic(0, 6)
]
};
}
#[test]
fn repeat_min_max_atomic_thrice() {
parses_to! {
parser: GrammarParser,
input: "abcabcabc",
rule: Rule::repeat_min_max_atomic,
tokens: [
repeat_min_max_atomic(0, 9)
]
};
}
#[test]
#[should_panic]
fn repeat_min_max_atomic_space() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_min_max_atomic,
tokens: []
};
}
#[test]
fn repeat_exact() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_exact,
tokens: [
repeat_exact(0, 7, [
string(0, 3),
string(4, 7)
])
]
};
}
#[test]
#[should_panic]
fn repeat_min_once() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::repeat_min,
tokens: []
};
}
#[test]
fn repeat_min_twice() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_min,
tokens: [
repeat_min(0, 7, [
string(0, 3),
string(4, 7)
])
]
};
}
#[test]
fn repeat_min_thrice() {
parses_to! {
parser: GrammarParser,
input: "abc abc abc",
rule: Rule::repeat_min,
tokens: [
repeat_min(0, 12, [
string(0, 3),
string(4, 7),
string(9, 12)
])
]
};
}
#[test]
#[should_panic]
fn repeat_min_atomic_once() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::repeat_min_atomic,
tokens: []
};
}
#[test]
fn repeat_min_atomic_twice() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::repeat_min_atomic,
tokens: [
repeat_min_atomic(0, 6)
]
};
}
#[test]
fn repeat_min_atomic_thrice() {
parses_to! {
parser: GrammarParser,
input: "abcabcabc",
rule: Rule::repeat_min_atomic,
tokens: [
repeat_min_atomic(0, 9)
]
};
}
#[test]
#[should_panic]
fn repeat_min_atomic_space() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_min_atomic,
tokens: []
};
}
#[test]
fn repeat_max_once() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::repeat_max,
tokens: [
repeat_max(0, 3, [
string(0, 3)
])
]
};
}
#[test]
fn repeat_max_twice() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_max,
tokens: [
repeat_max(0, 7, [
string(0, 3),
string(4, 7)
])
]
};
}
#[test]
#[should_panic]
fn repeat_max_thrice() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_max,
tokens: []
};
}
#[test]
fn repeat_max_atomic_once() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::repeat_max_atomic,
tokens: [
repeat_max_atomic(0, 3)
]
};
}
#[test]
fn repeat_max_atomic_twice() {
parses_to! {
parser: GrammarParser,
input: "abcabc",
rule: Rule::repeat_max_atomic,
tokens: [
repeat_max_atomic(0, 6)
]
};
}
#[test]
#[should_panic]
fn repeat_max_atomic_thrice() {
parses_to! {
parser: GrammarParser,
input: "abcabcabc",
rule: Rule::repeat_max_atomic,
tokens: []
};
}
#[test]
#[should_panic]
fn repeat_max_atomic_space() {
parses_to! {
parser: GrammarParser,
input: "abc abc",
rule: Rule::repeat_max_atomic,
tokens: []
};
}
#[test]
fn repeat_comment() {
parses_to! {
parser: GrammarParser,
input: "abc$$$ $$$abc",
rule: Rule::repeat_once,
tokens: [
repeat_once(0, 13, [
string(0, 3),
string(10, 13)
])
]
};
}
#[test]
fn soi_at_start() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::soi_at_start,
tokens: [
soi_at_start(0, 3, [
string(0, 3)
])
]
};
}
#[test]
fn peek() {
parses_to! {
parser: GrammarParser,
input: "0111",
rule: Rule::peek_,
tokens: [
peek_(0, 4, [
range(0, 1),
range(1, 2)
])
]
};
}
#[test]
fn peek_all() {
parses_to! {
parser: GrammarParser,
input: "0110",
rule: Rule::peek_all,
tokens: [
peek_all(0, 4, [
range(0, 1),
range(1, 2)
])
]
};
}
#[test]
fn peek_slice_23() {
parses_to! {
parser: GrammarParser,
input: "0123412",
rule: Rule::peek_slice_23,
tokens: [
peek_slice_23(0, 7, [
range(0, 1),
range(1, 2),
range(2, 3),
range(3, 4),
range(4, 5),
])
]
};
}
#[test]
fn pop() {
parses_to! {
parser: GrammarParser,
input: "0110",
rule: Rule::pop_,
tokens: [
pop_(0, 4, [
range(0, 1),
range(1, 2)
])
]
};
}
#[test]
fn pop_all() {
parses_to! {
parser: GrammarParser,
input: "0110",
rule: Rule::pop_all,
tokens: [
pop_all(0, 4, [
range(0, 1),
range(1, 2)
])
]
};
}
#[test]
fn pop_fail() {
parses_to! {
parser: GrammarParser,
input: "010",
rule: Rule::pop_fail,
tokens: [
pop_fail(0, 3, [
range(0, 1),
range(1, 2)
])
]
};
}
#[test]
fn repeat_mutate_stack() {
parses_to! {
parser: GrammarParser,
input: "a,b,c,cba",
rule: Rule::repeat_mutate_stack,
tokens: [
repeat_mutate_stack(0, 9)
]
};
}
#[test]
fn checkpoint_restore() {
parses_to! {
parser: GrammarParser,
input: "a",
rule: Rule::checkpoint_restore,
tokens: [
checkpoint_restore(0, 1, [EOI(1, 1)])
]
};
}
#[test]
fn ascii_digits() {
parses_to! {
parser: GrammarParser,
input: "6",
rule: Rule::ascii_digits,
tokens: [
ascii_digits(0, 1)
]
};
}
#[test]
fn ascii_nonzero_digits() {
parses_to! {
parser: GrammarParser,
input: "5",
rule: Rule::ascii_nonzero_digits,
tokens: [
ascii_nonzero_digits(0, 1)
]
};
}
#[test]
fn ascii_bin_digits() {
parses_to! {
parser: GrammarParser,
input: "1",
rule: Rule::ascii_bin_digits,
tokens: [
ascii_bin_digits(0, 1)
]
};
}
#[test]
fn ascii_oct_digits() {
parses_to! {
parser: GrammarParser,
input: "3",
rule: Rule::ascii_oct_digits,
tokens: [
ascii_oct_digits(0, 1)
]
};
}
#[test]
fn ascii_hex_digits() {
parses_to! {
parser: GrammarParser,
input: "6bC",
rule: Rule::ascii_hex_digits,
tokens: [
ascii_hex_digits(0, 3)
]
};
}
#[test]
fn ascii_alpha_lowers() {
parses_to! {
parser: GrammarParser,
input: "a",
rule: Rule::ascii_alpha_lowers,
tokens: [
ascii_alpha_lowers(0, 1)
]
};
}
#[test]
fn ascii_alpha_uppers() {
parses_to! {
parser: GrammarParser,
input: "K",
rule: Rule::ascii_alpha_uppers,
tokens: [
ascii_alpha_uppers(0, 1)
]
};
}
#[test]
fn ascii_alphas() {
parses_to! {
parser: GrammarParser,
input: "wF",
rule: Rule::ascii_alphas,
tokens: [
ascii_alphas(0, 2)
]
};
}
#[test]
fn ascii_alphanumerics() {
parses_to! {
parser: GrammarParser,
input: "4jU",
rule: Rule::ascii_alphanumerics,
tokens: [
ascii_alphanumerics(0, 3)
]
};
}
#[test]
fn asciis() {
parses_to! {
parser: GrammarParser,
input: "x02",
rule: Rule::asciis,
tokens: [
asciis(0, 3)
]
};
}
#[test]
fn newline() {
parses_to! {
parser: GrammarParser,
input: "\n\r\n\r",
rule: Rule::newline,
tokens: [
newline(0, 4)
]
};
}
#[test]
fn unicode() {
parses_to! {
parser: GrammarParser,
input: "نامهای",
rule: Rule::unicode,
tokens: [
unicode(0, 12)
]
}
}
#[test]
fn shadowing() {
parses_to! {
parser: GrammarParser,
input: "shadows builtin",
rule: Rule::SYMBOL,
tokens: [
SYMBOL(0, 15)
]
}
}
pest_derive-2.1.0/tests/grammar_inline.rs 0100644 0000765 0000024 00000001252 13407230676 0016746 0 ustar 00 0000000 0000000 // Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
#[macro_use]
extern crate pest;
#[macro_use]
extern crate pest_derive;
#[derive(Parser)]
#[grammar_inline = "string = { \"abc\" }"]
struct GrammarParser;
#[test]
fn inline_string() {
parses_to! {
parser: GrammarParser,
input: "abc",
rule: Rule::string,
tokens: [
string(0, 3)
]
};
}
pest_derive-2.1.0/tests/lists.pest 0100644 0000765 0000024 00000001337 13336135761 0015453 0 ustar 00 0000000 0000000 // pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
item = { (!"\n" ~ ANY)* }
lists = _{ lines ~ EOI }
lines = _{ top_first ~ ("\n" ~ top_continue)* }
top_first = _{ "- " ~ item ~ ("\n" ~ children)? }
top_continue = _{ PEEK_ALL ~ "- " ~ item ~ ("\n" ~ children)? }
indentation = _{ (" " | "\t")+ }
children = { PEEK_ALL ~ PUSH(indentation) ~ lines ~ DROP }
pest_derive-2.1.0/tests/lists.rs 0100644 0000765 0000024 00000004531 13336135761 0015123 0 ustar 00 0000000 0000000 // pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
#[macro_use]
extern crate pest;
#[macro_use]
extern crate pest_derive;
#[derive(Parser)]
#[grammar = "../tests/lists.pest"]
struct ListsParser;
#[test]
fn item() {
parses_to! {
parser: ListsParser,
input: "- a",
rule: Rule::lists,
tokens: [
item(2, 3)
]
};
}
#[test]
fn items() {
parses_to! {
parser: ListsParser,
input: "- a\n- b",
rule: Rule::lists,
tokens: [
item(2, 3),
item(6, 7)
]
};
}
#[test]
fn children() {
parses_to! {
parser: ListsParser,
input: " - b",
rule: Rule::children,
tokens: [
children(0, 5, [
item(4, 5)
])
]
};
}
#[test]
fn nested_item() {
parses_to! {
parser: ListsParser,
input: "- a\n - b",
rule: Rule::lists,
tokens: [
item(2, 3),
children(4, 9, [
item(8, 9)
])
]
};
}
#[test]
fn nested_items() {
parses_to! {
parser: ListsParser,
input: "- a\n - b\n - c",
rule: Rule::lists,
tokens: [
item(2, 3),
children(4, 15, [
item(8, 9),
item(14, 15)
])
]
};
}
#[test]
fn nested_two_levels() {
parses_to! {
parser: ListsParser,
input: "- a\n - b\n - c",
rule: Rule::lists,
tokens: [
item(2, 3),
children(4, 17, [
item(8, 9),
children(10, 17, [
item(16, 17)
])
])
]
};
}
#[test]
fn nested_then_not() {
parses_to! {
parser: ListsParser,
input: "- a\n - b\n- c",
rule: Rule::lists,
tokens: [
item(2, 3),
children(4, 9, [
item(8, 9)
]),
item(12, 13)
]
};
}
pest_derive-2.1.0/tests/reporting.pest 0100644 0000765 0000024 00000001356 13335174470 0016326 0 ustar 00 0000000 0000000 // pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
a = { "a" }
b = { "b" }
c = { "c" }
d = { ANY }
choices = _{ a | b | c }
choices_no_progress = { a | b | c }
choices_a_progress = { a ~ a | b | c }
choices_b_progress = { a | b ~ b | c }
level1 = _{ level2 }
level2 = _{ a | b | c }
negative = _{ !d }
negative_match = _{ !a ~ b }
mixed = _{ !d | a }
mixed_progress = _{ (!d | a | b) ~ a }
pest_derive-2.1.0/tests/reporting.rs 0100644 0000765 0000024 00000005042 13335174470 0015773 0 ustar 00 0000000 0000000 // pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
// Licensed under the Apache License, Version 2.0
// or the MIT
// license , at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
#[macro_use]
extern crate pest;
#[macro_use]
extern crate pest_derive;
#[derive(Parser)]
#[grammar = "../tests/reporting.pest"]
struct ReportingParser;
#[test]
fn choices() {
fails_with! {
parser: ReportingParser,
input: "x",
rule: Rule::choices,
positives: vec![Rule::a, Rule::b, Rule::c],
negatives: vec![],
pos: 0
};
}
#[test]
fn choices_no_progress() {
fails_with! {
parser: ReportingParser,
input: "x",
rule: Rule::choices_no_progress,
positives: vec![Rule::choices_no_progress],
negatives: vec![],
pos: 0
};
}
#[test]
fn choices_a_progress() {
fails_with! {
parser: ReportingParser,
input: "a",
rule: Rule::choices_a_progress,
positives: vec![Rule::a],
negatives: vec![],
pos: 1
};
}
#[test]
fn choices_b_progress() {
fails_with! {
parser: ReportingParser,
input: "b",
rule: Rule::choices_b_progress,
positives: vec![Rule::b],
negatives: vec![],
pos: 1
};
}
#[test]
fn nested() {
fails_with! {
parser: ReportingParser,
input: "x",
rule: Rule::level1,
positives: vec![Rule::a, Rule::b, Rule::c],
negatives: vec![],
pos: 0
};
}
#[test]
fn negative() {
fails_with! {
parser: ReportingParser,
input: "x",
rule: Rule::negative,
positives: vec![],
negatives: vec![Rule::d],
pos: 0
};
}
#[test]
fn negative_match() {
fails_with! {
parser: ReportingParser,
input: "x",
rule: Rule::negative_match,
positives: vec![Rule::b],
negatives: vec![],
pos: 0
};
}
#[test]
fn mixed() {
fails_with! {
parser: ReportingParser,
input: "x",
rule: Rule::mixed,
positives: vec![Rule::a],
negatives: vec![Rule::d],
pos: 0
};
}
#[test]
fn mixed_progress() {
fails_with! {
parser: ReportingParser,
input: "b",
rule: Rule::mixed_progress,
positives: vec![Rule::a],
negatives: vec![],
pos: 1
};
}
pest_derive-2.1.0/.cargo_vcs_info.json 0000644 00000000112 00000000000 0013376 0 ustar 00 {
"git": {
"sha1": "f781c62e1aa15784c2c25a7a33dae44d7caf0f6b"
}
}