aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-28 21:59:57 +0100
committerAleksey Kladov <[email protected]>2018-08-28 21:59:57 +0100
commit15f15d92eb4d6ab791047eefbd6dd9b2baba1140 (patch)
tree3f8fde6b53446c9ecfeac4f6bd2f34da5a91aa33 /crates/libsyntax2/src
parentba02a55330d2ec9a0ea2c5cd457b82782ae299e9 (diff)
add impl works with lifetimes
Diffstat (limited to 'crates/libsyntax2/src')
-rw-r--r--crates/libsyntax2/src/ast/generated.rs86
-rw-r--r--crates/libsyntax2/src/ast/mod.rs6
-rw-r--r--crates/libsyntax2/src/grammar.ron28
-rw-r--r--crates/libsyntax2/src/grammar/expressions/atom.rs7
4 files changed, 119 insertions, 8 deletions
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index e8743caa8..d72e2091a 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -4,6 +4,28 @@ use {
4 SyntaxKind::*, 4 SyntaxKind::*,
5}; 5};
6 6
7// ArgList
8#[derive(Debug, Clone, Copy)]
9pub struct ArgList<'a> {
10 syntax: SyntaxNodeRef<'a>,
11}
12
13impl<'a> AstNode<'a> for ArgList<'a> {
14 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
15 match syntax.kind() {
16 ARG_LIST => Some(ArgList { syntax }),
17 _ => None,
18 }
19 }
20 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
21}
22
23impl<'a> ArgList<'a> {
24 pub fn args(self) -> impl Iterator<Item = Expr<'a>> + 'a {
25 super::children(self)
26 }
27}
28
7// ArrayExpr 29// ArrayExpr
8#[derive(Debug, Clone, Copy)] 30#[derive(Debug, Clone, Copy)]
9pub struct ArrayExpr<'a> { 31pub struct ArrayExpr<'a> {
@@ -181,7 +203,15 @@ impl<'a> AstNode<'a> for CallExpr<'a> {
181 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 203 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
182} 204}
183 205
184impl<'a> CallExpr<'a> {} 206impl<'a> CallExpr<'a> {
207 pub fn expr(self) -> Option<Expr<'a>> {
208 super::child_opt(self)
209 }
210
211 pub fn arg_list(self) -> Option<ArgList<'a>> {
212 super::child_opt(self)
213 }
214}
185 215
186// CastExpr 216// CastExpr
187#[derive(Debug, Clone, Copy)] 217#[derive(Debug, Clone, Copy)]
@@ -705,7 +735,15 @@ impl<'a> AstNode<'a> for LambdaExpr<'a> {
705 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } 735 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
706} 736}
707 737
708impl<'a> LambdaExpr<'a> {} 738impl<'a> LambdaExpr<'a> {
739 pub fn param_list(self) -> Option<ParamList<'a>> {
740 super::child_opt(self)
741 }
742
743 pub fn body(self) -> Option<Expr<'a>> {
744 super::child_opt(self)
745 }
746}
709 747
710// LetStmt 748// LetStmt
711#[derive(Debug, Clone, Copy)] 749#[derive(Debug, Clone, Copy)]
@@ -733,6 +771,46 @@ impl<'a> LetStmt<'a> {
733 } 771 }
734} 772}
735 773
774// Lifetime
775#[derive(Debug, Clone, Copy)]
776pub struct Lifetime<'a> {
777 syntax: SyntaxNodeRef<'a>,
778}
779
780impl<'a> AstNode<'a> for Lifetime<'a> {
781 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
782 match syntax.kind() {
783 LIFETIME => Some(Lifetime { syntax }),
784 _ => None,
785 }
786 }
787 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
788}
789
790impl<'a> Lifetime<'a> {}
791
792// LifetimeParam
793#[derive(Debug, Clone, Copy)]
794pub struct LifetimeParam<'a> {
795 syntax: SyntaxNodeRef<'a>,
796}
797
798impl<'a> AstNode<'a> for LifetimeParam<'a> {
799 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
800 match syntax.kind() {
801 LIFETIME_PARAM => Some(LifetimeParam { syntax }),
802 _ => None,
803 }
804 }
805 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
806}
807
808impl<'a> LifetimeParam<'a> {
809 pub fn lifetime(self) -> Option<Lifetime<'a>> {
810 super::child_opt(self)
811 }
812}
813
736// Literal 814// Literal
737#[derive(Debug, Clone, Copy)] 815#[derive(Debug, Clone, Copy)]
738pub struct Literal<'a> { 816pub struct Literal<'a> {
@@ -1813,6 +1891,10 @@ impl<'a> TypeParamList<'a> {
1813 pub fn type_params(self) -> impl Iterator<Item = TypeParam<'a>> + 'a { 1891 pub fn type_params(self) -> impl Iterator<Item = TypeParam<'a>> + 'a {
1814 super::children(self) 1892 super::children(self)
1815 } 1893 }
1894
1895 pub fn lifetime_params(self) -> impl Iterator<Item = LifetimeParam<'a>> + 'a {
1896 super::children(self)
1897 }
1816} 1898}
1817 1899
1818// TypeRef 1900// TypeRef
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index 9941138a7..3b5e9269f 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -67,6 +67,12 @@ impl<'a> Attr<'a> {
67 } 67 }
68} 68}
69 69
70impl<'a> Lifetime<'a> {
71 pub fn text(&self) -> SmolStr {
72 self.syntax().leaf_text().unwrap()
73 }
74}
75
70impl<'a> Name<'a> { 76impl<'a> Name<'a> {
71 pub fn text(&self) -> SmolStr { 77 pub fn text(&self) -> SmolStr {
72 let ident = self.syntax().first_child() 78 let ident = self.syntax().first_child()
diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron
index f1907d1ce..3c293d3e4 100644
--- a/crates/libsyntax2/src/grammar.ron
+++ b/crates/libsyntax2/src/grammar.ron
@@ -344,7 +344,12 @@ Grammar(
344 "ArrayExpr": (), 344 "ArrayExpr": (),
345 "ParenExpr": (), 345 "ParenExpr": (),
346 "PathExpr": (), 346 "PathExpr": (),
347 "LambdaExpr": (), 347 "LambdaExpr": (
348 options: [
349 ["param_list", "ParamList"],
350 ["body", "Expr"],
351 ]
352 ),
348 "IfExpr": ( 353 "IfExpr": (
349 options: [ ["condition", "Condition"] ] 354 options: [ ["condition", "Condition"] ]
350 ), 355 ),
@@ -378,7 +383,12 @@ Grammar(
378 "StructLit": (), 383 "StructLit": (),
379 "NamedFieldList": (), 384 "NamedFieldList": (),
380 "NamedField": (), 385 "NamedField": (),
381 "CallExpr": (), 386 "CallExpr": (
387 options: [
388 [ "expr", "Expr" ],
389 [ "arg_list", "ArgList" ],
390 ]
391 ),
382 "IndexExpr": (), 392 "IndexExpr": (),
383 "MethodCallExpr": (), 393 "MethodCallExpr": (),
384 "FieldExpr": (), 394 "FieldExpr": (),
@@ -457,8 +467,15 @@ Grammar(
457 "NameRef": (), 467 "NameRef": (),
458 "Attr": ( options: [ ["value", "TokenTree"] ] ), 468 "Attr": ( options: [ ["value", "TokenTree"] ] ),
459 "TokenTree": (), 469 "TokenTree": (),
460 "TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]), 470 "TypeParamList": (
471 collections: [
472 ["type_params", "TypeParam" ],
473 ["lifetime_params", "LifetimeParam" ],
474 ]
475 ),
461 "TypeParam": ( traits: ["NameOwner"] ), 476 "TypeParam": ( traits: ["NameOwner"] ),
477 "LifetimeParam": ( options: [ ["lifetime", "Lifetime"] ] ),
478 "Lifetime": (),
462 "WhereClause": (), 479 "WhereClause": (),
463 "ExprStmt": ( 480 "ExprStmt": (
464 options: [ ["expr", "Expr"] ] 481 options: [ ["expr", "Expr"] ]
@@ -492,5 +509,10 @@ Grammar(
492 ), 509 ),
493 "UseItem": (), 510 "UseItem": (),
494 "ExternCrateItem": (), 511 "ExternCrateItem": (),
512 "ArgList": (
513 collections: [
514 ["args", "Expr"]
515 ]
516 )
495 }, 517 },
496) 518)
diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs
index 0769bb5a8..a17c27b31 100644
--- a/crates/libsyntax2/src/grammar/expressions/atom.rs
+++ b/crates/libsyntax2/src/grammar/expressions/atom.rs
@@ -151,10 +151,11 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
151 p.eat(MOVE_KW); 151 p.eat(MOVE_KW);
152 params::param_list_opt_types(p); 152 params::param_list_opt_types(p);
153 if opt_fn_ret_type(p) { 153 if opt_fn_ret_type(p) {
154 block(p); 154 if !p.at(L_CURLY) {
155 } else { 155 p.error("expected `{`");
156 expr(p); 156 }
157 } 157 }
158 expr(p);
158 m.complete(p, LAMBDA_EXPR) 159 m.complete(p, LAMBDA_EXPR)
159} 160}
160 161