forked from rust-lang/rustfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.rs
347 lines (304 loc) · 10.6 KB
/
parser.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::borrow::Cow;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::path::{Path, PathBuf};
use rustc_errors::{Diagnostic, PResult};
use rustc_parse::{new_sub_parser_from_file, parser::Parser as RawParser};
use rustc_span::{symbol::kw, Span, DUMMY_SP};
use syntax::ast;
use syntax::token::{DelimToken, TokenKind};
use crate::syntux::session::ParseSess;
use crate::{Config, Input};
pub(crate) type DirectoryOwnership = rustc_parse::DirectoryOwnership;
pub(crate) type ModulePathSuccess = rustc_parse::parser::ModulePathSuccess;
#[derive(Clone)]
pub(crate) struct Directory {
pub(crate) path: PathBuf,
pub(crate) ownership: DirectoryOwnership,
}
impl<'a> Directory {
fn to_syntax_directory(&'a self) -> rustc_parse::Directory<'a> {
rustc_parse::Directory {
path: Cow::Borrowed(&self.path),
ownership: self.ownership,
}
}
}
/// A parser for Rust source code.
pub(crate) struct Parser<'a> {
parser: RawParser<'a>,
sess: &'a ParseSess,
}
/// A builder for the `Parser`.
#[derive(Default)]
pub(crate) struct ParserBuilder<'a> {
config: Option<&'a Config>,
sess: Option<&'a ParseSess>,
input: Option<Input>,
directory_ownership: Option<DirectoryOwnership>,
}
impl<'a> ParserBuilder<'a> {
pub(crate) fn input(mut self, input: Input) -> ParserBuilder<'a> {
self.input = Some(input);
self
}
pub(crate) fn sess(mut self, sess: &'a ParseSess) -> ParserBuilder<'a> {
self.sess = Some(sess);
self
}
pub(crate) fn config(mut self, config: &'a Config) -> ParserBuilder<'a> {
self.config = Some(config);
self
}
pub(crate) fn directory_ownership(
mut self,
directory_ownership: Option<DirectoryOwnership>,
) -> ParserBuilder<'a> {
self.directory_ownership = directory_ownership;
self
}
pub(crate) fn build(self) -> Result<Parser<'a>, ParserError> {
let config = self.config.ok_or(ParserError::NoConfig)?;
let sess = self.sess.ok_or(ParserError::NoParseSess)?;
let input = self.input.ok_or(ParserError::NoInput)?;
let mut parser = match Self::parser(sess.inner(), input, self.directory_ownership) {
Ok(p) => p,
Err(db) => {
sess.emit_diagnostics(db);
return Err(ParserError::ParserCreationError);
}
};
parser.cfg_mods = false;
parser.recurse_into_file_modules = config.recursive();
Ok(Parser { parser, sess })
}
fn parser(
sess: &'a rustc_session::parse::ParseSess,
input: Input,
directory_ownership: Option<DirectoryOwnership>,
) -> Result<rustc_parse::parser::Parser<'a>, Vec<Diagnostic>> {
match input {
Input::File(ref file) => Ok(if let Some(directory_ownership) = directory_ownership {
rustc_parse::new_sub_parser_from_file(
sess,
file,
directory_ownership,
None,
DUMMY_SP,
)
} else {
rustc_parse::new_parser_from_file(sess, file)
}),
Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
sess,
rustc_span::FileName::Custom("stdin".to_owned()),
text,
)
.map(|mut parser| {
parser.recurse_into_file_modules = false;
parser
}),
}
}
}
#[derive(Debug, PartialEq)]
pub(crate) enum ParserError {
NoConfig,
NoParseSess,
NoInput,
ParserCreationError,
ParseError,
ParsePanicError,
}
impl<'a> Parser<'a> {
pub(crate) fn submod_path_from_attr(attrs: &[ast::Attribute], path: &Path) -> Option<PathBuf> {
rustc_parse::parser::Parser::submod_path_from_attr(attrs, path)
}
// FIXME(topecongiro) Use the method from libsyntax[1] once it become public.
//
// [1] https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/attr.rs
fn parse_inner_attrs(parser: &mut RawParser<'a>) -> PResult<'a, Vec<ast::Attribute>> {
let mut attrs: Vec<ast::Attribute> = vec![];
loop {
match parser.token.kind {
TokenKind::Pound => {
// Don't even try to parse if it's not an inner attribute.
if !parser.look_ahead(1, |t| t == &TokenKind::Not) {
break;
}
let attr = parser.parse_attribute(true)?;
assert_eq!(attr.style, ast::AttrStyle::Inner);
attrs.push(attr);
}
TokenKind::DocComment(s) => {
// we need to get the position of this token before we bump.
let attr = syntax::attr::mk_doc_comment(
syntax::util::comments::doc_comment_style(&s.as_str()),
s,
parser.token.span,
);
if attr.style == ast::AttrStyle::Inner {
attrs.push(attr);
parser.bump();
} else {
break;
}
}
_ => break,
}
}
Ok(attrs)
}
fn parse_mod_items(parser: &mut RawParser<'a>, span: Span) -> PResult<'a, ast::Mod> {
let mut items = vec![];
while let Some(item) = parser.parse_item()? {
items.push(item);
}
let hi = if parser.token.span.is_dummy() {
span
} else {
parser.prev_span
};
Ok(ast::Mod {
inner: span.to(hi),
items,
inline: false,
})
}
pub(crate) fn parse_file_as_module(
directory_ownership: DirectoryOwnership,
sess: &'a ParseSess,
path: &Path,
) -> Option<ast::Mod> {
let result = catch_unwind(AssertUnwindSafe(|| {
let mut parser =
new_sub_parser_from_file(sess.inner(), &path, directory_ownership, None, DUMMY_SP);
parser.cfg_mods = false;
let lo = parser.token.span;
// FIXME(topecongiro) Format inner attributes (#3606).
match Parser::parse_inner_attrs(&mut parser) {
Ok(_attrs) => (),
Err(mut e) => {
e.cancel();
sess.reset_errors();
return None;
}
}
match Parser::parse_mod_items(&mut parser, lo) {
Ok(m) => Some(m),
Err(mut db) => {
db.cancel();
sess.reset_errors();
None
}
}
}));
match result {
Ok(Some(m)) => Some(m),
_ => None,
}
}
pub(crate) fn parse_crate(
config: &'a Config,
input: Input,
directory_ownership: Option<DirectoryOwnership>,
sess: &'a ParseSess,
) -> Result<ast::Crate, ParserError> {
let mut parser = ParserBuilder::default()
.config(config)
.input(input)
.directory_ownership(directory_ownership)
.sess(sess)
.build()?;
parser.parse_crate_inner()
}
fn parse_crate_inner(&mut self) -> Result<ast::Crate, ParserError> {
let mut parser = AssertUnwindSafe(&mut self.parser);
match catch_unwind(move || parser.parse_crate_mod()) {
Ok(Ok(krate)) => {
if !self.sess.has_errors() {
return Ok(krate);
}
if self.sess.can_reset_errors() {
self.sess.reset_errors();
return Ok(krate);
}
Err(ParserError::ParseError)
}
Ok(Err(mut db)) => {
db.emit();
Err(ParserError::ParseError)
}
Err(_) => Err(ParserError::ParsePanicError),
}
}
pub(crate) fn parse_cfg_if(
sess: &'a ParseSess,
mac: &'a ast::Mac,
base_dir: &Directory,
) -> Result<Vec<ast::Item>, &'static str> {
match catch_unwind(AssertUnwindSafe(|| {
Parser::parse_cfg_if_inner(sess, mac, base_dir)
})) {
Ok(Ok(items)) => Ok(items),
Ok(err @ Err(_)) => err,
Err(..) => Err("failed to parse cfg_if!"),
}
}
fn parse_cfg_if_inner(
sess: &'a ParseSess,
mac: &'a ast::Mac,
base_dir: &Directory,
) -> Result<Vec<ast::Item>, &'static str> {
let token_stream = mac.args.inner_tokens();
let mut parser = rustc_parse::stream_to_parser_with_base_dir(
sess.inner(),
token_stream,
base_dir.to_syntax_directory(),
);
parser.cfg_mods = false;
let mut items = vec![];
let mut process_if_cfg = true;
while parser.token.kind != TokenKind::Eof {
if process_if_cfg {
if !parser.eat_keyword(kw::If) {
return Err("Expected `if`");
}
parser
.parse_attribute(false)
.map_err(|_| "Failed to parse attributes")?;
}
if !parser.eat(&TokenKind::OpenDelim(DelimToken::Brace)) {
return Err("Expected an opening brace");
}
while parser.token != TokenKind::CloseDelim(DelimToken::Brace)
&& parser.token.kind != TokenKind::Eof
{
let item = match parser.parse_item() {
Ok(Some(item_ptr)) => item_ptr.into_inner(),
Ok(None) => continue,
Err(mut err) => {
err.cancel();
parser.sess.span_diagnostic.reset_err_count();
return Err(
"Expected item inside cfg_if block, but failed to parse it as an item",
);
}
};
if let ast::ItemKind::Mod(..) = item.kind {
items.push(item);
}
}
if !parser.eat(&TokenKind::CloseDelim(DelimToken::Brace)) {
return Err("Expected a closing brace");
}
if parser.eat(&TokenKind::Eof) {
break;
}
if !parser.eat_keyword(kw::Else) {
return Err("Expected `else`");
}
process_if_cfg = parser.token.is_keyword(kw::If);
}
Ok(items)
}
}