mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +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,9 +10,8 @@
|
|||
use std::mem;
|
||||
|
||||
use crate::{
|
||||
ParseError,
|
||||
tree_traversal::TreeTraversal,
|
||||
SyntaxKind::{self, *},
|
||||
TreeSink,
|
||||
};
|
||||
|
||||
/// `Parser` produces a flat list of `Event`s.
|
||||
|
@ -77,7 +76,7 @@ pub(crate) enum Event {
|
|||
},
|
||||
|
||||
Error {
|
||||
msg: ParseError,
|
||||
msg: String,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -88,7 +87,8 @@ impl Event {
|
|||
}
|
||||
|
||||
/// Generate the syntax tree with the control of events.
|
||||
pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
|
||||
pub(super) fn process(mut events: Vec<Event>) -> TreeTraversal {
|
||||
let mut res = TreeTraversal::default();
|
||||
let mut forward_parents = Vec::new();
|
||||
|
||||
for i in 0..events.len() {
|
||||
|
@ -117,15 +117,17 @@ pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
|
|||
|
||||
for kind in forward_parents.drain(..).rev() {
|
||||
if kind != TOMBSTONE {
|
||||
sink.start_node(kind);
|
||||
res.enter_node(kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Finish => sink.finish_node(),
|
||||
Event::Finish => res.leave_node(),
|
||||
Event::Token { kind, n_raw_tokens } => {
|
||||
sink.token(kind, n_raw_tokens);
|
||||
res.token(kind, n_raw_tokens);
|
||||
}
|
||||
Event::Error { msg } => sink.error(msg),
|
||||
Event::Error { msg } => res.error(msg),
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
|
|
@ -25,31 +25,19 @@ mod event;
|
|||
mod parser;
|
||||
mod grammar;
|
||||
mod tokens;
|
||||
mod tree_traversal;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub(crate) use token_set::TokenSet;
|
||||
|
||||
pub use crate::{lexed_str::LexedStr, syntax_kind::SyntaxKind, tokens::Tokens};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct ParseError(pub Box<String>);
|
||||
|
||||
/// `TreeSink` abstracts details of a particular syntax tree implementation.
|
||||
pub trait TreeSink {
|
||||
/// Adds new token to the current branch.
|
||||
fn token(&mut self, kind: SyntaxKind, n_tokens: u8);
|
||||
|
||||
/// Start new branch and make it current.
|
||||
fn start_node(&mut self, kind: SyntaxKind);
|
||||
|
||||
/// Finish current branch and restore previous
|
||||
/// branch as current.
|
||||
fn finish_node(&mut self);
|
||||
|
||||
fn error(&mut self, error: ParseError);
|
||||
}
|
||||
pub use crate::{
|
||||
lexed_str::LexedStr,
|
||||
syntax_kind::SyntaxKind,
|
||||
tokens::Tokens,
|
||||
tree_traversal::{TraversalStep, TreeTraversal},
|
||||
};
|
||||
|
||||
/// rust-analyzer parser allows you to choose one of the possible entry points.
|
||||
///
|
||||
|
@ -74,11 +62,11 @@ pub enum ParserEntryPoint {
|
|||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a rust file.
|
||||
pub fn parse_source_file(tokens: &Tokens, tree_sink: &mut dyn TreeSink) {
|
||||
parse(tokens, tree_sink, ParserEntryPoint::SourceFile);
|
||||
pub fn parse_source_file(tokens: &Tokens) -> TreeTraversal {
|
||||
parse(tokens, ParserEntryPoint::SourceFile)
|
||||
}
|
||||
|
||||
pub fn parse(tokens: &Tokens, tree_sink: &mut dyn TreeSink, entry_point: ParserEntryPoint) {
|
||||
pub fn parse(tokens: &Tokens, entry_point: ParserEntryPoint) -> TreeTraversal {
|
||||
let entry_point: fn(&'_ mut parser::Parser) = match entry_point {
|
||||
ParserEntryPoint::SourceFile => grammar::entry_points::source_file,
|
||||
ParserEntryPoint::Path => grammar::entry_points::path,
|
||||
|
@ -99,7 +87,7 @@ pub fn parse(tokens: &Tokens, tree_sink: &mut dyn TreeSink, entry_point: ParserE
|
|||
let mut p = parser::Parser::new(tokens);
|
||||
entry_point(&mut p);
|
||||
let events = p.finish();
|
||||
event::process(tree_sink, events);
|
||||
event::process(events)
|
||||
}
|
||||
|
||||
/// A parsing function for a specific braced-block.
|
||||
|
@ -119,11 +107,11 @@ impl Reparser {
|
|||
///
|
||||
/// Tokens must start with `{`, end with `}` and form a valid brace
|
||||
/// sequence.
|
||||
pub fn parse(self, tokens: &Tokens, tree_sink: &mut dyn TreeSink) {
|
||||
pub fn parse(self, tokens: &Tokens) -> TreeTraversal {
|
||||
let Reparser(r) = self;
|
||||
let mut p = parser::Parser::new(tokens);
|
||||
r(&mut p);
|
||||
let events = p.finish();
|
||||
event::process(tree_sink, events);
|
||||
event::process(events)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ use limit::Limit;
|
|||
use crate::{
|
||||
event::Event,
|
||||
tokens::Tokens,
|
||||
ParseError,
|
||||
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
|
||||
TokenSet, T,
|
||||
};
|
||||
|
@ -196,7 +195,7 @@ impl<'t> Parser<'t> {
|
|||
/// structured errors with spans and notes, like rustc
|
||||
/// does.
|
||||
pub(crate) fn error<T: Into<String>>(&mut self, message: T) {
|
||||
let msg = ParseError(Box::new(message.into()));
|
||||
let msg = message.into();
|
||||
self.push_event(Event::Error { msg });
|
||||
}
|
||||
|
||||
|
|
67
crates/parser/src/tree_traversal.rs
Normal file
67
crates/parser/src/tree_traversal.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
//! TODO
|
||||
use crate::SyntaxKind;
|
||||
|
||||
/// Output of the parser.
|
||||
#[derive(Default)]
|
||||
pub struct TreeTraversal {
|
||||
/// 32-bit encoding of events. If LSB is zero, then that's an index into the
|
||||
/// error vector. Otherwise, it's one of the thee other variants, with data encoded as
|
||||
///
|
||||
/// |16 bit kind|8 bit n_raw_tokens|4 bit tag|4 bit leftover|
|
||||
///
|
||||
event: Vec<u32>,
|
||||
error: Vec<String>,
|
||||
}
|
||||
|
||||
pub enum TraversalStep<'a> {
|
||||
Token { kind: SyntaxKind, n_raw_tokens: u8 },
|
||||
EnterNode { kind: SyntaxKind },
|
||||
LeaveNode,
|
||||
Error { msg: &'a str },
|
||||
}
|
||||
|
||||
impl TreeTraversal {
|
||||
pub fn iter(&self) -> impl Iterator<Item = TraversalStep<'_>> {
|
||||
self.event.iter().map(|&event| {
|
||||
if event & 0b1 == 0 {
|
||||
return TraversalStep::Error { msg: self.error[(event as usize) >> 1].as_str() };
|
||||
}
|
||||
let tag = ((event & 0x0000_00F0) >> 4) as u8;
|
||||
match tag {
|
||||
0 => {
|
||||
let kind: SyntaxKind = (((event & 0xFFFF_0000) >> 16) as u16).into();
|
||||
let n_raw_tokens = ((event & 0x0000_FF00) >> 8) as u8;
|
||||
TraversalStep::Token { kind, n_raw_tokens }
|
||||
}
|
||||
1 => {
|
||||
let kind: SyntaxKind = (((event & 0xFFFF_0000) >> 16) as u16).into();
|
||||
TraversalStep::EnterNode { kind }
|
||||
}
|
||||
2 => TraversalStep::LeaveNode,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
|
||||
let e = ((kind as u16 as u32) << 16) | ((n_tokens as u32) << 8) | (0 << 4) | 1;
|
||||
self.event.push(e)
|
||||
}
|
||||
|
||||
pub(crate) fn enter_node(&mut self, kind: SyntaxKind) {
|
||||
let e = ((kind as u16 as u32) << 16) | (1 << 4) | 1;
|
||||
self.event.push(e)
|
||||
}
|
||||
|
||||
pub(crate) fn leave_node(&mut self) {
|
||||
let e = 2 << 4 | 1;
|
||||
self.event.push(e)
|
||||
}
|
||||
|
||||
pub(crate) fn error(&mut self, error: String) {
|
||||
let idx = self.error.len();
|
||||
self.error.push(error);
|
||||
let e = (idx as u32) << 1;
|
||||
self.event.push(e);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue