]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/lib.rs
Rollup merge of #82401 - osa1:remove_redundant_macro, r=matthewjasper
[rust.git] / compiler / rustc_parse / src / lib.rs
1 //! The main parser interface.
2
3 #![feature(crate_visibility_modifier)]
4 #![feature(bindings_after_at)]
5 #![feature(iter_order_by)]
6 #![feature(or_patterns)]
7 #![feature(box_syntax)]
8 #![feature(box_patterns)]
9
10 use rustc_ast as ast;
11 use rustc_ast::token::{self, Nonterminal};
12 use rustc_ast::tokenstream::{self, CanSynthesizeMissingTokens, LazyTokenStream, TokenStream};
13 use rustc_ast::AstLike;
14 use rustc_ast_pretty::pprust;
15 use rustc_data_structures::sync::Lrc;
16 use rustc_errors::{Diagnostic, FatalError, Level, PResult};
17 use rustc_session::parse::ParseSess;
18 use rustc_span::{FileName, SourceFile, Span};
19
20 use std::path::Path;
21 use std::str;
22
23 use tracing::debug;
24
25 pub const MACRO_ARGUMENTS: Option<&str> = Some("macro arguments");
26
27 #[macro_use]
28 pub mod parser;
29 use parser::{emit_unclosed_delims, make_unclosed_delims_error, Parser};
30 pub mod lexer;
31 pub mod validate_attr;
32
33 // A bunch of utility functions of the form `parse_<thing>_from_<source>`
34 // where <thing> includes crate, expr, item, stmt, tts, and one that
35 // uses a HOF to parse anything, and <source> includes file and
36 // `source_str`.
37
38 /// A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
39 macro_rules! panictry_buffer {
40     ($handler:expr, $e:expr) => {{
41         use rustc_errors::FatalError;
42         use std::result::Result::{Err, Ok};
43         match $e {
44             Ok(e) => e,
45             Err(errs) => {
46                 for e in errs {
47                     $handler.emit_diagnostic(&e);
48                 }
49                 FatalError.raise()
50             }
51         }
52     }};
53 }
54
55 pub fn parse_crate_from_file<'a>(input: &Path, sess: &'a ParseSess) -> PResult<'a, ast::Crate> {
56     let mut parser = new_parser_from_file(sess, input, None);
57     parser.parse_crate_mod()
58 }
59
60 pub fn parse_crate_attrs_from_file<'a>(
61     input: &Path,
62     sess: &'a ParseSess,
63 ) -> PResult<'a, Vec<ast::Attribute>> {
64     let mut parser = new_parser_from_file(sess, input, None);
65     parser.parse_inner_attributes()
66 }
67
68 pub fn parse_crate_from_source_str(
69     name: FileName,
70     source: String,
71     sess: &ParseSess,
72 ) -> PResult<'_, ast::Crate> {
73     new_parser_from_source_str(sess, name, source).parse_crate_mod()
74 }
75
76 pub fn parse_crate_attrs_from_source_str(
77     name: FileName,
78     source: String,
79     sess: &ParseSess,
80 ) -> PResult<'_, Vec<ast::Attribute>> {
81     new_parser_from_source_str(sess, name, source).parse_inner_attributes()
82 }
83
84 pub fn parse_stream_from_source_str(
85     name: FileName,
86     source: String,
87     sess: &ParseSess,
88     override_span: Option<Span>,
89 ) -> TokenStream {
90     let (stream, mut errors) =
91         source_file_to_stream(sess, sess.source_map().new_source_file(name, source), override_span);
92     emit_unclosed_delims(&mut errors, &sess);
93     stream
94 }
95
96 /// Creates a new parser from a source string.
97 pub fn new_parser_from_source_str(sess: &ParseSess, name: FileName, source: String) -> Parser<'_> {
98     panictry_buffer!(&sess.span_diagnostic, maybe_new_parser_from_source_str(sess, name, source))
99 }
100
101 /// Creates a new parser from a source string. Returns any buffered errors from lexing the initial
102 /// token stream.
103 pub fn maybe_new_parser_from_source_str(
104     sess: &ParseSess,
105     name: FileName,
106     source: String,
107 ) -> Result<Parser<'_>, Vec<Diagnostic>> {
108     maybe_source_file_to_parser(sess, sess.source_map().new_source_file(name, source))
109 }
110
111 /// Creates a new parser, handling errors as appropriate if the file doesn't exist.
112 /// If a span is given, that is used on an error as the source of the problem.
113 pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, sp: Option<Span>) -> Parser<'a> {
114     source_file_to_parser(sess, file_to_source_file(sess, path, sp))
115 }
116
117 /// Given a `source_file` and config, returns a parser.
118 fn source_file_to_parser(sess: &ParseSess, source_file: Lrc<SourceFile>) -> Parser<'_> {
119     panictry_buffer!(&sess.span_diagnostic, maybe_source_file_to_parser(sess, source_file))
120 }
121
122 /// Given a `source_file` and config, return a parser. Returns any buffered errors from lexing the
123 /// initial token stream.
124 fn maybe_source_file_to_parser(
125     sess: &ParseSess,
126     source_file: Lrc<SourceFile>,
127 ) -> Result<Parser<'_>, Vec<Diagnostic>> {
128     let end_pos = source_file.end_pos;
129     let (stream, unclosed_delims) = maybe_file_to_stream(sess, source_file, None)?;
130     let mut parser = stream_to_parser(sess, stream, None);
131     parser.unclosed_delims = unclosed_delims;
132     if parser.token == token::Eof {
133         parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt());
134     }
135
136     Ok(parser)
137 }
138
139 // Base abstractions
140
141 /// Given a session and a path and an optional span (for error reporting),
142 /// add the path to the session's source_map and return the new source_file or
143 /// error when a file can't be read.
144 fn try_file_to_source_file(
145     sess: &ParseSess,
146     path: &Path,
147     spanopt: Option<Span>,
148 ) -> Result<Lrc<SourceFile>, Diagnostic> {
149     sess.source_map().load_file(path).map_err(|e| {
150         let msg = format!("couldn't read {}: {}", path.display(), e);
151         let mut diag = Diagnostic::new(Level::Fatal, &msg);
152         if let Some(sp) = spanopt {
153             diag.set_span(sp);
154         }
155         diag
156     })
157 }
158
159 /// Given a session and a path and an optional span (for error reporting),
160 /// adds the path to the session's `source_map` and returns the new `source_file`.
161 fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option<Span>) -> Lrc<SourceFile> {
162     match try_file_to_source_file(sess, path, spanopt) {
163         Ok(source_file) => source_file,
164         Err(d) => {
165             sess.span_diagnostic.emit_diagnostic(&d);
166             FatalError.raise();
167         }
168     }
169 }
170
171 /// Given a `source_file`, produces a sequence of token trees.
172 pub fn source_file_to_stream(
173     sess: &ParseSess,
174     source_file: Lrc<SourceFile>,
175     override_span: Option<Span>,
176 ) -> (TokenStream, Vec<lexer::UnmatchedBrace>) {
177     panictry_buffer!(&sess.span_diagnostic, maybe_file_to_stream(sess, source_file, override_span))
178 }
179
180 /// Given a source file, produces a sequence of token trees. Returns any buffered errors from
181 /// parsing the token stream.
182 pub fn maybe_file_to_stream(
183     sess: &ParseSess,
184     source_file: Lrc<SourceFile>,
185     override_span: Option<Span>,
186 ) -> Result<(TokenStream, Vec<lexer::UnmatchedBrace>), Vec<Diagnostic>> {
187     let src = source_file.src.as_ref().unwrap_or_else(|| {
188         sess.span_diagnostic
189             .bug(&format!("cannot lex `source_file` without source: {}", source_file.name));
190     });
191
192     let (token_trees, unmatched_braces) =
193         lexer::parse_token_trees(sess, src.as_str(), source_file.start_pos, override_span);
194
195     match token_trees {
196         Ok(stream) => Ok((stream, unmatched_braces)),
197         Err(err) => {
198             let mut buffer = Vec::with_capacity(1);
199             err.buffer(&mut buffer);
200             // Not using `emit_unclosed_delims` to use `db.buffer`
201             for unmatched in unmatched_braces {
202                 if let Some(err) = make_unclosed_delims_error(unmatched, &sess) {
203                     err.buffer(&mut buffer);
204                 }
205             }
206             Err(buffer)
207         }
208     }
209 }
210
211 /// Given a stream and the `ParseSess`, produces a parser.
212 pub fn stream_to_parser<'a>(
213     sess: &'a ParseSess,
214     stream: TokenStream,
215     subparser_name: Option<&'static str>,
216 ) -> Parser<'a> {
217     Parser::new(sess, stream, false, subparser_name)
218 }
219
220 /// Runs the given subparser `f` on the tokens of the given `attr`'s item.
221 pub fn parse_in<'a, T>(
222     sess: &'a ParseSess,
223     tts: TokenStream,
224     name: &'static str,
225     mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
226 ) -> PResult<'a, T> {
227     let mut parser = Parser::new(sess, tts, false, Some(name));
228     let result = f(&mut parser)?;
229     if parser.token != token::Eof {
230         parser.unexpected()?;
231     }
232     Ok(result)
233 }
234
235 // NOTE(Centril): The following probably shouldn't be here but it acknowledges the
236 // fact that architecturally, we are using parsing (read on below to understand why).
237
238 pub fn nt_to_tokenstream(
239     nt: &Nonterminal,
240     sess: &ParseSess,
241     synthesize_tokens: CanSynthesizeMissingTokens,
242 ) -> TokenStream {
243     // A `Nonterminal` is often a parsed AST item. At this point we now
244     // need to convert the parsed AST to an actual token stream, e.g.
245     // un-parse it basically.
246     //
247     // Unfortunately there's not really a great way to do that in a
248     // guaranteed lossless fashion right now. The fallback here is to just
249     // stringify the AST node and reparse it, but this loses all span
250     // information.
251     //
252     // As a result, some AST nodes are annotated with the token stream they
253     // came from. Here we attempt to extract these lossless token streams
254     // before we fall back to the stringification.
255
256     let convert_tokens =
257         |tokens: Option<&LazyTokenStream>| tokens.as_ref().map(|t| t.create_token_stream());
258
259     let tokens = match *nt {
260         Nonterminal::NtItem(ref item) => prepend_attrs(sess, &item.attrs, nt, item.tokens.as_ref()),
261         Nonterminal::NtBlock(ref block) => convert_tokens(block.tokens.as_ref()),
262         Nonterminal::NtStmt(ref stmt) => {
263             let do_prepend = |tokens| prepend_attrs(sess, stmt.attrs(), nt, tokens);
264             if let ast::StmtKind::Empty = stmt.kind {
265                 let tokens: TokenStream =
266                     tokenstream::TokenTree::token(token::Semi, stmt.span).into();
267                 do_prepend(Some(&LazyTokenStream::new(tokens)))
268             } else {
269                 do_prepend(stmt.tokens())
270             }
271         }
272         Nonterminal::NtPat(ref pat) => convert_tokens(pat.tokens.as_ref()),
273         Nonterminal::NtTy(ref ty) => convert_tokens(ty.tokens.as_ref()),
274         Nonterminal::NtIdent(ident, is_raw) => {
275             Some(tokenstream::TokenTree::token(token::Ident(ident.name, is_raw), ident.span).into())
276         }
277         Nonterminal::NtLifetime(ident) => {
278             Some(tokenstream::TokenTree::token(token::Lifetime(ident.name), ident.span).into())
279         }
280         Nonterminal::NtMeta(ref attr) => convert_tokens(attr.tokens.as_ref()),
281         Nonterminal::NtPath(ref path) => convert_tokens(path.tokens.as_ref()),
282         Nonterminal::NtVis(ref vis) => convert_tokens(vis.tokens.as_ref()),
283         Nonterminal::NtTT(ref tt) => Some(tt.clone().into()),
284         Nonterminal::NtExpr(ref expr) | Nonterminal::NtLiteral(ref expr) => {
285             if expr.tokens.is_none() {
286                 debug!("missing tokens for expr {:?}", expr);
287             }
288             prepend_attrs(sess, &expr.attrs, nt, expr.tokens.as_ref())
289         }
290     };
291
292     if let Some(tokens) = tokens {
293         return tokens;
294     } else if matches!(synthesize_tokens, CanSynthesizeMissingTokens::Yes) {
295         return fake_token_stream(sess, nt);
296     } else {
297         panic!("Missing tokens for nt at {:?}: {:?}", nt.span(), pprust::nonterminal_to_string(nt));
298     }
299 }
300
301 pub fn fake_token_stream(sess: &ParseSess, nt: &Nonterminal) -> TokenStream {
302     let source = pprust::nonterminal_to_string(nt);
303     let filename = FileName::macro_expansion_source_code(&source);
304     parse_stream_from_source_str(filename, source, sess, Some(nt.span()))
305 }
306
307 fn prepend_attrs(
308     sess: &ParseSess,
309     attrs: &[ast::Attribute],
310     nt: &Nonterminal,
311     tokens: Option<&tokenstream::LazyTokenStream>,
312 ) -> Option<tokenstream::TokenStream> {
313     if attrs.is_empty() {
314         return Some(tokens?.create_token_stream());
315     }
316     let mut builder = tokenstream::TokenStreamBuilder::new();
317     for attr in attrs {
318         // FIXME: Correctly handle tokens for inner attributes.
319         // For now, we fall back to reparsing the original AST node
320         if attr.style == ast::AttrStyle::Inner {
321             return Some(fake_token_stream(sess, nt));
322         }
323         builder.push(attr.tokens());
324     }
325     builder.push(tokens?.create_token_stream());
326     Some(builder.build())
327 }