2021-09-09 17:05:03 +00:00
|
|
|
|
use crate::lexer::unicode_chars::UNICODE_ARRAY;
|
2021-06-14 12:56:49 +00:00
|
|
|
|
use rustc_ast::ast::{self, AttrStyle};
|
2022-04-26 15:40:14 +03:00
|
|
|
|
use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
|
2020-09-03 17:21:53 +02:00
|
|
|
|
use rustc_ast::tokenstream::{Spacing, TokenStream};
|
2021-11-04 23:31:42 +01:00
|
|
|
|
use rustc_ast::util::unicode::contains_text_flow_control_chars;
|
2022-03-09 16:11:28 -08:00
|
|
|
|
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
|
2020-09-03 15:37:03 +02:00
|
|
|
|
use rustc_lexer::unescape::{self, Mode};
|
|
|
|
|
use rustc_lexer::{Base, DocStyle, RawStrError};
|
2021-08-19 11:40:00 -07:00
|
|
|
|
use rustc_session::lint::builtin::{
|
2021-11-01 10:39:43 +01:00
|
|
|
|
RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX, TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
|
2021-08-19 11:40:00 -07:00
|
|
|
|
};
|
2021-06-14 12:56:49 +00:00
|
|
|
|
use rustc_session::lint::BuiltinLintDiagnostics;
|
2020-01-11 15:03:15 +01:00
|
|
|
|
use rustc_session::parse::ParseSess;
|
2019-12-31 20:15:40 +03:00
|
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2021-06-14 20:38:57 +00:00
|
|
|
|
use rustc_span::{edition::Edition, BytePos, Pos, Span};
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
2020-08-13 23:05:01 -07:00
|
|
|
|
use tracing::debug;
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
2017-01-12 23:32:00 +00:00
|
|
|
|
mod tokentrees;
|
2019-10-11 12:32:18 +02:00
|
|
|
|
mod unescape_error_reporting;
|
2015-11-15 02:37:49 +05:30
|
|
|
|
mod unicode_chars;
|
2020-03-28 01:46:20 -04:00
|
|
|
|
|
2021-01-23 12:48:31 -08:00
|
|
|
|
use unescape_error_reporting::{emit_unescape_error, escaped_char};
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
2022-07-27 14:21:08 +10:00
|
|
|
|
// This type is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
|
//
|
|
|
|
|
// This assertion is in this crate, rather than in `rustc_lexer`, because that
|
|
|
|
|
// crate cannot depend on `rustc_data_structures`.
|
|
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
|
|
|
|
rustc_data_structures::static_assert_size!(rustc_lexer::Token, 72);
|
|
|
|
|
|
2019-01-27 21:04:50 -08:00
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct UnmatchedBrace {
|
2022-04-26 15:40:14 +03:00
|
|
|
|
pub expected_delim: Delimiter,
|
|
|
|
|
pub found_delim: Option<Delimiter>,
|
2019-01-27 21:04:50 -08:00
|
|
|
|
pub found_span: Span,
|
|
|
|
|
pub unclosed_span: Option<Span>,
|
|
|
|
|
pub candidate_span: Option<Span>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
|
pub(crate) fn parse_token_trees<'a>(
|
2020-09-03 15:37:03 +02:00
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
|
src: &'a str,
|
|
|
|
|
start_pos: BytePos,
|
|
|
|
|
override_span: Option<Span>,
|
|
|
|
|
) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) {
|
2022-07-27 12:50:22 +10:00
|
|
|
|
StringReader { sess, start_pos, pos: start_pos, src, override_span }.into_token_trees()
|
2020-09-03 15:37:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct StringReader<'a> {
|
2019-07-31 19:55:01 +03:00
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
|
/// Initial position, read-only.
|
|
|
|
|
start_pos: BytePos,
|
|
|
|
|
/// The absolute offset within the source_map of the current character.
|
2020-05-14 20:02:40 +02:00
|
|
|
|
pos: BytePos,
|
2019-07-31 19:55:01 +03:00
|
|
|
|
/// Source text to tokenize.
|
2020-09-03 15:37:03 +02:00
|
|
|
|
src: &'a str,
|
2019-05-12 19:55:16 +03:00
|
|
|
|
override_span: Option<Span>,
|
2017-03-15 00:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 23:32:00 +00:00
|
|
|
|
impl<'a> StringReader<'a> {
|
2017-03-28 05:32:43 +00:00
|
|
|
|
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
|
2019-08-11 01:44:55 +03:00
|
|
|
|
self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
|
2017-03-28 05:32:43 +00:00
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
/// Returns the next token, and info about preceding whitespace, if any.
|
2020-09-03 17:21:53 +02:00
|
|
|
|
fn next_token(&mut self) -> (Spacing, Token) {
|
|
|
|
|
let mut spacing = Spacing::Joint;
|
2020-08-31 19:00:49 +02:00
|
|
|
|
|
|
|
|
|
// Skip `#!` at the start of the file
|
2022-07-27 12:15:35 +10:00
|
|
|
|
if self.pos == self.start_pos
|
|
|
|
|
&& let Some(shebang_len) = rustc_lexer::strip_shebang(self.src)
|
|
|
|
|
{
|
|
|
|
|
self.pos = self.pos + BytePos::from_usize(shebang_len);
|
|
|
|
|
spacing = Spacing::Alone;
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
// Skip trivial (whitespace & comments) tokens
|
|
|
|
|
loop {
|
|
|
|
|
let start_src_index = self.src_index(self.pos);
|
2022-07-27 12:50:22 +10:00
|
|
|
|
let text: &str = &self.src[start_src_index..];
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
if text.is_empty() {
|
|
|
|
|
let span = self.mk_sp(self.pos, self.pos);
|
2020-09-03 17:21:53 +02:00
|
|
|
|
return (spacing, Token::new(token::Eof, span));
|
2017-01-12 23:32:00 +00:00
|
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
let token = rustc_lexer::first_token(text);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
let start = self.pos;
|
|
|
|
|
self.pos = self.pos + BytePos::from_usize(token.len);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
debug!("next_token: {:?}({:?})", token.kind, self.str_from(start));
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
match self.cook_lexer_token(token.kind, start) {
|
|
|
|
|
Some(kind) => {
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
2020-09-03 17:21:53 +02:00
|
|
|
|
return (spacing, Token::new(kind, span));
|
2020-08-31 19:00:49 +02:00
|
|
|
|
}
|
2020-09-03 17:21:53 +02:00
|
|
|
|
None => spacing = Spacing::Alone,
|
2020-08-31 19:00:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-01 11:57:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-24 01:12:22 -07:00
|
|
|
|
/// Report a fatal lexical error with a given span.
|
2022-03-09 16:11:28 -08:00
|
|
|
|
fn fatal_span(&self, sp: Span, m: &str) -> ! {
|
2017-01-17 01:14:53 +00:00
|
|
|
|
self.sess.span_diagnostic.span_fatal(sp, m)
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-24 01:12:22 -07:00
|
|
|
|
/// Report a lexical error with a given span.
|
2018-05-31 16:53:30 -06:00
|
|
|
|
fn err_span(&self, sp: Span, m: &str) {
|
2019-01-11 19:56:41 -08:00
|
|
|
|
self.sess.span_diagnostic.struct_span_err(sp, m).emit();
|
2014-05-24 01:12:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Report a fatal error spanning [`from_pos`, `to_pos`).
|
2022-03-09 16:11:28 -08:00
|
|
|
|
fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> ! {
|
2017-03-28 05:32:43 +00:00
|
|
|
|
self.fatal_span(self.mk_sp(from_pos, to_pos), m)
|
2014-05-24 01:12:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Report a lexical error spanning [`from_pos`, `to_pos`).
|
|
|
|
|
fn err_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
|
2017-03-28 05:32:43 +00:00
|
|
|
|
self.err_span(self.mk_sp(from_pos, to_pos), m)
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-12 15:43:51 +02:00
|
|
|
|
fn struct_fatal_span_char(
|
|
|
|
|
&self,
|
|
|
|
|
from_pos: BytePos,
|
|
|
|
|
to_pos: BytePos,
|
|
|
|
|
m: &str,
|
|
|
|
|
c: char,
|
2022-03-09 16:11:28 -08:00
|
|
|
|
) -> DiagnosticBuilder<'a, !> {
|
2021-01-23 12:48:31 -08:00
|
|
|
|
self.sess
|
|
|
|
|
.span_diagnostic
|
|
|
|
|
.struct_span_fatal(self.mk_sp(from_pos, to_pos), &format!("{}: {}", m, escaped_char(c)))
|
2015-12-21 10:00:43 +13:00
|
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
2022-03-09 16:11:28 -08:00
|
|
|
|
fn struct_err_span_char(
|
|
|
|
|
&self,
|
|
|
|
|
from_pos: BytePos,
|
|
|
|
|
to_pos: BytePos,
|
|
|
|
|
m: &str,
|
|
|
|
|
c: char,
|
|
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
|
|
|
|
self.sess
|
|
|
|
|
.span_diagnostic
|
|
|
|
|
.struct_span_err(self.mk_sp(from_pos, to_pos), &format!("{}: {}", m, escaped_char(c)))
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 11:40:00 -07:00
|
|
|
|
/// Detect usages of Unicode codepoints changing the direction of the text on screen and loudly
|
|
|
|
|
/// complain about it.
|
|
|
|
|
fn lint_unicode_text_flow(&self, start: BytePos) {
|
|
|
|
|
// Opening delimiter of the length 2 is not included into the comment text.
|
|
|
|
|
let content_start = start + BytePos(2);
|
|
|
|
|
let content = self.str_from(content_start);
|
2021-11-04 23:31:42 +01:00
|
|
|
|
if contains_text_flow_control_chars(content) {
|
2021-11-03 23:37:23 +01:00
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
2021-08-19 11:40:00 -07:00
|
|
|
|
self.sess.buffer_lint_with_diagnostic(
|
|
|
|
|
&TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
|
|
|
|
|
span,
|
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
|
"unicode codepoint changing visible direction of text present in comment",
|
|
|
|
|
BuiltinLintDiagnostics::UnicodeTextFlow(span, content.to_string()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
|
/// Turns simple `rustc_lexer::TokenKind` enum into a rich
|
2021-04-07 14:47:01 -05:00
|
|
|
|
/// `rustc_ast::TokenKind`. This turns strings into interned
|
2019-05-06 11:53:40 +03:00
|
|
|
|
/// symbols and runs additional validation.
|
2020-08-31 19:00:49 +02:00
|
|
|
|
fn cook_lexer_token(&self, token: rustc_lexer::TokenKind, start: BytePos) -> Option<TokenKind> {
|
|
|
|
|
Some(match token {
|
2020-08-17 18:43:35 +02:00
|
|
|
|
rustc_lexer::TokenKind::LineComment { doc_style } => {
|
2020-08-31 19:00:49 +02:00
|
|
|
|
// Skip non-doc comments
|
2022-02-15 05:58:25 +01:00
|
|
|
|
let Some(doc_style) = doc_style else {
|
2021-08-19 11:40:00 -07:00
|
|
|
|
self.lint_unicode_text_flow(start);
|
|
|
|
|
return None;
|
|
|
|
|
};
|
2020-08-17 18:43:35 +02:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
// Opening delimiter of the length 3 is not included into the symbol.
|
|
|
|
|
let content_start = start + BytePos(3);
|
|
|
|
|
let content = self.str_from(content_start);
|
|
|
|
|
self.cook_doc_comment(content_start, content, CommentKind::Line, doc_style)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-08-17 18:43:35 +02:00
|
|
|
|
rustc_lexer::TokenKind::BlockComment { doc_style, terminated } => {
|
2019-05-06 11:53:40 +03:00
|
|
|
|
if !terminated {
|
2022-04-14 03:22:02 +09:00
|
|
|
|
self.report_unterminated_block_comment(start, doc_style);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-08-31 19:00:49 +02:00
|
|
|
|
|
|
|
|
|
// Skip non-doc comments
|
2022-02-15 05:58:25 +01:00
|
|
|
|
let Some(doc_style) = doc_style else {
|
2021-08-19 11:40:00 -07:00
|
|
|
|
self.lint_unicode_text_flow(start);
|
|
|
|
|
return None;
|
|
|
|
|
};
|
2020-08-31 19:00:49 +02:00
|
|
|
|
|
|
|
|
|
// Opening delimiter of the length 3 and closing delimiter of the length 2
|
|
|
|
|
// are not included into the symbol.
|
|
|
|
|
let content_start = start + BytePos(3);
|
|
|
|
|
let content_end = self.pos - BytePos(if terminated { 2 } else { 0 });
|
|
|
|
|
let content = self.str_from_to(content_start, content_end);
|
|
|
|
|
self.cook_doc_comment(content_start, content, CommentKind::Block, doc_style)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-08-31 19:00:49 +02:00
|
|
|
|
rustc_lexer::TokenKind::Whitespace => return None,
|
2021-05-16 11:10:05 +08:00
|
|
|
|
rustc_lexer::TokenKind::Ident
|
|
|
|
|
| rustc_lexer::TokenKind::RawIdent
|
2021-06-14 16:50:20 +00:00
|
|
|
|
| rustc_lexer::TokenKind::UnknownPrefix => {
|
2019-05-06 11:53:40 +03:00
|
|
|
|
let is_raw_ident = token == rustc_lexer::TokenKind::RawIdent;
|
2021-06-14 16:50:20 +00:00
|
|
|
|
let is_unknown_prefix = token == rustc_lexer::TokenKind::UnknownPrefix;
|
2019-05-06 11:53:40 +03:00
|
|
|
|
let mut ident_start = start;
|
|
|
|
|
if is_raw_ident {
|
|
|
|
|
ident_start = ident_start + BytePos(2);
|
|
|
|
|
}
|
2021-06-14 16:50:20 +00:00
|
|
|
|
if is_unknown_prefix {
|
|
|
|
|
self.report_unknown_prefix(start);
|
2021-05-16 11:10:05 +08:00
|
|
|
|
}
|
2019-12-30 20:00:05 +08:00
|
|
|
|
let sym = nfc_normalize(self.str_from(ident_start));
|
2020-04-25 09:38:31 +08:00
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
|
self.sess.symbol_gallery.insert(sym, span);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
if is_raw_ident {
|
|
|
|
|
if !sym.can_be_raw() {
|
|
|
|
|
self.err_span(span, &format!("`{}` cannot be a raw identifier", sym));
|
|
|
|
|
}
|
|
|
|
|
self.sess.raw_identifier_spans.borrow_mut().push(span);
|
|
|
|
|
}
|
|
|
|
|
token::Ident(sym, is_raw_ident)
|
|
|
|
|
}
|
2021-09-09 17:05:03 +00:00
|
|
|
|
rustc_lexer::TokenKind::InvalidIdent
|
2021-09-10 07:30:58 +00:00
|
|
|
|
// Do not recover an identifier with emoji if the codepoint is a confusable
|
2021-09-09 17:05:03 +00:00
|
|
|
|
// with a recoverable substitution token, like `➖`.
|
2022-04-13 22:51:34 +02:00
|
|
|
|
if !UNICODE_ARRAY
|
2021-09-09 17:05:03 +00:00
|
|
|
|
.iter()
|
2022-04-13 22:51:34 +02:00
|
|
|
|
.any(|&(c, _, _)| {
|
2021-09-09 17:05:03 +00:00
|
|
|
|
let sym = self.str_from(start);
|
|
|
|
|
sym.chars().count() == 1 && c == sym.chars().next().unwrap()
|
|
|
|
|
})
|
2022-04-13 22:51:34 +02:00
|
|
|
|
=>
|
2021-09-09 17:05:03 +00:00
|
|
|
|
{
|
2021-08-29 08:34:23 +00:00
|
|
|
|
let sym = nfc_normalize(self.str_from(start));
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
|
self.sess.bad_unicode_identifiers.borrow_mut().entry(sym).or_default().push(span);
|
|
|
|
|
token::Ident(sym, false)
|
|
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
|
rustc_lexer::TokenKind::Literal { kind, suffix_start } => {
|
|
|
|
|
let suffix_start = start + BytePos(suffix_start as u32);
|
|
|
|
|
let (kind, symbol) = self.cook_lexer_literal(start, suffix_start, kind);
|
|
|
|
|
let suffix = if suffix_start < self.pos {
|
|
|
|
|
let string = self.str_from(suffix_start);
|
|
|
|
|
if string == "_" {
|
|
|
|
|
self.sess
|
|
|
|
|
.span_diagnostic
|
|
|
|
|
.struct_span_warn(
|
|
|
|
|
self.mk_sp(suffix_start, self.pos),
|
|
|
|
|
"underscore literal suffix is not allowed",
|
|
|
|
|
)
|
|
|
|
|
.warn(
|
|
|
|
|
"this was previously accepted by the compiler but is \
|
|
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
|
a future release!",
|
|
|
|
|
)
|
|
|
|
|
.note(
|
2020-02-07 13:06:35 +01:00
|
|
|
|
"see issue #42326 \
|
|
|
|
|
<https://github.com/rust-lang/rust/issues/42326> \
|
|
|
|
|
for more information",
|
2019-05-06 11:53:40 +03:00
|
|
|
|
)
|
|
|
|
|
.emit();
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(Symbol::intern(string))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
token::Literal(token::Lit { kind, symbol, suffix })
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::Lifetime { starts_with_number } => {
|
|
|
|
|
// Include the leading `'` in the real identifier, for macro
|
|
|
|
|
// expansion purposes. See #12512 for the gory details of why
|
|
|
|
|
// this is necessary.
|
|
|
|
|
let lifetime_name = self.str_from(start);
|
|
|
|
|
if starts_with_number {
|
|
|
|
|
self.err_span_(start, self.pos, "lifetimes cannot start with a number");
|
|
|
|
|
}
|
|
|
|
|
let ident = Symbol::intern(lifetime_name);
|
|
|
|
|
token::Lifetime(ident)
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::Semi => token::Semi,
|
|
|
|
|
rustc_lexer::TokenKind::Comma => token::Comma,
|
|
|
|
|
rustc_lexer::TokenKind::Dot => token::Dot,
|
2022-04-26 15:40:14 +03:00
|
|
|
|
rustc_lexer::TokenKind::OpenParen => token::OpenDelim(Delimiter::Parenthesis),
|
|
|
|
|
rustc_lexer::TokenKind::CloseParen => token::CloseDelim(Delimiter::Parenthesis),
|
|
|
|
|
rustc_lexer::TokenKind::OpenBrace => token::OpenDelim(Delimiter::Brace),
|
|
|
|
|
rustc_lexer::TokenKind::CloseBrace => token::CloseDelim(Delimiter::Brace),
|
|
|
|
|
rustc_lexer::TokenKind::OpenBracket => token::OpenDelim(Delimiter::Bracket),
|
|
|
|
|
rustc_lexer::TokenKind::CloseBracket => token::CloseDelim(Delimiter::Bracket),
|
2019-05-06 11:53:40 +03:00
|
|
|
|
rustc_lexer::TokenKind::At => token::At,
|
|
|
|
|
rustc_lexer::TokenKind::Pound => token::Pound,
|
|
|
|
|
rustc_lexer::TokenKind::Tilde => token::Tilde,
|
|
|
|
|
rustc_lexer::TokenKind::Question => token::Question,
|
|
|
|
|
rustc_lexer::TokenKind::Colon => token::Colon,
|
|
|
|
|
rustc_lexer::TokenKind::Dollar => token::Dollar,
|
|
|
|
|
rustc_lexer::TokenKind::Eq => token::Eq,
|
2020-08-20 13:51:39 +00:00
|
|
|
|
rustc_lexer::TokenKind::Bang => token::Not,
|
2019-05-06 11:53:40 +03:00
|
|
|
|
rustc_lexer::TokenKind::Lt => token::Lt,
|
|
|
|
|
rustc_lexer::TokenKind::Gt => token::Gt,
|
|
|
|
|
rustc_lexer::TokenKind::Minus => token::BinOp(token::Minus),
|
|
|
|
|
rustc_lexer::TokenKind::And => token::BinOp(token::And),
|
|
|
|
|
rustc_lexer::TokenKind::Or => token::BinOp(token::Or),
|
|
|
|
|
rustc_lexer::TokenKind::Plus => token::BinOp(token::Plus),
|
|
|
|
|
rustc_lexer::TokenKind::Star => token::BinOp(token::Star),
|
|
|
|
|
rustc_lexer::TokenKind::Slash => token::BinOp(token::Slash),
|
|
|
|
|
rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
|
|
|
|
|
rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),
|
|
|
|
|
|
2021-09-09 17:05:03 +00:00
|
|
|
|
rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
|
2019-05-06 11:53:40 +03:00
|
|
|
|
let c = self.str_from(start).chars().next().unwrap();
|
|
|
|
|
let mut err =
|
2022-03-09 16:11:28 -08:00
|
|
|
|
self.struct_err_span_char(start, self.pos, "unknown start of token", c);
|
2019-07-25 11:22:46 -07:00
|
|
|
|
// FIXME: the lexer could be used to turn the ASCII version of unicode homoglyphs,
|
|
|
|
|
// instead of keeping a table in `check_for_substitution`into the token. Ideally,
|
|
|
|
|
// this should be inside `rustc_lexer`. However, we should first remove compound
|
|
|
|
|
// tokens like `<<` from `rustc_lexer`, and then add fancier error recovery to it,
|
|
|
|
|
// as there will be less overall work to do this way.
|
2020-08-31 19:00:49 +02:00
|
|
|
|
let token = unicode_chars::check_for_substitution(self, start, c, &mut err);
|
2021-02-06 19:17:14 -05:00
|
|
|
|
if c == '\x00' {
|
2021-02-07 11:02:53 -05:00
|
|
|
|
err.help("source files must contain UTF-8 encoded text, unexpected null bytes might occur when a different encoding is used");
|
2021-02-06 19:17:14 -05:00
|
|
|
|
}
|
2019-07-30 12:33:32 +03:00
|
|
|
|
err.emit();
|
2020-08-31 19:00:49 +02:00
|
|
|
|
token?
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-08-31 19:00:49 +02:00
|
|
|
|
})
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 18:43:35 +02:00
|
|
|
|
fn cook_doc_comment(
|
|
|
|
|
&self,
|
|
|
|
|
content_start: BytePos,
|
|
|
|
|
content: &str,
|
|
|
|
|
comment_kind: CommentKind,
|
|
|
|
|
doc_style: DocStyle,
|
|
|
|
|
) -> TokenKind {
|
2020-08-17 21:52:49 +02:00
|
|
|
|
if content.contains('\r') {
|
|
|
|
|
for (idx, _) in content.char_indices().filter(|&(_, c)| c == '\r') {
|
|
|
|
|
self.err_span_(
|
|
|
|
|
content_start + BytePos(idx as u32),
|
|
|
|
|
content_start + BytePos(idx as u32 + 1),
|
|
|
|
|
match comment_kind {
|
|
|
|
|
CommentKind::Line => "bare CR not allowed in doc-comment",
|
|
|
|
|
CommentKind::Block => "bare CR not allowed in block doc-comment",
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-08-17 18:43:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let attr_style = match doc_style {
|
|
|
|
|
DocStyle::Outer => AttrStyle::Outer,
|
|
|
|
|
DocStyle::Inner => AttrStyle::Inner,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
token::DocComment(comment_kind, attr_style, Symbol::intern(content))
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
|
fn cook_lexer_literal(
|
|
|
|
|
&self,
|
|
|
|
|
start: BytePos,
|
|
|
|
|
suffix_start: BytePos,
|
|
|
|
|
kind: rustc_lexer::LiteralKind,
|
|
|
|
|
) -> (token::LitKind, Symbol) {
|
2020-05-13 09:52:01 +02:00
|
|
|
|
// prefix means `"` or `br"` or `r###"`, ...
|
|
|
|
|
let (lit_kind, mode, prefix_len, postfix_len) = match kind {
|
2019-05-06 11:53:40 +03:00
|
|
|
|
rustc_lexer::LiteralKind::Char { terminated } => {
|
|
|
|
|
if !terminated {
|
2021-03-27 22:48:21 -04:00
|
|
|
|
self.sess.span_diagnostic.span_fatal_with_code(
|
|
|
|
|
self.mk_sp(start, suffix_start),
|
|
|
|
|
"unterminated character literal",
|
|
|
|
|
error_code!(E0762),
|
|
|
|
|
)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-05-13 09:52:01 +02:00
|
|
|
|
(token::Char, Mode::Char, 1, 1) // ' '
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::Byte { terminated } => {
|
|
|
|
|
if !terminated {
|
2021-03-27 22:48:21 -04:00
|
|
|
|
self.sess.span_diagnostic.span_fatal_with_code(
|
|
|
|
|
self.mk_sp(start + BytePos(1), suffix_start),
|
|
|
|
|
"unterminated byte constant",
|
|
|
|
|
error_code!(E0763),
|
|
|
|
|
)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-05-13 09:52:01 +02:00
|
|
|
|
(token::Byte, Mode::Byte, 2, 1) // b' '
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::Str { terminated } => {
|
|
|
|
|
if !terminated {
|
2021-03-27 22:48:21 -04:00
|
|
|
|
self.sess.span_diagnostic.span_fatal_with_code(
|
|
|
|
|
self.mk_sp(start, suffix_start),
|
|
|
|
|
"unterminated double quote string",
|
|
|
|
|
error_code!(E0765),
|
|
|
|
|
)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-05-13 09:52:01 +02:00
|
|
|
|
(token::Str, Mode::Str, 1, 1) // " "
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::ByteStr { terminated } => {
|
|
|
|
|
if !terminated {
|
2021-03-27 22:48:21 -04:00
|
|
|
|
self.sess.span_diagnostic.span_fatal_with_code(
|
|
|
|
|
self.mk_sp(start + BytePos(1), suffix_start),
|
|
|
|
|
"unterminated double quote byte string",
|
|
|
|
|
error_code!(E0766),
|
|
|
|
|
)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-05-13 09:52:01 +02:00
|
|
|
|
(token::ByteStr, Mode::ByteStr, 2, 1) // b" "
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-05-29 17:37:16 +02:00
|
|
|
|
rustc_lexer::LiteralKind::RawStr { n_hashes, err } => {
|
|
|
|
|
self.report_raw_str_error(start, err);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
let n = u32::from(n_hashes);
|
2020-05-13 09:52:01 +02:00
|
|
|
|
(token::StrRaw(n_hashes), Mode::RawStr, 2 + n, 1 + n) // r##" "##
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-05-29 17:37:16 +02:00
|
|
|
|
rustc_lexer::LiteralKind::RawByteStr { n_hashes, err } => {
|
|
|
|
|
self.report_raw_str_error(start, err);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
let n = u32::from(n_hashes);
|
2020-05-13 09:52:01 +02:00
|
|
|
|
(token::ByteStrRaw(n_hashes), Mode::RawByteStr, 3 + n, 1 + n) // br##" "##
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::Int { base, empty_int } => {
|
2020-05-13 09:52:01 +02:00
|
|
|
|
return if empty_int {
|
2020-07-04 00:12:16 +02:00
|
|
|
|
self.sess
|
|
|
|
|
.span_diagnostic
|
|
|
|
|
.struct_span_err_with_code(
|
|
|
|
|
self.mk_sp(start, suffix_start),
|
|
|
|
|
"no valid digits found for number",
|
|
|
|
|
error_code!(E0768),
|
|
|
|
|
)
|
|
|
|
|
.emit();
|
2019-05-06 11:53:40 +03:00
|
|
|
|
(token::Integer, sym::integer(0))
|
|
|
|
|
} else {
|
|
|
|
|
self.validate_int_literal(base, start, suffix_start);
|
|
|
|
|
(token::Integer, self.symbol_from_to(start, suffix_start))
|
2020-05-13 09:52:01 +02:00
|
|
|
|
};
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
|
|
|
|
|
if empty_exponent {
|
2020-06-01 13:25:41 +02:00
|
|
|
|
self.err_span_(start, self.pos, "expected at least one digit in exponent");
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match base {
|
|
|
|
|
Base::Hexadecimal => self.err_span_(
|
|
|
|
|
start,
|
|
|
|
|
suffix_start,
|
|
|
|
|
"hexadecimal float literal is not supported",
|
2019-12-22 17:42:04 -05:00
|
|
|
|
),
|
2019-05-06 11:53:40 +03:00
|
|
|
|
Base::Octal => {
|
|
|
|
|
self.err_span_(start, suffix_start, "octal float literal is not supported")
|
|
|
|
|
}
|
|
|
|
|
Base::Binary => {
|
|
|
|
|
self.err_span_(start, suffix_start, "binary float literal is not supported")
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let id = self.symbol_from_to(start, suffix_start);
|
2020-05-13 09:52:01 +02:00
|
|
|
|
return (token::Float, id);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2020-05-13 09:52:01 +02:00
|
|
|
|
};
|
|
|
|
|
let content_start = start + BytePos(prefix_len);
|
|
|
|
|
let content_end = suffix_start - BytePos(postfix_len);
|
|
|
|
|
let id = self.symbol_from_to(content_start, content_end);
|
2021-01-23 12:48:31 -08:00
|
|
|
|
self.validate_literal_escape(mode, content_start, content_end, prefix_len, postfix_len);
|
2020-06-09 15:57:08 +02:00
|
|
|
|
(lit_kind, id)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 12:49:39 +10:00
|
|
|
|
#[inline]
|
|
|
|
|
fn src_index(&self, pos: BytePos) -> usize {
|
2019-07-31 19:55:01 +03:00
|
|
|
|
(pos - self.start_pos).to_usize()
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 21:02:19 +03:00
|
|
|
|
/// Slice of the source text from `start` up to but excluding `self.pos`,
|
|
|
|
|
/// meaning the slice does not include the character `self.ch`.
|
|
|
|
|
fn str_from(&self, start: BytePos) -> &str {
|
|
|
|
|
self.str_from_to(start, self.pos)
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 21:37:25 +03:00
|
|
|
|
/// As symbol_from, with an explicit endpoint.
|
|
|
|
|
fn symbol_from_to(&self, start: BytePos, end: BytePos) -> Symbol {
|
2014-12-20 00:09:35 -08:00
|
|
|
|
debug!("taking an ident from {:?} to {:?}", start, end);
|
2019-06-25 21:02:19 +03:00
|
|
|
|
Symbol::intern(self.str_from_to(start, end))
|
2014-06-24 17:44:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 21:02:19 +03:00
|
|
|
|
/// Slice of the source text spanning from `start` up to but excluding `end`.
|
|
|
|
|
fn str_from_to(&self, start: BytePos, end: BytePos) -> &str {
|
|
|
|
|
&self.src[self.src_index(start)..self.src_index(end)]
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 17:37:16 +02:00
|
|
|
|
fn report_raw_str_error(&self, start: BytePos, opt_err: Option<RawStrError>) {
|
|
|
|
|
match opt_err {
|
|
|
|
|
Some(RawStrError::InvalidStarter { bad_char }) => {
|
|
|
|
|
self.report_non_started_raw_string(start, bad_char)
|
|
|
|
|
}
|
|
|
|
|
Some(RawStrError::NoTerminator { expected, found, possible_terminator_offset }) => self
|
|
|
|
|
.report_unterminated_raw_string(start, expected, possible_terminator_offset, found),
|
|
|
|
|
Some(RawStrError::TooManyDelimiters { found }) => {
|
|
|
|
|
self.report_too_many_hashes(start, found)
|
2020-03-28 01:46:20 -04:00
|
|
|
|
}
|
2020-05-29 17:37:16 +02:00
|
|
|
|
None => (),
|
2020-03-28 01:46:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 17:37:16 +02:00
|
|
|
|
fn report_non_started_raw_string(&self, start: BytePos, bad_char: char) -> ! {
|
2019-05-06 11:53:40 +03:00
|
|
|
|
self.struct_fatal_span_char(
|
|
|
|
|
start,
|
|
|
|
|
self.pos,
|
2020-06-01 13:25:41 +02:00
|
|
|
|
"found invalid character; only `#` is allowed in raw string delimitation",
|
2019-05-06 11:53:40 +03:00
|
|
|
|
bad_char,
|
|
|
|
|
)
|
2022-03-09 16:11:28 -08:00
|
|
|
|
.emit()
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 01:46:20 -04:00
|
|
|
|
fn report_unterminated_raw_string(
|
|
|
|
|
&self,
|
|
|
|
|
start: BytePos,
|
|
|
|
|
n_hashes: usize,
|
|
|
|
|
possible_offset: Option<usize>,
|
|
|
|
|
found_terminators: usize,
|
|
|
|
|
) -> ! {
|
2020-02-18 14:50:27 +01:00
|
|
|
|
let mut err = self.sess.span_diagnostic.struct_span_fatal_with_code(
|
|
|
|
|
self.mk_sp(start, start),
|
|
|
|
|
"unterminated raw string",
|
|
|
|
|
error_code!(E0748),
|
|
|
|
|
);
|
2020-03-28 01:46:20 -04:00
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
|
err.span_label(self.mk_sp(start, start), "unterminated raw string");
|
2014-07-02 09:39:48 -07:00
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
|
if n_hashes > 0 {
|
|
|
|
|
err.note(&format!(
|
|
|
|
|
"this raw string should be terminated with `\"{}`",
|
2020-03-28 01:46:20 -04:00
|
|
|
|
"#".repeat(n_hashes)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
));
|
2019-04-25 11:48:25 +03:00
|
|
|
|
}
|
2014-07-02 09:39:48 -07:00
|
|
|
|
|
2020-03-28 01:46:20 -04:00
|
|
|
|
if let Some(possible_offset) = possible_offset {
|
2020-03-29 11:12:48 -04:00
|
|
|
|
let lo = start + BytePos(possible_offset as u32);
|
|
|
|
|
let hi = lo + BytePos(found_terminators as u32);
|
|
|
|
|
let span = self.mk_sp(lo, hi);
|
2020-03-28 01:46:20 -04:00
|
|
|
|
err.span_suggestion(
|
|
|
|
|
span,
|
2020-03-29 11:12:48 -04:00
|
|
|
|
"consider terminating the string here",
|
2020-03-28 01:46:20 -04:00
|
|
|
|
"#".repeat(n_hashes),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 16:11:28 -08:00
|
|
|
|
err.emit()
|
2014-07-02 09:39:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 03:22:02 +09:00
|
|
|
|
fn report_unterminated_block_comment(&self, start: BytePos, doc_style: Option<DocStyle>) {
|
|
|
|
|
let msg = match doc_style {
|
|
|
|
|
Some(_) => "unterminated block doc-comment",
|
|
|
|
|
None => "unterminated block comment",
|
|
|
|
|
};
|
|
|
|
|
let last_bpos = self.pos;
|
|
|
|
|
let mut err = self.sess.span_diagnostic.struct_span_fatal_with_code(
|
|
|
|
|
self.mk_sp(start, last_bpos),
|
|
|
|
|
msg,
|
|
|
|
|
error_code!(E0758),
|
|
|
|
|
);
|
|
|
|
|
let mut nested_block_comment_open_idxs = vec![];
|
|
|
|
|
let mut last_nested_block_comment_idxs = None;
|
2022-04-14 21:18:27 +09:00
|
|
|
|
let mut content_chars = self.str_from(start).char_indices().peekable();
|
2022-04-14 03:22:02 +09:00
|
|
|
|
|
2022-04-14 21:18:27 +09:00
|
|
|
|
while let Some((idx, current_char)) = content_chars.next() {
|
|
|
|
|
match content_chars.peek() {
|
|
|
|
|
Some((_, '*')) if current_char == '/' => {
|
|
|
|
|
nested_block_comment_open_idxs.push(idx);
|
|
|
|
|
}
|
|
|
|
|
Some((_, '/')) if current_char == '*' => {
|
|
|
|
|
last_nested_block_comment_idxs =
|
|
|
|
|
nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
};
|
2022-04-14 03:22:02 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs {
|
|
|
|
|
err.span_label(self.mk_sp(start, start + BytePos(2)), msg)
|
|
|
|
|
.span_label(
|
|
|
|
|
self.mk_sp(
|
2022-04-14 21:18:27 +09:00
|
|
|
|
start + BytePos(nested_open_idx as u32),
|
|
|
|
|
start + BytePos(nested_open_idx as u32 + 2),
|
2022-04-14 03:22:02 +09:00
|
|
|
|
),
|
|
|
|
|
"...as last nested comment starts here, maybe you want to close this instead?",
|
|
|
|
|
)
|
|
|
|
|
.span_label(
|
|
|
|
|
self.mk_sp(
|
2022-04-14 21:18:27 +09:00
|
|
|
|
start + BytePos(nested_close_idx as u32),
|
|
|
|
|
start + BytePos(nested_close_idx as u32 + 2),
|
2022-04-14 03:22:02 +09:00
|
|
|
|
),
|
2022-04-14 21:18:27 +09:00
|
|
|
|
"...and last nested comment terminates here.",
|
2022-04-14 03:22:02 +09:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 16:46:46 +00:00
|
|
|
|
// RFC 3101 introduced the idea of (reserved) prefixes. As of Rust 2021,
|
|
|
|
|
// using a (unknown) prefix is an error. In earlier editions, however, they
|
|
|
|
|
// only result in a (allowed by default) lint, and are treated as regular
|
|
|
|
|
// identifier tokens.
|
2021-06-14 16:50:20 +00:00
|
|
|
|
fn report_unknown_prefix(&self, start: BytePos) {
|
2021-06-14 12:56:49 +00:00
|
|
|
|
let prefix_span = self.mk_sp(start, self.pos);
|
2021-07-31 15:37:36 +02:00
|
|
|
|
let prefix_str = self.str_from_to(start, self.pos);
|
|
|
|
|
let msg = format!("prefix `{}` is unknown", prefix_str);
|
2021-06-14 12:56:49 +00:00
|
|
|
|
|
2021-06-14 20:38:57 +00:00
|
|
|
|
let expn_data = prefix_span.ctxt().outer_expn_data();
|
|
|
|
|
|
|
|
|
|
if expn_data.edition >= Edition::Edition2021 {
|
2021-06-14 16:48:07 +00:00
|
|
|
|
// In Rust 2021, this is a hard error.
|
2021-06-14 20:38:57 +00:00
|
|
|
|
let mut err = self.sess.span_diagnostic.struct_span_err(prefix_span, &msg);
|
|
|
|
|
err.span_label(prefix_span, "unknown prefix");
|
2021-07-31 15:37:36 +02:00
|
|
|
|
if prefix_str == "rb" {
|
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
prefix_span,
|
|
|
|
|
"use `br` for a raw byte string",
|
2022-04-26 06:17:33 +01:00
|
|
|
|
"br",
|
2021-07-31 15:37:36 +02:00
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
|
|
|
|
} else if expn_data.is_root() {
|
2021-06-14 20:38:57 +00:00
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
|
prefix_span.shrink_to_hi(),
|
2021-06-14 16:48:07 +00:00
|
|
|
|
"consider inserting whitespace here",
|
2022-04-26 06:17:33 +01:00
|
|
|
|
" ",
|
2021-07-31 15:37:36 +02:00
|
|
|
|
Applicability::MaybeIncorrect,
|
2021-06-14 20:38:57 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
err.note("prefixed identifiers and literals are reserved since Rust 2021");
|
|
|
|
|
err.emit();
|
2021-06-14 16:48:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Before Rust 2021, only emit a lint for migration.
|
2021-06-14 12:56:49 +00:00
|
|
|
|
self.sess.buffer_lint_with_diagnostic(
|
2021-07-06 16:33:11 +02:00
|
|
|
|
&RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
|
2021-06-14 12:56:49 +00:00
|
|
|
|
prefix_span,
|
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
|
&msg,
|
|
|
|
|
BuiltinLintDiagnostics::ReservedPrefix(prefix_span),
|
|
|
|
|
);
|
2021-05-16 11:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 17:37:16 +02:00
|
|
|
|
fn report_too_many_hashes(&self, start: BytePos, found: usize) -> ! {
|
2020-03-28 01:46:20 -04:00
|
|
|
|
self.fatal_span_(
|
|
|
|
|
start,
|
|
|
|
|
self.pos,
|
2020-05-29 17:37:16 +02:00
|
|
|
|
&format!(
|
|
|
|
|
"too many `#` symbols: raw strings may be delimited \
|
2022-03-30 18:20:30 +02:00
|
|
|
|
by up to 255 `#` symbols, but found {}",
|
2020-05-29 17:37:16 +02:00
|
|
|
|
found
|
|
|
|
|
),
|
2020-03-28 01:46:20 -04:00
|
|
|
|
)
|
2014-07-02 09:39:48 -07:00
|
|
|
|
}
|
2019-04-25 11:48:25 +03:00
|
|
|
|
|
2021-01-23 12:48:31 -08:00
|
|
|
|
fn validate_literal_escape(
|
|
|
|
|
&self,
|
|
|
|
|
mode: Mode,
|
|
|
|
|
content_start: BytePos,
|
|
|
|
|
content_end: BytePos,
|
|
|
|
|
prefix_len: u32,
|
|
|
|
|
postfix_len: u32,
|
|
|
|
|
) {
|
2020-05-13 09:52:01 +02:00
|
|
|
|
let lit_content = self.str_from_to(content_start, content_end);
|
|
|
|
|
unescape::unescape_literal(lit_content, mode, &mut |range, result| {
|
|
|
|
|
// Here we only check for errors. The actual unescaping is done later.
|
|
|
|
|
if let Err(err) = result {
|
2021-01-23 12:48:31 -08:00
|
|
|
|
let span_with_quotes = self
|
|
|
|
|
.mk_sp(content_start - BytePos(prefix_len), content_end + BytePos(postfix_len));
|
|
|
|
|
let (start, end) = (range.start as u32, range.end as u32);
|
|
|
|
|
let lo = content_start + BytePos(start);
|
|
|
|
|
let hi = lo + BytePos(end - start);
|
|
|
|
|
let span = self.mk_sp(lo, hi);
|
2019-04-25 11:48:25 +03:00
|
|
|
|
emit_unescape_error(
|
|
|
|
|
&self.sess.span_diagnostic,
|
2020-05-13 09:52:01 +02:00
|
|
|
|
lit_content,
|
|
|
|
|
span_with_quotes,
|
2021-01-23 12:48:31 -08:00
|
|
|
|
span,
|
2020-05-13 09:52:01 +02:00
|
|
|
|
mode,
|
2019-06-25 21:02:19 +03:00
|
|
|
|
range,
|
2019-04-25 11:48:25 +03:00
|
|
|
|
err,
|
2020-05-13 09:52:01 +02:00
|
|
|
|
);
|
2019-06-25 21:02:19 +03:00
|
|
|
|
}
|
2020-05-13 09:52:01 +02:00
|
|
|
|
});
|
2019-04-25 11:48:25 +03:00
|
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
|
fn validate_int_literal(&self, base: Base, content_start: BytePos, content_end: BytePos) {
|
|
|
|
|
let base = match base {
|
|
|
|
|
Base::Binary => 2,
|
|
|
|
|
Base::Octal => 8,
|
|
|
|
|
_ => return,
|
|
|
|
|
};
|
|
|
|
|
let s = self.str_from_to(content_start + BytePos(2), content_end);
|
|
|
|
|
for (idx, c) in s.char_indices() {
|
|
|
|
|
let idx = idx as u32;
|
|
|
|
|
if c != '_' && c.to_digit(base).is_none() {
|
|
|
|
|
let lo = content_start + BytePos(2 + idx);
|
|
|
|
|
let hi = content_start + BytePos(2 + idx + c.len_utf8() as u32);
|
|
|
|
|
self.err_span_(lo, hi, &format!("invalid digit for a base {} literal", base));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-03 11:14:09 +02:00
|
|
|
|
}
|
2019-12-29 19:50:43 +08:00
|
|
|
|
|
|
|
|
|
pub fn nfc_normalize(string: &str) -> Symbol {
|
|
|
|
|
use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
|
|
|
|
|
match is_nfc_quick(string.chars()) {
|
|
|
|
|
IsNormalized::Yes => Symbol::intern(string),
|
|
|
|
|
_ => {
|
|
|
|
|
let normalized_str: String = string.chars().nfc().collect();
|
|
|
|
|
Symbol::intern(&normalized_str)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|