aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-08 09:23:10 +0000
committerAleksey Kladov <[email protected]>2019-01-08 09:23:10 +0000
commitfa6e0b0d38d2a030b959be91232927b9c096272b (patch)
tree81b546c105388e1fc3154c90a42a8cc5fb930d0b /crates/ra_syntax
parent3f4be819125ce4a22edd86721fa56b5caba99c2e (diff)
itroduce trait for ast tokens
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast.rs52
-rw-r--r--crates/ra_syntax/src/ast/generated.rs7
-rw-r--r--crates/ra_syntax/src/grammar.ron14
-rw-r--r--crates/ra_syntax/src/validation/byte.rs2
-rw-r--r--crates/ra_syntax/src/validation/byte_string.rs2
-rw-r--r--crates/ra_syntax/src/validation/char.rs2
-rw-r--r--crates/ra_syntax/src/validation/string.rs2
7 files changed, 28 insertions, 53 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 0e303ee98..96879ae5a 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -23,6 +23,12 @@ pub trait AstNode: rowan::TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>
23 fn to_owned(&self) -> TreePtr<Self>; 23 fn to_owned(&self) -> TreePtr<Self>;
24} 24}
25 25
26pub trait AstToken: AstNode {
27 fn text(&self) -> &SmolStr {
28 self.syntax().leaf_text().unwrap()
29 }
30}
31
26pub trait NameOwner: AstNode { 32pub trait NameOwner: AstNode {
27 fn name(&self) -> Option<&Name> { 33 fn name(&self) -> Option<&Name> {
28 child_opt(self) 34 child_opt(self)
@@ -155,41 +161,7 @@ impl Attr {
155 } 161 }
156} 162}
157 163
158impl Lifetime {
159 pub fn text(&self) -> SmolStr {
160 self.syntax().leaf_text().unwrap().clone()
161 }
162}
163
164impl Char {
165 pub fn text(&self) -> &SmolStr {
166 &self.syntax().leaf_text().unwrap()
167 }
168}
169
170impl Byte {
171 pub fn text(&self) -> &SmolStr {
172 &self.syntax().leaf_text().unwrap()
173 }
174}
175
176impl ByteString {
177 pub fn text(&self) -> &SmolStr {
178 &self.syntax().leaf_text().unwrap()
179 }
180}
181
182impl String {
183 pub fn text(&self) -> &SmolStr {
184 &self.syntax().leaf_text().unwrap()
185 }
186}
187
188impl Comment { 164impl Comment {
189 pub fn text(&self) -> &SmolStr {
190 self.syntax().leaf_text().unwrap()
191 }
192
193 pub fn flavor(&self) -> CommentFlavor { 165 pub fn flavor(&self) -> CommentFlavor {
194 let text = self.text(); 166 let text = self.text();
195 if text.starts_with("///") { 167 if text.starts_with("///") {
@@ -248,10 +220,6 @@ impl CommentFlavor {
248} 220}
249 221
250impl Whitespace { 222impl Whitespace {
251 pub fn text(&self) -> &SmolStr {
252 &self.syntax().leaf_text().unwrap()
253 }
254
255 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> { 223 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
256 self.text().chars().filter(|&c| c == '\n').map(|_| &()) 224 self.text().chars().filter(|&c| c == '\n').map(|_| &())
257 } 225 }
@@ -262,16 +230,16 @@ impl Whitespace {
262} 230}
263 231
264impl Name { 232impl Name {
265 pub fn text(&self) -> SmolStr { 233 pub fn text(&self) -> &SmolStr {
266 let ident = self.syntax().first_child().unwrap(); 234 let ident = self.syntax().first_child().unwrap();
267 ident.leaf_text().unwrap().clone() 235 ident.leaf_text().unwrap()
268 } 236 }
269} 237}
270 238
271impl NameRef { 239impl NameRef {
272 pub fn text(&self) -> SmolStr { 240 pub fn text(&self) -> &SmolStr {
273 let ident = self.syntax().first_child().unwrap(); 241 let ident = self.syntax().first_child().unwrap();
274 ident.leaf_text().unwrap().clone() 242 ident.leaf_text().unwrap()
275 } 243 }
276} 244}
277 245
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 5e96ab142..547e3c003 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -288,6 +288,7 @@ impl AstNode for Byte {
288} 288}
289 289
290 290
291impl ast::AstToken for Byte {}
291impl Byte {} 292impl Byte {}
292 293
293// ByteString 294// ByteString
@@ -312,6 +313,7 @@ impl AstNode for ByteString {
312} 313}
313 314
314 315
316impl ast::AstToken for ByteString {}
315impl ByteString {} 317impl ByteString {}
316 318
317// CallExpr 319// CallExpr
@@ -397,6 +399,7 @@ impl AstNode for Char {
397} 399}
398 400
399 401
402impl ast::AstToken for Char {}
400impl Char {} 403impl Char {}
401 404
402// Comment 405// Comment
@@ -421,6 +424,7 @@ impl AstNode for Comment {
421} 424}
422 425
423 426
427impl ast::AstToken for Comment {}
424impl Comment {} 428impl Comment {}
425 429
426// Condition 430// Condition
@@ -1270,6 +1274,7 @@ impl AstNode for Lifetime {
1270} 1274}
1271 1275
1272 1276
1277impl ast::AstToken for Lifetime {}
1273impl Lifetime {} 1278impl Lifetime {}
1274 1279
1275// LifetimeParam 1280// LifetimeParam
@@ -2766,6 +2771,7 @@ impl AstNode for String {
2766} 2771}
2767 2772
2768 2773
2774impl ast::AstToken for String {}
2769impl String {} 2775impl String {}
2770 2776
2771// StructDef 2777// StructDef
@@ -3391,5 +3397,6 @@ impl AstNode for Whitespace {
3391} 3397}
3392 3398
3393 3399
3400impl ast::AstToken for Whitespace {}
3394impl Whitespace {} 3401impl Whitespace {}
3395 3402
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index d7505ea06..bddd96a5c 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -424,10 +424,10 @@ Grammar(
424 "PrefixExpr": (options: ["Expr"]), 424 "PrefixExpr": (options: ["Expr"]),
425 "RangeExpr": (), 425 "RangeExpr": (),
426 "BinExpr": (), 426 "BinExpr": (),
427 "String": (), 427 "String": ( traits: ["AstToken"] ),
428 "Byte": (), 428 "Byte": ( traits: ["AstToken"] ),
429 "ByteString": (), 429 "ByteString": ( traits: ["AstToken"] ),
430 "Char": (), 430 "Char": ( traits: ["AstToken"] ),
431 "Literal": (), 431 "Literal": (),
432 432
433 "Expr": ( 433 "Expr": (
@@ -505,7 +505,7 @@ Grammar(
505 ), 505 ),
506 "TypeParam": ( traits: ["NameOwner"] ), 506 "TypeParam": ( traits: ["NameOwner"] ),
507 "LifetimeParam": ( options: [ "Lifetime" ] ), 507 "LifetimeParam": ( options: [ "Lifetime" ] ),
508 "Lifetime": (), 508 "Lifetime": ( traits: ["AstToken"] ),
509 "WhereClause": (), 509 "WhereClause": (),
510 "ExprStmt": ( 510 "ExprStmt": (
511 options: [ ["expr", "Expr"] ] 511 options: [ ["expr", "Expr"] ]
@@ -562,7 +562,7 @@ Grammar(
562 "PathSegment": ( 562 "PathSegment": (
563 options: [ "NameRef" ] 563 options: [ "NameRef" ]
564 ), 564 ),
565 "Comment": (), 565 "Comment": ( traits: ["AstToken"] ),
566 "Whitespace": (), 566 "Whitespace": ( traits: ["AstToken"] ),
567 }, 567 },
568) 568)
diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs
index 4deb302a7..9ab4b18a3 100644
--- a/crates/ra_syntax/src/validation/byte.rs
+++ b/crates/ra_syntax/src/validation/byte.rs
@@ -1,7 +1,7 @@
1//! Validation of byte literals 1//! Validation of byte literals
2 2
3use crate::{ 3use crate::{
4 ast::{self, AstNode}, 4 ast::{self, AstNode, AstToken},
5 string_lexing::{self, StringComponentKind}, 5 string_lexing::{self, StringComponentKind},
6 TextRange, 6 TextRange,
7 validation::char, 7 validation::char,
diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs
index 670c43a09..cd41a0a68 100644
--- a/crates/ra_syntax/src/validation/byte_string.rs
+++ b/crates/ra_syntax/src/validation/byte_string.rs
@@ -1,5 +1,5 @@
1use crate::{ 1use crate::{
2 ast::{self, AstNode}, 2 ast::{self, AstNode, AstToken},
3 string_lexing::{self, StringComponentKind}, 3 string_lexing::{self, StringComponentKind},
4 yellow::{ 4 yellow::{
5 SyntaxError, 5 SyntaxError,
diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs
index 9cbd43fba..169c88f56 100644
--- a/crates/ra_syntax/src/validation/char.rs
+++ b/crates/ra_syntax/src/validation/char.rs
@@ -5,7 +5,7 @@ use std::u32;
5use arrayvec::ArrayString; 5use arrayvec::ArrayString;
6 6
7use crate::{ 7use crate::{
8 ast::{self, AstNode}, 8 ast::{self, AstNode, AstToken},
9 string_lexing::{self, StringComponentKind}, 9 string_lexing::{self, StringComponentKind},
10 TextRange, 10 TextRange,
11 yellow::{ 11 yellow::{
diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs
index 7b2a68d12..cb86b765f 100644
--- a/crates/ra_syntax/src/validation/string.rs
+++ b/crates/ra_syntax/src/validation/string.rs
@@ -1,5 +1,5 @@
1use crate::{ 1use crate::{
2 ast::{self, AstNode}, 2 ast::{self, AstNode, AstToken},
3 string_lexing, 3 string_lexing,
4 yellow::{ 4 yellow::{
5 SyntaxError, 5 SyntaxError,