Skip to content

Commit

Permalink
Re-wrap Rust pattern in a Group
Browse files Browse the repository at this point in the history
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 rust-lang/rust#107420
  • Loading branch information
kevinmehall committed Apr 21, 2024
1 parent 08cb3b3 commit 1b2da45
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions peg-macros/translate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use proc_macro2::Delimiter;
use proc_macro2::{Group, Ident, Literal, Span, TokenStream, TokenTree};
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/run-pass/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1b2da45

Please sign in to comment.