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