From 1b2da451a3b785f2b35c134c3e8058b983153fa6 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Sun, 21 Apr 2024 10:14:41 -0600 Subject: [PATCH] Re-wrap Rust pattern in a Group After removing the brackets, we still want a group with delimiter None. Because the `spacing` of the last token of the pattern is passed through, it can conflict with the following `=` in the expansion and cause an error like https://github.com/rust-lang/rust/issues/107420 --- peg-macros/translate.rs | 3 +++ tests/run-pass/pattern.rs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/peg-macros/translate.rs b/peg-macros/translate.rs index 6b5c659..166f18f 100644 --- a/peg-macros/translate.rs +++ b/peg-macros/translate.rs @@ -1,3 +1,4 @@ +use proc_macro2::Delimiter; use proc_macro2::{Group, Ident, Literal, Span, TokenStream, TokenTree}; use std::collections::{HashMap, HashSet}; @@ -511,6 +512,8 @@ fn compile_pattern_expr( (pattern_group.stream(), success_res, failure_res) }; + let pattern = Group::new(Delimiter::None, pattern); + quote_spanned! { span => match ::peg::ParseElem::parse_elem(__input, __pos) { ::peg::RuleResult::Matched(__next, #result_name) => match #result_name { diff --git a/tests/run-pass/pattern.rs b/tests/run-pass/pattern.rs index 63ec4ae..e9f9192 100644 --- a/tests/run-pass/pattern.rs +++ b/tests/run-pass/pattern.rs @@ -4,6 +4,8 @@ peg::parser!( grammar test() for str { pub rule capture() -> char = ['a'..='z'] pub rule capture2() -> (char, char) = a:['a'..='z'] b:['0'..='9'] { (a, b) } + + pub rule open_range() -> char = ['a'..] }); fn main() {