diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-31 10:11:48 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-31 10:11:48 +0100 |
commit | c5ca49678f129045e59438df279829902034ec71 (patch) | |
tree | 3fd54185e0c064fbd6f718ac345064ce9c559efb /crates/ra_syntax/src | |
parent | 23dd53eb35ff50508d0c0fc5878a55754b12d381 (diff) | |
parent | 55dcdb7d094f473c73f87ecf997b24f8e35f2a5e (diff) |
Merge #1077
1077: Improve parsing of type bounds r=matklad a=vipentti
This adds new TYPE_BOUND_LIST and TYPE_BOUND syntax kinds. These are now used when parsing type bounds. In addition parsing paths inside a bound now does not recursively parse paths, rather they are treated as separate bounds, separated by +.
Basically now the generic params `struct S<T: 'a + ?Sized + (Copy)>;` in will be parsed as
```
TYPE_PARAM_LIST@[8; 33)
L_ANGLE@[8; 9)
TYPE_PARAM@[9; 32)
NAME@[9; 10)
IDENT@[9; 10) "T"
COLON@[10; 11)
WHITESPACE@[11; 12)
TYPE_BOUND_LIST@[12; 32)
TYPE_BOUND@[12; 14)
LIFETIME@[12; 14) "'a"
WHITESPACE@[14; 15)
PLUS@[15; 16)
WHITESPACE@[16; 17)
TYPE_BOUND@[17; 23)
QUESTION@[17; 18)
PATH_TYPE@[18; 23)
PATH@[18; 23)
PATH_SEGMENT@[18; 23)
NAME_REF@[18; 23)
IDENT@[18; 23) "Sized"
WHITESPACE@[23; 24)
PLUS@[24; 25)
WHITESPACE@[25; 26)
TYPE_BOUND@[26; 32)
L_PAREN@[26; 27)
PATH_TYPE@[27; 31)
PATH@[27; 31)
PATH_SEGMENT@[27; 31)
NAME_REF@[27; 31)
IDENT@[27; 31) "Copy"
R_PAREN@[31; 32)
R_ANGLE@[32; 33)
```
Previously it was parsed, with the paths nested:
```
TYPE_PARAM_LIST@[8; 33)
L_ANGLE@[8; 9)
TYPE_PARAM@[9; 32)
NAME@[9; 10)
IDENT@[9; 10) "T"
COLON@[10; 11)
WHITESPACE@[11; 12)
LIFETIME@[12; 14) "'a"
WHITESPACE@[14; 15)
PLUS@[15; 16)
WHITESPACE@[16; 17)
QUESTION@[17; 18)
PATH_TYPE@[18; 32)
PATH@[18; 23)
PATH_SEGMENT@[18; 23)
NAME_REF@[18; 23)
IDENT@[18; 23) "Sized"
WHITESPACE@[23; 24)
PLUS@[24; 25)
WHITESPACE@[25; 26)
L_PAREN@[26; 27)
PATH_TYPE@[27; 31)
PATH@[27; 31)
PATH_SEGMENT@[27; 31)
NAME_REF@[27; 31)
IDENT@[27; 31) "Copy"
R_PAREN@[31; 32)
R_ANGLE@[32; 33)
```
Looking for feedback.
Co-authored-by: Ville Penttinen <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 6 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 73 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 28 |
3 files changed, 102 insertions, 5 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index a6fac07c4..4fddc00ea 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -110,6 +110,12 @@ pub trait TypeParamsOwner: AstNode { | |||
110 | } | 110 | } |
111 | } | 111 | } |
112 | 112 | ||
113 | pub trait TypeBoundsOwner: AstNode { | ||
114 | fn type_bound_list(&self) -> Option<&TypeBoundList> { | ||
115 | child_opt(self) | ||
116 | } | ||
117 | } | ||
118 | |||
113 | pub trait AttrsOwner: AstNode { | 119 | pub trait AttrsOwner: AstNode { |
114 | fn attrs(&self) -> AstChildren<Attr> { | 120 | fn attrs(&self) -> AstChildren<Attr> { |
115 | children(self) | 121 | children(self) |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 47a37e4d1..9ea423b40 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -685,6 +685,7 @@ impl ToOwned for DynTraitType { | |||
685 | } | 685 | } |
686 | 686 | ||
687 | 687 | ||
688 | impl ast::TypeBoundsOwner for DynTraitType {} | ||
688 | impl DynTraitType {} | 689 | impl DynTraitType {} |
689 | 690 | ||
690 | // EnumDef | 691 | // EnumDef |
@@ -1581,6 +1582,7 @@ impl ToOwned for ImplTraitType { | |||
1581 | } | 1582 | } |
1582 | 1583 | ||
1583 | 1584 | ||
1585 | impl ast::TypeBoundsOwner for ImplTraitType {} | ||
1584 | impl ImplTraitType {} | 1586 | impl ImplTraitType {} |
1585 | 1587 | ||
1586 | // IndexExpr | 1588 | // IndexExpr |
@@ -4061,6 +4063,7 @@ impl ast::NameOwner for TraitDef {} | |||
4061 | impl ast::AttrsOwner for TraitDef {} | 4063 | impl ast::AttrsOwner for TraitDef {} |
4062 | impl ast::DocCommentsOwner for TraitDef {} | 4064 | impl ast::DocCommentsOwner for TraitDef {} |
4063 | impl ast::TypeParamsOwner for TraitDef {} | 4065 | impl ast::TypeParamsOwner for TraitDef {} |
4066 | impl ast::TypeBoundsOwner for TraitDef {} | ||
4064 | impl TraitDef { | 4067 | impl TraitDef { |
4065 | pub fn item_list(&self) -> Option<&ItemList> { | 4068 | pub fn item_list(&self) -> Option<&ItemList> { |
4066 | super::child_opt(self) | 4069 | super::child_opt(self) |
@@ -4291,6 +4294,7 @@ impl ast::NameOwner for TypeAliasDef {} | |||
4291 | impl ast::TypeParamsOwner for TypeAliasDef {} | 4294 | impl ast::TypeParamsOwner for TypeAliasDef {} |
4292 | impl ast::AttrsOwner for TypeAliasDef {} | 4295 | impl ast::AttrsOwner for TypeAliasDef {} |
4293 | impl ast::DocCommentsOwner for TypeAliasDef {} | 4296 | impl ast::DocCommentsOwner for TypeAliasDef {} |
4297 | impl ast::TypeBoundsOwner for TypeAliasDef {} | ||
4294 | impl TypeAliasDef { | 4298 | impl TypeAliasDef { |
4295 | pub fn type_ref(&self) -> Option<&TypeRef> { | 4299 | pub fn type_ref(&self) -> Option<&TypeRef> { |
4296 | super::child_opt(self) | 4300 | super::child_opt(self) |
@@ -4369,6 +4373,74 @@ impl TypeArgList { | |||
4369 | } | 4373 | } |
4370 | } | 4374 | } |
4371 | 4375 | ||
4376 | // TypeBound | ||
4377 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
4378 | #[repr(transparent)] | ||
4379 | pub struct TypeBound { | ||
4380 | pub(crate) syntax: SyntaxNode, | ||
4381 | } | ||
4382 | unsafe impl TransparentNewType for TypeBound { | ||
4383 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
4384 | } | ||
4385 | |||
4386 | impl AstNode for TypeBound { | ||
4387 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
4388 | match syntax.kind() { | ||
4389 | TYPE_BOUND => Some(TypeBound::from_repr(syntax.into_repr())), | ||
4390 | _ => None, | ||
4391 | } | ||
4392 | } | ||
4393 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
4394 | } | ||
4395 | |||
4396 | impl ToOwned for TypeBound { | ||
4397 | type Owned = TreeArc<TypeBound>; | ||
4398 | fn to_owned(&self) -> TreeArc<TypeBound> { TreeArc::cast(self.syntax.to_owned()) } | ||
4399 | } | ||
4400 | |||
4401 | |||
4402 | impl TypeBound { | ||
4403 | pub fn type_ref(&self) -> Option<&TypeRef> { | ||
4404 | super::child_opt(self) | ||
4405 | } | ||
4406 | |||
4407 | pub fn lifetime(&self) -> Option<&Lifetime> { | ||
4408 | super::child_opt(self) | ||
4409 | } | ||
4410 | } | ||
4411 | |||
4412 | // TypeBoundList | ||
4413 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
4414 | #[repr(transparent)] | ||
4415 | pub struct TypeBoundList { | ||
4416 | pub(crate) syntax: SyntaxNode, | ||
4417 | } | ||
4418 | unsafe impl TransparentNewType for TypeBoundList { | ||
4419 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
4420 | } | ||
4421 | |||
4422 | impl AstNode for TypeBoundList { | ||
4423 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
4424 | match syntax.kind() { | ||
4425 | TYPE_BOUND_LIST => Some(TypeBoundList::from_repr(syntax.into_repr())), | ||
4426 | _ => None, | ||
4427 | } | ||
4428 | } | ||
4429 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
4430 | } | ||
4431 | |||
4432 | impl ToOwned for TypeBoundList { | ||
4433 | type Owned = TreeArc<TypeBoundList>; | ||
4434 | fn to_owned(&self) -> TreeArc<TypeBoundList> { TreeArc::cast(self.syntax.to_owned()) } | ||
4435 | } | ||
4436 | |||
4437 | |||
4438 | impl TypeBoundList { | ||
4439 | pub fn bounds(&self) -> impl Iterator<Item = &TypeBound> { | ||
4440 | super::children(self) | ||
4441 | } | ||
4442 | } | ||
4443 | |||
4372 | // TypeParam | 4444 | // TypeParam |
4373 | #[derive(Debug, PartialEq, Eq, Hash)] | 4445 | #[derive(Debug, PartialEq, Eq, Hash)] |
4374 | #[repr(transparent)] | 4446 | #[repr(transparent)] |
@@ -4397,6 +4469,7 @@ impl ToOwned for TypeParam { | |||
4397 | 4469 | ||
4398 | impl ast::NameOwner for TypeParam {} | 4470 | impl ast::NameOwner for TypeParam {} |
4399 | impl ast::AttrsOwner for TypeParam {} | 4471 | impl ast::AttrsOwner for TypeParam {} |
4472 | impl ast::TypeBoundsOwner for TypeParam {} | ||
4400 | impl TypeParam {} | 4473 | impl TypeParam {} |
4401 | 4474 | ||
4402 | // TypeParamList | 4475 | // TypeParamList |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index ad6d74162..18730a894 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -243,6 +243,8 @@ Grammar( | |||
243 | "PARAM", | 243 | "PARAM", |
244 | "SELF_PARAM", | 244 | "SELF_PARAM", |
245 | "ARG_LIST", | 245 | "ARG_LIST", |
246 | "TYPE_BOUND", | ||
247 | "TYPE_BOUND_LIST", | ||
246 | ], | 248 | ], |
247 | ast: { | 249 | ast: { |
248 | "SourceFile": ( | 250 | "SourceFile": ( |
@@ -293,7 +295,7 @@ Grammar( | |||
293 | "EnumVariantList": ( collections: [["variants", "EnumVariant"]] ), | 295 | "EnumVariantList": ( collections: [["variants", "EnumVariant"]] ), |
294 | "EnumVariant": ( traits: ["NameOwner", "DocCommentsOwner", "AttrsOwner"], options: ["Expr"] ), | 296 | "EnumVariant": ( traits: ["NameOwner", "DocCommentsOwner", "AttrsOwner"], options: ["Expr"] ), |
295 | "TraitDef": ( | 297 | "TraitDef": ( |
296 | traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeParamsOwner"], | 298 | traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeParamsOwner", "TypeBoundsOwner"], |
297 | options: ["ItemList"] | 299 | options: ["ItemList"] |
298 | ), | 300 | ), |
299 | "Module": ( | 301 | "Module": ( |
@@ -330,7 +332,8 @@ Grammar( | |||
330 | "NameOwner", | 332 | "NameOwner", |
331 | "TypeParamsOwner", | 333 | "TypeParamsOwner", |
332 | "AttrsOwner", | 334 | "AttrsOwner", |
333 | "DocCommentsOwner" | 335 | "DocCommentsOwner", |
336 | "TypeBoundsOwner", | ||
334 | ], | 337 | ], |
335 | options: ["TypeRef"] | 338 | options: ["TypeRef"] |
336 | ), | 339 | ), |
@@ -347,8 +350,12 @@ Grammar( | |||
347 | "PlaceholderType": (), | 350 | "PlaceholderType": (), |
348 | "FnPointerType": (options: ["ParamList", "RetType"]), | 351 | "FnPointerType": (options: ["ParamList", "RetType"]), |
349 | "ForType": (options: ["TypeRef"]), | 352 | "ForType": (options: ["TypeRef"]), |
350 | "ImplTraitType": (), | 353 | "ImplTraitType": ( |
351 | "DynTraitType": (), | 354 | traits: ["TypeBoundsOwner"], |
355 | ), | ||
356 | "DynTraitType": ( | ||
357 | traits: ["TypeBoundsOwner"], | ||
358 | ), | ||
352 | 359 | ||
353 | "TypeRef": ( enum: [ | 360 | "TypeRef": ( enum: [ |
354 | "ParenType", | 361 | "ParenType", |
@@ -571,12 +578,23 @@ Grammar( | |||
571 | ["lifetime_params", "LifetimeParam" ], | 578 | ["lifetime_params", "LifetimeParam" ], |
572 | ] | 579 | ] |
573 | ), | 580 | ), |
574 | "TypeParam": ( traits: ["NameOwner", "AttrsOwner"] ), | 581 | "TypeParam": ( traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"] ), |
575 | "LifetimeParam": ( | 582 | "LifetimeParam": ( |
576 | options: [ "Lifetime"], | 583 | options: [ "Lifetime"], |
577 | traits: ["AttrsOwner"], | 584 | traits: ["AttrsOwner"], |
578 | ), | 585 | ), |
579 | "Lifetime": ( traits: ["AstToken"] ), | 586 | "Lifetime": ( traits: ["AstToken"] ), |
587 | "TypeBound": ( | ||
588 | options: [ | ||
589 | "TypeRef", | ||
590 | "Lifetime", | ||
591 | ] | ||
592 | ), | ||
593 | "TypeBoundList": ( | ||
594 | collections: [ | ||
595 | ["bounds", "TypeBound"], | ||
596 | ] | ||
597 | ), | ||
580 | "WhereClause": (), | 598 | "WhereClause": (), |
581 | "ExprStmt": ( | 599 | "ExprStmt": ( |
582 | options: [ ["expr", "Expr"] ] | 600 | options: [ ["expr", "Expr"] ] |