2019-05-19 01:04:26 +03:00
|
|
|
use crate::parse::ParseSess;
|
2019-06-04 18:48:40 +03:00
|
|
|
use crate::parse::token::{self, Token, TokenKind};
|
2019-05-22 19:25:39 +10:00
|
|
|
use crate::symbol::{sym, Symbol};
|
2019-04-25 11:48:25 +03:00
|
|
|
use crate::parse::unescape_error_reporting::{emit_unescape_error, push_escaped_char};
|
2019-02-07 02:33:01 +09:00
|
|
|
|
2019-04-25 11:48:25 +03:00
|
|
|
use errors::{FatalError, Diagnostic, DiagnosticBuilder};
|
2019-04-04 10:55:30 +03:00
|
|
|
use syntax_pos::{BytePos, Pos, Span, NO_EXPANSION};
|
2019-05-06 11:53:40 +03:00
|
|
|
use rustc_lexer::Base;
|
2019-07-21 16:46:11 +03:00
|
|
|
use rustc_lexer::unescape;
|
2014-05-21 16:57:31 -07:00
|
|
|
|
2015-04-15 22:15:50 -07:00
|
|
|
use std::borrow::Cow;
|
2014-05-21 16:57:31 -07:00
|
|
|
use std::char;
|
2018-10-27 21:41:26 +09:00
|
|
|
use std::iter;
|
2019-05-06 11:53:40 +03:00
|
|
|
use std::convert::TryInto;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2019-02-07 02:33:01 +09:00
|
|
|
use log::debug;
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
|
|
pub mod comments;
|
2017-01-12 23:32:00 +00:00
|
|
|
mod tokentrees;
|
2015-11-15 02:37:49 +05:30
|
|
|
mod unicode_chars;
|
2014-05-21 16:57:31 -07:00
|
|
|
|
2019-01-27 21:04:50 -08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct UnmatchedBrace {
|
|
|
|
pub expected_delim: token::DelimToken,
|
|
|
|
pub found_delim: token::DelimToken,
|
|
|
|
pub found_span: Span,
|
|
|
|
pub unclosed_span: Option<Span>,
|
|
|
|
pub candidate_span: Option<Span>,
|
|
|
|
}
|
|
|
|
|
2014-05-21 16:57:31 -07:00
|
|
|
pub 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.
|
|
|
|
pos: BytePos,
|
2018-05-09 12:55:13 +10:00
|
|
|
/// Stop reading src at this index.
|
2019-07-31 19:55:01 +03:00
|
|
|
end_src_index: usize,
|
2018-05-31 16:53:30 -06:00
|
|
|
fatal_errs: Vec<DiagnosticBuilder<'a>>,
|
2019-07-31 19:55:01 +03:00
|
|
|
/// Source text to tokenize.
|
2018-05-09 12:49:39 +10:00
|
|
|
src: Lrc<String>,
|
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> {
|
2019-07-03 13:31:52 +03:00
|
|
|
pub fn new(sess: &'a ParseSess,
|
|
|
|
source_file: Lrc<syntax_pos::SourceFile>,
|
|
|
|
override_span: Option<Span>) -> Self {
|
2019-05-06 11:53:40 +03:00
|
|
|
if source_file.src.is_none() {
|
|
|
|
sess.span_diagnostic.bug(&format!("Cannot lex source_file without source: {}",
|
|
|
|
source_file.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
let src = (*source_file.src.as_ref().unwrap()).clone();
|
|
|
|
|
|
|
|
StringReader {
|
|
|
|
sess,
|
2019-07-31 19:55:01 +03:00
|
|
|
start_pos: source_file.start_pos,
|
2019-05-06 11:53:40 +03:00
|
|
|
pos: source_file.start_pos,
|
|
|
|
end_src_index: src.len(),
|
|
|
|
src,
|
|
|
|
fatal_errs: Vec::new(),
|
|
|
|
override_span,
|
|
|
|
}
|
2019-07-03 13:31:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
|
|
|
|
let begin = sess.source_map().lookup_byte_offset(span.lo());
|
|
|
|
let end = sess.source_map().lookup_byte_offset(span.hi());
|
|
|
|
|
|
|
|
// Make the range zero-length if the span is invalid.
|
|
|
|
if span.lo() > span.hi() || begin.sf.start_pos != end.sf.start_pos {
|
|
|
|
span = span.shrink_to_lo();
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
let mut sr = StringReader::new(sess, begin.sf, None);
|
2019-07-03 13:31:52 +03:00
|
|
|
|
|
|
|
// Seek the lexer to the right byte range.
|
|
|
|
sr.end_src_index = sr.src_index(span.hi());
|
|
|
|
|
|
|
|
sr
|
2018-05-17 09:30:43 -07:00
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
|
|
|
|
2017-03-28 05:32:43 +00:00
|
|
|
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
|
2019-07-03 15:09:06 +03:00
|
|
|
self.override_span.unwrap_or_else(|| Span::new(lo, hi, NO_EXPANSION))
|
2017-03-28 05:32:43 +00:00
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2019-06-04 18:48:40 +03:00
|
|
|
fn unwrap_or_abort(&mut self, res: Result<Token, ()>) -> Token {
|
2017-01-12 23:32:00 +00:00
|
|
|
match res {
|
|
|
|
Ok(tok) => tok,
|
|
|
|
Err(_) => {
|
|
|
|
self.emit_fatal_errors();
|
2018-01-21 12:47:58 +01:00
|
|
|
FatalError.raise();
|
2017-01-12 23:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2019-07-03 14:06:10 +03:00
|
|
|
/// Returns the next token, including trivia like whitespace or comments.
|
|
|
|
///
|
|
|
|
/// `Err(())` means that some errors were encountered, which can be
|
|
|
|
/// retrieved using `buffer_fatal_errors`.
|
2019-06-04 18:48:40 +03:00
|
|
|
pub fn try_next_token(&mut self) -> Result<Token, ()> {
|
2018-08-12 15:43:51 +02:00
|
|
|
assert!(self.fatal_errs.is_empty());
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
|
|
let start_src_index = self.src_index(self.pos);
|
|
|
|
let text: &str = &self.src[start_src_index..self.end_src_index];
|
|
|
|
|
|
|
|
if text.is_empty() {
|
2019-07-31 19:55:01 +03:00
|
|
|
let span = self.mk_sp(self.pos, self.pos);
|
2019-05-06 11:53:40 +03:00
|
|
|
return Ok(Token::new(token::Eof, span));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-07-31 19:55:01 +03:00
|
|
|
let is_beginning_of_file = self.pos == self.start_pos;
|
2019-05-06 11:53:40 +03:00
|
|
|
if is_beginning_of_file {
|
|
|
|
if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
|
|
|
|
let start = self.pos;
|
|
|
|
self.pos = self.pos + BytePos::from_usize(shebang_len);
|
|
|
|
|
|
|
|
let sym = self.symbol_from(start + BytePos::from_usize("#!".len()));
|
|
|
|
let kind = token::Shebang(sym);
|
|
|
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
return Ok(Token::new(kind, span));
|
|
|
|
}
|
2017-01-12 23:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
|
|
let token = rustc_lexer::first_token(text);
|
|
|
|
|
|
|
|
let start = self.pos;
|
|
|
|
self.pos = self.pos + BytePos::from_usize(token.len);
|
|
|
|
|
|
|
|
debug!("try_next_token: {:?}({:?})", token.kind, self.str_from(start));
|
|
|
|
|
|
|
|
// This could use `?`, but that makes code significantly (10-20%) slower.
|
|
|
|
// https://github.com/rust-lang/rust/issues/37939
|
|
|
|
let kind = match self.cook_lexer_token(token.kind, start) {
|
|
|
|
Ok(it) => it,
|
|
|
|
Err(err) => return Err(self.fatal_errs.push(err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
Ok(Token::new(kind, span))
|
2017-01-12 23:32:00 +00:00
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2019-07-03 15:07:41 +03:00
|
|
|
/// Returns the next token, including trivia like whitespace or comments.
|
|
|
|
///
|
|
|
|
/// Aborts in case of an error.
|
|
|
|
pub fn next_token(&mut self) -> Token {
|
|
|
|
let res = self.try_next_token();
|
2017-01-12 23:32:00 +00:00
|
|
|
self.unwrap_or_abort(res)
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn emit_fatal_errors(&mut self) {
|
2016-04-25 17:20:32 +02:00
|
|
|
for err in &mut self.fatal_errs {
|
|
|
|
err.emit();
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2016-04-25 17:20:32 +02:00
|
|
|
self.fatal_errs.clear();
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2018-11-01 11:57:29 -05:00
|
|
|
pub fn buffer_fatal_errors(&mut self) -> Vec<Diagnostic> {
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
|
|
|
for err in self.fatal_errs.drain(..) {
|
|
|
|
err.buffer(&mut buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2014-05-24 01:12:22 -07:00
|
|
|
/// Report a fatal lexical error with a given span.
|
2018-05-31 16:53:30 -06:00
|
|
|
fn fatal_span(&self, sp: Span, m: &str) -> FatalError {
|
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
|
|
|
}
|
|
|
|
|
2015-07-10 21:37:21 +03:00
|
|
|
|
2014-05-24 01:12:22 -07:00
|
|
|
/// Report a fatal error spanning [`from_pos`, `to_pos`).
|
2015-10-23 19:20:03 -07:00
|
|
|
fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> FatalError {
|
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_span_fatal(&self, from_pos: BytePos, to_pos: BytePos, m: &str)
|
|
|
|
-> DiagnosticBuilder<'a>
|
|
|
|
{
|
2018-02-26 15:04:40 +01:00
|
|
|
self.sess.span_diagnostic.struct_span_fatal(self.mk_sp(from_pos, to_pos), m)
|
|
|
|
}
|
|
|
|
|
2018-08-12 15:43:51 +02:00
|
|
|
fn struct_fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char)
|
|
|
|
-> DiagnosticBuilder<'a>
|
|
|
|
{
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut m = m.to_string();
|
|
|
|
m.push_str(": ");
|
2019-04-25 11:48:25 +03:00
|
|
|
push_escaped_char(&mut m, c);
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2017-03-28 05:32:43 +00:00
|
|
|
self.sess.span_diagnostic.struct_span_fatal(self.mk_sp(from_pos, to_pos), &m[..])
|
2015-12-21 10:00:43 +13:00
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
/// Turns simple `rustc_lexer::TokenKind` enum into a rich
|
|
|
|
/// `libsyntax::TokenKind`. This turns strings into interned
|
|
|
|
/// symbols and runs additional validation.
|
|
|
|
fn cook_lexer_token(
|
|
|
|
&self,
|
|
|
|
token: rustc_lexer::TokenKind,
|
|
|
|
start: BytePos,
|
|
|
|
) -> Result<TokenKind, DiagnosticBuilder<'a>> {
|
|
|
|
let kind = match token {
|
|
|
|
rustc_lexer::TokenKind::LineComment => {
|
|
|
|
let string = self.str_from(start);
|
|
|
|
// comments with only more "/"s are not doc comments
|
|
|
|
let tok = if is_doc_comment(string) {
|
|
|
|
let mut idx = 0;
|
|
|
|
loop {
|
|
|
|
idx = match string[idx..].find('\r') {
|
|
|
|
None => break,
|
2019-07-22 12:59:18 +03:00
|
|
|
Some(it) => idx + it + 1
|
2019-05-06 11:53:40 +03:00
|
|
|
};
|
|
|
|
if string[idx..].chars().next() != Some('\n') {
|
|
|
|
self.err_span_(start + BytePos(idx as u32 - 1),
|
|
|
|
start + BytePos(idx as u32),
|
|
|
|
"bare CR not allowed in doc-comment");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token::DocComment(Symbol::intern(string))
|
|
|
|
} else {
|
|
|
|
token::Comment
|
|
|
|
};
|
|
|
|
|
|
|
|
tok
|
|
|
|
}
|
|
|
|
rustc_lexer::TokenKind::BlockComment { terminated } => {
|
|
|
|
let string = self.str_from(start);
|
|
|
|
// block comments starting with "/**" or "/*!" are doc-comments
|
|
|
|
// but comments with only "*"s between two "/"s are not
|
|
|
|
let is_doc_comment = is_block_doc_comment(string);
|
|
|
|
|
|
|
|
if !terminated {
|
|
|
|
let msg = if is_doc_comment {
|
|
|
|
"unterminated block doc-comment"
|
|
|
|
} else {
|
|
|
|
"unterminated block comment"
|
|
|
|
};
|
|
|
|
let last_bpos = self.pos;
|
|
|
|
self.fatal_span_(start, last_bpos, msg).raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
let tok = if is_doc_comment {
|
|
|
|
let has_cr = string.contains('\r');
|
|
|
|
let string = if has_cr {
|
|
|
|
self.translate_crlf(start,
|
|
|
|
string,
|
|
|
|
"bare CR not allowed in block doc-comment")
|
|
|
|
} else {
|
|
|
|
string.into()
|
|
|
|
};
|
|
|
|
token::DocComment(Symbol::intern(&string[..]))
|
|
|
|
} else {
|
|
|
|
token::Comment
|
|
|
|
};
|
|
|
|
|
|
|
|
tok
|
|
|
|
}
|
|
|
|
rustc_lexer::TokenKind::Whitespace => token::Whitespace,
|
|
|
|
rustc_lexer::TokenKind::Ident | rustc_lexer::TokenKind::RawIdent => {
|
|
|
|
let is_raw_ident = token == rustc_lexer::TokenKind::RawIdent;
|
|
|
|
let mut ident_start = start;
|
|
|
|
if is_raw_ident {
|
|
|
|
ident_start = ident_start + BytePos(2);
|
|
|
|
}
|
|
|
|
// FIXME: perform NFKC normalization here. (Issue #2253)
|
|
|
|
let sym = self.symbol_from(ident_start);
|
|
|
|
if is_raw_ident {
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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("for more information, see issue #42326 \
|
|
|
|
<https://github.com/rust-lang/rust/issues/42326>")
|
|
|
|
.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::DotDotDot => token::DotDotDot,
|
|
|
|
rustc_lexer::TokenKind::DotDotEq => token::DotDotEq,
|
|
|
|
rustc_lexer::TokenKind::DotDot => token::DotDot,
|
|
|
|
rustc_lexer::TokenKind::Dot => token::Dot,
|
|
|
|
rustc_lexer::TokenKind::OpenParen => token::OpenDelim(token::Paren),
|
|
|
|
rustc_lexer::TokenKind::CloseParen => token::CloseDelim(token::Paren),
|
|
|
|
rustc_lexer::TokenKind::OpenBrace => token::OpenDelim(token::Brace),
|
|
|
|
rustc_lexer::TokenKind::CloseBrace => token::CloseDelim(token::Brace),
|
|
|
|
rustc_lexer::TokenKind::OpenBracket => token::OpenDelim(token::Bracket),
|
|
|
|
rustc_lexer::TokenKind::CloseBracket => token::CloseDelim(token::Bracket),
|
|
|
|
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::ColonColon => token::ModSep,
|
|
|
|
rustc_lexer::TokenKind::Colon => token::Colon,
|
|
|
|
rustc_lexer::TokenKind::Dollar => token::Dollar,
|
|
|
|
rustc_lexer::TokenKind::EqEq => token::EqEq,
|
|
|
|
rustc_lexer::TokenKind::Eq => token::Eq,
|
|
|
|
rustc_lexer::TokenKind::FatArrow => token::FatArrow,
|
|
|
|
rustc_lexer::TokenKind::Ne => token::Ne,
|
|
|
|
rustc_lexer::TokenKind::Not => token::Not,
|
|
|
|
rustc_lexer::TokenKind::Le => token::Le,
|
|
|
|
rustc_lexer::TokenKind::LArrow => token::LArrow,
|
|
|
|
rustc_lexer::TokenKind::Lt => token::Lt,
|
|
|
|
rustc_lexer::TokenKind::ShlEq => token::BinOpEq(token::Shl),
|
|
|
|
rustc_lexer::TokenKind::Shl => token::BinOp(token::Shl),
|
|
|
|
rustc_lexer::TokenKind::Ge => token::Ge,
|
|
|
|
rustc_lexer::TokenKind::Gt => token::Gt,
|
|
|
|
rustc_lexer::TokenKind::ShrEq => token::BinOpEq(token::Shr),
|
|
|
|
rustc_lexer::TokenKind::Shr => token::BinOp(token::Shr),
|
|
|
|
rustc_lexer::TokenKind::RArrow => token::RArrow,
|
|
|
|
rustc_lexer::TokenKind::Minus => token::BinOp(token::Minus),
|
|
|
|
rustc_lexer::TokenKind::MinusEq => token::BinOpEq(token::Minus),
|
|
|
|
rustc_lexer::TokenKind::And => token::BinOp(token::And),
|
|
|
|
rustc_lexer::TokenKind::AndEq => token::BinOpEq(token::And),
|
|
|
|
rustc_lexer::TokenKind::AndAnd => token::AndAnd,
|
|
|
|
rustc_lexer::TokenKind::Or => token::BinOp(token::Or),
|
|
|
|
rustc_lexer::TokenKind::OrEq => token::BinOpEq(token::Or),
|
|
|
|
rustc_lexer::TokenKind::OrOr => token::OrOr,
|
|
|
|
rustc_lexer::TokenKind::Plus => token::BinOp(token::Plus),
|
|
|
|
rustc_lexer::TokenKind::PlusEq => token::BinOpEq(token::Plus),
|
|
|
|
rustc_lexer::TokenKind::Star => token::BinOp(token::Star),
|
|
|
|
rustc_lexer::TokenKind::StarEq => token::BinOpEq(token::Star),
|
|
|
|
rustc_lexer::TokenKind::Slash => token::BinOp(token::Slash),
|
|
|
|
rustc_lexer::TokenKind::SlashEq => token::BinOpEq(token::Slash),
|
|
|
|
rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
|
|
|
|
rustc_lexer::TokenKind::CaretEq => token::BinOpEq(token::Caret),
|
|
|
|
rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),
|
|
|
|
rustc_lexer::TokenKind::PercentEq => token::BinOpEq(token::Percent),
|
|
|
|
|
|
|
|
rustc_lexer::TokenKind::Unknown => {
|
|
|
|
let c = self.str_from(start).chars().next().unwrap();
|
|
|
|
let mut err = self.struct_fatal_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.
|
|
|
|
return match unicode_chars::check_for_substitution(self, start, c, &mut err) {
|
|
|
|
Some(token) => {
|
|
|
|
err.emit();
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
None => Err(err),
|
2019-07-24 16:10:42 -07:00
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cook_lexer_literal(
|
|
|
|
&self,
|
|
|
|
start: BytePos,
|
|
|
|
suffix_start: BytePos,
|
|
|
|
kind: rustc_lexer::LiteralKind
|
|
|
|
) -> (token::LitKind, Symbol) {
|
|
|
|
match kind {
|
|
|
|
rustc_lexer::LiteralKind::Char { terminated } => {
|
|
|
|
if !terminated {
|
|
|
|
self.fatal_span_(start, suffix_start,
|
|
|
|
"unterminated character literal".into())
|
|
|
|
.raise()
|
|
|
|
}
|
|
|
|
let content_start = start + BytePos(1);
|
|
|
|
let content_end = suffix_start - BytePos(1);
|
|
|
|
self.validate_char_escape(content_start, content_end);
|
|
|
|
let id = self.symbol_from_to(content_start, content_end);
|
|
|
|
(token::Char, id)
|
|
|
|
},
|
|
|
|
rustc_lexer::LiteralKind::Byte { terminated } => {
|
|
|
|
if !terminated {
|
|
|
|
self.fatal_span_(start + BytePos(1), suffix_start,
|
|
|
|
"unterminated byte constant".into())
|
|
|
|
.raise()
|
|
|
|
}
|
|
|
|
let content_start = start + BytePos(2);
|
|
|
|
let content_end = suffix_start - BytePos(1);
|
|
|
|
self.validate_byte_escape(content_start, content_end);
|
|
|
|
let id = self.symbol_from_to(content_start, content_end);
|
|
|
|
(token::Byte, id)
|
|
|
|
},
|
|
|
|
rustc_lexer::LiteralKind::Str { terminated } => {
|
|
|
|
if !terminated {
|
|
|
|
self.fatal_span_(start, suffix_start,
|
|
|
|
"unterminated double quote string".into())
|
|
|
|
.raise()
|
|
|
|
}
|
|
|
|
let content_start = start + BytePos(1);
|
|
|
|
let content_end = suffix_start - BytePos(1);
|
|
|
|
self.validate_str_escape(content_start, content_end);
|
|
|
|
let id = self.symbol_from_to(content_start, content_end);
|
|
|
|
(token::Str, id)
|
|
|
|
}
|
|
|
|
rustc_lexer::LiteralKind::ByteStr { terminated } => {
|
|
|
|
if !terminated {
|
|
|
|
self.fatal_span_(start + BytePos(1), suffix_start,
|
|
|
|
"unterminated double quote byte string".into())
|
|
|
|
.raise()
|
|
|
|
}
|
|
|
|
let content_start = start + BytePos(2);
|
|
|
|
let content_end = suffix_start - BytePos(1);
|
|
|
|
self.validate_byte_str_escape(content_start, content_end);
|
|
|
|
let id = self.symbol_from_to(content_start, content_end);
|
|
|
|
(token::ByteStr, id)
|
|
|
|
}
|
|
|
|
rustc_lexer::LiteralKind::RawStr { n_hashes, started, terminated } => {
|
|
|
|
if !started {
|
|
|
|
self.report_non_started_raw_string(start);
|
|
|
|
}
|
|
|
|
if !terminated {
|
|
|
|
self.report_unterminated_raw_string(start, n_hashes)
|
|
|
|
}
|
|
|
|
let n_hashes: u16 = self.restrict_n_hashes(start, n_hashes);
|
|
|
|
let n = u32::from(n_hashes);
|
|
|
|
let content_start = start + BytePos(2 + n);
|
|
|
|
let content_end = suffix_start - BytePos(1 + n);
|
|
|
|
self.validate_raw_str_escape(content_start, content_end);
|
|
|
|
let id = self.symbol_from_to(content_start, content_end);
|
|
|
|
(token::StrRaw(n_hashes), id)
|
|
|
|
}
|
|
|
|
rustc_lexer::LiteralKind::RawByteStr { n_hashes, started, terminated } => {
|
|
|
|
if !started {
|
|
|
|
self.report_non_started_raw_string(start);
|
|
|
|
}
|
|
|
|
if !terminated {
|
|
|
|
self.report_unterminated_raw_string(start, n_hashes)
|
|
|
|
}
|
|
|
|
let n_hashes: u16 = self.restrict_n_hashes(start, n_hashes);
|
|
|
|
let n = u32::from(n_hashes);
|
|
|
|
let content_start = start + BytePos(3 + n);
|
|
|
|
let content_end = suffix_start - BytePos(1 + n);
|
|
|
|
self.validate_raw_byte_str_escape(content_start, content_end);
|
|
|
|
let id = self.symbol_from_to(content_start, content_end);
|
|
|
|
(token::ByteStrRaw(n_hashes), id)
|
|
|
|
}
|
|
|
|
rustc_lexer::LiteralKind::Int { base, empty_int } => {
|
|
|
|
if empty_int {
|
|
|
|
self.err_span_(start, suffix_start, "no valid digits found for number");
|
|
|
|
(token::Integer, sym::integer(0))
|
|
|
|
} else {
|
|
|
|
self.validate_int_literal(base, start, suffix_start);
|
|
|
|
(token::Integer, self.symbol_from_to(start, suffix_start))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
|
|
|
|
if empty_exponent {
|
|
|
|
let mut err = self.struct_span_fatal(
|
|
|
|
start, self.pos,
|
|
|
|
"expected at least one digit in exponent"
|
|
|
|
);
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
match base {
|
|
|
|
Base::Hexadecimal => {
|
|
|
|
self.err_span_(start, suffix_start,
|
|
|
|
"hexadecimal float literal is not supported")
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
(token::Float, id)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2014-12-08 13:28:32 -05:00
|
|
|
{
|
2019-06-25 21:02:19 +03:00
|
|
|
self.str_from_to(start, self.pos)
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2019-06-25 21:37:25 +03:00
|
|
|
/// Creates a Symbol from a given offset to the current offset.
|
|
|
|
fn symbol_from(&self, start: BytePos) -> Symbol {
|
2016-10-04 11:46:54 +11:00
|
|
|
debug!("taking an ident from {:?} to {:?}", start, self.pos);
|
2019-06-25 21:02:19 +03:00
|
|
|
Symbol::intern(self.str_from(start))
|
2014-06-24 17:44:50 -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
|
2014-12-08 13:28:32 -05:00
|
|
|
{
|
2019-06-25 21:02:19 +03:00
|
|
|
&self.src[self.src_index(start)..self.src_index(end)]
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2014-05-24 01:13:59 -07:00
|
|
|
/// Converts CRLF to LF in the given string, raising an error on bare CR.
|
2016-01-03 11:14:09 +02:00
|
|
|
fn translate_crlf<'b>(&self, start: BytePos, s: &'b str, errmsg: &'b str) -> Cow<'b, str> {
|
2018-10-27 21:41:26 +09:00
|
|
|
let mut chars = s.char_indices().peekable();
|
|
|
|
while let Some((i, ch)) = chars.next() {
|
2014-05-24 01:13:59 -07:00
|
|
|
if ch == '\r' {
|
2018-10-27 21:41:26 +09:00
|
|
|
if let Some((lf_idx, '\n')) = chars.peek() {
|
|
|
|
return translate_crlf_(self, start, s, *lf_idx, chars, errmsg).into();
|
2014-05-24 01:13:59 -07:00
|
|
|
}
|
|
|
|
let pos = start + BytePos(i as u32);
|
2018-10-27 21:41:26 +09:00
|
|
|
let end_pos = start + BytePos((i + ch.len_utf8()) as u32);
|
2014-05-24 01:13:59 -07:00
|
|
|
self.err_span_(pos, end_pos, errmsg);
|
|
|
|
}
|
|
|
|
}
|
2015-04-15 22:15:50 -07:00
|
|
|
return s.into();
|
2014-05-24 01:13:59 -07:00
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
fn translate_crlf_(rdr: &StringReader<'_>,
|
2016-01-03 11:14:09 +02:00
|
|
|
start: BytePos,
|
|
|
|
s: &str,
|
2018-10-27 21:41:26 +09:00
|
|
|
mut j: usize,
|
|
|
|
mut chars: iter::Peekable<impl Iterator<Item = (usize, char)>>,
|
|
|
|
errmsg: &str)
|
2016-01-03 11:14:09 +02:00
|
|
|
-> String {
|
2014-05-24 01:13:59 -07:00
|
|
|
let mut buf = String::with_capacity(s.len());
|
2018-10-27 21:41:26 +09:00
|
|
|
// Skip first CR
|
|
|
|
buf.push_str(&s[.. j - 1]);
|
|
|
|
while let Some((i, ch)) = chars.next() {
|
2014-05-24 01:13:59 -07:00
|
|
|
if ch == '\r' {
|
2016-01-03 11:14:09 +02:00
|
|
|
if j < i {
|
|
|
|
buf.push_str(&s[j..i]);
|
|
|
|
}
|
2018-10-27 21:41:26 +09:00
|
|
|
let next = i + ch.len_utf8();
|
2014-05-24 01:13:59 -07:00
|
|
|
j = next;
|
2018-10-27 21:41:26 +09:00
|
|
|
if chars.peek().map(|(_, ch)| *ch) != Some('\n') {
|
2014-05-24 01:13:59 -07:00
|
|
|
let pos = start + BytePos(i as u32);
|
|
|
|
let end_pos = start + BytePos(next as u32);
|
|
|
|
rdr.err_span_(pos, end_pos, errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-03 11:14:09 +02:00
|
|
|
if j < s.len() {
|
|
|
|
buf.push_str(&s[j..]);
|
|
|
|
}
|
2014-05-24 01:13:59 -07:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn report_non_started_raw_string(&self, start: BytePos) -> ! {
|
|
|
|
let bad_char = self.str_from(start).chars().last().unwrap();
|
|
|
|
self
|
|
|
|
.struct_fatal_span_char(
|
|
|
|
start,
|
|
|
|
self.pos,
|
|
|
|
"found invalid character; only `#` is allowed \
|
|
|
|
in raw string delimitation",
|
|
|
|
bad_char,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
FatalError.raise()
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn report_unterminated_raw_string(&self, start: BytePos, n_hashes: usize) -> ! {
|
|
|
|
let mut err = self.struct_span_fatal(
|
|
|
|
start, start,
|
|
|
|
"unterminated raw string",
|
|
|
|
);
|
|
|
|
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 `\"{}`",
|
|
|
|
"#".repeat(n_hashes as usize)));
|
2019-04-25 11:48:25 +03:00
|
|
|
}
|
2014-07-02 09:39:48 -07:00
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
err.emit();
|
|
|
|
FatalError.raise()
|
2014-07-02 09:39:48 -07:00
|
|
|
}
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn restrict_n_hashes(&self, start: BytePos, n_hashes: usize) -> u16 {
|
|
|
|
match n_hashes.try_into() {
|
|
|
|
Ok(n_hashes) => n_hashes,
|
|
|
|
Err(_) => {
|
|
|
|
self.fatal_span_(start,
|
|
|
|
self.pos,
|
2019-05-13 11:42:12 +02:00
|
|
|
"too many `#` symbols: raw strings may be \
|
2019-05-06 11:53:40 +03:00
|
|
|
delimited by up to 65535 `#` symbols").raise();
|
2014-07-02 09:39:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 11:48:25 +03:00
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn validate_char_escape(&self, content_start: BytePos, content_end: BytePos) {
|
|
|
|
let lit = self.str_from_to(content_start, content_end);
|
2019-06-25 21:02:19 +03:00
|
|
|
if let Err((off, err)) = unescape::unescape_char(lit) {
|
|
|
|
emit_unescape_error(
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
lit,
|
2019-05-06 11:53:40 +03:00
|
|
|
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
|
2019-06-25 21:02:19 +03:00
|
|
|
unescape::Mode::Char,
|
|
|
|
0..off,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn validate_byte_escape(&self, content_start: BytePos, content_end: BytePos) {
|
|
|
|
let lit = self.str_from_to(content_start, content_end);
|
2019-06-25 21:02:19 +03:00
|
|
|
if let Err((off, err)) = unescape::unescape_byte(lit) {
|
|
|
|
emit_unescape_error(
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
lit,
|
2019-05-06 11:53:40 +03:00
|
|
|
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
|
2019-06-25 21:02:19 +03:00
|
|
|
unescape::Mode::Byte,
|
|
|
|
0..off,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn validate_str_escape(&self, content_start: BytePos, content_end: BytePos) {
|
|
|
|
let lit = self.str_from_to(content_start, content_end);
|
2019-06-25 21:02:19 +03:00
|
|
|
unescape::unescape_str(lit, &mut |range, c| {
|
|
|
|
if let Err(err) = c {
|
2019-04-25 11:48:25 +03:00
|
|
|
emit_unescape_error(
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
lit,
|
2019-05-06 11:53:40 +03:00
|
|
|
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
|
2019-06-25 21:02:19 +03:00
|
|
|
unescape::Mode::Str,
|
|
|
|
range,
|
2019-04-25 11:48:25 +03:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
2019-06-25 21:02:19 +03:00
|
|
|
})
|
2019-04-25 11:48:25 +03:00
|
|
|
}
|
|
|
|
|
2019-06-25 21:02:19 +03:00
|
|
|
fn validate_raw_str_escape(&self, content_start: BytePos, content_end: BytePos) {
|
|
|
|
let lit = self.str_from_to(content_start, content_end);
|
|
|
|
unescape::unescape_raw_str(lit, &mut |range, c| {
|
|
|
|
if let Err(err) = c {
|
2019-04-25 11:48:25 +03:00
|
|
|
emit_unescape_error(
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
lit,
|
2019-06-25 21:02:19 +03:00
|
|
|
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
|
|
|
|
unescape::Mode::Str,
|
|
|
|
range,
|
2019-04-25 11:48:25 +03:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
2019-06-25 21:02:19 +03:00
|
|
|
})
|
2019-05-13 19:52:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 20:21:44 +02:00
|
|
|
fn validate_raw_byte_str_escape(&self, content_start: BytePos, content_end: BytePos) {
|
2019-06-25 21:02:19 +03:00
|
|
|
let lit = self.str_from_to(content_start, content_end);
|
|
|
|
unescape::unescape_raw_byte_str(lit, &mut |range, c| {
|
|
|
|
if let Err(err) = c {
|
|
|
|
emit_unescape_error(
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
lit,
|
|
|
|
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
|
|
|
|
unescape::Mode::ByteStr,
|
|
|
|
range,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2019-05-13 20:21:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
fn validate_byte_str_escape(&self, content_start: BytePos, content_end: BytePos) {
|
|
|
|
let lit = self.str_from_to(content_start, content_end);
|
2019-06-25 21:02:19 +03:00
|
|
|
unescape::unescape_byte_str(lit, &mut |range, c| {
|
|
|
|
if let Err(err) = c {
|
|
|
|
emit_unescape_error(
|
|
|
|
&self.sess.span_diagnostic,
|
|
|
|
lit,
|
2019-05-06 11:53:40 +03:00
|
|
|
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
|
2019-06-25 21:02:19 +03:00
|
|
|
unescape::Mode::ByteStr,
|
|
|
|
range,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
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));
|
2014-05-21 16:57:31 -07:00
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-03 11:14:09 +02:00
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
|
2018-05-31 16:53:30 -06:00
|
|
|
fn is_doc_comment(s: &str) -> bool {
|
2016-01-03 11:14:09 +02:00
|
|
|
let res = (s.starts_with("///") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'/') ||
|
|
|
|
s.starts_with("//!");
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("is {:?} a doc comment? {}", s, res);
|
2014-07-04 22:30:39 -07:00
|
|
|
res
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 16:53:30 -06:00
|
|
|
fn is_block_doc_comment(s: &str) -> bool {
|
2016-01-03 11:20:06 +02:00
|
|
|
// Prevent `/**/` from being parsed as a doc comment
|
2016-01-03 11:14:09 +02:00
|
|
|
let res = ((s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*') ||
|
2016-01-03 11:20:06 +02:00
|
|
|
s.starts_with("/*!")) && s.len() >= 5;
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("is {:?} a doc comment? {}", s, res);
|
2014-07-04 22:30:39 -07:00
|
|
|
res
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2015-04-24 17:30:41 +02:00
|
|
|
mod tests {
|
2014-05-21 16:57:31 -07:00
|
|
|
use super::*;
|
|
|
|
|
2019-06-08 19:45:12 +03:00
|
|
|
use crate::ast::CrateConfig;
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::symbol::Symbol;
|
2019-04-04 10:55:30 +03:00
|
|
|
use crate::source_map::{SourceMap, FilePathMapping};
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::feature_gate::UnstableFeatures;
|
|
|
|
use crate::parse::token;
|
|
|
|
use crate::diagnostics::plugin::ErrorMap;
|
2019-04-06 00:15:49 +02:00
|
|
|
use crate::with_default_globals;
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::io;
|
2017-12-14 08:09:19 +01:00
|
|
|
use std::path::PathBuf;
|
2019-04-06 00:15:49 +02:00
|
|
|
use syntax_pos::{BytePos, Span, NO_EXPANSION, edition::Edition};
|
2019-04-22 19:37:23 -07:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
|
2019-07-18 23:29:57 +03:00
|
|
|
use rustc_data_structures::sync::{Lock, Once};
|
2019-02-07 02:33:01 +09:00
|
|
|
|
2018-10-29 21:26:13 +01:00
|
|
|
fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess {
|
2017-09-16 19:24:08 +02:00
|
|
|
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
|
2018-10-29 21:26:13 +01:00
|
|
|
Some(sm.clone()),
|
2018-01-28 18:37:55 -08:00
|
|
|
false,
|
2019-03-13 14:06:25 +01:00
|
|
|
false,
|
2017-09-16 19:24:08 +02:00
|
|
|
false);
|
2017-01-17 01:14:53 +00:00
|
|
|
ParseSess {
|
2019-03-07 11:15:47 -08:00
|
|
|
span_diagnostic: errors::Handler::with_emitter(true, None, Box::new(emitter)),
|
2017-01-17 01:14:53 +00:00
|
|
|
unstable_features: UnstableFeatures::from_environment(),
|
2018-08-18 13:55:43 +03:00
|
|
|
config: CrateConfig::default(),
|
2018-02-15 10:52:26 +01:00
|
|
|
included_mod_stack: Lock::new(Vec::new()),
|
2018-10-29 21:26:13 +01:00
|
|
|
source_map: sm,
|
2018-08-18 13:55:43 +03:00
|
|
|
missing_fragment_specifiers: Lock::new(FxHashSet::default()),
|
2018-02-15 10:52:26 +01:00
|
|
|
raw_identifier_spans: Lock::new(Vec::new()),
|
2018-03-07 02:43:33 +01:00
|
|
|
registered_diagnostics: Lock::new(ErrorMap::new()),
|
2018-07-13 23:48:15 -05:00
|
|
|
buffered_lints: Lock::new(vec![]),
|
2019-04-06 00:15:49 +02:00
|
|
|
edition: Edition::from_session(),
|
2019-05-06 16:00:21 -07:00
|
|
|
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
|
2019-06-09 07:58:40 -03:00
|
|
|
param_attr_spans: Lock::new(Vec::new()),
|
2019-06-17 01:18:22 +02:00
|
|
|
let_chains_spans: Lock::new(Vec::new()),
|
2019-07-02 04:10:19 +02:00
|
|
|
async_closure_spans: Lock::new(Vec::new()),
|
2019-07-18 23:29:57 +03:00
|
|
|
injected_crate_name: Once::new(),
|
2017-01-17 01:14:53 +00:00
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// open a string reader for the given string
|
2018-10-29 21:26:13 +01:00
|
|
|
fn setup<'a>(sm: &SourceMap,
|
2017-01-17 01:14:53 +00:00
|
|
|
sess: &'a ParseSess,
|
2016-01-03 11:14:09 +02:00
|
|
|
teststr: String)
|
|
|
|
-> StringReader<'a> {
|
2018-12-04 15:18:03 -05:00
|
|
|
let sf = sm.new_source_file(PathBuf::from(teststr.clone()).into(), teststr);
|
2019-07-03 13:30:12 +03:00
|
|
|
StringReader::new(sess, sf, None)
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn t1() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
|
|
|
let mut string_reader = setup(&sm,
|
2018-03-07 02:44:10 +01:00
|
|
|
&sh,
|
|
|
|
"/* my source file */ fn main() { println!(\"zebra\"); }\n"
|
|
|
|
.to_string());
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(string_reader.next_token(), token::Comment);
|
|
|
|
assert_eq!(string_reader.next_token(), token::Whitespace);
|
2018-03-07 02:44:10 +01:00
|
|
|
let tok1 = string_reader.next_token();
|
2019-06-05 09:39:34 +03:00
|
|
|
let tok2 = Token::new(
|
2019-06-08 19:45:12 +03:00
|
|
|
mk_ident("fn"),
|
2019-06-05 09:39:34 +03:00
|
|
|
Span::new(BytePos(21), BytePos(23), NO_EXPANSION),
|
|
|
|
);
|
2019-06-04 18:48:40 +03:00
|
|
|
assert_eq!(tok1.kind, tok2.kind);
|
|
|
|
assert_eq!(tok1.span, tok2.span);
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(string_reader.next_token(), token::Whitespace);
|
2018-03-07 02:44:10 +01:00
|
|
|
// read another token:
|
|
|
|
let tok3 = string_reader.next_token();
|
2019-07-02 17:08:11 +03:00
|
|
|
assert_eq!(string_reader.pos.clone(), BytePos(28));
|
2019-06-05 09:39:34 +03:00
|
|
|
let tok4 = Token::new(
|
|
|
|
mk_ident("main"),
|
|
|
|
Span::new(BytePos(24), BytePos(28), NO_EXPANSION),
|
|
|
|
);
|
2019-06-04 18:48:40 +03:00
|
|
|
assert_eq!(tok3.kind, tok4.kind);
|
|
|
|
assert_eq!(tok3.span, tok4.span);
|
2019-07-02 17:08:11 +03:00
|
|
|
|
|
|
|
assert_eq!(string_reader.next_token(), token::OpenDelim(token::Paren));
|
2018-03-07 02:44:10 +01:00
|
|
|
assert_eq!(string_reader.pos.clone(), BytePos(29))
|
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// check that the given reader produces the desired stream
|
|
|
|
// of tokens (stop checking after exhausting the expected vec)
|
2019-06-04 17:55:23 +03:00
|
|
|
fn check_tokenization(mut string_reader: StringReader<'_>, expected: Vec<TokenKind>) {
|
2015-01-31 12:20:46 -05:00
|
|
|
for expected_tok in &expected {
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(&string_reader.next_token(), expected_tok);
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 02:01:44 +11:00
|
|
|
// make the identifier by looking up the string in the interner
|
2019-06-04 17:55:23 +03:00
|
|
|
fn mk_ident(id: &str) -> TokenKind {
|
2019-06-08 19:45:12 +03:00
|
|
|
token::Ident(Symbol::intern(id), false)
|
2019-05-19 01:04:26 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 17:55:23 +03:00
|
|
|
fn mk_lit(kind: token::LitKind, symbol: &str, suffix: Option<&str>) -> TokenKind {
|
|
|
|
TokenKind::lit(kind, Symbol::intern(symbol), suffix.map(Symbol::intern))
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn doublecolonparsing() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
|
|
|
check_tokenization(setup(&sm, &sh, "a b".to_string()),
|
2018-03-07 02:44:10 +01:00
|
|
|
vec![mk_ident("a"), token::Whitespace, mk_ident("b")]);
|
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn dcparsing_2() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
|
|
|
check_tokenization(setup(&sm, &sh, "a::b".to_string()),
|
2018-03-07 02:44:10 +01:00
|
|
|
vec![mk_ident("a"), token::ModSep, mk_ident("b")]);
|
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn dcparsing_3() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
|
|
|
check_tokenization(setup(&sm, &sh, "a ::b".to_string()),
|
2018-03-07 02:44:10 +01:00
|
|
|
vec![mk_ident("a"), token::Whitespace, token::ModSep, mk_ident("b")]);
|
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn dcparsing_4() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
|
|
|
check_tokenization(setup(&sm, &sh, "a:: b".to_string()),
|
2018-03-07 02:44:10 +01:00
|
|
|
vec![mk_ident("a"), token::ModSep, token::Whitespace, mk_ident("b")]);
|
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn character_a() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "'a'".to_string()).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::Char, "a", None));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn character_space() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "' '".to_string()).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::Char, " ", None));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn character_escaped() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "'\\n'".to_string()).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::Char, "\\n", None));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn lifetime_name() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "'abc".to_string()).next_token(),
|
2019-06-05 11:00:22 +03:00
|
|
|
token::Lifetime(Symbol::intern("'abc")));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn raw_string() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "r###\"\"#a\\b\x00c\"\"###".to_string()).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::StrRaw(3), "\"#a\\b\x00c\"", None));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2014-11-19 15:48:38 +11:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn literal_suffixes() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
2018-03-07 02:44:10 +01:00
|
|
|
macro_rules! test {
|
|
|
|
($input: expr, $tok_type: ident, $tok_contents: expr) => {{
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, format!("{}suffix", $input)).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::$tok_type, $tok_contents, Some("suffix")));
|
2018-03-07 02:44:10 +01:00
|
|
|
// with a whitespace separator:
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, format!("{} suffix", $input)).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::$tok_type, $tok_contents, None));
|
2018-03-07 02:44:10 +01:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
test!("'a'", Char, "a");
|
|
|
|
test!("b'a'", Byte, "a");
|
2019-05-19 01:04:26 +03:00
|
|
|
test!("\"a\"", Str, "a");
|
2018-03-07 02:44:10 +01:00
|
|
|
test!("b\"a\"", ByteStr, "a");
|
|
|
|
test!("1234", Integer, "1234");
|
|
|
|
test!("0b101", Integer, "0b101");
|
|
|
|
test!("0xABC", Integer, "0xABC");
|
|
|
|
test!("1.0", Float, "1.0");
|
|
|
|
test!("1.0e10", Float, "1.0e10");
|
|
|
|
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "2us".to_string()).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::Integer, "2", Some("us")));
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "r###\"raw\"###suffix".to_string()).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::StrRaw(3), "raw", Some("suffix")));
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(setup(&sm, &sh, "br###\"raw\"###suffix".to_string()).next_token(),
|
2019-05-19 01:04:26 +03:00
|
|
|
mk_lit(token::ByteStrRaw(3), "raw", Some("suffix")));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn line_doc_comments() {
|
2014-07-04 22:30:39 -07:00
|
|
|
assert!(is_doc_comment("///"));
|
|
|
|
assert!(is_doc_comment("/// blah"));
|
|
|
|
assert!(!is_doc_comment("////"));
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn nested_block_comments() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
|
|
|
let mut lexer = setup(&sm, &sh, "/* /* */ */'a'".to_string());
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(lexer.next_token(), token::Comment);
|
|
|
|
assert_eq!(lexer.next_token(), mk_lit(token::Char, "a", None));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
fn crlf_comments() {
|
2019-04-06 00:15:49 +02:00
|
|
|
with_default_globals(|| {
|
2018-10-29 21:26:13 +01:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
let sh = mk_sess(sm.clone());
|
|
|
|
let mut lexer = setup(&sm, &sh, "// test\r\n/// test\r\n".to_string());
|
2018-03-07 02:44:10 +01:00
|
|
|
let comment = lexer.next_token();
|
2019-06-04 18:48:40 +03:00
|
|
|
assert_eq!(comment.kind, token::Comment);
|
|
|
|
assert_eq!((comment.span.lo(), comment.span.hi()), (BytePos(0), BytePos(7)));
|
2019-06-04 20:42:43 +03:00
|
|
|
assert_eq!(lexer.next_token(), token::Whitespace);
|
|
|
|
assert_eq!(lexer.next_token(), token::DocComment(Symbol::intern("/// test")));
|
2018-03-07 02:44:10 +01:00
|
|
|
})
|
2015-05-13 22:06:26 +01:00
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
}
|