From 14a481d939a6faca2503ce79eb39579fe5e7537c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 12 Sep 2023 23:27:43 -0600 Subject: [PATCH] Parse rustc's representation of macro expansion error --- src/parse.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/parse.rs b/src/parse.rs index c084e4c..1430d73 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -161,6 +161,10 @@ fn word_break(input: Cursor) -> Result { } } +// Rustc's representation of a macro expansion error in expression position or +// type position. +const ERROR: &str = "(/*ERROR*/)"; + pub(crate) fn token_stream(mut input: Cursor) -> Result { let mut trees = TokenStreamBuilder::new(); let mut stack = Vec::new(); @@ -192,7 +196,7 @@ pub(crate) fn token_stream(mut input: Cursor) -> Result { }; if let Some(open_delimiter) = match first { - b'(' => Some(Delimiter::Parenthesis), + b'(' if !input.starts_with(ERROR) => Some(Delimiter::Parenthesis), b'[' => Some(Delimiter::Bracket), b'{' => Some(Delimiter::Brace), _ => None, @@ -267,6 +271,10 @@ fn leaf_token(input: Cursor) -> PResult { Ok((input, TokenTree::Punct(p))) } else if let Ok((input, i)) = ident(input) { Ok((input, TokenTree::Ident(i))) + } else if input.starts_with(ERROR) { + let rest = input.advance(ERROR.len()); + let repr = crate::Literal::_new_fallback(Literal::_new(ERROR.to_owned())); + Ok((rest, TokenTree::Literal(repr))) } else { Err(Reject) }