https://github.com/zmitchell/sentence-parser
An example of using proptest to test a parser
https://github.com/zmitchell/sentence-parser
parsing property-based-testing rust
Last synced: 3 months ago
JSON representation
An example of using proptest to test a parser
- Host: GitHub
- URL: https://github.com/zmitchell/sentence-parser
- Owner: zmitchell
- License: apache-2.0
- Created: 2020-06-03T22:56:05.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-03T23:40:01.000Z (over 5 years ago)
- Last Synced: 2025-02-24T12:27:58.905Z (12 months ago)
- Topics: parsing, property-based-testing, rust
- Language: Rust
- Size: 12.7 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# sentence-parser
This is a very basic parser meant to be an example of how to use `proptest` to write property-based tests in Rust.
This parser attempts to parse a sentence, with a very loose definition of "sentence." We define a "word" to be any sequence of one or more ASCII letters (case doesn't matter). Some of the words may also be enclosed in delimiters such as commas or parentheses. A "sentence" then consists of a sequence of words, some of which may be enclosed in delimiters, followed by valid punctuation (period, question mark, or exclamation point). In this framework `(a) b.` is a valid sentence.
The `pest` crate is used to write the parser because the grammar file lets you declaratively define the parsing rules. This is the entire definition of the parser:
```
word = { ASCII_ALPHA+ }
words = ${ word ~ (" " ~ word)* }
enclosed = ${
"(" ~ words ~ ")" |
", " ~ words ~ ","
}
chunk = ${ words | enclosed }
punctuation = { "." | "!" | "?" }
sentence = ${ SOI ~ chunk ~ (" " ~ chunk)* ~ punctuation ~ EOI }
WHITESPACE = _{" "}
```
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.