diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
commit | 7c67612b8a894187fa3b64725531a5459f9211bf (patch) | |
tree | 9e2a536efa0c880d921fd8d4d74423afc9451fd4 /crates/libsyntax2/src/grammar/type_params.rs | |
parent | 26262aaf05983c5b7f41cc438e287523268fe1eb (diff) |
organizize
Diffstat (limited to 'crates/libsyntax2/src/grammar/type_params.rs')
-rw-r--r-- | crates/libsyntax2/src/grammar/type_params.rs | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/crates/libsyntax2/src/grammar/type_params.rs b/crates/libsyntax2/src/grammar/type_params.rs new file mode 100644 index 000000000..0a3e8fd07 --- /dev/null +++ b/crates/libsyntax2/src/grammar/type_params.rs | |||
@@ -0,0 +1,127 @@ | |||
1 | use super::*; | ||
2 | |||
3 | pub(super) fn type_param_list(p: &mut Parser) { | ||
4 | if !p.at(L_ANGLE) { | ||
5 | return; | ||
6 | } | ||
7 | let m = p.start(); | ||
8 | p.bump(); | ||
9 | |||
10 | while !p.at(EOF) && !p.at(R_ANGLE) { | ||
11 | match p.current() { | ||
12 | LIFETIME => lifetime_param(p), | ||
13 | IDENT => type_param(p), | ||
14 | _ => p.err_and_bump("expected type parameter"), | ||
15 | } | ||
16 | if !p.at(R_ANGLE) && !p.expect(COMMA) { | ||
17 | break; | ||
18 | } | ||
19 | } | ||
20 | p.expect(R_ANGLE); | ||
21 | m.complete(p, TYPE_PARAM_LIST); | ||
22 | |||
23 | fn lifetime_param(p: &mut Parser) { | ||
24 | assert!(p.at(LIFETIME)); | ||
25 | let m = p.start(); | ||
26 | p.bump(); | ||
27 | if p.at(COLON) { | ||
28 | lifetime_bounds(p); | ||
29 | } | ||
30 | m.complete(p, LIFETIME_PARAM); | ||
31 | } | ||
32 | |||
33 | fn type_param(p: &mut Parser) { | ||
34 | assert!(p.at(IDENT)); | ||
35 | let m = p.start(); | ||
36 | name(p); | ||
37 | if p.at(COLON) { | ||
38 | bounds(p); | ||
39 | } | ||
40 | // test type_param_default | ||
41 | // struct S<T = i32>; | ||
42 | if p.at(EQ) { | ||
43 | p.bump(); | ||
44 | types::type_(p) | ||
45 | } | ||
46 | m.complete(p, TYPE_PARAM); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | // test type_param_bounds | ||
51 | // struct S<T: 'a + ?Sized + (Copy)>; | ||
52 | pub(super) fn bounds(p: &mut Parser) { | ||
53 | assert!(p.at(COLON)); | ||
54 | p.bump(); | ||
55 | bounds_without_colon(p); | ||
56 | } | ||
57 | |||
58 | fn lifetime_bounds(p: &mut Parser) { | ||
59 | assert!(p.at(COLON)); | ||
60 | p.bump(); | ||
61 | while p.at(LIFETIME) { | ||
62 | p.bump(); | ||
63 | if !p.eat(PLUS) { | ||
64 | break; | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | pub(super) fn bounds_without_colon(p: &mut Parser) { | ||
70 | loop { | ||
71 | let has_paren = p.eat(L_PAREN); | ||
72 | p.eat(QUESTION); | ||
73 | if p.at(FOR_KW) { | ||
74 | //TODO | ||
75 | } | ||
76 | if p.at(LIFETIME) { | ||
77 | p.bump(); | ||
78 | } else if paths::is_path_start(p) { | ||
79 | paths::type_path(p); | ||
80 | } else { | ||
81 | break; | ||
82 | } | ||
83 | if has_paren { | ||
84 | p.expect(R_PAREN); | ||
85 | } | ||
86 | if !p.eat(PLUS) { | ||
87 | break; | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | // test where_clause | ||
93 | // fn foo() | ||
94 | // where | ||
95 | // 'a: 'b + 'c, | ||
96 | // T: Clone + Copy + 'static, | ||
97 | // Iterator::Item: 'a, | ||
98 | // {} | ||
99 | pub(super) fn where_clause(p: &mut Parser) { | ||
100 | if !p.at(WHERE_KW) { | ||
101 | return; | ||
102 | } | ||
103 | let m = p.start(); | ||
104 | p.bump(); | ||
105 | loop { | ||
106 | if !(paths::is_path_start(p) || p.current() == LIFETIME) { | ||
107 | break | ||
108 | } | ||
109 | where_predicate(p); | ||
110 | if p.current() != L_CURLY && p.current() != SEMI { | ||
111 | p.expect(COMMA); | ||
112 | } | ||
113 | } | ||
114 | m.complete(p, WHERE_CLAUSE); | ||
115 | } | ||
116 | |||
117 | fn where_predicate(p: &mut Parser) { | ||
118 | let m = p.start(); | ||
119 | if p.at(LIFETIME) { | ||
120 | p.eat(LIFETIME); | ||
121 | lifetime_bounds(p) | ||
122 | } else { | ||
123 | types::path_type(p); | ||
124 | bounds(p); | ||
125 | } | ||
126 | m.complete(p, WHERE_PRED); | ||
127 | } | ||