diff --git a/derive/examples/help-menu.pest b/derive/examples/help-menu.pest new file mode 100644 index 00000000..f7d39853 --- /dev/null +++ b/derive/examples/help-menu.pest @@ -0,0 +1,36 @@ +WHITESPACE = _{ " " } + +Rew = ! { ( ASCII_DIGIT | ASCII_ALPHA_UPPER | "_" | "-" ) + } +Lit = ! { ( ASCII_ALPHANUMERIC | "_" | "-" ) + } +Word = _ { Rew | Lit } +WordGroup = _ { Word ~ ( " " ~ Word ) * } + +// Argument groups are nonatomic; +// "" + +ArgOptChoiceGroup = { ( "[" ~ Word + ~ + ( "|" ~ ( Word + | ArgChoiceGroup ) + ) * ~ + "]" ) + } +ArgReqChoiceGroup = { ( "<" ~ Word + ~ + ( "|" ~ ( Word + | ArgChoiceGroup ) + ) * ~ + ">" ) + } +ArgChoiceGroup = _ { ArgOptChoiceGroup + | ArgReqChoiceGroup } + +Command = { + ( WordGroup ) ~ + ( ArgChoiceGroup ) * +} + +HelpMenu = { + SOI ~ + + ( + Command ~ + NEWLINE + ) * ~ + + EOI +} diff --git a/derive/examples/help-menu.rs b/derive/examples/help-menu.rs new file mode 100644 index 00000000..8bafc7a7 --- /dev/null +++ b/derive/examples/help-menu.rs @@ -0,0 +1,28 @@ +#[macro_use] +extern crate pest_derive; +extern crate pest; + +use pest::Parser; + +#[derive(Parser)] +#[grammar = "../examples/help-menu.pest"] +struct HelpMenuGrammar; + +const INPUT: &str = r"cli help +cli positional-command [optional-single-argument] +cli [choice | of | one | or | none | of | these | options] +cli +cli [nesting | ] +"; + +fn main() { + HelpMenuGrammar::parse(Rule::HelpMenu, INPUT) + .expect("Error parsing file") + .next() + .expect("Infallible") + .into_inner() + .filter(|pair| Rule::Command == pair.as_rule()) + .for_each(|pair| { + println!("{:#?}", pair); + }); +}