diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-18 04:16:20 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-18 04:16:20 +0000 |
commit | 4c1ea0b628f949612b48dd3b65c7d8bb2255f572 (patch) | |
tree | b57d8b8b433fa06c9c6bd8795c2328507064e002 /crates | |
parent | 40c6dd1f4c57d6a8ec26c1bdef24753c884c38aa (diff) | |
parent | 3d9c2beb8e0d4e0fbaded7dac259cadf7616a3ad (diff) |
Merge #987
987: Refactor maybe_item to use Marker argument r=pcpthm a=pcpthm
As suggested at <https://github.com/rust-analyzer/rust-analyzer/pull/980#issuecomment-473659745>.
For expression paring functions, changing signature
- from `fn(&mut Parser) -> Option<CompletedMarker>` to `fn(&mut Parser, Marker) -> Result<CompletedMarker, Marker>`
- from `fn(&mut Parser) -> CompletedMarker` to `fn(&mut Parser, Marker) -> CompletedMarker`
is my plan.
Co-authored-by: pcpthm <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_parser/src/grammar/expressions.rs | 70 | ||||
-rw-r--r-- | crates/ra_parser/src/grammar/items.rs | 84 |
2 files changed, 70 insertions, 84 deletions
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 5a3b9f589..ccbc905ab 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs | |||
@@ -62,47 +62,41 @@ pub(crate) fn expr_block_contents(p: &mut Parser) { | |||
62 | continue; | 62 | continue; |
63 | } | 63 | } |
64 | 64 | ||
65 | match items::maybe_item(p, items::ItemFlavor::Mod) { | 65 | let m = match items::maybe_item(p, m, items::ItemFlavor::Mod) { |
66 | items::MaybeItem::Item(kind) => { | 66 | Ok(()) => continue, |
67 | m.complete(p, kind); | 67 | Err(m) => m, |
68 | } | 68 | }; |
69 | items::MaybeItem::Modifiers => { | 69 | |
70 | // test pub_expr | ||
71 | // fn foo() { pub 92; } //FIXME | ||
72 | if has_attrs { | ||
73 | m.abandon(p); | ||
74 | p.error("expected a let statement or an item after attributes in block"); | ||
75 | } else { | ||
76 | let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; | ||
77 | if p.at(R_CURLY) { | ||
70 | m.abandon(p); | 78 | m.abandon(p); |
71 | p.error("expected an item"); | 79 | } else { |
72 | } | 80 | // test no_semi_after_block |
73 | // test pub_expr | 81 | // fn foo() { |
74 | // fn foo() { pub 92; } //FIXME | 82 | // if true {} |
75 | items::MaybeItem::None => { | 83 | // loop {} |
76 | if has_attrs { | 84 | // match () {} |
77 | m.abandon(p); | 85 | // while true {} |
78 | p.error("expected a let statement or an item after attributes in block"); | 86 | // for _ in () {} |
87 | // {} | ||
88 | // {} | ||
89 | // macro_rules! test { | ||
90 | // () => {} | ||
91 | // } | ||
92 | // test!{} | ||
93 | // } | ||
94 | if is_blocklike { | ||
95 | p.eat(SEMI); | ||
79 | } else { | 96 | } else { |
80 | let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block; | 97 | p.expect(SEMI); |
81 | if p.at(R_CURLY) { | ||
82 | m.abandon(p); | ||
83 | } else { | ||
84 | // test no_semi_after_block | ||
85 | // fn foo() { | ||
86 | // if true {} | ||
87 | // loop {} | ||
88 | // match () {} | ||
89 | // while true {} | ||
90 | // for _ in () {} | ||
91 | // {} | ||
92 | // {} | ||
93 | // macro_rules! test { | ||
94 | // () => {} | ||
95 | // } | ||
96 | // test!{} | ||
97 | // } | ||
98 | if is_blocklike { | ||
99 | p.eat(SEMI); | ||
100 | } else { | ||
101 | p.expect(SEMI); | ||
102 | } | ||
103 | m.complete(p, EXPR_STMT); | ||
104 | } | ||
105 | } | 98 | } |
99 | m.complete(p, EXPR_STMT); | ||
106 | } | 100 | } |
107 | } | 101 | } |
108 | } | 102 | } |
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index a057c8167..94b93a02b 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs | |||
@@ -37,52 +37,40 @@ pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ | |||
37 | pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { | 37 | pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { |
38 | let m = p.start(); | 38 | let m = p.start(); |
39 | attributes::outer_attributes(p); | 39 | attributes::outer_attributes(p); |
40 | match maybe_item(p, flavor) { | 40 | let m = match maybe_item(p, m, flavor) { |
41 | MaybeItem::Item(kind) => { | 41 | Ok(()) => return, |
42 | m.complete(p, kind); | 42 | Err(m) => m, |
43 | } | 43 | }; |
44 | MaybeItem::None => { | 44 | if paths::is_path_start(p) { |
45 | if paths::is_path_start(p) { | 45 | match macro_call(p) { |
46 | match macro_call(p) { | 46 | BlockLike::Block => (), |
47 | BlockLike::Block => (), | 47 | BlockLike::NotBlock => { |
48 | BlockLike::NotBlock => { | 48 | p.expect(SEMI); |
49 | p.expect(SEMI); | ||
50 | } | ||
51 | } | ||
52 | m.complete(p, MACRO_CALL); | ||
53 | } else { | ||
54 | m.abandon(p); | ||
55 | if p.at(L_CURLY) { | ||
56 | error_block(p, "expected an item"); | ||
57 | } else if p.at(R_CURLY) && !stop_on_r_curly { | ||
58 | let e = p.start(); | ||
59 | p.error("unmatched `}`"); | ||
60 | p.bump(); | ||
61 | e.complete(p, ERROR); | ||
62 | } else if !p.at(EOF) && !p.at(R_CURLY) { | ||
63 | p.err_and_bump("expected an item"); | ||
64 | } else { | ||
65 | p.error("expected an item"); | ||
66 | } | ||
67 | } | 49 | } |
68 | } | 50 | } |
69 | MaybeItem::Modifiers => { | 51 | m.complete(p, MACRO_CALL); |
70 | p.error("expected fn, trait or impl"); | 52 | } else { |
71 | m.complete(p, ERROR); | 53 | m.abandon(p); |
54 | if p.at(L_CURLY) { | ||
55 | error_block(p, "expected an item"); | ||
56 | } else if p.at(R_CURLY) && !stop_on_r_curly { | ||
57 | let e = p.start(); | ||
58 | p.error("unmatched `}`"); | ||
59 | p.bump(); | ||
60 | e.complete(p, ERROR); | ||
61 | } else if !p.at(EOF) && !p.at(R_CURLY) { | ||
62 | p.err_and_bump("expected an item"); | ||
63 | } else { | ||
64 | p.error("expected an item"); | ||
72 | } | 65 | } |
73 | } | 66 | } |
74 | } | 67 | } |
75 | 68 | ||
76 | pub(super) enum MaybeItem { | 69 | pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Result<(), Marker> { |
77 | None, | ||
78 | Item(SyntaxKind), | ||
79 | Modifiers, | ||
80 | } | ||
81 | |||
82 | pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { | ||
83 | opt_visibility(p); | 70 | opt_visibility(p); |
84 | if let Some(kind) = items_without_modifiers(p) { | 71 | if let Some(kind) = items_without_modifiers(p) { |
85 | return MaybeItem::Item(kind); | 72 | m.complete(p, kind); |
73 | return Ok(()); | ||
86 | } | 74 | } |
87 | 75 | ||
88 | let mut has_mods = false; | 76 | let mut has_mods = false; |
@@ -115,7 +103,7 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { | |||
115 | } | 103 | } |
116 | 104 | ||
117 | // items | 105 | // items |
118 | let kind = match p.current() { | 106 | match p.current() { |
119 | // test async_fn | 107 | // test async_fn |
120 | // async fn foo() {} | 108 | // async fn foo() {} |
121 | 109 | ||
@@ -135,7 +123,7 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { | |||
135 | // unsafe fn foo() {} | 123 | // unsafe fn foo() {} |
136 | FN_KW => { | 124 | FN_KW => { |
137 | fn_def(p, flavor); | 125 | fn_def(p, flavor); |
138 | FN_DEF | 126 | m.complete(p, FN_DEF); |
139 | } | 127 | } |
140 | 128 | ||
141 | // test unsafe_trait | 129 | // test unsafe_trait |
@@ -148,7 +136,7 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { | |||
148 | // unsafe auto trait T {} | 136 | // unsafe auto trait T {} |
149 | TRAIT_KW => { | 137 | TRAIT_KW => { |
150 | traits::trait_def(p); | 138 | traits::trait_def(p); |
151 | TRAIT_DEF | 139 | m.complete(p, TRAIT_DEF); |
152 | } | 140 | } |
153 | 141 | ||
154 | // test unsafe_impl | 142 | // test unsafe_impl |
@@ -161,14 +149,18 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { | |||
161 | // unsafe default impl Foo {} | 149 | // unsafe default impl Foo {} |
162 | IMPL_KW => { | 150 | IMPL_KW => { |
163 | traits::impl_block(p); | 151 | traits::impl_block(p); |
164 | IMPL_BLOCK | 152 | m.complete(p, IMPL_BLOCK); |
165 | } | 153 | } |
166 | _ => { | 154 | _ => { |
167 | return if has_mods { MaybeItem::Modifiers } else { MaybeItem::None }; | 155 | if !has_mods { |
156 | return Err(m); | ||
157 | } else { | ||
158 | p.error("expected fn, trait or impl"); | ||
159 | m.complete(p, ERROR); | ||
160 | } | ||
168 | } | 161 | } |
169 | }; | 162 | } |
170 | 163 | Ok(()) | |
171 | MaybeItem::Item(kind) | ||
172 | } | 164 | } |
173 | 165 | ||
174 | fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> { | 166 | fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> { |