diff options
author | Aleksey Kladov <[email protected]> | 2018-08-13 16:40:47 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-13 16:40:47 +0100 |
commit | 7d0c9cf54652c0630bf8f955fc3affb288b5e4cf (patch) | |
tree | 31938c2f1178ff242cf8f9a89e7d17b4a505018e | |
parent | c146331b1c92c3a6d58217b61f95e69155a3a4f8 (diff) |
Optional patterns in trait methods
-rw-r--r-- | crates/libsyntax2/src/grammar/expressions/atom.rs | 2 | ||||
-rw-r--r-- | crates/libsyntax2/src/grammar/items/mod.rs | 23 | ||||
-rw-r--r-- | crates/libsyntax2/src/grammar/items/traits.rs | 4 | ||||
-rw-r--r-- | crates/libsyntax2/tests/data/parser/ok/0030_traits.txt | 207 |
4 files changed, 92 insertions, 144 deletions
diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs index af9f47c5e..e62b16654 100644 --- a/crates/libsyntax2/src/grammar/expressions/atom.rs +++ b/crates/libsyntax2/src/grammar/expressions/atom.rs | |||
@@ -284,7 +284,7 @@ pub(super) fn block_expr(p: &mut Parser) -> CompletedMarker { | |||
284 | // test block_items | 284 | // test block_items |
285 | // fn a() { fn b() {} } | 285 | // fn a() { fn b() {} } |
286 | let m = p.start(); | 286 | let m = p.start(); |
287 | match items::maybe_item(p) { | 287 | match items::maybe_item(p, items::ItemFlavor::Mod) { |
288 | items::MaybeItem::Item(kind) => { | 288 | items::MaybeItem::Item(kind) => { |
289 | m.complete(p, kind); | 289 | m.complete(p, kind); |
290 | } | 290 | } |
diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 883b5a946..fc02f0c5c 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs | |||
@@ -14,13 +14,17 @@ mod use_item; | |||
14 | pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { | 14 | pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { |
15 | attributes::inner_attributes(p); | 15 | attributes::inner_attributes(p); |
16 | while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { | 16 | while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { |
17 | item_or_macro(p, stop_on_r_curly) | 17 | item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) |
18 | } | 18 | } |
19 | } | 19 | } |
20 | 20 | ||
21 | pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) { | 21 | pub(super) enum ItemFlavor { |
22 | Mod, Trait | ||
23 | } | ||
24 | |||
25 | pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { | ||
22 | let m = p.start(); | 26 | let m = p.start(); |
23 | match maybe_item(p) { | 27 | match maybe_item(p, flavor) { |
24 | MaybeItem::Item(kind) => { | 28 | MaybeItem::Item(kind) => { |
25 | m.complete(p, kind); | 29 | m.complete(p, kind); |
26 | } | 30 | } |
@@ -60,7 +64,7 @@ pub(super) enum MaybeItem { | |||
60 | Modifiers, | 64 | Modifiers, |
61 | } | 65 | } |
62 | 66 | ||
63 | pub(super) fn maybe_item(p: &mut Parser) -> MaybeItem { | 67 | pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { |
64 | attributes::outer_attributes(p); | 68 | attributes::outer_attributes(p); |
65 | visibility(p); | 69 | visibility(p); |
66 | if let Some(kind) = items_without_modifiers(p) { | 70 | if let Some(kind) = items_without_modifiers(p) { |
@@ -107,7 +111,7 @@ pub(super) fn maybe_item(p: &mut Parser) -> MaybeItem { | |||
107 | // test unsafe_fn | 111 | // test unsafe_fn |
108 | // unsafe fn foo() {} | 112 | // unsafe fn foo() {} |
109 | FN_KW => { | 113 | FN_KW => { |
110 | function(p); | 114 | function(p, flavor); |
111 | FN_DEF | 115 | FN_DEF |
112 | } | 116 | } |
113 | 117 | ||
@@ -217,7 +221,7 @@ fn extern_block(p: &mut Parser) { | |||
217 | p.expect(R_CURLY); | 221 | p.expect(R_CURLY); |
218 | } | 222 | } |
219 | 223 | ||
220 | fn function(p: &mut Parser) { | 224 | fn function(p: &mut Parser, flavor: ItemFlavor) { |
221 | assert!(p.at(FN_KW)); | 225 | assert!(p.at(FN_KW)); |
222 | p.bump(); | 226 | p.bump(); |
223 | 227 | ||
@@ -227,7 +231,12 @@ fn function(p: &mut Parser) { | |||
227 | type_params::type_param_list(p); | 231 | type_params::type_param_list(p); |
228 | 232 | ||
229 | if p.at(L_PAREN) { | 233 | if p.at(L_PAREN) { |
230 | params::param_list(p); | 234 | match flavor { |
235 | ItemFlavor::Mod => | ||
236 | params::param_list(p), | ||
237 | ItemFlavor::Trait => | ||
238 | params::param_list_opt_patterns(p), | ||
239 | } | ||
231 | } else { | 240 | } else { |
232 | p.error("expected function arguments"); | 241 | p.error("expected function arguments"); |
233 | } | 242 | } |
diff --git a/crates/libsyntax2/src/grammar/items/traits.rs b/crates/libsyntax2/src/grammar/items/traits.rs index 53a636f6c..daecaff5c 100644 --- a/crates/libsyntax2/src/grammar/items/traits.rs +++ b/crates/libsyntax2/src/grammar/items/traits.rs | |||
@@ -20,7 +20,7 @@ pub(super) fn trait_def(p: &mut Parser) { | |||
20 | // fn bar(&self); | 20 | // fn bar(&self); |
21 | // } | 21 | // } |
22 | while !p.at(EOF) && !p.at(R_CURLY) { | 22 | while !p.at(EOF) && !p.at(R_CURLY) { |
23 | item_or_macro(p, true); | 23 | item_or_macro(p, true, ItemFlavor::Trait); |
24 | } | 24 | } |
25 | p.expect(R_CURLY); | 25 | p.expect(R_CURLY); |
26 | } | 26 | } |
@@ -55,7 +55,7 @@ pub(super) fn impl_item(p: &mut Parser) { | |||
55 | // fn bar(&self) {} | 55 | // fn bar(&self) {} |
56 | // } | 56 | // } |
57 | while !p.at(EOF) && !p.at(R_CURLY) { | 57 | while !p.at(EOF) && !p.at(R_CURLY) { |
58 | item_or_macro(p, true); | 58 | item_or_macro(p, true, ItemFlavor::Mod); |
59 | } | 59 | } |
60 | p.expect(R_CURLY); | 60 | p.expect(R_CURLY); |
61 | } | 61 | } |
diff --git a/crates/libsyntax2/tests/data/parser/ok/0030_traits.txt b/crates/libsyntax2/tests/data/parser/ok/0030_traits.txt index 5adab705b..88282ca1a 100644 --- a/crates/libsyntax2/tests/data/parser/ok/0030_traits.txt +++ b/crates/libsyntax2/tests/data/parser/ok/0030_traits.txt | |||
@@ -1,5 +1,5 @@ | |||
1 | FILE@[0; 164) | 1 | FILE@[0; 164) |
2 | TRAIT_DEF@[0; 164) | 2 | TRAIT_DEF@[0; 66) |
3 | VISIBILITY@[0; 3) | 3 | VISIBILITY@[0; 3) |
4 | PUB_KW@[0; 3) | 4 | PUB_KW@[0; 3) |
5 | WHITESPACE@[3; 4) | 5 | WHITESPACE@[3; 4) |
@@ -10,142 +10,81 @@ FILE@[0; 164) | |||
10 | WHITESPACE@[22; 23) | 10 | WHITESPACE@[22; 23) |
11 | L_CURLY@[23; 24) | 11 | L_CURLY@[23; 24) |
12 | WHITESPACE@[24; 29) | 12 | WHITESPACE@[24; 29) |
13 | FN_DEF@[29; 164) | 13 | FN_DEF@[29; 64) |
14 | FN_KW@[29; 31) | 14 | FN_KW@[29; 31) |
15 | WHITESPACE@[31; 32) | 15 | WHITESPACE@[31; 32) |
16 | NAME@[32; 45) | 16 | NAME@[32; 45) |
17 | IDENT@[32; 45) "write_message" | 17 | IDENT@[32; 45) "write_message" |
18 | PARAM_LIST@[45; 164) | 18 | PARAM_LIST@[45; 63) |
19 | L_PAREN@[45; 46) | 19 | L_PAREN@[45; 46) |
20 | PARAM@[46; 63) | 20 | PARAM@[46; 62) |
21 | REF_PAT@[46; 62) | 21 | REFERENCE_TYPE@[46; 62) |
22 | AMP@[46; 47) | 22 | AMP@[46; 47) |
23 | BIND_PAT@[47; 62) | 23 | PATH_TYPE@[47; 62) |
24 | NAME@[47; 62) | 24 | PATH@[47; 62) |
25 | IDENT@[47; 62) "FrontendMessage" | 25 | PATH_SEGMENT@[47; 62) |
26 | err: `expected COLON` | 26 | NAME_REF@[47; 62) |
27 | err: `expected type` | 27 | IDENT@[47; 62) "FrontendMessage" |
28 | ERROR@[62; 63) | 28 | R_PAREN@[62; 63) |
29 | R_PAREN@[62; 63) | 29 | SEMI@[63; 64) |
30 | err: `expected COMMA` | 30 | WHITESPACE@[64; 65) |
31 | err: `expected pattern` | 31 | R_CURLY@[65; 66) |
32 | PARAM@[63; 66) | 32 | WHITESPACE@[66; 68) |
33 | ERROR@[63; 64) | 33 | TRAIT_DEF@[68; 104) |
34 | SEMI@[63; 64) | 34 | TRAIT_KW@[68; 73) |
35 | err: `expected COLON` | 35 | WHITESPACE@[73; 74) |
36 | WHITESPACE@[64; 65) | 36 | NAME@[74; 82) |
37 | err: `expected type` | 37 | IDENT@[74; 82) "Runnable" |
38 | ERROR@[65; 66) | 38 | WHITESPACE@[82; 83) |
39 | R_CURLY@[65; 66) | 39 | L_CURLY@[83; 84) |
40 | err: `expected COMMA` | 40 | WHITESPACE@[84; 89) |
41 | WHITESPACE@[66; 68) | 41 | FN_DEF@[89; 102) |
42 | err: `expected pattern` | 42 | FN_KW@[89; 91) |
43 | PARAM@[68; 82) | 43 | WHITESPACE@[91; 92) |
44 | ERROR@[68; 73) | 44 | NAME@[92; 99) |
45 | TRAIT_KW@[68; 73) | 45 | IDENT@[92; 99) "handler" |
46 | err: `expected COLON` | 46 | PARAM_LIST@[99; 101) |
47 | WHITESPACE@[73; 74) | 47 | L_PAREN@[99; 100) |
48 | PATH_TYPE@[74; 82) | 48 | R_PAREN@[100; 101) |
49 | PATH@[74; 82) | 49 | SEMI@[101; 102) |
50 | PATH_SEGMENT@[74; 82) | 50 | WHITESPACE@[102; 103) |
51 | NAME_REF@[74; 82) | 51 | R_CURLY@[103; 104) |
52 | IDENT@[74; 82) "Runnable" | 52 | WHITESPACE@[104; 106) |
53 | err: `expected COMMA` | 53 | TRAIT_DEF@[106; 163) |
54 | WHITESPACE@[82; 83) | 54 | TRAIT_KW@[106; 111) |
55 | err: `expected pattern` | 55 | WHITESPACE@[111; 112) |
56 | PARAM@[83; 91) | 56 | NAME@[112; 125) |
57 | ERROR@[83; 84) | 57 | IDENT@[112; 125) "TraitWithExpr" |
58 | L_CURLY@[83; 84) | 58 | WHITESPACE@[125; 126) |
59 | err: `expected COLON` | 59 | L_CURLY@[126; 127) |
60 | WHITESPACE@[84; 89) | 60 | WHITESPACE@[127; 132) |
61 | FN_POINTER_TYPE@[89; 91) | 61 | FN_DEF@[132; 161) |
62 | FN_KW@[89; 91) | 62 | FN_KW@[132; 134) |
63 | err: `expected parameters` | 63 | WHITESPACE@[134; 135) |
64 | err: `expected COMMA` | 64 | NAME@[135; 147) |
65 | WHITESPACE@[91; 92) | 65 | IDENT@[135; 147) "fn_with_expr" |
66 | PARAM@[92; 102) | 66 | PARAM_LIST@[147; 160) |
67 | TUPLE_STRUCT_PAT@[92; 101) | 67 | L_PAREN@[147; 148) |
68 | PATH@[92; 99) | 68 | PARAM@[148; 159) |
69 | PATH_SEGMENT@[92; 99) | 69 | BIND_PAT@[148; 149) |
70 | NAME_REF@[92; 99) | 70 | NAME@[148; 149) |
71 | IDENT@[92; 99) "handler" | 71 | IDENT@[148; 149) "x" |
72 | L_PAREN@[99; 100) | 72 | COLON@[149; 150) |
73 | R_PAREN@[100; 101) | 73 | WHITESPACE@[150; 151) |
74 | err: `expected COLON` | 74 | ARRAY_TYPE@[151; 159) |
75 | err: `expected type` | 75 | L_BRACK@[151; 152) |
76 | ERROR@[101; 102) | 76 | PATH_TYPE@[152; 155) |
77 | SEMI@[101; 102) | 77 | PATH@[152; 155) |
78 | err: `expected COMMA` | 78 | PATH_SEGMENT@[152; 155) |
79 | WHITESPACE@[102; 103) | 79 | NAME_REF@[152; 155) |
80 | err: `expected pattern` | 80 | IDENT@[152; 155) "i32" |
81 | PARAM@[103; 111) | 81 | SEMI@[155; 156) |
82 | ERROR@[103; 104) | 82 | WHITESPACE@[156; 157) |
83 | R_CURLY@[103; 104) | 83 | LITERAL@[157; 158) |
84 | err: `expected COLON` | 84 | INT_NUMBER@[157; 158) "1" |
85 | WHITESPACE@[104; 106) | 85 | R_BRACK@[158; 159) |
86 | err: `expected type` | 86 | R_PAREN@[159; 160) |
87 | ERROR@[106; 111) | 87 | SEMI@[160; 161) |
88 | TRAIT_KW@[106; 111) | 88 | WHITESPACE@[161; 162) |
89 | err: `expected COMMA` | 89 | R_CURLY@[162; 163) |
90 | WHITESPACE@[111; 112) | 90 | WHITESPACE@[163; 164) |
91 | PARAM@[112; 164) | ||
92 | STRUCT_PAT@[112; 163) | ||
93 | PATH@[112; 125) | ||
94 | PATH_SEGMENT@[112; 125) | ||
95 | NAME_REF@[112; 125) | ||
96 | IDENT@[112; 125) "TraitWithExpr" | ||
97 | WHITESPACE@[125; 126) | ||
98 | L_CURLY@[126; 127) | ||
99 | WHITESPACE@[127; 132) | ||
100 | err: `expected a name` | ||
101 | BIND_PAT@[132; 134) | ||
102 | ERROR@[132; 134) | ||
103 | FN_KW@[132; 134) | ||
104 | err: `expected COMMA` | ||
105 | WHITESPACE@[134; 135) | ||
106 | BIND_PAT@[135; 147) | ||
107 | NAME@[135; 147) | ||
108 | IDENT@[135; 147) "fn_with_expr" | ||
109 | err: `expected COMMA` | ||
110 | err: `expected a name` | ||
111 | BIND_PAT@[147; 148) | ||
112 | ERROR@[147; 148) | ||
113 | L_PAREN@[147; 148) | ||
114 | err: `expected COMMA` | ||
115 | IDENT@[148; 149) "x" | ||
116 | COLON@[149; 150) | ||
117 | WHITESPACE@[150; 151) | ||
118 | SLICE_PAT@[151; 159) | ||
119 | L_BRACK@[151; 152) | ||
120 | BIND_PAT@[152; 155) | ||
121 | NAME@[152; 155) | ||
122 | IDENT@[152; 155) "i32" | ||
123 | err: `expected COMMA` | ||
124 | err: `expected pattern` | ||
125 | ERROR@[155; 156) | ||
126 | SEMI@[155; 156) | ||
127 | err: `expected COMMA` | ||
128 | WHITESPACE@[156; 157) | ||
129 | LITERAL@[157; 158) | ||
130 | INT_NUMBER@[157; 158) "1" | ||
131 | R_BRACK@[158; 159) | ||
132 | err: `expected COMMA` | ||
133 | err: `expected a name` | ||
134 | BIND_PAT@[159; 160) | ||
135 | ERROR@[159; 160) | ||
136 | R_PAREN@[159; 160) | ||
137 | err: `expected COMMA` | ||
138 | err: `expected a name` | ||
139 | BIND_PAT@[160; 161) | ||
140 | ERROR@[160; 161) | ||
141 | SEMI@[160; 161) | ||
142 | WHITESPACE@[161; 162) | ||
143 | R_CURLY@[162; 163) | ||
144 | err: `expected COLON` | ||
145 | WHITESPACE@[163; 164) | ||
146 | err: `expected type` | ||
147 | err: `expected COMMA` | ||
148 | err: `expected R_PAREN` | ||
149 | err: `expected block` | ||
150 | err: `expected R_CURLY` | ||
151 | ERROR@[164; 164) | ||