diff options
author | Luca Barbieri <[email protected]> | 2020-04-03 20:12:08 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-04-08 16:15:12 +0100 |
commit | 68196ccc10c60de52bb771d295879456f73ede95 (patch) | |
tree | cec60917f9c4475c49d4842c05469f19ad2bff86 /crates/ra_syntax/src/ast | |
parent | 4762c6d9c66dc1b6be9b9010dbe787ef8d69530a (diff) |
Add AstElement trait, generate tokens, support tokens in enums
- Adds a new AstElement trait that is implemented by all generated
node, token and enum structs
- Overhauls the code generators to code-generate all tokens, and
also enhances enums to support including tokens, node, and nested
enums
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/tokens.rs | 62 |
1 files changed, 2 insertions, 60 deletions
diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index 1a51b8d3b..e8320b57e 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs | |||
@@ -1,26 +1,10 @@ | |||
1 | //! There are many AstNodes, but only a few tokens, so we hand-write them here. | 1 | //! There are many AstNodes, but only a few tokens, so we hand-write them here. |
2 | 2 | ||
3 | use crate::{ | 3 | use crate::{ |
4 | ast::AstToken, | 4 | ast::{AstToken, Comment, RawString, String, Whitespace}, |
5 | SyntaxKind::{COMMENT, RAW_STRING, STRING, WHITESPACE}, | 5 | TextRange, TextUnit, |
6 | SyntaxToken, TextRange, TextUnit, | ||
7 | }; | 6 | }; |
8 | 7 | ||
9 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
10 | pub struct Comment(SyntaxToken); | ||
11 | |||
12 | impl AstToken for Comment { | ||
13 | fn cast(token: SyntaxToken) -> Option<Self> { | ||
14 | match token.kind() { | ||
15 | COMMENT => Some(Comment(token)), | ||
16 | _ => None, | ||
17 | } | ||
18 | } | ||
19 | fn syntax(&self) -> &SyntaxToken { | ||
20 | &self.0 | ||
21 | } | ||
22 | } | ||
23 | |||
24 | impl Comment { | 8 | impl Comment { |
25 | pub fn kind(&self) -> CommentKind { | 9 | pub fn kind(&self) -> CommentKind { |
26 | kind_by_prefix(self.text()) | 10 | kind_by_prefix(self.text()) |
@@ -89,20 +73,6 @@ fn prefix_by_kind(kind: CommentKind) -> &'static str { | |||
89 | unreachable!() | 73 | unreachable!() |
90 | } | 74 | } |
91 | 75 | ||
92 | pub struct Whitespace(SyntaxToken); | ||
93 | |||
94 | impl AstToken for Whitespace { | ||
95 | fn cast(token: SyntaxToken) -> Option<Self> { | ||
96 | match token.kind() { | ||
97 | WHITESPACE => Some(Whitespace(token)), | ||
98 | _ => None, | ||
99 | } | ||
100 | } | ||
101 | fn syntax(&self) -> &SyntaxToken { | ||
102 | &self.0 | ||
103 | } | ||
104 | } | ||
105 | |||
106 | impl Whitespace { | 76 | impl Whitespace { |
107 | pub fn spans_multiple_lines(&self) -> bool { | 77 | pub fn spans_multiple_lines(&self) -> bool { |
108 | let text = self.text(); | 78 | let text = self.text(); |
@@ -168,20 +138,6 @@ pub trait HasStringValue: HasQuotes { | |||
168 | fn value(&self) -> Option<std::string::String>; | 138 | fn value(&self) -> Option<std::string::String>; |
169 | } | 139 | } |
170 | 140 | ||
171 | pub struct String(SyntaxToken); | ||
172 | |||
173 | impl AstToken for String { | ||
174 | fn cast(token: SyntaxToken) -> Option<Self> { | ||
175 | match token.kind() { | ||
176 | STRING => Some(String(token)), | ||
177 | _ => None, | ||
178 | } | ||
179 | } | ||
180 | fn syntax(&self) -> &SyntaxToken { | ||
181 | &self.0 | ||
182 | } | ||
183 | } | ||
184 | |||
185 | impl HasStringValue for String { | 141 | impl HasStringValue for String { |
186 | fn value(&self) -> Option<std::string::String> { | 142 | fn value(&self) -> Option<std::string::String> { |
187 | let text = self.text().as_str(); | 143 | let text = self.text().as_str(); |
@@ -201,20 +157,6 @@ impl HasStringValue for String { | |||
201 | } | 157 | } |
202 | } | 158 | } |
203 | 159 | ||
204 | pub struct RawString(SyntaxToken); | ||
205 | |||
206 | impl AstToken for RawString { | ||
207 | fn cast(token: SyntaxToken) -> Option<Self> { | ||
208 | match token.kind() { | ||
209 | RAW_STRING => Some(RawString(token)), | ||
210 | _ => None, | ||
211 | } | ||
212 | } | ||
213 | fn syntax(&self) -> &SyntaxToken { | ||
214 | &self.0 | ||
215 | } | ||
216 | } | ||
217 | |||
218 | impl HasStringValue for RawString { | 160 | impl HasStringValue for RawString { |
219 | fn value(&self) -> Option<std::string::String> { | 161 | fn value(&self) -> Option<std::string::String> { |
220 | let text = self.text().as_str(); | 162 | let text = self.text().as_str(); |