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
vis(p: &mut Parser<'_>)53 pub(crate) fn vis(p: &mut Parser<'_>) {
54 opt_visibility(p, false);
55 }
56
block(p: &mut Parser<'_>)57 pub(crate) fn block(p: &mut Parser<'_>) {
58 expressions::block_expr(p);
59 }
60
stmt(p: &mut Parser<'_>)61 pub(crate) fn stmt(p: &mut Parser<'_>) {
62 expressions::stmt(p, expressions::Semicolon::Forbidden);
63 }
64
pat(p: &mut Parser<'_>)65 pub(crate) fn pat(p: &mut Parser<'_>) {
66 patterns::pattern_single(p);
67 }
68
pat_top(p: &mut Parser<'_>)69 pub(crate) fn pat_top(p: &mut Parser<'_>) {
70 patterns::pattern_top(p);
71 }
72
ty(p: &mut Parser<'_>)73 pub(crate) fn ty(p: &mut Parser<'_>) {
74 types::type_(p);
75 }
expr(p: &mut Parser<'_>)76 pub(crate) fn expr(p: &mut Parser<'_>) {
77 expressions::expr(p);
78 }
path(p: &mut Parser<'_>)79 pub(crate) fn path(p: &mut Parser<'_>) {
80 paths::type_path(p);
81 }
item(p: &mut Parser<'_>)82 pub(crate) fn item(p: &mut Parser<'_>) {
83 items::item_or_macro(p, true);
84 }
85 // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
meta_item(p: &mut Parser<'_>)86 pub(crate) fn meta_item(p: &mut Parser<'_>) {
87 attributes::meta(p);
88 }
89 }
90
91 pub(crate) mod top {
92 use super::*;
93
source_file(p: &mut Parser<'_>)94 pub(crate) fn source_file(p: &mut Parser<'_>) {
95 let m = p.start();
96 p.eat(SHEBANG);
97 items::mod_contents(p, false);
98 m.complete(p, SOURCE_FILE);
99 }
100
macro_stmts(p: &mut Parser<'_>)101 pub(crate) fn macro_stmts(p: &mut Parser<'_>) {
102 let m = p.start();
103
104 while !p.at(EOF) {
105 expressions::stmt(p, expressions::Semicolon::Optional);
106 }
107
108 m.complete(p, MACRO_STMTS);
109 }
110
macro_items(p: &mut Parser<'_>)111 pub(crate) fn macro_items(p: &mut Parser<'_>) {
112 let m = p.start();
113 items::mod_contents(p, false);
114 m.complete(p, MACRO_ITEMS);
115 }
116
pattern(p: &mut Parser<'_>)117 pub(crate) fn pattern(p: &mut Parser<'_>) {
118 let m = p.start();
119 patterns::pattern_top(p);
120 if p.at(EOF) {
121 m.abandon(p);
122 return;
123 }
124 while !p.at(EOF) {
125 p.bump_any();
126 }
127 m.complete(p, ERROR);
128 }
129
type_(p: &mut Parser<'_>)130 pub(crate) fn type_(p: &mut Parser<'_>) {
131 let m = p.start();
132 types::type_(p);
133 if p.at(EOF) {
134 m.abandon(p);
135 return;
136 }
137 while !p.at(EOF) {
138 p.bump_any();
139 }
140 m.complete(p, ERROR);
141 }
142
expr(p: &mut Parser<'_>)143 pub(crate) fn expr(p: &mut Parser<'_>) {
144 let m = p.start();
145 expressions::expr(p);
146 if p.at(EOF) {
147 m.abandon(p);
148 return;
149 }
150 while !p.at(EOF) {
151 p.bump_any();
152 }
153 m.complete(p, ERROR);
154 }
155
meta_item(p: &mut Parser<'_>)156 pub(crate) fn meta_item(p: &mut Parser<'_>) {
157 let m = p.start();
158 attributes::meta(p);
159 if p.at(EOF) {
160 m.abandon(p);
161 return;
162 }
163 while !p.at(EOF) {
164 p.bump_any();
165 }
166 m.complete(p, ERROR);
167 }
168 }
169 }
170
reparser( node: SyntaxKind, first_child: Option<SyntaxKind>, parent: Option<SyntaxKind>, ) -> Option<fn(&mut Parser<'_>)>171 pub(crate) fn reparser(
172 node: SyntaxKind,
173 first_child: Option<SyntaxKind>,
174 parent: Option<SyntaxKind>,
175 ) -> Option<fn(&mut Parser<'_>)> {
176 let res = match node {
177 BLOCK_EXPR => expressions::block_expr,
178 RECORD_FIELD_LIST => items::record_field_list,
179 RECORD_EXPR_FIELD_LIST => items::record_expr_field_list,
180 VARIANT_LIST => items::variant_list,
181 MATCH_ARM_LIST => items::match_arm_list,
182 USE_TREE_LIST => items::use_tree_list,
183 EXTERN_ITEM_LIST => items::extern_item_list,
184 TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
185 ASSOC_ITEM_LIST => match parent? {
186 IMPL | TRAIT => items::assoc_item_list,
187 _ => return None,
188 },
189 ITEM_LIST => items::item_list,
190 _ => return None,
191 };
192 Some(res)
193 }
194
195 #[derive(Clone, Copy, PartialEq, Eq)]
196 enum BlockLike {
197 Block,
198 NotBlock,
199 }
200
201 impl BlockLike {
is_block(self) -> bool202 fn is_block(self) -> bool {
203 self == BlockLike::Block
204 }
205
is_blocklike(kind: SyntaxKind) -> bool206 fn is_blocklike(kind: SyntaxKind) -> bool {
207 matches!(kind, BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR)
208 }
209 }
210
211 const VISIBILITY_FIRST: TokenSet = TokenSet::new(&[T![pub], T![crate]]);
212
opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool213 fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
214 match p.current() {
215 T![pub] => {
216 let m = p.start();
217 p.bump(T![pub]);
218 if p.at(T!['(']) {
219 match p.nth(1) {
220 // test crate_visibility
221 // pub(crate) struct S;
222 // pub(self) struct S;
223 // pub(super) struct S;
224
225 // test_err crate_visibility_empty_recover
226 // pub() struct S;
227
228 // test pub_parens_typepath
229 // struct B(pub (super::A));
230 // struct B(pub (crate::A,));
231 T![crate] | T![self] | T![super] | T![ident] | T![')'] if p.nth(2) != T![:] => {
232 // If we are in a tuple struct, then the parens following `pub`
233 // might be an tuple field, not part of the visibility. So in that
234 // case we don't want to consume an identifier.
235
236 // test pub_tuple_field
237 // struct MyStruct(pub (u32, u32));
238 // struct MyStruct(pub (u32));
239 // struct MyStruct(pub ());
240 if !(in_tuple_field && matches!(p.nth(1), T![ident] | T![')'])) {
241 p.bump(T!['(']);
242 paths::use_path(p);
243 p.expect(T![')']);
244 }
245 }
246 // test crate_visibility_in
247 // pub(in super::A) struct S;
248 // pub(in crate) struct S;
249 T![in] => {
250 p.bump(T!['(']);
251 p.bump(T![in]);
252 paths::use_path(p);
253 p.expect(T![')']);
254 }
255 _ => {}
256 }
257 }
258 m.complete(p, VISIBILITY);
259 true
260 }
261 // test crate_keyword_vis
262 // crate fn main() { }
263 // struct S { crate field: u32 }
264 // struct T(crate u32);
265 T![crate] => {
266 if p.nth_at(1, T![::]) {
267 // test crate_keyword_path
268 // fn foo() { crate::foo(); }
269 return false;
270 }
271 let m = p.start();
272 p.bump(T![crate]);
273 m.complete(p, VISIBILITY);
274 true
275 }
276 _ => false,
277 }
278 }
279
opt_rename(p: &mut Parser<'_>)280 fn opt_rename(p: &mut Parser<'_>) {
281 if p.at(T![as]) {
282 let m = p.start();
283 p.bump(T![as]);
284 if !p.eat(T![_]) {
285 name(p);
286 }
287 m.complete(p, RENAME);
288 }
289 }
290
abi(p: &mut Parser<'_>)291 fn abi(p: &mut Parser<'_>) {
292 assert!(p.at(T![extern]));
293 let abi = p.start();
294 p.bump(T![extern]);
295 p.eat(STRING);
296 abi.complete(p, ABI);
297 }
298
opt_ret_type(p: &mut Parser<'_>) -> bool299 fn opt_ret_type(p: &mut Parser<'_>) -> bool {
300 if p.at(T![->]) {
301 let m = p.start();
302 p.bump(T![->]);
303 types::type_no_bounds(p);
304 m.complete(p, RET_TYPE);
305 true
306 } else {
307 false
308 }
309 }
310
name_r(p: &mut Parser<'_>, recovery: TokenSet)311 fn name_r(p: &mut Parser<'_>, recovery: TokenSet) {
312 if p.at(IDENT) {
313 let m = p.start();
314 p.bump(IDENT);
315 m.complete(p, NAME);
316 } else {
317 p.err_recover("expected a name", recovery);
318 }
319 }
320
name(p: &mut Parser<'_>)321 fn name(p: &mut Parser<'_>) {
322 name_r(p, TokenSet::EMPTY);
323 }
324
name_ref(p: &mut Parser<'_>)325 fn name_ref(p: &mut Parser<'_>) {
326 if p.at(IDENT) {
327 let m = p.start();
328 p.bump(IDENT);
329 m.complete(p, NAME_REF);
330 } else {
331 p.err_and_bump("expected identifier");
332 }
333 }
334
name_ref_or_index(p: &mut Parser<'_>)335 fn name_ref_or_index(p: &mut Parser<'_>) {
336 assert!(p.at(IDENT) || p.at(INT_NUMBER));
337 let m = p.start();
338 p.bump_any();
339 m.complete(p, NAME_REF);
340 }
341
lifetime(p: &mut Parser<'_>)342 fn lifetime(p: &mut Parser<'_>) {
343 assert!(p.at(LIFETIME_IDENT));
344 let m = p.start();
345 p.bump(LIFETIME_IDENT);
346 m.complete(p, LIFETIME);
347 }
348
error_block(p: &mut Parser<'_>, message: &str)349 fn error_block(p: &mut Parser<'_>, message: &str) {
350 assert!(p.at(T!['{']));
351 let m = p.start();
352 p.error(message);
353 p.bump(T!['{']);
354 expressions::expr_block_contents(p);
355 p.eat(T!['}']);
356 m.complete(p, ERROR);
357 }
358
359 /// The `parser` passed this is required to at least consume one token if it returns `true`.
360 /// If the `parser` returns false, parsing will stop.
delimited( p: &mut Parser<'_>, bra: SyntaxKind, ket: SyntaxKind, delim: SyntaxKind, first_set: TokenSet, mut parser: impl FnMut(&mut Parser<'_>) -> bool, )361 fn delimited(
362 p: &mut Parser<'_>,
363 bra: SyntaxKind,
364 ket: SyntaxKind,
365 delim: SyntaxKind,
366 first_set: TokenSet,
367 mut parser: impl FnMut(&mut Parser<'_>) -> bool,
368 ) {
369 p.bump(bra);
370 while !p.at(ket) && !p.at(EOF) {
371 if !parser(p) {
372 break;
373 }
374 if !p.at(delim) {
375 if p.at_ts(first_set) {
376 p.error(format!("expected {:?}", delim));
377 } else {
378 break;
379 }
380 } else {
381 p.bump(delim);
382 }
383 }
384 p.expect(ket);
385 }
386