2021-05-22 16:53:47 +03:00
|
|
|
//! See [`TextTreeSink`].
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2019-02-21 15:24:42 +03:00
|
|
|
use std::mem;
|
|
|
|
|
2020-08-12 17:06:49 +02:00
|
|
|
use parser::{ParseError, TreeSink};
|
2019-02-21 13:37:32 +03:00
|
|
|
|
2018-10-15 19:55:32 +03:00
|
|
|
use crate::{
|
2020-11-12 12:09:12 +01:00
|
|
|
ast,
|
2019-02-21 13:37:32 +03:00
|
|
|
parsing::Token,
|
2019-02-23 16:55:01 +03:00
|
|
|
syntax_node::GreenNode,
|
2021-01-20 14:04:53 +03:00
|
|
|
SyntaxError,
|
2019-07-04 23:05:17 +03:00
|
|
|
SyntaxKind::{self, *},
|
2020-04-24 23:40:41 +02:00
|
|
|
SyntaxTreeBuilder, TextRange, TextSize,
|
2018-07-29 13:51:55 +03:00
|
|
|
};
|
2019-02-20 16:16:14 +03:00
|
|
|
|
2019-02-21 15:24:42 +03:00
|
|
|
/// Bridges the parser with our specific syntax tree representation.
|
|
|
|
///
|
2019-02-23 16:07:29 +03:00
|
|
|
/// `TextTreeSink` also handles attachment of trivia (whitespace) to nodes.
|
|
|
|
pub(crate) struct TextTreeSink<'a> {
|
2019-02-21 12:03:42 +03:00
|
|
|
text: &'a str,
|
|
|
|
tokens: &'a [Token],
|
2020-04-24 23:40:41 +02:00
|
|
|
text_pos: TextSize,
|
2019-02-21 12:03:42 +03:00
|
|
|
token_pos: usize,
|
2019-02-21 15:24:42 +03:00
|
|
|
state: State,
|
2019-02-23 16:55:01 +03:00
|
|
|
inner: SyntaxTreeBuilder,
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:24:42 +03:00
|
|
|
enum State {
|
|
|
|
PendingStart,
|
|
|
|
Normal,
|
|
|
|
PendingFinish,
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:07:29 +03:00
|
|
|
impl<'a> TreeSink for TextTreeSink<'a> {
|
2019-03-30 13:25:53 +03:00
|
|
|
fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
|
2019-02-21 15:24:42 +03:00
|
|
|
match mem::replace(&mut self.state, State::Normal) {
|
|
|
|
State::PendingStart => unreachable!(),
|
2019-03-30 13:25:53 +03:00
|
|
|
State::PendingFinish => self.inner.finish_node(),
|
2019-02-21 15:24:42 +03:00
|
|
|
State::Normal => (),
|
|
|
|
}
|
2019-02-21 12:03:42 +03:00
|
|
|
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)
|
2020-04-24 23:40:41 +02:00
|
|
|
.sum::<TextSize>();
|
2019-03-30 13:25:53 +03:00
|
|
|
self.do_token(kind, len, n_tokens);
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
2019-03-30 13:25:53 +03:00
|
|
|
fn start_node(&mut self, kind: SyntaxKind) {
|
2019-02-21 15:24:42 +03:00
|
|
|
match mem::replace(&mut self.state, State::Normal) {
|
|
|
|
State::PendingStart => {
|
2019-03-30 13:25:53 +03:00
|
|
|
self.inner.start_node(kind);
|
2019-02-21 15:24:42 +03:00
|
|
|
// No need to attach trivias to previous node: there is no
|
|
|
|
// previous node.
|
|
|
|
return;
|
|
|
|
}
|
2019-03-30 13:25:53 +03:00
|
|
|
State::PendingFinish => self.inner.finish_node(),
|
2019-02-21 15:24:42 +03:00
|
|
|
State::Normal => (),
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
2019-02-21 15:24:42 +03:00
|
|
|
|
2019-02-21 12:03:42 +03:00
|
|
|
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 =
|
2020-04-24 23:40:41 +02:00
|
|
|
self.text_pos + leading_trivias.iter().map(|it| it.len).sum::<TextSize>();
|
2019-02-21 12:03:42 +03:00
|
|
|
|
|
|
|
let n_attached_trivias = {
|
|
|
|
let leading_trivias = leading_trivias.iter().rev().map(|it| {
|
|
|
|
let next_end = trivia_end - it.len;
|
2020-04-24 23:40:41 +02:00
|
|
|
let range = TextRange::new(next_end, trivia_end);
|
2019-02-21 12:03:42 +03:00
|
|
|
trivia_end = next_end;
|
|
|
|
(it.kind, &self.text[range])
|
|
|
|
});
|
|
|
|
n_attached_trivias(kind, leading_trivias)
|
|
|
|
};
|
|
|
|
self.eat_n_trivias(n_trivias - n_attached_trivias);
|
2019-03-30 13:25:53 +03:00
|
|
|
self.inner.start_node(kind);
|
2019-02-21 12:03:42 +03:00
|
|
|
self.eat_n_trivias(n_attached_trivias);
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
2019-03-30 13:25:53 +03:00
|
|
|
fn finish_node(&mut self) {
|
2019-02-21 15:24:42 +03:00
|
|
|
match mem::replace(&mut self.state, State::PendingFinish) {
|
|
|
|
State::PendingStart => unreachable!(),
|
2019-03-30 13:25:53 +03:00
|
|
|
State::PendingFinish => self.inner.finish_node(),
|
2019-02-21 15:24:42 +03:00
|
|
|
State::Normal => (),
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
|
2019-02-20 23:17:07 +03:00
|
|
|
fn error(&mut self, error: ParseError) {
|
2021-10-03 23:39:43 +11:00
|
|
|
self.inner.error(error, self.text_pos);
|
2018-07-29 13:51:55 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 12:03:42 +03:00
|
|
|
|
2019-02-23 16:07:29 +03:00
|
|
|
impl<'a> TextTreeSink<'a> {
|
2020-01-28 07:09:13 +02:00
|
|
|
pub(super) fn new(text: &'a str, tokens: &'a [Token]) -> Self {
|
2020-01-26 20:44:49 +02:00
|
|
|
Self {
|
2019-02-21 12:03:42 +03:00
|
|
|
text,
|
|
|
|
tokens,
|
|
|
|
text_pos: 0.into(),
|
|
|
|
token_pos: 0,
|
2019-02-21 15:24:42 +03:00
|
|
|
state: State::PendingStart,
|
2020-01-28 07:09:13 +02:00
|
|
|
inner: SyntaxTreeBuilder::default(),
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 13:37:32 +03:00
|
|
|
|
2019-02-21 15:24:42 +03:00
|
|
|
pub(super) fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) {
|
|
|
|
match mem::replace(&mut self.state, State::Normal) {
|
|
|
|
State::PendingFinish => {
|
|
|
|
self.eat_trivias();
|
2021-10-03 23:39:43 +11:00
|
|
|
self.inner.finish_node();
|
2019-02-21 15:24:42 +03:00
|
|
|
}
|
|
|
|
State::PendingStart | State::Normal => unreachable!(),
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:55:01 +03:00
|
|
|
self.inner.finish_raw()
|
2019-02-21 13:37:32 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 12:03:42 +03:00
|
|
|
fn eat_trivias(&mut self) {
|
|
|
|
while let Some(&token) = self.tokens.get(self.token_pos) {
|
|
|
|
if !token.kind.is_trivia() {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-30 13:25:53 +03:00
|
|
|
self.do_token(token.kind, token.len, 1);
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eat_n_trivias(&mut self, n: usize) {
|
|
|
|
for _ in 0..n {
|
|
|
|
let token = self.tokens[self.token_pos];
|
|
|
|
assert!(token.kind.is_trivia());
|
2019-03-30 13:25:53 +03:00
|
|
|
self.do_token(token.kind, token.len, 1);
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:40:41 +02:00
|
|
|
fn do_token(&mut self, kind: SyntaxKind, len: TextSize, n_tokens: usize) {
|
|
|
|
let range = TextRange::at(self.text_pos, len);
|
2021-01-20 14:04:53 +03:00
|
|
|
let text = &self.text[range];
|
2019-02-21 12:03:42 +03:00
|
|
|
self.text_pos += len;
|
|
|
|
self.token_pos += n_tokens;
|
2019-03-30 13:25:53 +03:00
|
|
|
self.inner.token(kind, text);
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn n_attached_trivias<'a>(
|
|
|
|
kind: SyntaxKind,
|
|
|
|
trivias: impl Iterator<Item = (SyntaxKind, &'a str)>,
|
|
|
|
) -> usize {
|
|
|
|
match kind {
|
2021-05-15 17:23:20 +02:00
|
|
|
CONST | ENUM | FN | IMPL | MACRO_CALL | MACRO_DEF | MACRO_RULES | MODULE | RECORD_FIELD
|
|
|
|
| STATIC | STRUCT | TRAIT | TUPLE_FIELD | TYPE_ALIAS | UNION | USE | VARIANT => {
|
2019-02-21 12:03:42 +03:00
|
|
|
let mut res = 0;
|
2020-03-31 14:19:21 +02:00
|
|
|
let mut trivias = trivias.enumerate().peekable();
|
|
|
|
|
|
|
|
while let Some((i, (kind, text))) = trivias.next() {
|
2019-02-21 12:03:42 +03:00
|
|
|
match kind {
|
2020-11-12 12:09:12 +01:00
|
|
|
WHITESPACE if text.contains("\n\n") => {
|
|
|
|
// we check whether the next token is a doc-comment
|
|
|
|
// and skip the whitespace in this case
|
|
|
|
if let Some((COMMENT, peek_text)) = trivias.peek().map(|(_, pair)| pair) {
|
|
|
|
let comment_kind = ast::CommentKind::from_text(peek_text);
|
|
|
|
if comment_kind.doc == Some(ast::CommentPlacement::Outer) {
|
|
|
|
continue;
|
2020-03-31 14:19:21 +02:00
|
|
|
}
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
2020-11-12 12:09:12 +01:00
|
|
|
break;
|
2019-02-21 12:03:42 +03:00
|
|
|
}
|
|
|
|
COMMENT => {
|
2020-11-12 12:09:12 +01:00
|
|
|
let comment_kind = ast::CommentKind::from_text(text);
|
|
|
|
if comment_kind.doc == Some(ast::CommentPlacement::Inner) {
|
|
|
|
break;
|
|
|
|
}
|
2019-02-21 12:03:42 +03:00
|
|
|
res = i + 1;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|