mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-22 11:24:24 +00:00
internal: replace TreeSink with a data structure
The general theme of this is to make parser a better independent library. The specific thing we do here is replacing callback based TreeSink with a data structure. That is, rather than calling user-provided tree construction methods, the parser now spits out a very bare-bones tree, effectively a log of a DFS traversal. This makes the parser usable without any *specifc* tree sink, and allows us to, eg, move tests into this crate. Now, it's also true that this is a distinction without a difference, as the old and the new interface are equivalent in expressiveness. Still, this new thing seems somewhat simpler. But yeah, I admit I don't have a suuper strong motivation here, just a hunch that this is better.
This commit is contained in:
parent
2f63558dc5
commit
d0d05075ed
10 changed files with 172 additions and 110 deletions
|
@ -10,7 +10,7 @@ use parser::Reparser;
|
|||
use text_edit::Indel;
|
||||
|
||||
use crate::{
|
||||
parsing::text_tree_sink::TextTreeSink,
|
||||
parsing::text_tree_sink::build_tree,
|
||||
syntax_node::{GreenNode, GreenToken, NodeOrToken, SyntaxElement, SyntaxNode},
|
||||
SyntaxError,
|
||||
SyntaxKind::*,
|
||||
|
@ -94,11 +94,9 @@ fn reparse_block(
|
|||
return None;
|
||||
}
|
||||
|
||||
let mut tree_sink = TextTreeSink::new(lexed);
|
||||
let tree_traversal = reparser.parse(&parser_tokens);
|
||||
|
||||
reparser.parse(&parser_tokens, &mut tree_sink);
|
||||
|
||||
let (green, new_parser_errors) = tree_sink.finish();
|
||||
let (green, new_parser_errors, _eof) = build_tree(lexed, tree_traversal, false);
|
||||
|
||||
Some((node.replace_with(green), new_parser_errors, node.text_range()))
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::mem;
|
||||
|
||||
use parser::{LexedStr, ParseError, TreeSink};
|
||||
use parser::{LexedStr, TreeTraversal};
|
||||
|
||||
use crate::{
|
||||
ast,
|
||||
|
@ -12,6 +12,36 @@ use crate::{
|
|||
SyntaxTreeBuilder, TextRange,
|
||||
};
|
||||
|
||||
pub(crate) fn build_tree(
|
||||
lexed: LexedStr<'_>,
|
||||
tree_traversal: TreeTraversal,
|
||||
synthetic_root: bool,
|
||||
) -> (GreenNode, Vec<SyntaxError>, bool) {
|
||||
let mut builder = TextTreeSink::new(lexed);
|
||||
|
||||
if synthetic_root {
|
||||
builder.start_node(SyntaxKind::SOURCE_FILE);
|
||||
}
|
||||
|
||||
for event in tree_traversal.iter() {
|
||||
match event {
|
||||
parser::TraversalStep::Token { kind, n_raw_tokens } => {
|
||||
builder.token(kind, n_raw_tokens)
|
||||
}
|
||||
parser::TraversalStep::EnterNode { kind } => builder.start_node(kind),
|
||||
parser::TraversalStep::LeaveNode => builder.finish_node(),
|
||||
parser::TraversalStep::Error { msg } => {
|
||||
let text_pos = builder.lexed.text_start(builder.pos).try_into().unwrap();
|
||||
builder.inner.error(msg.to_string(), text_pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
if synthetic_root {
|
||||
builder.finish_node()
|
||||
}
|
||||
builder.finish_eof()
|
||||
}
|
||||
|
||||
/// Bridges the parser with our specific syntax tree representation.
|
||||
///
|
||||
/// `TextTreeSink` also handles attachment of trivia (whitespace) to nodes.
|
||||
|
@ -28,7 +58,7 @@ enum State {
|
|||
PendingFinish,
|
||||
}
|
||||
|
||||
impl<'a> TreeSink for TextTreeSink<'a> {
|
||||
impl<'a> TextTreeSink<'a> {
|
||||
fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
|
||||
match mem::replace(&mut self.state, State::Normal) {
|
||||
State::PendingStart => unreachable!(),
|
||||
|
@ -70,11 +100,6 @@ impl<'a> TreeSink for TextTreeSink<'a> {
|
|||
State::Normal => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn error(&mut self, error: ParseError) {
|
||||
let text_pos = self.lexed.text_start(self.pos).try_into().unwrap();
|
||||
self.inner.error(error, text_pos);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TextTreeSink<'a> {
|
||||
|
@ -106,11 +131,6 @@ impl<'a> TextTreeSink<'a> {
|
|||
(node, errors, is_eof)
|
||||
}
|
||||
|
||||
pub(super) fn finish(self) -> (GreenNode, Vec<SyntaxError>) {
|
||||
let (node, errors, _eof) = self.finish_eof();
|
||||
(node, errors)
|
||||
}
|
||||
|
||||
fn eat_trivias(&mut self) {
|
||||
while self.pos < self.lexed.len() {
|
||||
let kind = self.lexed.kind(self.pos);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue