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_points {
50 pub(crate) fn source_file(p: &mut Parser) {
53 items::mod_contents(p, false);
54 m.complete(p, SOURCE_FILE);
57 pub(crate) use expressions::block_expr;
59 pub(crate) use paths::type_path as path;
61 pub(crate) use patterns::pattern_single as pattern;
63 pub(crate) use types::type_;
65 pub(crate) fn expr(p: &mut Parser) {
66 let _ = expressions::expr_with_attrs(p);
69 pub(crate) fn stmt(p: &mut Parser) {
70 expressions::stmt(p, expressions::StmtWithSemi::No, true)
73 pub(crate) fn stmt_optional_semi(p: &mut Parser) {
74 expressions::stmt(p, expressions::StmtWithSemi::Optional, false)
77 pub(crate) fn visibility(p: &mut Parser) {
78 let _ = opt_visibility(p);
81 // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
82 pub(crate) fn meta_item(p: &mut Parser) {
86 pub(crate) fn item(p: &mut Parser) {
87 items::item_or_macro(p, true)
90 pub(crate) fn macro_items(p: &mut Parser) {
92 items::mod_contents(p, false);
93 m.complete(p, MACRO_ITEMS);
96 pub(crate) fn macro_stmts(p: &mut Parser) {
105 expressions::stmt(p, expressions::StmtWithSemi::Optional, true);
108 m.complete(p, MACRO_STMTS);
111 pub(crate) fn attr(p: &mut Parser) {
112 attributes::outer_attrs(p)
116 pub(crate) fn reparser(
118 first_child: Option<SyntaxKind>,
119 parent: Option<SyntaxKind>,
120 ) -> Option<fn(&mut Parser)> {
121 let res = match node {
122 BLOCK_EXPR => expressions::block_expr,
123 RECORD_FIELD_LIST => items::record_field_list,
124 RECORD_EXPR_FIELD_LIST => items::record_expr_field_list,
125 VARIANT_LIST => items::variant_list,
126 MATCH_ARM_LIST => items::match_arm_list,
127 USE_TREE_LIST => items::use_tree_list,
128 EXTERN_ITEM_LIST => items::extern_item_list,
129 TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
130 ASSOC_ITEM_LIST => match parent? {
131 IMPL => items::assoc_item_list,
132 TRAIT => items::assoc_item_list,
135 ITEM_LIST => items::item_list,
141 #[derive(Clone, Copy, PartialEq, Eq)]
148 fn is_block(self) -> bool {
149 self == BlockLike::Block
153 fn opt_visibility(p: &mut Parser) -> bool {
160 // test crate_visibility
161 // pub(crate) struct S;
162 // pub(self) struct S;
163 // pub(super) struct S;
165 // test pub_parens_typepath
166 // struct B(pub (super::A));
167 // struct B(pub (crate::A,));
168 T![crate] | T![self] | T![super] | T![ident] if p.nth(2) != T![:] => {
170 let path_m = p.start();
171 let path_segment_m = p.start();
172 let name_ref_m = p.start();
174 name_ref_m.complete(p, NAME_REF);
175 path_segment_m.complete(p, PATH_SEGMENT);
176 path_m.complete(p, PATH);
179 // test crate_visibility_in
180 // pub(in super::A) struct S;
181 // pub(in crate) struct S;
191 m.complete(p, VISIBILITY);
193 // test crate_keyword_vis
194 // crate fn main() { }
195 // struct S { crate field: u32 }
196 // struct T(crate u32);
198 // test crate_keyword_path
199 // fn foo() { crate::foo(); }
200 T![crate] if !p.nth_at(1, T![::]) => {
203 m.complete(p, VISIBILITY);
210 fn opt_rename(p: &mut Parser) {
217 m.complete(p, RENAME);
221 fn abi(p: &mut Parser) {
222 assert!(p.at(T![extern]));
226 abi.complete(p, ABI);
229 fn opt_ret_type(p: &mut Parser) -> bool {
233 types::type_no_bounds(p);
234 m.complete(p, RET_TYPE);
241 fn name_r(p: &mut Parser, recovery: TokenSet) {
247 p.err_recover("expected a name", recovery);
251 fn name(p: &mut Parser) {
252 name_r(p, TokenSet::EMPTY)
255 fn name_ref(p: &mut Parser) {
259 m.complete(p, NAME_REF);
261 p.err_and_bump("expected identifier");
265 fn name_ref_or_index(p: &mut Parser) {
266 assert!(p.at(IDENT) || p.at(INT_NUMBER));
269 m.complete(p, NAME_REF);
272 fn lifetime(p: &mut Parser) {
273 assert!(p.at(LIFETIME_IDENT));
275 p.bump(LIFETIME_IDENT);
276 m.complete(p, LIFETIME);
279 fn error_block(p: &mut Parser, message: &str) {
280 assert!(p.at(T!['{']));
284 expressions::expr_block_contents(p);
286 m.complete(p, ERROR);