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
|
|
|
|
//! modules just wraps its API.
|
|
|
|
|
2019-02-23 13:55:01 +00:00
|
|
|
use ra_parser::ParseError;
|
2019-07-20 17:04:34 +00:00
|
|
|
use rowan::{GreenNodeBuilder, Language};
|
2018-08-10 14:49:45 +00:00
|
|
|
|
2019-02-20 13:16:14 +00:00
|
|
|
use crate::{
|
2019-02-23 13:55:01 +00:00
|
|
|
syntax_error::{SyntaxError, SyntaxErrorKind},
|
2019-07-20 17:04:34 +00:00
|
|
|
Parse, SmolStr, SyntaxKind, TextUnit,
|
2019-02-20 13:16:14 +00:00
|
|
|
};
|
|
|
|
|
2019-04-08 22:06:30 +00:00
|
|
|
pub(crate) use rowan::{GreenNode, GreenToken};
|
2019-02-21 13:12:15 +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
|
|
|
|
2019-07-20 17:04:34 +00:00
|
|
|
fn kind_from_raw(raw: rowan::cursor::SyntaxKind) -> SyntaxKind {
|
|
|
|
SyntaxKind::from(raw.0)
|
2018-10-17 16:52:25 +00:00
|
|
|
}
|
2019-02-21 12:51:22 +00:00
|
|
|
|
2019-07-20 17:04:34 +00:00
|
|
|
fn kind_to_raw(kind: SyntaxKind) -> rowan::cursor::SyntaxKind {
|
|
|
|
rowan::cursor::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>;
|
|
|
|
pub type SyntaxElement = rowan::NodeOrToken<SyntaxNode, SyntaxToken>;
|
|
|
|
pub type SyntaxNodeChildren = rowan::SyntaxNodeChildren<RustLanguage>;
|
|
|
|
pub type SyntaxElementChildren = rowan::SyntaxElementChildren<RustLanguage>;
|
2019-03-30 10:25:53 +00:00
|
|
|
|
2019-07-20 17:04:34 +00:00
|
|
|
pub use rowan::{Direction, NodeOrToken};
|
2019-03-30 10:25:53 +00:00
|
|
|
|
2019-02-23 13:55:01 +00:00
|
|
|
pub struct SyntaxTreeBuilder {
|
|
|
|
errors: Vec<SyntaxError>,
|
2019-04-08 22:06:30 +00:00
|
|
|
inner: GreenNodeBuilder,
|
2019-02-23 13:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SyntaxTreeBuilder {
|
|
|
|
fn default() -> SyntaxTreeBuilder {
|
|
|
|
SyntaxTreeBuilder { errors: Vec::new(), inner: GreenNodeBuilder::new() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-20 17:04:34 +00:00
|
|
|
let node = SyntaxNode::new_root(green);
|
2019-02-23 13:55:01 +00:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
crate::validation::validate_block_structure(&node);
|
|
|
|
}
|
2019-07-20 17:04:34 +00:00
|
|
|
Parse::new(node.green().clone(), errors)
|
2019-02-23 13:55:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 10:25:53 +00:00
|
|
|
pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error(&mut self, error: ParseError, text_pos: TextUnit) {
|
|
|
|
let error = SyntaxError::new(SyntaxErrorKind::ParseError(error), text_pos);
|
|
|
|
self.errors.push(error)
|
|
|
|
}
|
|
|
|
}
|