From cc77c971f392105a4262a0a49174665dd9cc7f81 Mon Sep 17 00:00:00 2001 From: Johannes Schilling Date: Thu, 21 Nov 2019 12:46:47 +0100 Subject: [PATCH 1/2] Allow crate-relative imports in grammars --- peg-macros/grammar.rustpeg | 4 +++- tests/run-pass/crate_import.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/run-pass/crate_import.rs diff --git a/peg-macros/grammar.rustpeg b/peg-macros/grammar.rustpeg index b2e2b00..d938493 100644 --- a/peg-macros/grammar.rustpeg +++ b/peg-macros/grammar.rustpeg @@ -43,7 +43,9 @@ rule rust_use() -> TokenStream / ("as" IDENT())? ) ";") { v.to_owned() } -rule rust_path() = IDENT() ++ "::" +rule rust_path() + = "crate::" IDENT() ++ "::" + / IDENT() ++ "::" rule rust_type() = BRACKET_GROUP() diff --git a/tests/run-pass/crate_import.rs b/tests/run-pass/crate_import.rs new file mode 100644 index 0000000..135c5ca --- /dev/null +++ b/tests/run-pass/crate_import.rs @@ -0,0 +1,19 @@ +extern crate peg; + +peg::parser!{ + pub grammar foo_parser() for str { + use crate::types::Foo; + + pub rule foo() -> Foo + = "foo" { Foo } + } +} + +mod types { + pub struct Foo; +} + + +fn main() { + assert_eq!(foo_parser::foo("foo"), Ok(crate::types::Foo)); +} From 796d5aaf6322f09424806ff86f49b1bc14b51165 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Thu, 21 Nov 2019 08:03:09 -0800 Subject: [PATCH 2/2] Fix crate-relative imports patch and update grammar.rs --- peg-macros/grammar.rs | 115 ++++++++++++++++++++++++--------- peg-macros/grammar.rustpeg | 3 +- tests/run-pass/crate_import.rs | 1 + 3 files changed, 85 insertions(+), 34 deletions(-) diff --git a/peg-macros/grammar.rs b/peg-macros/grammar.rs index 53757ae..471cf07 100644 --- a/peg-macros/grammar.rs +++ b/peg-macros/grammar.rs @@ -1214,43 +1214,94 @@ pub mod peg { ) -> ::peg::RuleResult<()> { #![allow(non_snake_case, unused)] { - let mut __repeat_pos = __pos; - let mut __repeat_value = vec![]; - loop { - let __pos = __repeat_pos; - let __pos = if __repeat_value.is_empty() { - __pos - } else { - let __sep_res = - match ::peg::ParseLiteral::parse_string_literal(__input, __pos, "::") { - ::peg::RuleResult::Matched(__pos, __val) => { - ::peg::RuleResult::Matched(__pos, __val) + let __seq_res = match { + let __seq_res = + match ::peg::ParseLiteral::parse_string_literal(__input, __pos, "crate") { + ::peg::RuleResult::Matched(__pos, __val) => { + ::peg::RuleResult::Matched(__pos, __val) + } + ::peg::RuleResult::Failed => __err_state.mark_failure(__pos, "\"crate\""), + }; + match __seq_res { + ::peg::RuleResult::Matched(__pos, _) => { + let __seq_res = + match ::peg::ParseLiteral::parse_string_literal(__input, __pos, "::") { + ::peg::RuleResult::Matched(__pos, __val) => { + ::peg::RuleResult::Matched(__pos, __val) + } + ::peg::RuleResult::Failed => { + __err_state.mark_failure(__pos, "\"::\"") + } + }; + match __seq_res { + ::peg::RuleResult::Matched(__pos, _) => { + ::peg::RuleResult::Matched(__pos, {}) } - ::peg::RuleResult::Failed => __err_state.mark_failure(__pos, "\"::\""), - }; - match __sep_res { - ::peg::RuleResult::Matched(__newpos, _) => __newpos, - ::peg::RuleResult::Failed => break, + ::peg::RuleResult::Failed => ::peg::RuleResult::Failed, + } } - }; - let __step_res = match __parse_IDENT(__input, __state, __err_state, __pos) { - ::peg::RuleResult::Matched(pos, _) => ::peg::RuleResult::Matched(pos, ()), ::peg::RuleResult::Failed => ::peg::RuleResult::Failed, - }; - match __step_res { - ::peg::RuleResult::Matched(__newpos, __value) => { - __repeat_pos = __newpos; - __repeat_value.push(__value); - } - ::peg::RuleResult::Failed => { - break; + } + } { + ::peg::RuleResult::Matched(__newpos, _) => ::peg::RuleResult::Matched(__newpos, ()), + ::peg::RuleResult::Failed => ::peg::RuleResult::Matched(__pos, ()), + }; + match __seq_res { + ::peg::RuleResult::Matched(__pos, _) => { + let __seq_res = { + let mut __repeat_pos = __pos; + let mut __repeat_value = vec![]; + loop { + let __pos = __repeat_pos; + let __pos = if __repeat_value.is_empty() { + __pos + } else { + let __sep_res = match ::peg::ParseLiteral::parse_string_literal( + __input, __pos, "::", + ) { + ::peg::RuleResult::Matched(__pos, __val) => { + ::peg::RuleResult::Matched(__pos, __val) + } + ::peg::RuleResult::Failed => { + __err_state.mark_failure(__pos, "\"::\"") + } + }; + match __sep_res { + ::peg::RuleResult::Matched(__newpos, _) => __newpos, + ::peg::RuleResult::Failed => break, + } + }; + let __step_res = + match __parse_IDENT(__input, __state, __err_state, __pos) { + ::peg::RuleResult::Matched(pos, _) => { + ::peg::RuleResult::Matched(pos, ()) + } + ::peg::RuleResult::Failed => ::peg::RuleResult::Failed, + }; + match __step_res { + ::peg::RuleResult::Matched(__newpos, __value) => { + __repeat_pos = __newpos; + __repeat_value.push(__value); + } + ::peg::RuleResult::Failed => { + break; + } + } + } + if __repeat_value.len() >= 1 { + ::peg::RuleResult::Matched(__repeat_pos, ()) + } else { + ::peg::RuleResult::Failed + } + }; + match __seq_res { + ::peg::RuleResult::Matched(__pos, _) => { + ::peg::RuleResult::Matched(__pos, {}) + } + ::peg::RuleResult::Failed => ::peg::RuleResult::Failed, } } - } - if __repeat_value.len() >= 1 { - ::peg::RuleResult::Matched(__repeat_pos, ()) - } else { - ::peg::RuleResult::Failed + ::peg::RuleResult::Failed => ::peg::RuleResult::Failed, } } } diff --git a/peg-macros/grammar.rustpeg b/peg-macros/grammar.rustpeg index d938493..d0a3b52 100644 --- a/peg-macros/grammar.rustpeg +++ b/peg-macros/grammar.rustpeg @@ -44,8 +44,7 @@ rule rust_use() -> TokenStream ) ";") { v.to_owned() } rule rust_path() - = "crate::" IDENT() ++ "::" - / IDENT() ++ "::" + = ("crate" "::")? IDENT() ++ "::" rule rust_type() = BRACKET_GROUP() diff --git a/tests/run-pass/crate_import.rs b/tests/run-pass/crate_import.rs index 135c5ca..fbdd3fc 100644 --- a/tests/run-pass/crate_import.rs +++ b/tests/run-pass/crate_import.rs @@ -10,6 +10,7 @@ peg::parser!{ } mod types { + #[derive(PartialEq, Debug)] pub struct Foo; }