diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-08-12 17:31:42 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-08-12 17:31:42 +0100 |
commit | d583f2c46d22cf8d643ebf98be9cb7059a304431 (patch) | |
tree | 9d898eb9600b0c36a74e4f95238f679c683fa566 /crates/syntax/src/syntax_node.rs | |
parent | 3d6889cba72a9d02199f7adaa2ecc69bc30af834 (diff) | |
parent | a1c187eef3ba08076aedb5154929f7eda8d1b424 (diff) |
Merge #5729
5729: Rename ra_syntax -> syntax
r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/syntax/src/syntax_node.rs')
-rw-r--r-- | crates/syntax/src/syntax_node.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/crates/syntax/src/syntax_node.rs b/crates/syntax/src/syntax_node.rs new file mode 100644 index 000000000..b2abcbfbb --- /dev/null +++ b/crates/syntax/src/syntax_node.rs | |||
@@ -0,0 +1,77 @@ | |||
1 | //! This module defines Concrete Syntax Tree (CST), used by rust-analyzer. | ||
2 | //! | ||
3 | //! The CST includes comments and whitespace, provides a single node type, | ||
4 | //! `SyntaxNode`, and a basic traversal API (parent, children, siblings). | ||
5 | //! | ||
6 | //! The *real* implementation is in the (language-agnostic) `rowan` crate, this | ||
7 | //! module just wraps its API. | ||
8 | |||
9 | use rowan::{GreenNodeBuilder, Language}; | ||
10 | |||
11 | use crate::{Parse, SmolStr, SyntaxError, SyntaxKind, TextSize}; | ||
12 | |||
13 | pub use rowan::GreenNode; | ||
14 | |||
15 | pub(crate) use rowan::GreenToken; | ||
16 | |||
17 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
18 | pub enum RustLanguage {} | ||
19 | impl Language for RustLanguage { | ||
20 | type Kind = SyntaxKind; | ||
21 | |||
22 | fn kind_from_raw(raw: rowan::SyntaxKind) -> SyntaxKind { | ||
23 | SyntaxKind::from(raw.0) | ||
24 | } | ||
25 | |||
26 | fn kind_to_raw(kind: SyntaxKind) -> rowan::SyntaxKind { | ||
27 | rowan::SyntaxKind(kind.into()) | ||
28 | } | ||
29 | } | ||
30 | |||
31 | pub type SyntaxNode = rowan::SyntaxNode<RustLanguage>; | ||
32 | pub type SyntaxToken = rowan::SyntaxToken<RustLanguage>; | ||
33 | pub type SyntaxElement = rowan::SyntaxElement<RustLanguage>; | ||
34 | pub type SyntaxNodeChildren = rowan::SyntaxNodeChildren<RustLanguage>; | ||
35 | pub type SyntaxElementChildren = rowan::SyntaxElementChildren<RustLanguage>; | ||
36 | |||
37 | pub use rowan::{Direction, NodeOrToken}; | ||
38 | |||
39 | #[derive(Default)] | ||
40 | pub struct SyntaxTreeBuilder { | ||
41 | errors: Vec<SyntaxError>, | ||
42 | inner: GreenNodeBuilder<'static>, | ||
43 | } | ||
44 | |||
45 | impl SyntaxTreeBuilder { | ||
46 | pub(crate) fn finish_raw(self) -> (GreenNode, Vec<SyntaxError>) { | ||
47 | let green = self.inner.finish(); | ||
48 | (green, self.errors) | ||
49 | } | ||
50 | |||
51 | pub fn finish(self) -> Parse<SyntaxNode> { | ||
52 | let (green, errors) = self.finish_raw(); | ||
53 | if cfg!(debug_assertions) { | ||
54 | let node = SyntaxNode::new_root(green.clone()); | ||
55 | crate::validation::validate_block_structure(&node); | ||
56 | } | ||
57 | Parse::new(green, errors) | ||
58 | } | ||
59 | |||
60 | pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) { | ||
61 | let kind = RustLanguage::kind_to_raw(kind); | ||
62 | self.inner.token(kind, text) | ||
63 | } | ||
64 | |||
65 | pub fn start_node(&mut self, kind: SyntaxKind) { | ||
66 | let kind = RustLanguage::kind_to_raw(kind); | ||
67 | self.inner.start_node(kind) | ||
68 | } | ||
69 | |||
70 | pub fn finish_node(&mut self) { | ||
71 | self.inner.finish_node() | ||
72 | } | ||
73 | |||
74 | pub fn error(&mut self, error: parser::ParseError, text_pos: TextSize) { | ||
75 | self.errors.push(SyntaxError::new_at_offset(*error.0, text_pos)) | ||
76 | } | ||
77 | } | ||