aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src/grammar
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_parser/src/grammar')
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs2
-rw-r--r--crates/ra_parser/src/grammar/items.rs112
-rw-r--r--crates/ra_parser/src/grammar/items/traits.rs4
3 files changed, 29 insertions, 89 deletions
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index 51eaf7af6..3291e3f14 100644
--- a/crates/ra_parser/src/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
@@ -73,7 +73,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
73 73
74 // test block_items 74 // test block_items
75 // fn a() { fn b() {} } 75 // fn a() { fn b() {} }
76 let m = match items::maybe_item(p, m, items::ItemFlavor::Mod) { 76 let m = match items::maybe_item(p, m) {
77 Ok(()) => return, 77 Ok(()) => return,
78 Err(m) => m, 78 Err(m) => m,
79 }; 79 };
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index cca524cea..d091b0fbb 100644
--- a/crates/ra_parser/src/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
@@ -22,24 +22,19 @@ use super::*;
22pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { 22pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
23 attributes::inner_attributes(p); 23 attributes::inner_attributes(p);
24 while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) { 24 while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) {
25 item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) 25 item_or_macro(p, stop_on_r_curly)
26 } 26 }
27} 27}
28 28
29pub(super) enum ItemFlavor {
30 Mod,
31 Trait,
32}
33
34pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ 29pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![
35 FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW, 30 FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW,
36 CRATE_KW, USE_KW, MACRO_KW 31 CRATE_KW, USE_KW, MACRO_KW
37]; 32];
38 33
39pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { 34pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
40 let m = p.start(); 35 let m = p.start();
41 attributes::outer_attributes(p); 36 attributes::outer_attributes(p);
42 let m = match maybe_item(p, m, flavor) { 37 let m = match maybe_item(p, m) {
43 Ok(()) => { 38 Ok(()) => {
44 if p.at(T![;]) { 39 if p.at(T![;]) {
45 p.err_and_bump( 40 p.err_and_bump(
@@ -76,7 +71,7 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF
76 } 71 }
77} 72}
78 73
79pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Result<(), Marker> { 74pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
80 // test_err pub_expr 75 // test_err pub_expr
81 // fn foo() { pub 92; } 76 // fn foo() { pub 92; }
82 let has_visibility = opt_visibility(p); 77 let has_visibility = opt_visibility(p);
@@ -114,38 +109,31 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
114 has_mods = true; 109 has_mods = true;
115 } 110 }
116 111
117 if p.at(IDENT) 112 // test default_item
118 && p.at_contextual_kw("default") 113 // default impl T for Foo {}
119 && (match p.nth(1) { 114 if p.at(IDENT) && p.at_contextual_kw("default") {
120 T![impl] => true, 115 match p.nth(1) {
116 T![fn] | T![type] | T![const] | T![impl] => {
117 p.bump_remap(T![default]);
118 has_mods = true;
119 }
121 T![unsafe] => { 120 T![unsafe] => {
122 // test default_unsafe_impl 121 // test default_unsafe_item
123 // default unsafe impl Foo {} 122 // default unsafe impl T for Foo {
124
125 // test default_unsafe_fn
126 // impl T for Foo {
127 // default unsafe fn foo() {} 123 // default unsafe fn foo() {}
128 // } 124 // }
129 if p.nth(2) == T![impl] || p.nth(2) == T![fn] { 125 if matches!(p.nth(2), T![impl] | T![fn]) {
130 p.bump_remap(T![default]); 126 p.bump_remap(T![default]);
131 p.bump(T![unsafe]); 127 p.bump(T![unsafe]);
132 has_mods = true; 128 has_mods = true;
133 } 129 }
134 false
135 } 130 }
136 T![fn] | T![type] | T![const] => { 131 _ => (),
137 if let ItemFlavor::Mod = flavor { 132 }
138 true
139 } else {
140 false
141 }
142 }
143 _ => false,
144 })
145 {
146 p.bump_remap(T![default]);
147 has_mods = true;
148 } 133 }
134
135 // test existential_type
136 // existential type Foo: Fn() -> usize;
149 if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] { 137 if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] {
150 p.bump_remap(T![existential]); 138 p.bump_remap(T![existential]);
151 has_mods = true; 139 has_mods = true;
@@ -153,79 +141,31 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul
153 141
154 // items 142 // items
155 match p.current() { 143 match p.current() {
156 // test async_fn 144 // test fn
157 // async fn foo() {} 145 // fn foo() {}
158
159 // test extern_fn
160 // extern fn foo() {}
161
162 // test const_fn
163 // const fn foo() {}
164
165 // test const_unsafe_fn
166 // const unsafe fn foo() {}
167
168 // test unsafe_extern_fn
169 // unsafe extern "C" fn foo() {}
170
171 // test unsafe_fn
172 // unsafe fn foo() {}
173
174 // test combined_fns
175 // async unsafe fn foo() {}
176 // const unsafe fn bar() {}
177
178 // test_err wrong_order_fns
179 // unsafe async fn foo() {}
180 // unsafe const fn bar() {}
181 T![fn] => { 146 T![fn] => {
182 fn_def(p); 147 fn_def(p);
183 m.complete(p, FN); 148 m.complete(p, FN);
184 } 149 }
185 150
186 // test unsafe_trait 151 // test trait
187 // unsafe trait T {} 152 // trait T {}
188
189 // test auto_trait
190 // auto trait T {}
191
192 // test unsafe_auto_trait
193 // unsafe auto trait T {}
194 T![trait] => { 153 T![trait] => {
195 traits::trait_def(p); 154 traits::trait_def(p);
196 m.complete(p, TRAIT); 155 m.complete(p, TRAIT);
197 } 156 }
198 157
199 // test unsafe_impl
200 // unsafe impl Foo {}
201
202 // test default_impl
203 // default impl Foo {}
204
205 // test_err default_fn_type
206 // trait T {
207 // default type T = Bar;
208 // default fn foo() {}
209 // }
210
211 // test default_fn_type
212 // impl T for Foo {
213 // default type T = Bar;
214 // default fn foo() {}
215 // }
216 T![const] => { 158 T![const] => {
217 consts::const_def(p, m); 159 consts::const_def(p, m);
218 } 160 }
219 161
220 // test unsafe_default_impl 162 // test impl
221 // unsafe default impl Foo {} 163 // impl T for S {}
222 T![impl] => { 164 T![impl] => {
223 traits::impl_def(p); 165 traits::impl_def(p);
224 m.complete(p, IMPL); 166 m.complete(p, IMPL);
225 } 167 }
226 168
227 // test existential_type
228 // existential type Foo: Fn() -> usize;
229 T![type] => { 169 T![type] => {
230 type_def(p, m); 170 type_def(p, m);
231 } 171 }
diff --git a/crates/ra_parser/src/grammar/items/traits.rs b/crates/ra_parser/src/grammar/items/traits.rs
index ef9c8ff5b..751ce65f2 100644
--- a/crates/ra_parser/src/grammar/items/traits.rs
+++ b/crates/ra_parser/src/grammar/items/traits.rs
@@ -47,7 +47,7 @@ pub(crate) fn trait_item_list(p: &mut Parser) {
47 error_block(p, "expected an item"); 47 error_block(p, "expected an item");
48 continue; 48 continue;
49 } 49 }
50 item_or_macro(p, true, ItemFlavor::Trait); 50 item_or_macro(p, true);
51 } 51 }
52 p.expect(T!['}']); 52 p.expect(T!['}']);
53 m.complete(p, ASSOC_ITEM_LIST); 53 m.complete(p, ASSOC_ITEM_LIST);
@@ -104,7 +104,7 @@ pub(crate) fn impl_item_list(p: &mut Parser) {
104 error_block(p, "expected an item"); 104 error_block(p, "expected an item");
105 continue; 105 continue;
106 } 106 }
107 item_or_macro(p, true, ItemFlavor::Mod); 107 item_or_macro(p, true);
108 } 108 }
109 p.expect(T!['}']); 109 p.expect(T!['}']);
110 m.complete(p, ASSOC_ITEM_LIST); 110 m.complete(p, ASSOC_ITEM_LIST);