Merge TokenTreesReader
into StringReader
.
There is a not-very-useful layering in the lexer, where `TokenTreesReader` contains a `StringReader`. This commit combines them and names the result `Lexer`, which is a more obvious name for it. The methods of `Lexer` are now split across `mod.rs` and `tokentrees.rs` which isn't ideal, but it doesn't seem worth moving a bunch of code to avoid it.
This commit is contained in:
parent
481b5fadd7
commit
98777b4c49
3 changed files with 31 additions and 49 deletions
|
@ -18,6 +18,7 @@ use rustc_span::symbol::Symbol;
|
||||||
use rustc_span::{BytePos, Pos, Span};
|
use rustc_span::{BytePos, Pos, Span};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
use crate::lexer::diagnostics::TokenTreeDiagInfo;
|
||||||
use crate::lexer::unicode_chars::UNICODE_ARRAY;
|
use crate::lexer::unicode_chars::UNICODE_ARRAY;
|
||||||
use crate::{errors, make_unclosed_delims_error};
|
use crate::{errors, make_unclosed_delims_error};
|
||||||
|
|
||||||
|
@ -56,7 +57,7 @@ pub(crate) fn lex_token_trees<'psess, 'src>(
|
||||||
}
|
}
|
||||||
|
|
||||||
let cursor = Cursor::new(src);
|
let cursor = Cursor::new(src);
|
||||||
let string_reader = StringReader {
|
let mut lexer = Lexer {
|
||||||
psess,
|
psess,
|
||||||
start_pos,
|
start_pos,
|
||||||
pos: start_pos,
|
pos: start_pos,
|
||||||
|
@ -65,9 +66,12 @@ pub(crate) fn lex_token_trees<'psess, 'src>(
|
||||||
override_span,
|
override_span,
|
||||||
nbsp_is_whitespace: false,
|
nbsp_is_whitespace: false,
|
||||||
last_lifetime: None,
|
last_lifetime: None,
|
||||||
|
token: Token::dummy(),
|
||||||
|
diag_info: TokenTreeDiagInfo::default(),
|
||||||
};
|
};
|
||||||
let (stream, res, unmatched_delims) =
|
let (_open_spacing, stream, res) = lexer.lex_token_trees(/* is_delimited */ false);
|
||||||
tokentrees::TokenTreesReader::lex_all_token_trees(string_reader);
|
let unmatched_delims = lexer.diag_info.unmatched_delims;
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(()) if unmatched_delims.is_empty() => Ok(stream),
|
Ok(()) if unmatched_delims.is_empty() => Ok(stream),
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -92,7 +96,7 @@ pub(crate) fn lex_token_trees<'psess, 'src>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StringReader<'psess, 'src> {
|
struct Lexer<'psess, 'src> {
|
||||||
psess: &'psess ParseSess,
|
psess: &'psess ParseSess,
|
||||||
/// Initial position, read-only.
|
/// Initial position, read-only.
|
||||||
start_pos: BytePos,
|
start_pos: BytePos,
|
||||||
|
@ -111,9 +115,14 @@ struct StringReader<'psess, 'src> {
|
||||||
/// Track the `Span` for the leading `'` of the last lifetime. Used for
|
/// Track the `Span` for the leading `'` of the last lifetime. Used for
|
||||||
/// diagnostics to detect possible typo where `"` was meant.
|
/// diagnostics to detect possible typo where `"` was meant.
|
||||||
last_lifetime: Option<Span>,
|
last_lifetime: Option<Span>,
|
||||||
|
|
||||||
|
/// The current token.
|
||||||
|
token: Token,
|
||||||
|
|
||||||
|
diag_info: TokenTreeDiagInfo,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'psess, 'src> StringReader<'psess, 'src> {
|
impl<'psess, 'src> Lexer<'psess, 'src> {
|
||||||
fn dcx(&self) -> DiagCtxtHandle<'psess> {
|
fn dcx(&self) -> DiagCtxtHandle<'psess> {
|
||||||
self.psess.dcx()
|
self.psess.dcx()
|
||||||
}
|
}
|
||||||
|
@ -124,7 +133,7 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
|
||||||
|
|
||||||
/// Returns the next token, paired with a bool indicating if the token was
|
/// Returns the next token, paired with a bool indicating if the token was
|
||||||
/// preceded by whitespace.
|
/// preceded by whitespace.
|
||||||
fn next_token(&mut self) -> (Token, bool) {
|
fn next_token_from_cursor(&mut self) -> (Token, bool) {
|
||||||
let mut preceded_by_whitespace = false;
|
let mut preceded_by_whitespace = false;
|
||||||
let mut swallow_next_invalid = 0;
|
let mut swallow_next_invalid = 0;
|
||||||
// Skip trivial (whitespace & comments) tokens
|
// Skip trivial (whitespace & comments) tokens
|
||||||
|
|
|
@ -4,41 +4,19 @@ use rustc_ast_pretty::pprust::token_to_string;
|
||||||
use rustc_errors::{Applicability, PErr};
|
use rustc_errors::{Applicability, PErr};
|
||||||
use rustc_span::symbol::kw;
|
use rustc_span::symbol::kw;
|
||||||
|
|
||||||
use super::diagnostics::{
|
use super::diagnostics::{report_suspicious_mismatch_block, same_indentation_level};
|
||||||
TokenTreeDiagInfo, report_suspicious_mismatch_block, same_indentation_level,
|
use super::{Lexer, UnmatchedDelim};
|
||||||
};
|
|
||||||
use super::{StringReader, UnmatchedDelim};
|
|
||||||
use crate::Parser;
|
use crate::Parser;
|
||||||
|
|
||||||
pub(super) struct TokenTreesReader<'psess, 'src> {
|
impl<'psess, 'src> Lexer<'psess, 'src> {
|
||||||
string_reader: StringReader<'psess, 'src>,
|
|
||||||
/// The "next" token, which has been obtained from the `StringReader` but
|
|
||||||
/// not yet handled by the `TokenTreesReader`.
|
|
||||||
token: Token,
|
|
||||||
diag_info: TokenTreeDiagInfo,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
|
|
||||||
pub(super) fn lex_all_token_trees(
|
|
||||||
string_reader: StringReader<'psess, 'src>,
|
|
||||||
) -> (TokenStream, Result<(), Vec<PErr<'psess>>>, Vec<UnmatchedDelim>) {
|
|
||||||
let mut tt_reader = TokenTreesReader {
|
|
||||||
string_reader,
|
|
||||||
token: Token::dummy(),
|
|
||||||
diag_info: TokenTreeDiagInfo::default(),
|
|
||||||
};
|
|
||||||
let (_open_spacing, stream, res) = tt_reader.lex_token_trees(/* is_delimited */ false);
|
|
||||||
(stream, res, tt_reader.diag_info.unmatched_delims)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lex into a token stream. The `Spacing` in the result is that of the
|
// Lex into a token stream. The `Spacing` in the result is that of the
|
||||||
// opening delimiter.
|
// opening delimiter.
|
||||||
fn lex_token_trees(
|
pub(super) fn lex_token_trees(
|
||||||
&mut self,
|
&mut self,
|
||||||
is_delimited: bool,
|
is_delimited: bool,
|
||||||
) -> (Spacing, TokenStream, Result<(), Vec<PErr<'psess>>>) {
|
) -> (Spacing, TokenStream, Result<(), Vec<PErr<'psess>>>) {
|
||||||
// Move past the opening delimiter.
|
// Move past the opening delimiter.
|
||||||
let (_, open_spacing) = self.bump(false);
|
let open_spacing = self.bump(false).1;
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
|
@ -80,7 +58,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
|
||||||
|
|
||||||
fn eof_err(&mut self) -> PErr<'psess> {
|
fn eof_err(&mut self) -> PErr<'psess> {
|
||||||
let msg = "this file contains an unclosed delimiter";
|
let msg = "this file contains an unclosed delimiter";
|
||||||
let mut err = self.string_reader.dcx().struct_span_err(self.token.span, msg);
|
let mut err = self.dcx().struct_span_err(self.token.span, msg);
|
||||||
|
|
||||||
let unclosed_delimiter_show_limit = 5;
|
let unclosed_delimiter_show_limit = 5;
|
||||||
let len = usize::min(unclosed_delimiter_show_limit, self.diag_info.open_braces.len());
|
let len = usize::min(unclosed_delimiter_show_limit, self.diag_info.open_braces.len());
|
||||||
|
@ -110,7 +88,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
|
||||||
report_suspicious_mismatch_block(
|
report_suspicious_mismatch_block(
|
||||||
&mut err,
|
&mut err,
|
||||||
&self.diag_info,
|
&self.diag_info,
|
||||||
self.string_reader.psess.source_map(),
|
self.psess.source_map(),
|
||||||
*delim,
|
*delim,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -136,7 +114,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
|
||||||
|
|
||||||
// Expand to cover the entire delimited token tree.
|
// Expand to cover the entire delimited token tree.
|
||||||
let delim_span = DelimSpan::from_pair(pre_span, self.token.span);
|
let delim_span = DelimSpan::from_pair(pre_span, self.token.span);
|
||||||
let sm = self.string_reader.psess.source_map();
|
let sm = self.psess.source_map();
|
||||||
|
|
||||||
let close_spacing = match self.token.kind {
|
let close_spacing = match self.token.kind {
|
||||||
// Correct delimiter.
|
// Correct delimiter.
|
||||||
|
@ -228,7 +206,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
|
||||||
// Will glue adjacent single-char tokens together if `glue` is set.
|
// Will glue adjacent single-char tokens together if `glue` is set.
|
||||||
fn bump(&mut self, glue: bool) -> (Token, Spacing) {
|
fn bump(&mut self, glue: bool) -> (Token, Spacing) {
|
||||||
let (this_spacing, next_tok) = loop {
|
let (this_spacing, next_tok) = loop {
|
||||||
let (next_tok, is_next_tok_preceded_by_whitespace) = self.string_reader.next_token();
|
let (next_tok, is_next_tok_preceded_by_whitespace) = self.next_token_from_cursor();
|
||||||
|
|
||||||
if is_next_tok_preceded_by_whitespace {
|
if is_next_tok_preceded_by_whitespace {
|
||||||
break (Spacing::Alone, next_tok);
|
break (Spacing::Alone, next_tok);
|
||||||
|
@ -256,7 +234,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
|
||||||
) -> Vec<PErr<'psess>> {
|
) -> Vec<PErr<'psess>> {
|
||||||
// If there are unclosed delims, see if there are diff markers and if so, point them
|
// If there are unclosed delims, see if there are diff markers and if so, point them
|
||||||
// out instead of complaining about the unclosed delims.
|
// out instead of complaining about the unclosed delims.
|
||||||
let mut parser = Parser::new(self.string_reader.psess, tts, None);
|
let mut parser = Parser::new(self.psess, tts, None);
|
||||||
let mut diff_errs = vec![];
|
let mut diff_errs = vec![];
|
||||||
// Suggest removing a `{` we think appears in an `if`/`while` condition.
|
// Suggest removing a `{` we think appears in an `if`/`while` condition.
|
||||||
// We want to suggest removing a `{` only if we think we're in an `if`/`while` condition,
|
// We want to suggest removing a `{` only if we think we're in an `if`/`while` condition,
|
||||||
|
@ -314,14 +292,9 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
|
||||||
// An unexpected closing delimiter (i.e., there is no matching opening delimiter).
|
// An unexpected closing delimiter (i.e., there is no matching opening delimiter).
|
||||||
let token_str = token_to_string(&self.token);
|
let token_str = token_to_string(&self.token);
|
||||||
let msg = format!("unexpected closing delimiter: `{token_str}`");
|
let msg = format!("unexpected closing delimiter: `{token_str}`");
|
||||||
let mut err = self.string_reader.dcx().struct_span_err(self.token.span, msg);
|
let mut err = self.dcx().struct_span_err(self.token.span, msg);
|
||||||
|
|
||||||
report_suspicious_mismatch_block(
|
report_suspicious_mismatch_block(&mut err, &self.diag_info, self.psess.source_map(), delim);
|
||||||
&mut err,
|
|
||||||
&self.diag_info,
|
|
||||||
self.string_reader.psess.source_map(),
|
|
||||||
delim,
|
|
||||||
);
|
|
||||||
err.span_label(self.token.span, "unexpected closing delimiter");
|
err.span_label(self.token.span, "unexpected closing delimiter");
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
use rustc_span::symbol::kw;
|
use rustc_span::symbol::kw;
|
||||||
use rustc_span::{BytePos, Pos, Span};
|
use rustc_span::{BytePos, Pos, Span};
|
||||||
|
|
||||||
use super::StringReader;
|
use super::Lexer;
|
||||||
use crate::errors::TokenSubstitution;
|
use crate::errors::TokenSubstitution;
|
||||||
use crate::token::{self, Delimiter};
|
use crate::token::{self, Delimiter};
|
||||||
|
|
||||||
|
@ -338,7 +338,7 @@ const ASCII_ARRAY: &[(&str, &str, Option<token::TokenKind>)] = &[
|
||||||
];
|
];
|
||||||
|
|
||||||
pub(super) fn check_for_substitution(
|
pub(super) fn check_for_substitution(
|
||||||
reader: &StringReader<'_, '_>,
|
lexer: &Lexer<'_, '_>,
|
||||||
pos: BytePos,
|
pos: BytePos,
|
||||||
ch: char,
|
ch: char,
|
||||||
count: usize,
|
count: usize,
|
||||||
|
@ -351,11 +351,11 @@ pub(super) fn check_for_substitution(
|
||||||
|
|
||||||
let Some((_, ascii_name, token)) = ASCII_ARRAY.iter().find(|&&(s, _, _)| s == ascii_str) else {
|
let Some((_, ascii_name, token)) = ASCII_ARRAY.iter().find(|&&(s, _, _)| s == ascii_str) else {
|
||||||
let msg = format!("substitution character not found for '{ch}'");
|
let msg = format!("substitution character not found for '{ch}'");
|
||||||
reader.dcx().span_bug(span, msg);
|
lexer.dcx().span_bug(span, msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
// special help suggestion for "directed" double quotes
|
// special help suggestion for "directed" double quotes
|
||||||
let sugg = if let Some(s) = peek_delimited(&reader.src[reader.src_index(pos)..], '“', '”') {
|
let sugg = if let Some(s) = peek_delimited(&lexer.src[lexer.src_index(pos)..], '“', '”') {
|
||||||
let span = Span::with_root_ctxt(
|
let span = Span::with_root_ctxt(
|
||||||
pos,
|
pos,
|
||||||
pos + Pos::from_usize('“'.len_utf8() + s.len() + '”'.len_utf8()),
|
pos + Pos::from_usize('“'.len_utf8() + s.len() + '”'.len_utf8()),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue