use std::mem; use ra_parser::{TreeSink, ParseError}; use rowan::GreenNodeBuilder; use crate::{ SmolStr, SyntaxError, SyntaxErrorKind, TextUnit, TextRange, SyntaxKind::{self, *}, parsing::Token, syntax_node::{GreenNode, RaTypes}, }; /// Bridges the parser with our specific syntax tree representation. /// /// `TextTreeSink` also handles attachment of trivia (whitespace) to nodes. pub(crate) struct TextTreeSink<'a> { text: &'a str, tokens: &'a [Token], text_pos: TextUnit, token_pos: usize, state: State, errors: Vec, inner: GreenNodeBuilder, } enum State { PendingStart, Normal, PendingFinish, } impl<'a> TreeSink for TextTreeSink<'a> { fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8) { match mem::replace(&mut self.state, State::Normal) { State::PendingStart => unreachable!(), State::PendingFinish => self.inner.finish_internal(), State::Normal => (), } self.eat_trivias(); let n_tokens = n_tokens as usize; let len = self.tokens[self.token_pos..self.token_pos + n_tokens] .iter() .map(|it| it.len) .sum::(); self.do_leaf(kind, len, n_tokens); } fn start_branch(&mut self, kind: SyntaxKind) { match mem::replace(&mut self.state, State::Normal) { State::PendingStart => { self.inner.start_internal(kind); // No need to attach trivias to previous node: there is no // previous node. return; } State::PendingFinish => self.inner.finish_internal(), State::Normal => (), } let n_trivias = self.tokens[self.token_pos..].iter().take_while(|it| it.kind.is_trivia()).count(); let leading_trivias = &self.tokens[self.token_pos..self.token_pos + n_trivias]; let mut trivia_end = self.text_pos + leading_trivias.iter().map(|it| it.len).sum::(); let n_attached_trivias = { let leading_trivias = leading_trivias.iter().rev().map(|it| { let next_end = trivia_end - it.len; let range = TextRange::from_to(next_end, trivia_end); trivia_end = next_end; (it.kind, &self.text[range]) }); n_attached_trivias(kind, leading_trivias) }; self.eat_n_trivias(n_trivias - n_attached_trivias); self.inner.start_internal(kind); self.eat_n_trivias(n_attached_trivias); } fn finish_branch(&mut self) { match mem::replace(&mut self.state, State::PendingFinish) { State::PendingStart => unreachable!(), State::PendingFinish => self.inner.finish_internal(), State::Normal => (), } } fn error(&mut self, error: ParseError) { let error = SyntaxError::new(SyntaxErrorKind::ParseError(error), self.text_pos); self.errors.push(error) } } impl<'a> TextTreeSink<'a> { pub(super) fn new(text: &'a str, tokens: &'a [Token]) -> TextTreeSink<'a> { TextTreeSink { text, tokens, text_pos: 0.into(), token_pos: 0, state: State::PendingStart, errors: Vec::new(), inner: GreenNodeBuilder::new(), } } pub(super) fn finish(mut self) -> (GreenNode, Vec) { match mem::replace(&mut self.state, State::Normal) { State::PendingFinish => { self.eat_trivias(); self.inner.finish_internal() } State::PendingStart | State::Normal => unreachable!(), } (self.inner.finish(), self.errors) } fn eat_trivias(&mut self) { while let Some(&token) = self.tokens.get(self.token_pos) { if !token.kind.is_trivia() { break; } self.do_leaf(token.kind, token.len, 1); } } fn eat_n_trivias(&mut self, n: usize) { for _ in 0..n { let token = self.tokens[self.token_pos]; assert!(token.kind.is_trivia()); self.do_leaf(token.kind, token.len, 1); } } fn do_leaf(&mut self, kind: SyntaxKind, len: TextUnit, n_tokens: usize) { let range = TextRange::offset_len(self.text_pos, len); let text: SmolStr = self.text[range].into(); self.text_pos += len; self.token_pos += n_tokens; self.inner.leaf(kind, text); } } fn n_attached_trivias<'a>( kind: SyntaxKind, trivias: impl Iterator, ) -> usize { match kind { CONST_DEF | TYPE_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF | TRAIT_DEF | MODULE | NAMED_FIELD_DEF => { let mut res = 0; for (i, (kind, text)) in trivias.enumerate() { match kind { WHITESPACE => { if text.contains("\n\n") { break; } } COMMENT => { res = i + 1; } _ => (), } } res } _ => 0, } }