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>;
|
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();
|
2019-02-23 13:55:01 +00:00
|
|
|
if cfg!(debug_assertions) {
|
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);
|
|
|
|
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);
|
|
|
|
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) {
|
|
|
|
self.inner.finish_node()
|
2019-02-23 13:55:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 15:06:49 +00:00
|
|
|
pub fn error(&mut self, error: parser::ParseError, text_pos: TextSize) {
|
2020-05-16 16:06:23 +00:00
|
|
|
self.errors.push(SyntaxError::new_at_offset(*error.0, text_pos))
|
2019-02-23 13:55:01 +00:00
|
|
|
}
|
|
|
|
}
|