2019-02-21 15:24:42 +03:00
|
|
|
//! This module defines Concrete Syntax Tree (CST), used by rust-analyzer.
|
|
|
|
//!
|
|
|
|
//! The CST includes comments and whitespace, provides a single node type,
|
|
|
|
//! `SyntaxNode`, and a basic traversal API (parent, children, siblings).
|
|
|
|
//!
|
|
|
|
//! The *real* implementation is in the (language-agnostic) `rowan` crate, this
|
2020-01-26 20:44:49 +02:00
|
|
|
//! module just wraps its API.
|
2019-02-21 15:24:42 +03:00
|
|
|
|
2019-07-20 20:04:34 +03:00
|
|
|
use rowan::{GreenNodeBuilder, Language};
|
2018-08-10 17:49:45 +03:00
|
|
|
|
2021-01-20 14:04:53 +03:00
|
|
|
use crate::{Parse, SyntaxError, SyntaxKind, TextSize};
|
2019-02-21 16:12:15 +03:00
|
|
|
|
2020-11-02 13:13:32 +01:00
|
|
|
pub(crate) use rowan::{GreenNode, GreenToken, NodeOrToken};
|
2020-02-10 01:19:37 +02:00
|
|
|
|
2019-07-20 20:04:34 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum RustLanguage {}
|
|
|
|
impl Language for RustLanguage {
|
|
|
|
type Kind = SyntaxKind;
|
2019-03-30 13:25:53 +03:00
|
|
|
|
2020-01-09 16:20:05 +01:00
|
|
|
fn kind_from_raw(raw: rowan::SyntaxKind) -> SyntaxKind {
|
2019-07-20 20:04:34 +03:00
|
|
|
SyntaxKind::from(raw.0)
|
2018-10-17 19:52:25 +03:00
|
|
|
}
|
2019-02-21 15:51:22 +03:00
|
|
|
|
2020-01-09 16:20:05 +01:00
|
|
|
fn kind_to_raw(kind: SyntaxKind) -> rowan::SyntaxKind {
|
|
|
|
rowan::SyntaxKind(kind.into())
|
2019-04-22 13:01:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 20:04:34 +03:00
|
|
|
pub type SyntaxNode = rowan::SyntaxNode<RustLanguage>;
|
|
|
|
pub type SyntaxToken = rowan::SyntaxToken<RustLanguage>;
|
2020-01-09 16:20:05 +01:00
|
|
|
pub type SyntaxElement = rowan::SyntaxElement<RustLanguage>;
|
2019-07-20 20:04:34 +03:00
|
|
|
pub type SyntaxNodeChildren = rowan::SyntaxNodeChildren<RustLanguage>;
|
|
|
|
pub type SyntaxElementChildren = rowan::SyntaxElementChildren<RustLanguage>;
|
2021-09-19 18:30:29 +02:00
|
|
|
pub type PreorderWithTokens = rowan::api::PreorderWithTokens<RustLanguage>;
|
2019-03-30 13:25:53 +03:00
|
|
|
|
2020-01-26 20:44:49 +02:00
|
|
|
#[derive(Default)]
|
2019-02-23 16:55:01 +03:00
|
|
|
pub struct SyntaxTreeBuilder {
|
|
|
|
errors: Vec<SyntaxError>,
|
2019-12-04 17:15:55 +01:00
|
|
|
inner: GreenNodeBuilder<'static>,
|
2019-02-23 16:55:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SyntaxTreeBuilder {
|
|
|
|
pub(crate) fn finish_raw(self) -> (GreenNode, Vec<SyntaxError>) {
|
|
|
|
let green = self.inner.finish();
|
|
|
|
(green, self.errors)
|
|
|
|
}
|
|
|
|
|
2019-07-18 23:19:04 +03:00
|
|
|
pub fn finish(self) -> Parse<SyntaxNode> {
|
2019-05-05 11:34:39 +03:00
|
|
|
let (green, errors) = self.finish_raw();
|
2022-07-08 15:44:49 +02:00
|
|
|
// Disable block validation, see https://github.com/rust-lang/rust-analyzer/pull/10357
|
2021-09-26 15:49:23 +03:00
|
|
|
if cfg!(debug_assertions) && false {
|
2020-05-31 21:39:19 +03:00
|
|
|
let node = SyntaxNode::new_root(green.clone());
|
2019-02-23 16:55:01 +03:00
|
|
|
crate::validation::validate_block_structure(&node);
|
|
|
|
}
|
2020-05-31 21:39:19 +03:00
|
|
|
Parse::new(green, errors)
|
2019-02-23 16:55:01 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 14:04:53 +03:00
|
|
|
pub fn token(&mut self, kind: SyntaxKind, text: &str) {
|
2019-07-20 20:04:34 +03:00
|
|
|
let kind = RustLanguage::kind_to_raw(kind);
|
2021-10-03 23:39:43 +11:00
|
|
|
self.inner.token(kind, text);
|
2019-02-23 16:55:01 +03:00
|
|
|
}
|
|
|
|
|
2019-03-30 13:25:53 +03:00
|
|
|
pub fn start_node(&mut self, kind: SyntaxKind) {
|
2019-07-20 20:04:34 +03:00
|
|
|
let kind = RustLanguage::kind_to_raw(kind);
|
2021-10-03 23:39:43 +11:00
|
|
|
self.inner.start_node(kind);
|
2019-02-23 16:55:01 +03:00
|
|
|
}
|
|
|
|
|
2019-03-30 13:25:53 +03:00
|
|
|
pub fn finish_node(&mut self) {
|
2021-10-03 23:39:43 +11:00
|
|
|
self.inner.finish_node();
|
2019-02-23 16:55:01 +03: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.
2021-12-19 17:36:23 +03:00
|
|
|
pub fn error(&mut self, error: String, text_pos: TextSize) {
|
|
|
|
self.errors.push(SyntaxError::new_at_offset(error, text_pos));
|
2019-02-23 16:55:01 +03:00
|
|
|
}
|
|
|
|
}
|