]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/src/syntux/parser.rs
Auto merge of #90361 - Mark-Simulacrum:always-verify, r=michaelwoerister
[rust.git] / src / tools / rustfmt / src / syntux / parser.rs
1 use std::panic::{catch_unwind, AssertUnwindSafe};
2 use std::path::{Path, PathBuf};
3
4 use rustc_ast::token::{DelimToken, TokenKind};
5 use rustc_ast::{ast, ptr};
6 use rustc_errors::Diagnostic;
7 use rustc_parse::{
8     new_parser_from_file,
9     parser::{ForceCollect, Parser as RawParser},
10 };
11 use rustc_span::{sym, symbol::kw, Span};
12
13 use crate::attr::first_attr_value_str_by_name;
14 use crate::syntux::session::ParseSess;
15 use crate::Input;
16
17 pub(crate) type DirectoryOwnership = rustc_expand::module::DirOwnership;
18 pub(crate) type ModulePathSuccess = rustc_expand::module::ModulePathSuccess;
19 pub(crate) type ModError<'a> = rustc_expand::module::ModError<'a>;
20
21 #[derive(Clone)]
22 pub(crate) struct Directory {
23     pub(crate) path: PathBuf,
24     pub(crate) ownership: DirectoryOwnership,
25 }
26
27 /// A parser for Rust source code.
28 pub(crate) struct Parser<'a> {
29     parser: RawParser<'a>,
30 }
31
32 /// A builder for the `Parser`.
33 #[derive(Default)]
34 pub(crate) struct ParserBuilder<'a> {
35     sess: Option<&'a ParseSess>,
36     input: Option<Input>,
37 }
38
39 impl<'a> ParserBuilder<'a> {
40     pub(crate) fn input(mut self, input: Input) -> ParserBuilder<'a> {
41         self.input = Some(input);
42         self
43     }
44
45     pub(crate) fn sess(mut self, sess: &'a ParseSess) -> ParserBuilder<'a> {
46         self.sess = Some(sess);
47         self
48     }
49
50     pub(crate) fn build(self) -> Result<Parser<'a>, ParserError> {
51         let sess = self.sess.ok_or(ParserError::NoParseSess)?;
52         let input = self.input.ok_or(ParserError::NoInput)?;
53
54         let parser = match Self::parser(sess.inner(), input) {
55             Ok(p) => p,
56             Err(db) => {
57                 if let Some(diagnostics) = db {
58                     sess.emit_diagnostics(diagnostics);
59                     return Err(ParserError::ParserCreationError);
60                 }
61                 return Err(ParserError::ParsePanicError);
62             }
63         };
64
65         Ok(Parser { parser })
66     }
67
68     fn parser(
69         sess: &'a rustc_session::parse::ParseSess,
70         input: Input,
71     ) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diagnostic>>> {
72         match input {
73             Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || {
74                 new_parser_from_file(sess, file, None)
75             }))
76             .map_err(|_| None),
77             Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
78                 sess,
79                 rustc_span::FileName::Custom("stdin".to_owned()),
80                 text,
81             )
82             .map_err(Some),
83         }
84     }
85 }
86
87 #[derive(Debug, PartialEq)]
88 pub(crate) enum ParserError {
89     NoParseSess,
90     NoInput,
91     ParserCreationError,
92     ParseError,
93     ParsePanicError,
94 }
95
96 impl<'a> Parser<'a> {
97     pub(crate) fn submod_path_from_attr(attrs: &[ast::Attribute], path: &Path) -> Option<PathBuf> {
98         let path_string = first_attr_value_str_by_name(attrs, sym::path)?.as_str();
99         // On windows, the base path might have the form
100         // `\\?\foo\bar` in which case it does not tolerate
101         // mixed `/` and `\` separators, so canonicalize
102         // `/` to `\`.
103         #[cfg(windows)]
104         let path_string = path_string.replace("/", "\\");
105
106         Some(path.join(&*path_string))
107     }
108
109     pub(crate) fn parse_file_as_module(
110         sess: &'a ParseSess,
111         path: &Path,
112         span: Span,
113     ) -> Result<(Vec<ast::Attribute>, Vec<ptr::P<ast::Item>>, Span), ParserError> {
114         let result = catch_unwind(AssertUnwindSafe(|| {
115             let mut parser = new_parser_from_file(sess.inner(), path, Some(span));
116             match parser.parse_mod(&TokenKind::Eof) {
117                 Ok(result) => Some(result),
118                 Err(mut e) => {
119                     sess.emit_or_cancel_diagnostic(&mut e);
120                     if sess.can_reset_errors() {
121                         sess.reset_errors();
122                     }
123                     None
124                 }
125             }
126         }));
127         match result {
128             Ok(Some(m)) if !sess.has_errors() => Ok(m),
129             Ok(Some(m)) if sess.can_reset_errors() => {
130                 sess.reset_errors();
131                 Ok(m)
132             }
133             Ok(_) => Err(ParserError::ParseError),
134             Err(..) if path.exists() => Err(ParserError::ParseError),
135             Err(_) => Err(ParserError::ParsePanicError),
136         }
137     }
138
139     pub(crate) fn parse_crate(
140         input: Input,
141         sess: &'a ParseSess,
142     ) -> Result<ast::Crate, ParserError> {
143         let krate = Parser::parse_crate_inner(input, sess)?;
144         if !sess.has_errors() {
145             return Ok(krate);
146         }
147
148         if sess.can_reset_errors() {
149             sess.reset_errors();
150             return Ok(krate);
151         }
152
153         Err(ParserError::ParseError)
154     }
155
156     fn parse_crate_inner(input: Input, sess: &'a ParseSess) -> Result<ast::Crate, ParserError> {
157         ParserBuilder::default()
158             .input(input)
159             .sess(sess)
160             .build()?
161             .parse_crate_mod()
162     }
163
164     fn parse_crate_mod(&mut self) -> Result<ast::Crate, ParserError> {
165         let mut parser = AssertUnwindSafe(&mut self.parser);
166
167         match catch_unwind(move || parser.parse_crate_mod()) {
168             Ok(Ok(k)) => Ok(k),
169             Ok(Err(mut db)) => {
170                 db.emit();
171                 Err(ParserError::ParseError)
172             }
173             Err(_) => Err(ParserError::ParsePanicError),
174         }
175     }
176
177     pub(crate) fn parse_cfg_if(
178         sess: &'a ParseSess,
179         mac: &'a ast::MacCall,
180     ) -> Result<Vec<ast::Item>, &'static str> {
181         match catch_unwind(AssertUnwindSafe(|| Parser::parse_cfg_if_inner(sess, mac))) {
182             Ok(Ok(items)) => Ok(items),
183             Ok(err @ Err(_)) => err,
184             Err(..) => Err("failed to parse cfg_if!"),
185         }
186     }
187
188     fn parse_cfg_if_inner(
189         sess: &'a ParseSess,
190         mac: &'a ast::MacCall,
191     ) -> Result<Vec<ast::Item>, &'static str> {
192         let token_stream = mac.args.inner_tokens();
193         let mut parser = rustc_parse::stream_to_parser(sess.inner(), token_stream, Some(""));
194
195         let mut items = vec![];
196         let mut process_if_cfg = true;
197
198         while parser.token.kind != TokenKind::Eof {
199             if process_if_cfg {
200                 if !parser.eat_keyword(kw::If) {
201                     return Err("Expected `if`");
202                 }
203                 // Inner attributes are not actually syntactically permitted here, but we don't
204                 // care about inner vs outer attributes in this position. Our purpose with this
205                 // special case parsing of cfg_if macros is to ensure we can correctly resolve
206                 // imported modules that may have a custom `path` defined.
207                 //
208                 // As such, we just need to advance the parser past the attribute and up to
209                 // to the opening brace.
210                 // See also https://github.com/rust-lang/rust/pull/79433
211                 parser
212                     .parse_attribute(rustc_parse::parser::attr::InnerAttrPolicy::Permitted)
213                     .map_err(|_| "Failed to parse attributes")?;
214             }
215
216             if !parser.eat(&TokenKind::OpenDelim(DelimToken::Brace)) {
217                 return Err("Expected an opening brace");
218             }
219
220             while parser.token != TokenKind::CloseDelim(DelimToken::Brace)
221                 && parser.token.kind != TokenKind::Eof
222             {
223                 let item = match parser.parse_item(ForceCollect::No) {
224                     Ok(Some(item_ptr)) => item_ptr.into_inner(),
225                     Ok(None) => continue,
226                     Err(mut err) => {
227                         err.cancel();
228                         parser.sess.span_diagnostic.reset_err_count();
229                         return Err(
230                             "Expected item inside cfg_if block, but failed to parse it as an item",
231                         );
232                     }
233                 };
234                 if let ast::ItemKind::Mod(..) = item.kind {
235                     items.push(item);
236                 }
237             }
238
239             if !parser.eat(&TokenKind::CloseDelim(DelimToken::Brace)) {
240                 return Err("Expected a closing brace");
241             }
242
243             if parser.eat(&TokenKind::Eof) {
244                 break;
245             }
246
247             if !parser.eat_keyword(kw::Else) {
248                 return Err("Expected `else`");
249             }
250
251             process_if_cfg = parser.token.is_keyword(kw::If);
252         }
253
254         Ok(items)
255     }
256 }