diff options
author | Aleksey Kladov <[email protected]> | 2019-02-20 12:47:32 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-20 12:47:32 +0000 |
commit | 5222b8aba3b1c2c68706aacf6869423a8e4fe6d5 (patch) | |
tree | c8a6e999b8ac5f1f29bde86a2e0b3a53466bb369 /crates/ra_syntax/src/parsing/grammar.rs | |
parent | 9d0cda4bc84350961f3884e75a1c20e62c449ede (diff) |
move all parsing related bits to a separate module
Diffstat (limited to 'crates/ra_syntax/src/parsing/grammar.rs')
-rw-r--r-- | crates/ra_syntax/src/parsing/grammar.rs | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/parsing/grammar.rs b/crates/ra_syntax/src/parsing/grammar.rs new file mode 100644 index 000000000..bf86443de --- /dev/null +++ b/crates/ra_syntax/src/parsing/grammar.rs | |||
@@ -0,0 +1,186 @@ | |||
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 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)`. | ||
8 | //! | ||
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. | ||
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 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. | ||
24 | //! | ||
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. | ||
29 | mod attributes; | ||
30 | mod expressions; | ||
31 | mod items; | ||
32 | mod params; | ||
33 | mod paths; | ||
34 | mod patterns; | ||
35 | mod type_args; | ||
36 | mod type_params; | ||
37 | mod types; | ||
38 | |||
39 | pub(crate) use self::{ | ||
40 | expressions::block, | ||
41 | items::{ | ||
42 | enum_variant_list, extern_item_list, impl_item_list, match_arm_list, mod_item_list, | ||
43 | named_field_def_list, named_field_list, token_tree, trait_item_list, use_tree_list, | ||
44 | }, | ||
45 | }; | ||
46 | use crate::{ | ||
47 | SyntaxKind::{self, *}, | ||
48 | parsing::{ | ||
49 | token_set::TokenSet, | ||
50 | parser_api::{CompletedMarker, Marker, Parser} | ||
51 | }, | ||
52 | }; | ||
53 | |||
54 | pub(crate) fn root(p: &mut Parser) { | ||
55 | let m = p.start(); | ||
56 | p.eat(SHEBANG); | ||
57 | items::mod_contents(p, false); | ||
58 | m.complete(p, SOURCE_FILE); | ||
59 | } | ||
60 | |||
61 | #[derive(Clone, Copy, PartialEq, Eq)] | ||
62 | enum BlockLike { | ||
63 | Block, | ||
64 | NotBlock, | ||
65 | } | ||
66 | |||
67 | impl BlockLike { | ||
68 | fn is_block(self) -> bool { | ||
69 | self == BlockLike::Block | ||
70 | } | ||
71 | } | ||
72 | |||
73 | fn opt_visibility(p: &mut Parser) { | ||
74 | match p.current() { | ||
75 | PUB_KW => { | ||
76 | let m = p.start(); | ||
77 | p.bump(); | ||
78 | if p.at(L_PAREN) { | ||
79 | match p.nth(1) { | ||
80 | // test crate_visibility | ||
81 | // pub(crate) struct S; | ||
82 | // pub(self) struct S; | ||
83 | // pub(self) struct S; | ||
84 | // pub(self) struct S; | ||
85 | CRATE_KW | SELF_KW | SUPER_KW => { | ||
86 | p.bump(); | ||
87 | p.bump(); | ||
88 | p.expect(R_PAREN); | ||
89 | } | ||
90 | IN_KW => { | ||
91 | p.bump(); | ||
92 | p.bump(); | ||
93 | paths::use_path(p); | ||
94 | p.expect(R_PAREN); | ||
95 | } | ||
96 | _ => (), | ||
97 | } | ||
98 | } | ||
99 | m.complete(p, VISIBILITY); | ||
100 | } | ||
101 | // test crate_keyword_vis | ||
102 | // crate fn main() { } | ||
103 | CRATE_KW => { | ||
104 | let m = p.start(); | ||
105 | p.bump(); | ||
106 | m.complete(p, VISIBILITY); | ||
107 | } | ||
108 | _ => (), | ||
109 | } | ||
110 | } | ||
111 | |||
112 | fn opt_alias(p: &mut Parser) { | ||
113 | if p.at(AS_KW) { | ||
114 | let m = p.start(); | ||
115 | p.bump(); | ||
116 | name(p); | ||
117 | m.complete(p, ALIAS); | ||
118 | } | ||
119 | } | ||
120 | |||
121 | fn abi(p: &mut Parser) { | ||
122 | assert!(p.at(EXTERN_KW)); | ||
123 | let abi = p.start(); | ||
124 | p.bump(); | ||
125 | match p.current() { | ||
126 | STRING | RAW_STRING => p.bump(), | ||
127 | _ => (), | ||
128 | } | ||
129 | abi.complete(p, ABI); | ||
130 | } | ||
131 | |||
132 | fn opt_fn_ret_type(p: &mut Parser) -> bool { | ||
133 | if p.at(THIN_ARROW) { | ||
134 | let m = p.start(); | ||
135 | p.bump(); | ||
136 | types::type_(p); | ||
137 | m.complete(p, RET_TYPE); | ||
138 | true | ||
139 | } else { | ||
140 | false | ||
141 | } | ||
142 | } | ||
143 | |||
144 | fn name_r(p: &mut Parser, recovery: TokenSet) { | ||
145 | if p.at(IDENT) { | ||
146 | let m = p.start(); | ||
147 | p.bump(); | ||
148 | m.complete(p, NAME); | ||
149 | } else { | ||
150 | p.err_recover("expected a name", recovery); | ||
151 | } | ||
152 | } | ||
153 | |||
154 | fn name(p: &mut Parser) { | ||
155 | name_r(p, TokenSet::empty()) | ||
156 | } | ||
157 | |||
158 | fn name_ref(p: &mut Parser) { | ||
159 | if p.at(IDENT) { | ||
160 | let m = p.start(); | ||
161 | p.bump(); | ||
162 | m.complete(p, NAME_REF); | ||
163 | } else { | ||
164 | p.err_and_bump("expected identifier"); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | fn error_block(p: &mut Parser, message: &str) { | ||
169 | go(p, Some(message)); | ||
170 | fn go(p: &mut Parser, message: Option<&str>) { | ||
171 | assert!(p.at(L_CURLY)); | ||
172 | let m = p.start(); | ||
173 | if let Some(message) = message { | ||
174 | p.error(message); | ||
175 | } | ||
176 | p.bump(); | ||
177 | while !p.at(EOF) && !p.at(R_CURLY) { | ||
178 | match p.current() { | ||
179 | L_CURLY => go(p, None), | ||
180 | _ => p.bump(), | ||
181 | } | ||
182 | } | ||
183 | p.eat(R_CURLY); | ||
184 | m.complete(p, ERROR); | ||
185 | } | ||
186 | } | ||