diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 251ce757..f2dc2eb4 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -101,10 +101,10 @@ pub fn derive_parser(input: TokenStream, include_grammar: bool) -> TokenStream { Err(error) => panic!("error parsing \n{}", error.renamed_rules(rename_meta_rule)), }; - // parse `use "filename.pest"` to load partial and replace data + // parse `include!("filename.pest")` to load partial and replace data let mut partial_pairs = pairs.clone().flatten().peekable(); while let Some(pair) = partial_pairs.next() { - if pair.as_rule() == Rule::_use { + if pair.as_rule() == Rule::include { if let Some(filename) = partial_pairs.peek() { let partial_data = match read_file(partial_path(path.as_ref(), filename.as_str())) { Ok(data) => data, diff --git a/grammars/src/grammars/json.pest b/grammars/src/grammars/json.pest index cd1f2130..71f293af 100644 --- a/grammars/src/grammars/json.pest +++ b/grammars/src/grammars/json.pest @@ -6,7 +6,7 @@ // license , at your // option. All files in the project carrying such notice may not be copied, // modified, or distributed except according to those terms. -use "base.pest" +include!("base.pest") json = { SOI ~ (object | array) ~ EOI } diff --git a/grammars/src/grammars/toml.pest b/grammars/src/grammars/toml.pest index 1b9d9eda..c577898d 100644 --- a/grammars/src/grammars/toml.pest +++ b/grammars/src/grammars/toml.pest @@ -6,7 +6,7 @@ // license , at your // option. All files in the project carrying such notice may not be copied, // modified, or distributed except according to those terms. -use "./base" +include!("./base") toml = { SOI ~ (table | array_table | pair)* ~ EOI } diff --git a/meta/src/grammar.pest b/meta/src/grammar.pest index d3bb51a8..3fe7e9a5 100644 --- a/meta/src/grammar.pest +++ b/meta/src/grammar.pest @@ -6,13 +6,12 @@ // license , at your // option. All files in the project carrying such notice may not be copied, // modified, or distributed except according to those terms. - grammar_rules = _{ SOI ~ grammar_rule+ ~ EOI } grammar_rule = { identifier ~ assignment_operator ~ modifier? ~ opening_brace ~ expression ~ closing_brace | - _use + include } assignment_operator = { "=" } @@ -98,5 +97,5 @@ WHITESPACE = _{ " " | "\t" | newline } block_comment = _{ "/*" ~ (block_comment | !"*/" ~ ANY)* ~ "*/" } COMMENT = _{ block_comment | ("//" ~ (!newline ~ ANY)*) } -_use = ${ "use" ~ " "+ ~ "\"" ~ path ~ "\"" } +include = ${ "include!" ~ WHITESPACE* ~ "(" ~ WHITESPACE* ~ "\"" ~ path ~ "\"" ~ WHITESPACE* ~ ")" } path = @{ (!(newline | "\"") ~ ANY)* ~ ".pest"? } \ No newline at end of file diff --git a/meta/src/parser.rs b/meta/src/parser.rs index 288b0c76..c58fc22b 100644 --- a/meta/src/parser.rs +++ b/meta/src/parser.rs @@ -243,7 +243,7 @@ pub fn rename_meta_rule(rule: &Rule) -> String { Rule::insensitive_string => "`^`".to_owned(), Rule::range_operator => "`..`".to_owned(), Rule::single_quote => "`'`".to_owned(), - Rule::_use => "use".to_owned(), + Rule::include => "include!".to_owned(), other_rule => format!("{:?}", other_rule), } } @@ -1095,25 +1095,36 @@ mod tests { } #[test] - fn test_use() { + fn test_include() { parses_to! { parser: PestParser, - input: "use \"foo\"", - rule: Rule::_use, + input: "include!(\"foo\")", + rule: Rule::include, tokens: [ - _use(0, 9, [ - path(5, 8), + include(0, 15, [ + path(10, 13), ]) ] }; parses_to! { parser: PestParser, - input: "use \"foo.bar.pest\"", - rule: Rule::_use, + input: "include! ( \"foo.bar.pest\" )", + rule: Rule::include, tokens: [ - _use(0, 19, [ - path(6, 18), + include(0, 28, [ + path(13, 25), + ]) + ] + }; + + parses_to! { + parser: PestParser, + input: "include!(\n\"./foo/bar.pest\"\n)", + rule: Rule::include, + tokens: [ + include(0, 28, [ + path(11, 25), ]) ] };