1 //! This is the actual "grammar" of the Rust language.
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)`.
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.
13 //! Code in this module also contains inline tests, which start with
14 //! `// test name-of-the-test` comment and look like this:
17 //! // test function_with_zero_parameters
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.
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.
42 parser::{CompletedMarker, Marker, Parser},
43 SyntaxKind::{self, *},
47 pub(crate) mod entry {
50 pub(crate) mod prefix {
53 pub(crate) fn vis(p: &mut Parser) {
54 let _ = opt_visibility(p, false);
57 pub(crate) fn block(p: &mut Parser) {
58 expressions::block_expr(p);
61 pub(crate) fn stmt(p: &mut Parser) {
62 expressions::stmt(p, expressions::Semicolon::Forbidden);
65 pub(crate) fn pat(p: &mut Parser) {
66 patterns::pattern_single(p);
69 pub(crate) fn ty(p: &mut Parser) {
72 pub(crate) fn expr(p: &mut Parser) {
73 let _ = expressions::expr(p);
75 pub(crate) fn path(p: &mut Parser) {
76 let _ = paths::type_path(p);
78 pub(crate) fn item(p: &mut Parser) {
79 items::item_or_macro(p, true);
81 // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
82 pub(crate) fn meta_item(p: &mut Parser) {
90 pub(crate) fn source_file(p: &mut Parser) {
93 items::mod_contents(p, false);
94 m.complete(p, SOURCE_FILE);
97 pub(crate) fn macro_stmts(p: &mut Parser) {
101 expressions::stmt(p, expressions::Semicolon::Optional);
104 m.complete(p, MACRO_STMTS);
107 pub(crate) fn macro_items(p: &mut Parser) {
109 items::mod_contents(p, false);
110 m.complete(p, MACRO_ITEMS);
113 pub(crate) fn pattern(p: &mut Parser) {
115 patterns::pattern_single(p);
123 m.complete(p, ERROR);
126 pub(crate) fn type_(p: &mut Parser) {
136 m.complete(p, ERROR);
141 pub(crate) fn reparser(
143 first_child: Option<SyntaxKind>,
144 parent: Option<SyntaxKind>,
145 ) -> Option<fn(&mut Parser)> {
146 let res = match node {
147 BLOCK_EXPR => expressions::block_expr,
148 RECORD_FIELD_LIST => items::record_field_list,
149 RECORD_EXPR_FIELD_LIST => items::record_expr_field_list,
150 VARIANT_LIST => items::variant_list,
151 MATCH_ARM_LIST => items::match_arm_list,
152 USE_TREE_LIST => items::use_tree_list,
153 EXTERN_ITEM_LIST => items::extern_item_list,
154 TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
155 ASSOC_ITEM_LIST => match parent? {
156 IMPL | TRAIT => items::assoc_item_list,
159 ITEM_LIST => items::item_list,
165 #[derive(Clone, Copy, PartialEq, Eq)]
172 fn is_block(self) -> bool {
173 self == BlockLike::Block
177 fn opt_visibility(p: &mut Parser, in_tuple_field: bool) -> bool {
184 // test crate_visibility
185 // pub(crate) struct S;
186 // pub(self) struct S;
187 // pub(super) struct S;
189 // test pub_parens_typepath
190 // struct B(pub (super::A));
191 // struct B(pub (crate::A,));
192 T![crate] | T![self] | T![super] | T![ident] if p.nth(2) != T![:] => {
193 // If we are in a tuple struct, then the parens following `pub`
194 // might be an tuple field, not part of the visibility. So in that
195 // case we don't want to consume an identifier.
197 // test pub_tuple_field
198 // struct MyStruct(pub (u32, u32));
199 if !(in_tuple_field && matches!(p.nth(1), T![ident])) {
205 // test crate_visibility_in
206 // pub(in super::A) struct S;
207 // pub(in crate) struct S;
217 m.complete(p, VISIBILITY);
220 // test crate_keyword_vis
221 // crate fn main() { }
222 // struct S { crate field: u32 }
223 // struct T(crate u32);
225 if p.nth_at(1, T![::]) {
226 // test crate_keyword_path
227 // fn foo() { crate::foo(); }
232 m.complete(p, VISIBILITY);
239 fn opt_rename(p: &mut Parser) {
246 m.complete(p, RENAME);
250 fn abi(p: &mut Parser) {
251 assert!(p.at(T![extern]));
255 abi.complete(p, ABI);
258 fn opt_ret_type(p: &mut Parser) -> bool {
262 types::type_no_bounds(p);
263 m.complete(p, RET_TYPE);
270 fn name_r(p: &mut Parser, recovery: TokenSet) {
276 p.err_recover("expected a name", recovery);
280 fn name(p: &mut Parser) {
281 name_r(p, TokenSet::EMPTY);
284 fn name_ref(p: &mut Parser) {
288 m.complete(p, NAME_REF);
290 p.err_and_bump("expected identifier");
294 fn name_ref_or_index(p: &mut Parser) {
295 assert!(p.at(IDENT) || p.at(INT_NUMBER));
298 m.complete(p, NAME_REF);
301 fn lifetime(p: &mut Parser) {
302 assert!(p.at(LIFETIME_IDENT));
304 p.bump(LIFETIME_IDENT);
305 m.complete(p, LIFETIME);
308 fn error_block(p: &mut Parser, message: &str) {
309 assert!(p.at(T!['{']));
313 expressions::expr_block_contents(p);
315 m.complete(p, ERROR);