Skip to content

Commit 2f32379

Browse files
committed
restore invariatns
1 parent b536992 commit 2f32379

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

crates/parser/src/shortcuts.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl<'a> LexedStr<'a> {
5050
res
5151
}
5252

53+
/// NB: only valid to call with Output from Reparser/TopLevelEntry.
5354
pub fn intersperse_trivia(
5455
&self,
5556
output: &crate::Output,
@@ -76,7 +77,7 @@ impl<'a> LexedStr<'a> {
7677
builder.eat_trivias();
7778
(builder.sink)(StrStep::Exit);
7879
}
79-
State::PendingEnter | State::Normal => (),
80+
State::PendingEnter | State::Normal => unreachable!(),
8081
}
8182

8283
let is_eof = builder.pos == builder.lexed.len();
@@ -100,8 +101,9 @@ enum State {
100101
impl Builder<'_, '_> {
101102
fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
102103
match mem::replace(&mut self.state, State::Normal) {
104+
State::PendingEnter => unreachable!(),
103105
State::PendingExit => (self.sink)(StrStep::Exit),
104-
State::PendingEnter | State::Normal => (),
106+
State::Normal => (),
105107
}
106108
self.eat_trivias();
107109
self.do_token(kind, n_tokens as usize);

crates/parser/src/tests/entries.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{LexedStr, PrefixEntryPoint, StrStep};
1+
use crate::{LexedStr, PrefixEntryPoint, Step};
22

33
#[test]
44
fn vis() {
@@ -30,12 +30,25 @@ fn stmt() {
3030
fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) {
3131
let lexed = LexedStr::new(input);
3232
let input = lexed.to_input();
33-
let output = entry.parse(&input);
3433

35-
let mut buf = String::new();
36-
lexed.intersperse_trivia(&output, &mut |step| match step {
37-
StrStep::Token { kind: _, text } => buf.push_str(text),
38-
_ => (),
39-
});
40-
assert_eq!(buf.trim(), prefix)
34+
let mut n_tokens = 0;
35+
for step in entry.parse(&input).iter() {
36+
match step {
37+
Step::Token { n_input_tokens, .. } => n_tokens += n_input_tokens as usize,
38+
Step::Enter { .. } | Step::Exit | Step::Error { .. } => (),
39+
}
40+
}
41+
42+
let mut i = 0;
43+
loop {
44+
if n_tokens == 0 {
45+
break;
46+
}
47+
if !lexed.kind(i).is_trivia() {
48+
n_tokens -= 1;
49+
}
50+
i += 1;
51+
}
52+
let buf = &lexed.as_str()[..lexed.text_start(i)];
53+
assert_eq!(buf, prefix);
4154
}

0 commit comments

Comments
 (0)