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` 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 xtask codegen` 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.
41 parser::{CompletedMarker, Marker, Parser},
42 SyntaxKind::{self, *},
46 pub(crate) fn root(p: &mut Parser) {
49 items::mod_contents(p, false);
50 m.complete(p, SOURCE_FILE);
53 /// Various pieces of syntax that can be parsed by macros by example
54 pub(crate) mod fragments {
57 pub(crate) use super::{
58 expressions::block_expr, paths::type_path as path, patterns::pattern, types::type_,
61 pub(crate) fn expr(p: &mut Parser) {
62 let _ = expressions::expr(p);
65 pub(crate) fn stmt(p: &mut Parser) {
66 expressions::stmt(p, expressions::StmtWithSemi::No)
69 pub(crate) fn opt_visibility(p: &mut Parser) {
70 let _ = super::opt_visibility(p);
73 // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
74 pub(crate) fn meta_item(p: &mut Parser) {
75 fn is_delimiter(p: &mut Parser) -> bool {
76 matches!(p.current(), T!['{'] | T!['('] | T!['['])
90 // https://doc.rust-lang.org/reference/attributes.html
91 // https://doc.rust-lang.org/reference/paths.html#simple-paths
92 // The start of an meta must be a simple path
94 IDENT | T![::] | T![super] | T![self] | T![crate] => p.bump_any(),
98 c if c.is_literal() => p.bump_any(),
99 T![true] | T![false] => p.bump_any(),
109 m.complete(p, TOKEN_TREE);
112 pub(crate) fn item(p: &mut Parser) {
113 items::item_or_macro(p, true)
116 pub(crate) fn macro_items(p: &mut Parser) {
118 items::mod_contents(p, false);
119 m.complete(p, MACRO_ITEMS);
122 pub(crate) fn macro_stmts(p: &mut Parser) {
131 expressions::stmt(p, expressions::StmtWithSemi::Optional);
134 m.complete(p, MACRO_STMTS);
138 pub(crate) fn reparser(
140 first_child: Option<SyntaxKind>,
141 parent: Option<SyntaxKind>,
142 ) -> Option<fn(&mut Parser)> {
143 let res = match node {
144 BLOCK_EXPR => expressions::block_expr,
145 RECORD_FIELD_LIST => items::record_field_list,
146 RECORD_EXPR_FIELD_LIST => items::record_expr_field_list,
147 VARIANT_LIST => items::variant_list,
148 MATCH_ARM_LIST => items::match_arm_list,
149 USE_TREE_LIST => items::use_tree_list,
150 EXTERN_ITEM_LIST => items::extern_item_list,
151 TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
152 ASSOC_ITEM_LIST => match parent? {
153 IMPL => items::assoc_item_list,
154 TRAIT => items::assoc_item_list,
157 ITEM_LIST => items::item_list,
163 #[derive(Clone, Copy, PartialEq, Eq)]
170 fn is_block(self) -> bool {
171 self == BlockLike::Block
175 fn opt_visibility(p: &mut Parser) -> bool {
182 // test crate_visibility
183 // pub(crate) struct S;
184 // pub(self) struct S;
185 // pub(self) struct S;
186 // pub(self) struct S;
187 T![crate] | T![self] | T![super] => {
201 m.complete(p, VISIBILITY);
203 // test crate_keyword_vis
204 // crate fn main() { }
205 // struct S { crate field: u32 }
206 // struct T(crate u32);
208 // test crate_keyword_path
209 // fn foo() { crate::foo(); }
210 T![crate] if !p.nth_at(1, T![::]) => {
213 m.complete(p, VISIBILITY);
220 fn opt_rename(p: &mut Parser) {
227 m.complete(p, RENAME);
231 fn abi(p: &mut Parser) {
232 assert!(p.at(T![extern]));
236 STRING | RAW_STRING => p.bump_any(),
239 abi.complete(p, ABI);
242 fn opt_ret_type(p: &mut Parser) -> bool {
246 types::type_no_bounds(p);
247 m.complete(p, RET_TYPE);
254 fn name_r(p: &mut Parser, recovery: TokenSet) {
260 p.err_recover("expected a name", recovery);
264 fn name(p: &mut Parser) {
265 name_r(p, TokenSet::EMPTY)
268 fn name_ref(p: &mut Parser) {
272 m.complete(p, NAME_REF);
274 p.err_and_bump("expected identifier");
278 fn name_ref_or_index(p: &mut Parser) {
279 assert!(p.at(IDENT) || p.at(INT_NUMBER));
282 m.complete(p, NAME_REF);
285 fn error_block(p: &mut Parser, message: &str) {
286 assert!(p.at(T!['{']));
290 expressions::expr_block_contents(p);
292 m.complete(p, ERROR);