@@ -52,8 +52,15 @@ pub(crate) fn parse_token_trees<'a>(
52
52
}
53
53
54
54
let cursor = Cursor::new(src);
55
- let string_reader =
56
- StringReader { sess, start_pos, pos: start_pos, src, cursor, override_span };
55
+ let string_reader = StringReader {
56
+ sess,
57
+ start_pos,
58
+ pos: start_pos,
59
+ src,
60
+ cursor,
61
+ override_span,
62
+ nbsp_is_whitespace: false,
63
+ };
57
64
tokentrees::TokenTreesReader::parse_all_token_trees(string_reader)
58
65
}
59
66
@@ -68,6 +75,10 @@ struct StringReader<'a> {
68
75
/// Cursor for getting lexer tokens.
69
76
cursor: Cursor<'a>,
70
77
override_span: Option<Span>,
78
+ /// When a "unknown start of token: \u{a0}" has already been emitted earlier
79
+ /// in this file, it's safe to treat further occurrences of the non-breaking
80
+ /// space character as whitespace.
81
+ nbsp_is_whitespace: bool,
71
82
}
72
83
73
84
impl<'a> StringReader<'a> {
@@ -239,6 +250,16 @@ impl<'a> StringReader<'a> {
239
250
}
240
251
let mut it = self.str_from_to_end(start).chars();
241
252
let c = it.next().unwrap();
253
+ if c == '\u{00a0}' {
254
+ // If an error has already been reported on non-breaking
255
+ // space characters earlier in the file, treat all
256
+ // subsequent occurrences as whitespace.
257
+ if self.nbsp_is_whitespace {
258
+ preceded_by_whitespace = true;
259
+ continue;
260
+ }
261
+ self.nbsp_is_whitespace = true;
262
+ }
242
263
let repeats = it.take_while(|c1| *c1 == c).count();
243
264
let mut err =
244
265
self.struct_err_span_char(start, self.pos + Pos::from_usize(repeats * c.len_utf8()), "unknown start of token", c);
@@ -486,7 +507,7 @@ impl<'a> StringReader<'a> {
486
507
487
508
/// Slice of the source text from `start` up to but excluding `self.pos`,
488
509
/// meaning the slice does not include the character `self.ch`.
489
- fn str_from(&self, start: BytePos) -> &str {
510
+ fn str_from(&self, start: BytePos) -> &'a str {
490
511
self.str_from_to(start, self.pos)
491
512
}
492
513
@@ -497,12 +518,12 @@ impl<'a> StringReader<'a> {
497
518
}
498
519
499
520
/// Slice of the source text spanning from `start` up to but excluding `end`.
500
- fn str_from_to(&self, start: BytePos, end: BytePos) -> &str {
521
+ fn str_from_to(&self, start: BytePos, end: BytePos) -> &'a str {
501
522
&self.src[self.src_index(start)..self.src_index(end)]
502
523
}
503
524
504
525
/// Slice of the source text spanning from `start` until the end
505
- fn str_from_to_end(&self, start: BytePos) -> &str {
526
+ fn str_from_to_end(&self, start: BytePos) -> &'a str {
506
527
&self.src[self.src_index(start)..]
507
528
}
508
529
0 commit comments