]> git.lizzy.rs Git - rust.git/blob - crates/parser/src/grammar.rs
add TopEntryPoint
[rust.git] / crates / parser / src / grammar.rs
1 //! This is the actual "grammar" of the Rust language.
2 //!
3 //! Each function in this module and its children corresponds
4 //! to a production of the formal grammar. Submodules roughly
5 //! correspond to different *areas* of the grammar. By convention,
6 //! each submodule starts with `use super::*` import and exports
7 //! "public" productions via `pub(super)`.
8 //!
9 //! See docs for [`Parser`](super::parser::Parser) to learn about API,
10 //! available to the grammar, and see docs for [`Event`](super::event::Event)
11 //! to learn how this actually manages to produce parse trees.
12 //!
13 //! Code in this module also contains inline tests, which start with
14 //! `// test name-of-the-test` comment and look like this:
15 //!
16 //! ```
17 //! // test function_with_zero_parameters
18 //! // fn foo() {}
19 //! ```
20 //!
21 //! After adding a new inline-test, run `cargo test -p xtask` to
22 //! extract it as a standalone text-fixture into
23 //! `crates/syntax/test_data/parser/`, and run `cargo test` once to
24 //! create the "gold" value.
25 //!
26 //! Coding convention: rules like `where_clause` always produce either a
27 //! node or an error, rules like `opt_where_clause` may produce nothing.
28 //! Non-opt rules typically start with `assert!(p.at(FIRST_TOKEN))`, the
29 //! caller is responsible for branching on the first token.
30
31 mod attributes;
32 mod expressions;
33 mod items;
34 mod params;
35 mod paths;
36 mod patterns;
37 mod generic_args;
38 mod generic_params;
39 mod types;
40
41 use crate::{
42     parser::{CompletedMarker, Marker, Parser},
43     SyntaxKind::{self, *},
44     TokenSet, T,
45 };
46
47 pub(crate) mod entry {
48     use super::*;
49
50     pub(crate) mod prefix {
51         use super::*;
52
53         pub(crate) fn vis(p: &mut Parser) {
54             let _ = opt_visibility(p, false);
55         }
56
57         pub(crate) fn block(p: &mut Parser) {
58             expressions::block_expr(p);
59         }
60
61         pub(crate) fn stmt(p: &mut Parser) {
62             expressions::stmt(p, expressions::StmtWithSemi::No, true);
63         }
64
65         pub(crate) fn pat(p: &mut Parser) {
66             patterns::pattern_single(p);
67         }
68
69         pub(crate) fn ty(p: &mut Parser) {
70             types::type_(p);
71         }
72         pub(crate) fn expr(p: &mut Parser) {
73             let _ = expressions::expr(p);
74         }
75         pub(crate) fn path(p: &mut Parser) {
76             let _ = paths::type_path(p);
77         }
78         pub(crate) fn item(p: &mut Parser) {
79             items::item_or_macro(p, true);
80         }
81         // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
82         pub(crate) fn meta_item(p: &mut Parser) {
83             attributes::meta(p);
84         }
85     }
86
87     pub(crate) mod top {
88         use super::*;
89
90         pub(crate) fn source_file(p: &mut Parser) {
91             let m = p.start();
92             p.eat(SHEBANG);
93             items::mod_contents(p, false);
94             m.complete(p, SOURCE_FILE);
95         }
96
97         pub(crate) fn macro_stmts(p: &mut Parser) {
98             let m = p.start();
99
100             while !p.at(EOF) {
101                 if p.at(T![;]) {
102                     p.bump(T![;]);
103                     continue;
104                 }
105
106                 expressions::stmt(p, expressions::StmtWithSemi::Optional, true);
107             }
108
109             m.complete(p, MACRO_STMTS);
110         }
111
112         pub(crate) fn macro_items(p: &mut Parser) {
113             let m = p.start();
114             items::mod_contents(p, false);
115             m.complete(p, MACRO_ITEMS);
116         }
117     }
118 }
119
120 pub(crate) mod entry_points {
121     use super::*;
122
123     pub(crate) fn stmt_optional_semi(p: &mut Parser) {
124         expressions::stmt(p, expressions::StmtWithSemi::Optional, false);
125     }
126
127     pub(crate) fn attr(p: &mut Parser) {
128         attributes::outer_attrs(p);
129     }
130 }
131
132 pub(crate) fn reparser(
133     node: SyntaxKind,
134     first_child: Option<SyntaxKind>,
135     parent: Option<SyntaxKind>,
136 ) -> Option<fn(&mut Parser)> {
137     let res = match node {
138         BLOCK_EXPR => expressions::block_expr,
139         RECORD_FIELD_LIST => items::record_field_list,
140         RECORD_EXPR_FIELD_LIST => items::record_expr_field_list,
141         VARIANT_LIST => items::variant_list,
142         MATCH_ARM_LIST => items::match_arm_list,
143         USE_TREE_LIST => items::use_tree_list,
144         EXTERN_ITEM_LIST => items::extern_item_list,
145         TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
146         ASSOC_ITEM_LIST => match parent? {
147             IMPL | TRAIT => items::assoc_item_list,
148             _ => return None,
149         },
150         ITEM_LIST => items::item_list,
151         _ => return None,
152     };
153     Some(res)
154 }
155
156 #[derive(Clone, Copy, PartialEq, Eq)]
157 enum BlockLike {
158     Block,
159     NotBlock,
160 }
161
162 impl BlockLike {
163     fn is_block(self) -> bool {
164         self == BlockLike::Block
165     }
166 }
167
168 fn opt_visibility(p: &mut Parser, in_tuple_field: bool) -> bool {
169     match p.current() {
170         T![pub] => {
171             let m = p.start();
172             p.bump(T![pub]);
173             if p.at(T!['(']) {
174                 match p.nth(1) {
175                     // test crate_visibility
176                     // pub(crate) struct S;
177                     // pub(self) struct S;
178                     // pub(super) struct S;
179
180                     // test pub_parens_typepath
181                     // struct B(pub (super::A));
182                     // struct B(pub (crate::A,));
183                     T![crate] | T![self] | T![super] | T![ident] if p.nth(2) != T![:] => {
184                         // If we are in a tuple struct, then the parens following `pub`
185                         // might be an tuple field, not part of the visibility. So in that
186                         // case we don't want to consume an identifier.
187
188                         // test pub_tuple_field
189                         // struct MyStruct(pub (u32, u32));
190                         if !(in_tuple_field && matches!(p.nth(1), T![ident])) {
191                             p.bump(T!['(']);
192                             paths::use_path(p);
193                             p.expect(T![')']);
194                         }
195                     }
196                     // test crate_visibility_in
197                     // pub(in super::A) struct S;
198                     // pub(in crate) struct S;
199                     T![in] => {
200                         p.bump(T!['(']);
201                         p.bump(T![in]);
202                         paths::use_path(p);
203                         p.expect(T![')']);
204                     }
205                     _ => (),
206                 }
207             }
208             m.complete(p, VISIBILITY);
209             true
210         }
211         // test crate_keyword_vis
212         // crate fn main() { }
213         // struct S { crate field: u32 }
214         // struct T(crate u32);
215         T![crate] => {
216             if p.nth_at(1, T![::]) {
217                 // test crate_keyword_path
218                 // fn foo() { crate::foo(); }
219                 return false;
220             }
221             let m = p.start();
222             p.bump(T![crate]);
223             m.complete(p, VISIBILITY);
224             true
225         }
226         _ => false,
227     }
228 }
229
230 fn opt_rename(p: &mut Parser) {
231     if p.at(T![as]) {
232         let m = p.start();
233         p.bump(T![as]);
234         if !p.eat(T![_]) {
235             name(p);
236         }
237         m.complete(p, RENAME);
238     }
239 }
240
241 fn abi(p: &mut Parser) {
242     assert!(p.at(T![extern]));
243     let abi = p.start();
244     p.bump(T![extern]);
245     p.eat(STRING);
246     abi.complete(p, ABI);
247 }
248
249 fn opt_ret_type(p: &mut Parser) -> bool {
250     if p.at(T![->]) {
251         let m = p.start();
252         p.bump(T![->]);
253         types::type_no_bounds(p);
254         m.complete(p, RET_TYPE);
255         true
256     } else {
257         false
258     }
259 }
260
261 fn name_r(p: &mut Parser, recovery: TokenSet) {
262     if p.at(IDENT) {
263         let m = p.start();
264         p.bump(IDENT);
265         m.complete(p, NAME);
266     } else {
267         p.err_recover("expected a name", recovery);
268     }
269 }
270
271 fn name(p: &mut Parser) {
272     name_r(p, TokenSet::EMPTY);
273 }
274
275 fn name_ref(p: &mut Parser) {
276     if p.at(IDENT) {
277         let m = p.start();
278         p.bump(IDENT);
279         m.complete(p, NAME_REF);
280     } else {
281         p.err_and_bump("expected identifier");
282     }
283 }
284
285 fn name_ref_or_index(p: &mut Parser) {
286     assert!(p.at(IDENT) || p.at(INT_NUMBER));
287     let m = p.start();
288     p.bump_any();
289     m.complete(p, NAME_REF);
290 }
291
292 fn lifetime(p: &mut Parser) {
293     assert!(p.at(LIFETIME_IDENT));
294     let m = p.start();
295     p.bump(LIFETIME_IDENT);
296     m.complete(p, LIFETIME);
297 }
298
299 fn error_block(p: &mut Parser, message: &str) {
300     assert!(p.at(T!['{']));
301     let m = p.start();
302     p.error(message);
303     p.bump(T!['{']);
304     expressions::expr_block_contents(p);
305     p.eat(T!['}']);
306     m.complete(p, ERROR);
307 }