Skip to content

zmitchell/sentence-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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.

About

An example of using proptest to test a parser

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages