tighten up invariants

This commit is contained in:
Aleksey Kladov 2021-12-12 19:22:37 +03:00
parent 18d4737fb9
commit 57e6ef0bfb
3 changed files with 17 additions and 14 deletions

View File

@ -62,7 +62,9 @@ pub(crate) fn to_parser_tokens(buffer: &TokenBuffer) -> parser::Tokens {
let kind = SyntaxKind::from_char(punct.char) let kind = SyntaxKind::from_char(punct.char)
.unwrap_or_else(|| panic!("{:#?} is not a valid punct", punct)); .unwrap_or_else(|| panic!("{:#?} is not a valid punct", punct));
res.push(kind); res.push(kind);
res.was_joint(punct.spacing == tt::Spacing::Joint); if punct.spacing == tt::Spacing::Joint {
res.was_joint();
}
} }
} }
cursor.bump() cursor.bump()

View File

@ -51,11 +51,8 @@ impl Tokens {
/// tokens.push(curr_joint) /// tokens.push(curr_joint)
/// ``` /// ```
#[inline] #[inline]
pub fn was_joint(&mut self, yes: bool) { pub fn was_joint(&mut self) {
let idx = self.len(); self.set_joint(self.len() - 1);
if yes && idx > 0 {
self.set_joint(idx - 1);
}
} }
#[inline] #[inline]
pub fn push_ident(&mut self, contextual_kw: SyntaxKind) { pub fn push_ident(&mut self, contextual_kw: SyntaxKind) {

View File

@ -58,18 +58,22 @@ pub(crate) fn parse_text_as<T: AstNode>(
pub(crate) fn to_parser_tokens(text: &str, lexer_tokens: &[lexer::Token]) -> ::parser::Tokens { pub(crate) fn to_parser_tokens(text: &str, lexer_tokens: &[lexer::Token]) -> ::parser::Tokens {
let mut off = 0; let mut off = 0;
let mut res = parser::Tokens::default(); let mut res = parser::Tokens::default();
let mut was_joint = true; let mut was_joint = false;
for t in lexer_tokens { for t in lexer_tokens {
if t.kind.is_trivia() { if t.kind.is_trivia() {
was_joint = false; was_joint = false;
} else if t.kind == SyntaxKind::IDENT {
let token_text = &text[off..][..usize::from(t.len)];
let contextual_kw =
SyntaxKind::from_contextual_keyword(token_text).unwrap_or(SyntaxKind::IDENT);
res.push_ident(contextual_kw);
} else { } else {
res.was_joint(was_joint); if t.kind == SyntaxKind::IDENT {
res.push(t.kind); let token_text = &text[off..][..usize::from(t.len)];
let contextual_kw =
SyntaxKind::from_contextual_keyword(token_text).unwrap_or(SyntaxKind::IDENT);
res.push_ident(contextual_kw);
} else {
if was_joint {
res.was_joint();
}
res.push(t.kind);
}
was_joint = true; was_joint = true;
} }
off += usize::from(t.len); off += usize::from(t.len);