Skip to content

Commit

Permalink
Parse rustc's representation of macro expansion error
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 13, 2023
1 parent 2e96778 commit 14a481d
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ fn word_break(input: Cursor) -> Result<Cursor, Reject> {
}
}

// 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<TokenStream, LexError> {
let mut trees = TokenStreamBuilder::new();
let mut stack = Vec::new();
Expand Down Expand Up @@ -192,7 +196,7 @@ pub(crate) fn token_stream(mut input: Cursor) -> Result<TokenStream, LexError> {
};

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,
Expand Down Expand Up @@ -267,6 +271,10 @@ fn leaf_token(input: Cursor) -> PResult<TokenTree> {
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)
}
Expand Down

0 comments on commit 14a481d

Please sign in to comment.