Skip to content

Commit 32e4b26

Browse files
committed
Set up LexError to support tracking a Span
1 parent 068bd59 commit 32e4b26

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/fallback.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ pub(crate) struct TokenStream {
3535
}
3636

3737
#[derive(Debug)]
38-
pub(crate) struct LexError;
38+
pub(crate) struct LexError {}
39+
40+
impl LexError {
41+
// Codepaths that will need a meaningful span attached as part of
42+
// https://github.com/alexcrichton/proc-macro2/issues/178
43+
pub(crate) fn todo() -> Self {
44+
LexError {}
45+
}
46+
}
3947

4048
impl TokenStream {
4149
pub fn new() -> TokenStream {

src/parse.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub(crate) fn token_stream(mut input: Cursor) -> Result<TokenStream, LexError> {
168168
let first = match input.bytes().next() {
169169
Some(first) => first,
170170
None if stack.is_empty() => return Ok(TokenStream { inner: trees }),
171-
None => return Err(LexError),
171+
None => return Err(LexError::todo()),
172172
};
173173

174174
if let Some(open_delimiter) = match first {
@@ -190,12 +190,12 @@ pub(crate) fn token_stream(mut input: Cursor) -> Result<TokenStream, LexError> {
190190
_ => None,
191191
} {
192192
input = input.advance(1);
193-
let frame = stack.pop().ok_or(LexError)?;
193+
let frame = stack.pop().ok_or_else(LexError::todo)?;
194194
#[cfg(span_locations)]
195195
let (lo, frame) = frame;
196196
let (open_delimiter, outer) = frame;
197197
if open_delimiter != close_delimiter {
198-
return Err(LexError);
198+
return Err(LexError::todo());
199199
}
200200
let mut g = Group::new(open_delimiter, TokenStream { inner: trees });
201201
g.set_span(Span {
@@ -209,7 +209,7 @@ pub(crate) fn token_stream(mut input: Cursor) -> Result<TokenStream, LexError> {
209209
} else {
210210
let (rest, mut tt) = match leaf_token(input) {
211211
Ok((rest, tt)) => (rest, tt),
212-
Err(Reject) => return Err(LexError),
212+
Err(Reject) => return Err(LexError::todo()),
213213
};
214214
tt.set_span(crate::Span::_new_stable(Span {
215215
#[cfg(span_locations)]

src/wrapper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ impl FromStr for TokenStream {
107107

108108
// Work around https://github.com/rust-lang/rust/issues/58736.
109109
fn proc_macro_parse(src: &str) -> Result<proc_macro::TokenStream, LexError> {
110-
panic::catch_unwind(|| src.parse().map_err(LexError::Compiler))
111-
.unwrap_or(Err(LexError::Fallback(fallback::LexError)))
110+
let result = panic::catch_unwind(|| src.parse().map_err(LexError::Compiler));
111+
result.unwrap_or_else(|_| Err(LexError::Fallback(fallback::LexError::todo())))
112112
}
113113

114114
impl Display for TokenStream {
@@ -270,7 +270,7 @@ impl Display for LexError {
270270
#[cfg(lexerror_display)]
271271
LexError::Compiler(e) => Display::fmt(e, f),
272272
#[cfg(not(lexerror_display))]
273-
LexError::Compiler(_e) => Display::fmt(&fallback::LexError, f),
273+
LexError::Compiler(_e) => Display::fmt(&fallback::LexError::todo(), f),
274274
LexError::Fallback(e) => Display::fmt(e, f),
275275
}
276276
}

0 commit comments

Comments
 (0)