restore invariatns

This commit is contained in:
Aleksey Kladov 2021-12-29 20:37:08 +03:00
parent b5369927d7
commit 2f3237912d
2 changed files with 25 additions and 10 deletions

View file

@ -50,6 +50,7 @@ impl<'a> LexedStr<'a> {
res res
} }
/// NB: only valid to call with Output from Reparser/TopLevelEntry.
pub fn intersperse_trivia( pub fn intersperse_trivia(
&self, &self,
output: &crate::Output, output: &crate::Output,
@ -76,7 +77,7 @@ impl<'a> LexedStr<'a> {
builder.eat_trivias(); builder.eat_trivias();
(builder.sink)(StrStep::Exit); (builder.sink)(StrStep::Exit);
} }
State::PendingEnter | State::Normal => (), State::PendingEnter | State::Normal => unreachable!(),
} }
let is_eof = builder.pos == builder.lexed.len(); let is_eof = builder.pos == builder.lexed.len();
@ -100,8 +101,9 @@ enum State {
impl Builder<'_, '_> { impl Builder<'_, '_> {
fn token(&mut self, kind: SyntaxKind, n_tokens: u8) { fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
match mem::replace(&mut self.state, State::Normal) { match mem::replace(&mut self.state, State::Normal) {
State::PendingEnter => unreachable!(),
State::PendingExit => (self.sink)(StrStep::Exit), State::PendingExit => (self.sink)(StrStep::Exit),
State::PendingEnter | State::Normal => (), State::Normal => (),
} }
self.eat_trivias(); self.eat_trivias();
self.do_token(kind, n_tokens as usize); self.do_token(kind, n_tokens as usize);

View file

@ -1,4 +1,4 @@
use crate::{LexedStr, PrefixEntryPoint, StrStep}; use crate::{LexedStr, PrefixEntryPoint, Step};
#[test] #[test]
fn vis() { fn vis() {
@ -30,12 +30,25 @@ fn stmt() {
fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) { fn check_prefix(entry: PrefixEntryPoint, input: &str, prefix: &str) {
let lexed = LexedStr::new(input); let lexed = LexedStr::new(input);
let input = lexed.to_input(); let input = lexed.to_input();
let output = entry.parse(&input);
let mut buf = String::new(); let mut n_tokens = 0;
lexed.intersperse_trivia(&output, &mut |step| match step { for step in entry.parse(&input).iter() {
StrStep::Token { kind: _, text } => buf.push_str(text), match step {
_ => (), Step::Token { n_input_tokens, .. } => n_tokens += n_input_tokens as usize,
}); Step::Enter { .. } | Step::Exit | Step::Error { .. } => (),
assert_eq!(buf.trim(), prefix) }
}
let mut i = 0;
loop {
if n_tokens == 0 {
break;
}
if !lexed.kind(i).is_trivia() {
n_tokens -= 1;
}
i += 1;
}
let buf = &lexed.as_str()[..lexed.text_start(i)];
assert_eq!(buf, prefix);
} }