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 format 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` to learn about API, available to the grammar,
10 //! and see docs for `Event` to learn how this actually manages to
11 //! 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 collect-tests` to extract
22 //! it as a standalone text-fixture into `tests/data/parser/inline`, and
23 //! run `cargo test` once to create the "gold" value.
25 //! Coding convention: rules like `where_clause` always produce either a
26 //! node or an error, rules like `opt_where_clause` may produce nothing.
27 //! Non-opt rules typically start with `assert!(p.at(FIRST_TOKEN))`, the
28 //! caller is responsible for branching on the first token.
40 parser::{CompletedMarker, Marker, Parser},
41 SyntaxKind::{self, *},
45 pub(crate) fn root(p: &mut Parser) {
48 items::mod_contents(p, false);
49 m.complete(p, SOURCE_FILE);
52 /// Various pieces of syntax that can be parsed by macros by example
53 pub(crate) mod fragments {
56 pub(crate) use super::{
57 expressions::block, paths::type_path as path, patterns::pattern, types::type_,
60 pub(crate) fn expr(p: &mut Parser) {
61 let _ = expressions::expr(p);
64 pub(crate) fn stmt(p: &mut Parser) {
65 expressions::stmt(p, expressions::StmtWithSemi::No)
68 pub(crate) fn opt_visibility(p: &mut Parser) {
69 let _ = super::opt_visibility(p);
72 // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
73 pub(crate) fn meta_item(p: &mut Parser) {
74 fn is_delimiter(p: &mut Parser) -> bool {
76 T!['{'] | T!['('] | T!['['] => true,
92 // https://doc.rust-lang.org/reference/attributes.html
93 // https://doc.rust-lang.org/reference/paths.html#simple-paths
94 // The start of an meta must be a simple path
96 IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump_any(),
100 c if c.is_literal() => p.bump_any(),
101 T![true] | T![false] => p.bump_any(),
111 m.complete(p, TOKEN_TREE);
114 pub(crate) fn item(p: &mut Parser) {
115 items::item_or_macro(p, true, items::ItemFlavor::Mod)
118 pub(crate) fn macro_items(p: &mut Parser) {
120 items::mod_contents(p, false);
121 m.complete(p, MACRO_ITEMS);
124 pub(crate) fn macro_stmts(p: &mut Parser) {
133 expressions::stmt(p, expressions::StmtWithSemi::Optional);
136 m.complete(p, MACRO_STMTS);
140 pub(crate) fn reparser(
142 first_child: Option<SyntaxKind>,
143 parent: Option<SyntaxKind>,
144 ) -> Option<fn(&mut Parser)> {
145 let res = match node {
146 BLOCK => expressions::naked_block,
147 RECORD_FIELD_DEF_LIST => items::record_field_def_list,
148 RECORD_FIELD_LIST => items::record_field_list,
149 ENUM_VARIANT_LIST => items::enum_variant_list,
150 MATCH_ARM_LIST => items::match_arm_list,
151 USE_TREE_LIST => items::use_tree_list,
152 EXTERN_ITEM_LIST => items::extern_item_list,
153 TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
154 ITEM_LIST => match parent? {
155 IMPL_BLOCK => items::impl_item_list,
156 TRAIT_DEF => items::trait_item_list,
157 MODULE => items::mod_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) -> bool {
184 // test crate_visibility
185 // pub(crate) struct S;
186 // pub(self) struct S;
187 // pub(self) struct S;
188 // pub(self) struct S;
189 T![crate] | T![self] | T![super] => {
203 m.complete(p, VISIBILITY);
205 // test crate_keyword_vis
206 // crate fn main() { }
207 // struct S { crate field: u32 }
208 // struct T(crate u32);
210 // test crate_keyword_path
211 // fn foo() { crate::foo(); }
212 T![crate] if !p.nth_at(1, T![::]) => {
215 m.complete(p, VISIBILITY);
222 fn opt_alias(p: &mut Parser) {
229 m.complete(p, ALIAS);
233 fn abi(p: &mut Parser) {
234 assert!(p.at(T![extern]));
238 STRING | RAW_STRING => p.bump_any(),
241 abi.complete(p, ABI);
244 fn opt_fn_ret_type(p: &mut Parser) -> bool {
249 m.complete(p, RET_TYPE);
256 fn name_r(p: &mut Parser, recovery: TokenSet) {
262 p.err_recover("expected a name", recovery);
266 fn name(p: &mut Parser) {
267 name_r(p, TokenSet::EMPTY)
270 fn name_ref(p: &mut Parser) {
274 m.complete(p, NAME_REF);
275 } else if p.at(T![self]) {
278 m.complete(p, T![self]);
280 p.err_and_bump("expected identifier");
284 fn name_ref_or_index(p: &mut Parser) {
285 if p.at(IDENT) || p.at(INT_NUMBER) {
288 m.complete(p, NAME_REF);
290 p.err_and_bump("expected identifier");
294 fn error_block(p: &mut Parser, message: &str) {
295 assert!(p.at(T!['{']));
299 expressions::expr_block_contents(p);
301 m.complete(p, ERROR);