Skip to content

Commit

Permalink
Update sytanx from use "base.pest" to include!("base.pest")
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jan 4, 2023
1 parent 6f5ee1b commit 26cf4ed
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion grammars/src/grammars/json.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 }

Expand Down
2 changes: 1 addition & 1 deletion grammars/src/grammars/toml.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 }

Expand Down
5 changes: 2 additions & 3 deletions meta/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 = { "=" }
Expand Down Expand Up @@ -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"? }
31 changes: 21 additions & 10 deletions meta/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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),
])
]
};
Expand Down

0 comments on commit 26cf4ed

Please sign in to comment.