2019-02-08 13:53:55 +00:00
|
|
|
//! The main parser interface.
|
2012-11-29 00:20:41 +00:00
|
|
|
|
2019-05-10 23:31:34 +00:00
|
|
|
use crate::ast::{self, CrateConfig, NodeId};
|
2019-02-06 17:33:01 +00:00
|
|
|
use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
|
|
|
|
use crate::source_map::{SourceMap, FilePathMapping};
|
|
|
|
use crate::feature_gate::UnstableFeatures;
|
|
|
|
use crate::parse::parser::Parser;
|
2019-06-05 11:17:56 +00:00
|
|
|
use crate::parse::parser::emit_unclosed_delims;
|
|
|
|
use crate::parse::token::TokenKind;
|
2019-02-06 17:33:01 +00:00
|
|
|
use crate::tokenstream::{TokenStream, TokenTree};
|
2019-06-08 19:38:23 +00:00
|
|
|
use crate::print::pprust;
|
2019-07-18 19:29:07 +00:00
|
|
|
use crate::symbol::Symbol;
|
2019-02-06 17:33:01 +00:00
|
|
|
|
2019-05-02 23:13:28 +00:00
|
|
|
use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
|
2019-09-06 02:56:45 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
|
2019-07-18 19:29:07 +00:00
|
|
|
use rustc_data_structures::sync::{Lrc, Lock, Once};
|
2018-08-18 10:13:52 +00:00
|
|
|
use syntax_pos::{Span, SourceFile, FileName, MultiSpan};
|
2019-04-05 22:15:49 +00:00
|
|
|
use syntax_pos::edition::Edition;
|
2019-08-13 19:48:27 +00:00
|
|
|
use syntax_pos::hygiene::ExpnId;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
2018-05-18 06:19:35 +00:00
|
|
|
use std::borrow::Cow;
|
2015-02-27 05:00:43 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2013-10-14 01:48:47 +00:00
|
|
|
use std::str;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2019-08-01 21:26:40 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2015-03-28 21:58:51 +00:00
|
|
|
|
2015-01-06 17:24:46 +00:00
|
|
|
#[macro_use]
|
2013-01-09 03:37:25 +00:00
|
|
|
pub mod parser;
|
2019-05-10 23:31:34 +00:00
|
|
|
pub mod attr;
|
2014-12-19 04:09:57 +00:00
|
|
|
pub mod lexer;
|
2013-01-09 03:37:25 +00:00
|
|
|
pub mod token;
|
2019-04-25 08:48:25 +00:00
|
|
|
|
2019-05-10 23:31:34 +00:00
|
|
|
crate mod classify;
|
|
|
|
crate mod diagnostics;
|
|
|
|
crate mod literal;
|
|
|
|
crate mod unescape_error_reporting;
|
2019-04-25 08:48:25 +00:00
|
|
|
|
2019-08-01 21:26:40 +00:00
|
|
|
pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
|
|
|
|
|
2019-08-20 15:07:42 +00:00
|
|
|
/// Collected spans during parsing for places where a certain feature was
|
|
|
|
/// used and should be feature gated accordingly in `check_crate`.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct GatedSpans {
|
|
|
|
/// Spans collected for gating `param_attrs`, e.g. `fn foo(#[attr] x: u8) {}`.
|
|
|
|
pub param_attrs: Lock<Vec<Span>>,
|
|
|
|
/// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
|
|
|
|
pub let_chains: Lock<Vec<Span>>,
|
|
|
|
/// Spans collected for gating `async_closure`, e.g. `async || ..`.
|
|
|
|
pub async_closure: Lock<Vec<Span>>,
|
|
|
|
/// Spans collected for gating `yield e?` expressions (`generators` gate).
|
|
|
|
pub yields: Lock<Vec<Span>>,
|
|
|
|
/// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
|
|
|
|
pub or_patterns: Lock<Vec<Span>>,
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Info about a parsing session.
|
2013-02-21 08:16:31 +00:00
|
|
|
pub struct ParseSess {
|
2017-01-13 04:49:20 +00:00
|
|
|
pub span_diagnostic: Handler,
|
2016-09-24 17:04:07 +00:00
|
|
|
pub unstable_features: UnstableFeatures,
|
2016-10-27 06:36:56 +00:00
|
|
|
pub config: CrateConfig,
|
2019-04-05 22:15:49 +00:00
|
|
|
pub edition: Edition,
|
2018-08-18 10:55:43 +00:00
|
|
|
pub missing_fragment_specifiers: Lock<FxHashSet<Span>>,
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Places where raw identifiers were used. This is used for feature-gating raw identifiers.
|
2018-02-15 09:52:26 +00:00
|
|
|
pub raw_identifier_spans: Lock<Vec<Span>>,
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Used to determine and report recursive module inclusions.
|
2018-02-15 09:52:26 +00:00
|
|
|
included_mod_stack: Lock<Vec<PathBuf>>,
|
2018-10-29 20:26:13 +00:00
|
|
|
source_map: Lrc<SourceMap>,
|
2018-07-12 01:54:12 +00:00
|
|
|
pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
|
2019-05-02 23:13:28 +00:00
|
|
|
/// Contains the spans of block expressions that could have been incomplete based on the
|
|
|
|
/// operation token that followed it, but that the parser cannot identify without further
|
|
|
|
/// analysis.
|
2019-05-06 23:00:21 +00:00
|
|
|
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
|
2019-07-18 19:29:07 +00:00
|
|
|
pub injected_crate_name: Once<Symbol>,
|
2019-08-20 15:07:42 +00:00
|
|
|
pub gated_spans: GatedSpans,
|
2013-02-21 08:16:31 +00:00
|
|
|
}
|
2012-11-29 00:20:41 +00:00
|
|
|
|
2015-05-13 20:00:17 +00:00
|
|
|
impl ParseSess {
|
2017-04-24 17:01:19 +00:00
|
|
|
pub fn new(file_path_mapping: FilePathMapping) -> Self {
|
2018-08-18 10:13:35 +00:00
|
|
|
let cm = Lrc::new(SourceMap::new(file_path_mapping));
|
2019-09-06 02:56:45 +00:00
|
|
|
let handler = Handler::with_tty_emitter(
|
|
|
|
ColorConfig::Auto,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
Some(cm.clone()),
|
|
|
|
);
|
2015-12-13 22:17:55 +00:00
|
|
|
ParseSess::with_span_handler(handler, cm)
|
2013-02-21 08:16:31 +00:00
|
|
|
}
|
2012-11-29 00:20:41 +00:00
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
|
|
|
|
Self {
|
2015-12-13 22:17:55 +00:00
|
|
|
span_diagnostic: handler,
|
2016-09-24 17:04:07 +00:00
|
|
|
unstable_features: UnstableFeatures::from_environment(),
|
2018-08-18 10:55:43 +00:00
|
|
|
config: FxHashSet::default(),
|
2019-09-06 02:56:45 +00:00
|
|
|
edition: ExpnId::root().expn_data().edition,
|
2018-08-18 10:55:43 +00:00
|
|
|
missing_fragment_specifiers: Lock::new(FxHashSet::default()),
|
2018-02-15 09:52:26 +00:00
|
|
|
raw_identifier_spans: Lock::new(Vec::new()),
|
|
|
|
included_mod_stack: Lock::new(vec![]),
|
2018-10-29 20:26:13 +00:00
|
|
|
source_map,
|
2018-07-12 01:54:12 +00:00
|
|
|
buffered_lints: Lock::new(vec![]),
|
2019-05-06 23:00:21 +00:00
|
|
|
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
|
2019-07-18 19:29:07 +00:00
|
|
|
injected_crate_name: Once::new(),
|
2019-08-20 15:07:42 +00:00
|
|
|
gated_spans: GatedSpans::default(),
|
2015-05-13 20:00:17 +00:00
|
|
|
}
|
2013-02-21 08:16:31 +00:00
|
|
|
}
|
2015-05-13 20:08:02 +00:00
|
|
|
|
2018-12-04 15:26:34 +00:00
|
|
|
#[inline]
|
2018-08-18 10:14:09 +00:00
|
|
|
pub fn source_map(&self) -> &SourceMap {
|
2018-10-29 20:26:13 +00:00
|
|
|
&self.source_map
|
2015-05-13 20:08:02 +00:00
|
|
|
}
|
2018-07-12 01:54:12 +00:00
|
|
|
|
|
|
|
pub fn buffer_lint<S: Into<MultiSpan>>(&self,
|
|
|
|
lint_id: BufferedEarlyLintId,
|
|
|
|
span: S,
|
|
|
|
id: NodeId,
|
|
|
|
msg: &str,
|
|
|
|
) {
|
2018-07-14 04:40:29 +00:00
|
|
|
self.buffered_lints.with_lock(|buffered_lints| {
|
|
|
|
buffered_lints.push(BufferedEarlyLint{
|
2018-07-12 01:54:12 +00:00
|
|
|
span: span.into(),
|
|
|
|
id,
|
|
|
|
msg: msg.into(),
|
|
|
|
lint_id,
|
|
|
|
});
|
2018-07-14 04:40:29 +00:00
|
|
|
});
|
2018-07-12 01:54:12 +00:00
|
|
|
}
|
2019-05-02 23:13:28 +00:00
|
|
|
|
|
|
|
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
|
|
|
|
/// parser to continue parsing the following operation as part of the same expression.
|
|
|
|
pub fn expr_parentheses_needed(
|
|
|
|
&self,
|
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
|
|
|
span: Span,
|
|
|
|
alt_snippet: Option<String>,
|
|
|
|
) {
|
|
|
|
if let Some(snippet) = self.source_map().span_to_snippet(span).ok().or(alt_snippet) {
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
"parentheses are required to parse this as an expression",
|
|
|
|
format!("({})", snippet),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
#[derive(Clone)]
|
2018-05-18 06:19:35 +00:00
|
|
|
pub struct Directory<'a> {
|
|
|
|
pub path: Cow<'a, Path>,
|
2016-11-05 04:16:26 +00:00
|
|
|
pub ownership: DirectoryOwnership,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum DirectoryOwnership {
|
2017-11-28 02:14:24 +00:00
|
|
|
Owned {
|
2019-09-06 02:56:45 +00:00
|
|
|
// None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
|
2017-11-28 02:14:24 +00:00
|
|
|
relative: Option<ast::Ident>,
|
|
|
|
},
|
2016-11-05 04:16:26 +00:00
|
|
|
UnownedViaBlock,
|
2016-11-14 09:31:03 +00:00
|
|
|
UnownedViaMod(bool /* legacy warnings? */),
|
2016-11-05 04:16:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
// A bunch of utility functions of the form `parse_<thing>_from_<source>`
|
2013-02-11 21:36:24 +00:00
|
|
|
// where <thing> includes crate, expr, item, stmt, tts, and one that
|
|
|
|
// uses a HOF to parse anything, and <source> includes file and
|
2019-09-06 02:56:45 +00:00
|
|
|
// `source_str`.
|
2013-02-11 21:36:24 +00:00
|
|
|
|
2016-10-27 06:36:56 +00:00
|
|
|
pub fn parse_crate_from_file<'a>(input: &Path, sess: &'a ParseSess) -> PResult<'a, ast::Crate> {
|
|
|
|
let mut parser = new_parser_from_file(sess, input);
|
2016-02-13 17:05:16 +00:00
|
|
|
parser.parse_crate_mod()
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 06:36:56 +00:00
|
|
|
pub fn parse_crate_attrs_from_file<'a>(input: &Path, sess: &'a ParseSess)
|
2016-02-13 17:05:16 +00:00
|
|
|
-> PResult<'a, Vec<ast::Attribute>> {
|
2016-10-27 06:36:56 +00:00
|
|
|
let mut parser = new_parser_from_file(sess, input);
|
2016-02-13 17:05:16 +00:00
|
|
|
parser.parse_inner_attributes()
|
2013-12-19 20:23:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 07:09:19 +00:00
|
|
|
pub fn parse_crate_from_source_str(name: FileName, source: String, sess: &ParseSess)
|
2019-02-06 17:33:01 +00:00
|
|
|
-> PResult<'_, ast::Crate> {
|
2016-10-27 06:36:56 +00:00
|
|
|
new_parser_from_source_str(sess, name, source).parse_crate_mod()
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 07:09:19 +00:00
|
|
|
pub fn parse_crate_attrs_from_source_str(name: FileName, source: String, sess: &ParseSess)
|
2019-02-06 17:33:01 +00:00
|
|
|
-> PResult<'_, Vec<ast::Attribute>> {
|
2016-10-27 06:36:56 +00:00
|
|
|
new_parser_from_source_str(sess, name, source).parse_inner_attributes()
|
2013-12-19 20:23:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 05:04:50 +00:00
|
|
|
pub fn parse_stream_from_source_str(
|
|
|
|
name: FileName,
|
|
|
|
source: String,
|
|
|
|
sess: &ParseSess,
|
|
|
|
override_span: Option<Span>,
|
2019-03-04 20:59:43 +00:00
|
|
|
) -> TokenStream {
|
|
|
|
let (stream, mut errors) = source_file_to_stream(
|
|
|
|
sess,
|
|
|
|
sess.source_map().new_source_file(name, source),
|
|
|
|
override_span,
|
|
|
|
);
|
|
|
|
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
|
|
|
|
stream
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Creates a new parser from a source string.
|
2019-01-28 05:04:50 +00:00
|
|
|
pub fn new_parser_from_source_str(sess: &ParseSess, name: FileName, source: String) -> Parser<'_> {
|
2018-11-01 21:01:38 +00:00
|
|
|
panictry_buffer!(&sess.span_diagnostic, maybe_new_parser_from_source_str(sess, name, source))
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Creates a new parser from a source string. Returns any buffered errors from lexing the initial
|
2018-11-01 16:57:29 +00:00
|
|
|
/// token stream.
|
|
|
|
pub fn maybe_new_parser_from_source_str(sess: &ParseSess, name: FileName, source: String)
|
2019-02-06 17:33:01 +00:00
|
|
|
-> Result<Parser<'_>, Vec<Diagnostic>>
|
2018-11-01 16:57:29 +00:00
|
|
|
{
|
|
|
|
let mut parser = maybe_source_file_to_parser(sess,
|
|
|
|
sess.source_map().new_source_file(name, source))?;
|
2017-05-17 22:37:24 +00:00
|
|
|
parser.recurse_into_file_modules = false;
|
2018-11-01 16:57:29 +00:00
|
|
|
Ok(parser)
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
/// Creates a new parser, handling errors as appropriate if the file doesn't exist.
|
2016-10-27 06:36:56 +00:00
|
|
|
pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path) -> Parser<'a> {
|
2018-08-18 10:13:56 +00:00
|
|
|
source_file_to_parser(sess, file_to_source_file(sess, path, None))
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
/// Creates a new parser, returning buffered diagnostics if the file doesn't exist,
|
|
|
|
/// or from lexing the initial token stream.
|
2018-12-09 21:53:00 +00:00
|
|
|
pub fn maybe_new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path)
|
|
|
|
-> Result<Parser<'a>, Vec<Diagnostic>> {
|
|
|
|
let file = try_file_to_source_file(sess, path, None).map_err(|db| vec![db])?;
|
|
|
|
maybe_source_file_to_parser(sess, file)
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:57:41 +00:00
|
|
|
/// Given a session, a crate config, a path, and a span, add
|
2019-09-06 02:56:45 +00:00
|
|
|
/// the file at the given path to the `source_map`, and returns a parser.
|
|
|
|
/// On an error, uses the given span as the source of the problem.
|
2019-03-09 12:59:54 +00:00
|
|
|
pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess,
|
2014-03-16 18:56:24 +00:00
|
|
|
path: &Path,
|
2016-11-05 04:16:26 +00:00
|
|
|
directory_ownership: DirectoryOwnership,
|
2014-05-22 23:57:53 +00:00
|
|
|
module_name: Option<String>,
|
2014-03-16 18:56:24 +00:00
|
|
|
sp: Span) -> Parser<'a> {
|
2018-08-18 10:13:56 +00:00
|
|
|
let mut p = source_file_to_parser(sess, file_to_source_file(sess, path, Some(sp)));
|
2016-11-05 04:16:26 +00:00
|
|
|
p.directory.ownership = directory_ownership;
|
2014-05-16 21:23:04 +00:00
|
|
|
p.root_module_name = module_name;
|
|
|
|
p
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
/// Given a `source_file` and config, returns a parser.
|
2019-02-06 17:33:01 +00:00
|
|
|
fn source_file_to_parser(sess: &ParseSess, source_file: Lrc<SourceFile>) -> Parser<'_> {
|
2018-11-01 21:01:38 +00:00
|
|
|
panictry_buffer!(&sess.span_diagnostic,
|
|
|
|
maybe_source_file_to_parser(sess, source_file))
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
/// Given a `source_file` and config, return a parser. Returns any buffered errors from lexing the
|
2018-11-01 16:57:29 +00:00
|
|
|
/// initial token stream.
|
2019-01-28 05:04:50 +00:00
|
|
|
fn maybe_source_file_to_parser(
|
|
|
|
sess: &ParseSess,
|
|
|
|
source_file: Lrc<SourceFile>,
|
|
|
|
) -> Result<Parser<'_>, Vec<Diagnostic>> {
|
2018-08-18 10:13:56 +00:00
|
|
|
let end_pos = source_file.end_pos;
|
2019-01-28 05:04:50 +00:00
|
|
|
let (stream, unclosed_delims) = maybe_file_to_stream(sess, source_file, None)?;
|
2019-05-22 00:47:23 +00:00
|
|
|
let mut parser = stream_to_parser(sess, stream, None);
|
2019-01-28 05:04:50 +00:00
|
|
|
parser.unclosed_delims = unclosed_delims;
|
2019-06-07 10:31:13 +00:00
|
|
|
if parser.token == token::Eof && parser.token.span.is_dummy() {
|
|
|
|
parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt());
|
2015-07-06 02:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 16:57:29 +00:00
|
|
|
Ok(parser)
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
// Must preserve old name for now, because `quote!` from the *existing*
|
|
|
|
// compiler expands into it.
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn new_parser_from_tts(sess: &ParseSess, tts: Vec<TokenTree>) -> Parser<'_> {
|
2019-05-22 05:17:53 +00:00
|
|
|
stream_to_parser(sess, tts.into_iter().collect(), crate::MACRO_ARGUMENTS)
|
2016-07-14 16:52:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 17:57:41 +00:00
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
// Base abstractions
|
2013-04-23 17:57:41 +00:00
|
|
|
|
2018-12-09 21:53:00 +00:00
|
|
|
/// Given a session and a path and an optional span (for error reporting),
|
|
|
|
/// add the path to the session's source_map and return the new source_file or
|
|
|
|
/// error when a file can't be read.
|
|
|
|
fn try_file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
|
|
|
|
-> Result<Lrc<SourceFile>, Diagnostic> {
|
|
|
|
sess.source_map().load_file(path)
|
|
|
|
.map_err(|e| {
|
|
|
|
let msg = format!("couldn't read {}: {}", path.display(), e);
|
|
|
|
let mut diag = Diagnostic::new(Level::Fatal, &msg);
|
|
|
|
if let Some(sp) = spanopt {
|
|
|
|
diag.set_span(sp);
|
|
|
|
}
|
|
|
|
diag
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:57:41 +00:00
|
|
|
/// Given a session and a path and an optional span (for error reporting),
|
2019-09-06 02:56:45 +00:00
|
|
|
/// adds the path to the session's `source_map` and returns the new `source_file`.
|
2018-08-18 10:13:56 +00:00
|
|
|
fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
|
2018-08-18 10:13:52 +00:00
|
|
|
-> Lrc<SourceFile> {
|
2018-12-09 21:53:00 +00:00
|
|
|
match try_file_to_source_file(sess, path, spanopt) {
|
2018-08-18 10:13:56 +00:00
|
|
|
Ok(source_file) => source_file,
|
2018-12-09 21:53:00 +00:00
|
|
|
Err(d) => {
|
|
|
|
DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, d).emit();
|
|
|
|
FatalError.raise();
|
2014-05-16 17:45:16 +00:00
|
|
|
}
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
/// Given a `source_file`, produces a sequence of token trees.
|
2019-01-28 05:04:50 +00:00
|
|
|
pub fn source_file_to_stream(
|
|
|
|
sess: &ParseSess,
|
|
|
|
source_file: Lrc<SourceFile>,
|
|
|
|
override_span: Option<Span>,
|
|
|
|
) -> (TokenStream, Vec<lexer::UnmatchedBrace>) {
|
2018-11-01 21:01:38 +00:00
|
|
|
panictry_buffer!(&sess.span_diagnostic, maybe_file_to_stream(sess, source_file, override_span))
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
|
2019-05-12 16:55:16 +00:00
|
|
|
/// parsing the token stream.
|
2019-01-28 05:04:50 +00:00
|
|
|
pub fn maybe_file_to_stream(
|
|
|
|
sess: &ParseSess,
|
|
|
|
source_file: Lrc<SourceFile>,
|
|
|
|
override_span: Option<Span>,
|
|
|
|
) -> Result<(TokenStream, Vec<lexer::UnmatchedBrace>), Vec<Diagnostic>> {
|
2019-07-03 10:30:12 +00:00
|
|
|
let srdr = lexer::StringReader::new(sess, source_file, override_span);
|
2019-05-12 16:55:16 +00:00
|
|
|
let (token_trees, unmatched_braces) = srdr.into_token_trees();
|
2018-11-01 16:57:29 +00:00
|
|
|
|
2019-05-12 16:55:16 +00:00
|
|
|
match token_trees {
|
|
|
|
Ok(stream) => Ok((stream, unmatched_braces)),
|
2018-11-01 16:57:29 +00:00
|
|
|
Err(err) => {
|
|
|
|
let mut buffer = Vec::with_capacity(1);
|
|
|
|
err.buffer(&mut buffer);
|
2019-02-05 09:35:25 +00:00
|
|
|
// Not using `emit_unclosed_delims` to use `db.buffer`
|
2019-05-12 16:55:16 +00:00
|
|
|
for unmatched in unmatched_braces {
|
2019-01-28 05:04:50 +00:00
|
|
|
let mut db = sess.span_diagnostic.struct_span_err(unmatched.found_span, &format!(
|
|
|
|
"incorrect close delimiter: `{}`",
|
2019-06-08 19:38:23 +00:00
|
|
|
pprust::token_kind_to_string(&token::CloseDelim(unmatched.found_delim)),
|
2019-01-28 05:04:50 +00:00
|
|
|
));
|
|
|
|
db.span_label(unmatched.found_span, "incorrect close delimiter");
|
|
|
|
if let Some(sp) = unmatched.candidate_span {
|
|
|
|
db.span_label(sp, "close delimiter possibly meant for this");
|
|
|
|
}
|
|
|
|
if let Some(sp) = unmatched.unclosed_span {
|
|
|
|
db.span_label(sp, "un-closed delimiter");
|
|
|
|
}
|
|
|
|
db.buffer(&mut buffer);
|
|
|
|
}
|
2018-11-01 16:57:29 +00:00
|
|
|
Err(buffer)
|
|
|
|
}
|
|
|
|
}
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
/// Given a stream and the `ParseSess`, produces a parser.
|
2019-05-22 00:47:23 +00:00
|
|
|
pub fn stream_to_parser<'a>(
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
stream: TokenStream,
|
2019-05-22 05:17:53 +00:00
|
|
|
subparser_name: Option<&'static str>,
|
2019-05-22 00:47:23 +00:00
|
|
|
) -> Parser<'a> {
|
2019-05-22 05:17:53 +00:00
|
|
|
Parser::new(sess, stream, None, true, false, subparser_name)
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
2013-01-30 17:56:33 +00:00
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
/// Given a stream, the `ParseSess` and the base directory, produces a parser.
|
2019-05-21 13:57:34 +00:00
|
|
|
///
|
|
|
|
/// Use this function when you are creating a parser from the token stream
|
|
|
|
/// and also care about the current working directory of the parser (e.g.,
|
|
|
|
/// you are trying to resolve modules defined inside a macro invocation).
|
2019-05-21 14:17:59 +00:00
|
|
|
///
|
2019-05-21 13:57:34 +00:00
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// The main usage of this function is outside of rustc, for those who uses
|
|
|
|
/// libsyntax as a library. Please do not remove this function while refactoring
|
|
|
|
/// just because it is not used in rustc codebase!
|
2019-05-22 06:13:31 +00:00
|
|
|
pub fn stream_to_parser_with_base_dir<'a>(
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
stream: TokenStream,
|
|
|
|
base_dir: Directory<'a>,
|
|
|
|
) -> Parser<'a> {
|
|
|
|
Parser::new(sess, stream, Some(base_dir), true, false, None)
|
2019-05-21 04:18:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// A sequence separator.
|
2018-06-26 22:59:07 +00:00
|
|
|
pub struct SeqSep {
|
2019-08-01 08:13:31 +00:00
|
|
|
/// The separator token.
|
2019-06-05 11:17:56 +00:00
|
|
|
pub sep: Option<TokenKind>,
|
2019-02-08 13:53:55 +00:00
|
|
|
/// `true` if a trailing separator is allowed.
|
2018-06-26 22:59:07 +00:00
|
|
|
pub trailing_sep_allowed: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SeqSep {
|
2019-06-05 11:17:56 +00:00
|
|
|
pub fn trailing_allowed(t: TokenKind) -> SeqSep {
|
2018-06-26 22:59:07 +00:00
|
|
|
SeqSep {
|
|
|
|
sep: Some(t),
|
|
|
|
trailing_sep_allowed: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn none() -> SeqSep {
|
|
|
|
SeqSep {
|
|
|
|
sep: None,
|
|
|
|
trailing_sep_allowed: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|