1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use codemap::{BytePos, CharPos, CodeMap, Pos, Span};
14 use errors::{FatalError, Handler};
15 use ext::tt::transcribe::tt_next_token;
16 use parse::token::str_to_ident;
22 use std::mem::replace;
25 pub use ext::tt::transcribe::{TtReader, new_tt_reader, new_tt_reader_with_doc_flag};
31 fn is_eof(&self) -> bool;
32 fn next_token(&mut self) -> TokenAndSpan;
33 /// Report a fatal error with the current span.
34 fn fatal(&self, &str) -> FatalError;
35 /// Report a non-fatal error with the current span.
37 fn peek(&self) -> TokenAndSpan;
38 /// Get a token the parser cares about.
39 fn real_token(&mut self) -> TokenAndSpan {
40 let mut t = self.next_token();
43 token::Whitespace | token::Comment | token::Shebang(_) => {
44 t = self.next_token();
53 #[derive(Clone, PartialEq, Eq, Debug)]
54 pub struct TokenAndSpan {
55 pub tok: token::Token,
59 pub struct StringReader<'a> {
60 pub span_diagnostic: &'a Handler,
61 /// The absolute offset within the codemap of the next character to read
63 /// The absolute offset within the codemap of the last character read(curr)
64 pub last_pos: BytePos,
65 /// The column of the next character to read
67 /// The last character to be read
68 pub curr: Option<char>,
69 pub filemap: Rc<codemap::FileMap>,
71 pub peek_tok: token::Token,
74 // cache a direct reference to the source text, so that we don't have to
75 // retrieve it via `self.filemap.src.as_ref().unwrap()` all the time.
76 source_text: Rc<String>
79 impl<'a> Reader for StringReader<'a> {
80 fn is_eof(&self) -> bool { self.curr.is_none() }
81 /// Return the next token. EFFECT: advances the string_reader.
82 fn next_token(&mut self) -> TokenAndSpan {
83 let ret_val = TokenAndSpan {
84 tok: replace(&mut self.peek_tok, token::Underscore),
90 fn fatal(&self, m: &str) -> FatalError {
91 self.fatal_span(self.peek_span, m)
93 fn err(&self, m: &str) {
94 self.err_span(self.peek_span, m)
96 fn peek(&self) -> TokenAndSpan {
97 // FIXME(pcwalton): Bad copy!
99 tok: self.peek_tok.clone(),
105 impl<'a> Reader for TtReader<'a> {
106 fn is_eof(&self) -> bool {
107 self.cur_tok == token::Eof
109 fn next_token(&mut self) -> TokenAndSpan {
110 let r = tt_next_token(self);
111 debug!("TtReader: r={:?}", r);
114 fn fatal(&self, m: &str) -> FatalError {
115 self.sp_diag.span_fatal(self.cur_span, m)
117 fn err(&self, m: &str) {
118 self.sp_diag.span_err(self.cur_span, m);
120 fn peek(&self) -> TokenAndSpan {
122 tok: self.cur_tok.clone(),
128 impl<'a> StringReader<'a> {
129 /// For comments.rs, which hackily pokes into pos and curr
130 pub fn new_raw<'b>(span_diagnostic: &'b Handler,
131 filemap: Rc<codemap::FileMap>) -> StringReader<'b> {
132 if filemap.src.is_none() {
133 span_diagnostic.bug(&format!("Cannot lex filemap without source: {}",
137 let source_text = (*filemap.src.as_ref().unwrap()).clone();
139 let mut sr = StringReader {
140 span_diagnostic: span_diagnostic,
141 pos: filemap.start_pos,
142 last_pos: filemap.start_pos,
146 /* dummy values; not read */
147 peek_tok: token::Eof,
148 peek_span: codemap::DUMMY_SP,
149 source_text: source_text
155 pub fn new<'b>(span_diagnostic: &'b Handler,
156 filemap: Rc<codemap::FileMap>) -> StringReader<'b> {
157 let mut sr = StringReader::new_raw(span_diagnostic, filemap);
162 pub fn curr_is(&self, c: char) -> bool {
166 /// Report a fatal lexical error with a given span.
167 pub fn fatal_span(&self, sp: Span, m: &str) -> FatalError {
168 self.span_diagnostic.span_fatal(sp, m)
171 /// Report a lexical error with a given span.
172 pub fn err_span(&self, sp: Span, m: &str) {
173 self.span_diagnostic.span_err(sp, m)
176 /// Suggest some help with a given span.
177 pub fn help_span(&self, sp: Span, m: &str) {
178 self.span_diagnostic.span_help(sp, m)
181 /// Report a fatal error spanning [`from_pos`, `to_pos`).
182 fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> FatalError {
183 self.fatal_span(codemap::mk_sp(from_pos, to_pos), m)
186 /// Report a lexical error spanning [`from_pos`, `to_pos`).
187 fn err_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
188 self.err_span(codemap::mk_sp(from_pos, to_pos), m)
191 /// Suggest some help spanning [`from_pos`, `to_pos`).
192 fn help_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
193 self.help_span(codemap::mk_sp(from_pos, to_pos), m)
196 /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
197 /// escaped character to the error message
198 fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> FatalError {
199 let mut m = m.to_string();
201 for c in c.escape_default() { m.push(c) }
202 self.fatal_span_(from_pos, to_pos, &m[..])
205 /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
206 /// escaped character to the error message
207 fn err_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) {
208 let mut m = m.to_string();
210 for c in c.escape_default() { m.push(c) }
211 self.err_span_(from_pos, to_pos, &m[..]);
214 /// Report a lexical error spanning [`from_pos`, `to_pos`), appending the
215 /// offending string to the error message
216 fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> FatalError {
218 let from = self.byte_offset(from_pos).to_usize();
219 let to = self.byte_offset(to_pos).to_usize();
220 m.push_str(&self.source_text[from..to]);
221 self.fatal_span_(from_pos, to_pos, &m[..])
224 /// Advance peek_tok and peek_span to refer to the next token, and
225 /// possibly update the interner.
226 fn advance_token(&mut self) {
227 match self.scan_whitespace_or_comment() {
229 self.peek_span = comment.sp;
230 self.peek_tok = comment.tok;
234 self.peek_tok = token::Eof;
235 self.peek_span = codemap::mk_sp(self.filemap.end_pos, self.filemap.end_pos);
237 let start_bytepos = self.last_pos;
238 self.peek_tok = self.next_token_inner();
239 self.peek_span = codemap::mk_sp(start_bytepos,
246 fn byte_offset(&self, pos: BytePos) -> BytePos {
247 (pos - self.filemap.start_pos)
250 /// Calls `f` with a string slice of the source text spanning from `start`
251 /// up to but excluding `self.last_pos`, meaning the slice does not include
252 /// the character `self.curr`.
253 pub fn with_str_from<T, F>(&self, start: BytePos, f: F) -> T where
254 F: FnOnce(&str) -> T,
256 self.with_str_from_to(start, self.last_pos, f)
259 /// Create a Name from a given offset to the current offset, each
260 /// adjusted 1 towards each other (assumes that on either side there is a
261 /// single-byte delimiter).
262 pub fn name_from(&self, start: BytePos) -> ast::Name {
263 debug!("taking an ident from {:?} to {:?}", start, self.last_pos);
264 self.with_str_from(start, token::intern)
267 /// As name_from, with an explicit endpoint.
268 pub fn name_from_to(&self, start: BytePos, end: BytePos) -> ast::Name {
269 debug!("taking an ident from {:?} to {:?}", start, end);
270 self.with_str_from_to(start, end, token::intern)
273 /// Calls `f` with a string slice of the source text spanning from `start`
274 /// up to but excluding `end`.
275 fn with_str_from_to<T, F>(&self, start: BytePos, end: BytePos, f: F) -> T where
276 F: FnOnce(&str) -> T,
278 f(&self.source_text[self.byte_offset(start).to_usize()..
279 self.byte_offset(end).to_usize()])
282 /// Converts CRLF to LF in the given string, raising an error on bare CR.
283 fn translate_crlf<'b>(&self, start: BytePos,
284 s: &'b str, errmsg: &'b str) -> Cow<'b, str> {
287 let ch = char_at(s, i);
288 let next = i + ch.len_utf8();
290 if next < s.len() && char_at(s, next) == '\n' {
291 return translate_crlf_(self, start, s, errmsg, i).into();
293 let pos = start + BytePos(i as u32);
294 let end_pos = start + BytePos(next as u32);
295 self.err_span_(pos, end_pos, errmsg);
301 fn translate_crlf_(rdr: &StringReader, start: BytePos,
302 s: &str, errmsg: &str, mut i: usize) -> String {
303 let mut buf = String::with_capacity(s.len());
306 let ch = char_at(s, i);
307 let next = i + ch.len_utf8();
309 if j < i { buf.push_str(&s[j..i]); }
311 if next >= s.len() || char_at(s, next) != '\n' {
312 let pos = start + BytePos(i as u32);
313 let end_pos = start + BytePos(next as u32);
314 rdr.err_span_(pos, end_pos, errmsg);
319 if j < s.len() { buf.push_str(&s[j..]); }
325 /// Advance the StringReader by one character. If a newline is
326 /// discovered, add it to the FileMap's list of line start offsets.
327 pub fn bump(&mut self) {
328 self.last_pos = self.pos;
329 let current_byte_offset = self.byte_offset(self.pos).to_usize();
330 if current_byte_offset < self.source_text.len() {
331 assert!(self.curr.is_some());
332 let last_char = self.curr.unwrap();
333 let ch = char_at(&self.source_text, current_byte_offset);
334 let next = current_byte_offset + ch.len_utf8();
335 let byte_offset_diff = next - current_byte_offset;
336 self.pos = self.pos + Pos::from_usize(byte_offset_diff);
337 self.curr = Some(ch);
338 self.col = self.col + CharPos(1);
339 if last_char == '\n' {
340 self.filemap.next_line(self.last_pos);
341 self.col = CharPos(0);
344 if byte_offset_diff > 1 {
345 self.filemap.record_multibyte_char(self.last_pos, byte_offset_diff);
352 pub fn nextch(&self) -> Option<char> {
353 let offset = self.byte_offset(self.pos).to_usize();
354 if offset < self.source_text.len() {
355 Some(char_at(&self.source_text, offset))
361 pub fn nextch_is(&self, c: char) -> bool {
362 self.nextch() == Some(c)
365 pub fn nextnextch(&self) -> Option<char> {
366 let offset = self.byte_offset(self.pos).to_usize();
367 let s = &self.source_text[..];
368 if offset >= s.len() { return None }
369 let next = offset + char_at(s, offset).len_utf8();
371 Some(char_at(s, next))
377 pub fn nextnextch_is(&self, c: char) -> bool {
378 self.nextnextch() == Some(c)
381 /// Eats <XID_start><XID_continue>*, if possible.
382 fn scan_optional_raw_name(&mut self) -> Option<ast::Name> {
383 if !ident_start(self.curr) {
386 let start = self.last_pos;
387 while ident_continue(self.curr) {
391 self.with_str_from(start, |string| {
395 Some(token::intern(string))
400 /// PRECONDITION: self.curr is not whitespace
401 /// Eats any kind of comment.
402 fn scan_comment(&mut self) -> Option<TokenAndSpan> {
405 if c.is_whitespace() {
406 self.span_diagnostic.span_err(codemap::mk_sp(self.last_pos, self.last_pos),
407 "called consume_any_line_comment, but there was whitespace");
413 if self.curr_is('/') {
414 match self.nextch() {
419 // line comments starting with "///" or "//!" are doc-comments
420 let doc_comment = self.curr_is('/') || self.curr_is('!');
421 let start_bpos = if doc_comment {
422 self.pos - BytePos(3)
424 self.last_pos - BytePos(2)
427 while !self.is_eof() {
428 match self.curr.unwrap() {
431 if self.nextch_is('\n') {
434 } else if doc_comment {
435 self.err_span_(self.last_pos, self.pos,
436 "bare CR not allowed in doc-comment");
444 return if doc_comment {
445 self.with_str_from(start_bpos, |string| {
446 // comments with only more "/"s are not doc comments
447 let tok = if is_doc_comment(string) {
448 token::DocComment(token::intern(string))
455 sp: codemap::mk_sp(start_bpos, self.last_pos)
461 sp: codemap::mk_sp(start_bpos, self.last_pos)
466 self.bump(); self.bump();
467 self.scan_block_comment()
471 } else if self.curr_is('#') {
472 if self.nextch_is('!') {
474 // Parse an inner attribute.
475 if self.nextnextch_is('[') {
479 // I guess this is the only way to figure out if
480 // we're at the beginning of the file...
481 let cmap = CodeMap::new();
482 cmap.files.borrow_mut().push(self.filemap.clone());
483 let loc = cmap.lookup_char_pos_adj(self.last_pos);
484 debug!("Skipping a shebang");
485 if loc.line == 1 && loc.col == CharPos(0) {
486 // FIXME: Add shebang "token", return it
487 let start = self.last_pos;
488 while !self.curr_is('\n') && !self.is_eof() { self.bump(); }
489 return Some(TokenAndSpan {
490 tok: token::Shebang(self.name_from(start)),
491 sp: codemap::mk_sp(start, self.last_pos)
501 /// If there is whitespace, shebang, or a comment, scan it. Otherwise,
503 fn scan_whitespace_or_comment(&mut self) -> Option<TokenAndSpan> {
504 match self.curr.unwrap_or('\0') {
505 // # to handle shebang at start of file -- this is the entry point
506 // for skipping over all "junk"
508 let c = self.scan_comment();
509 debug!("scanning a comment {:?}", c);
512 c if is_whitespace(Some(c)) => {
513 let start_bpos = self.last_pos;
514 while is_whitespace(self.curr) { self.bump(); }
515 let c = Some(TokenAndSpan {
516 tok: token::Whitespace,
517 sp: codemap::mk_sp(start_bpos, self.last_pos)
519 debug!("scanning whitespace: {:?}", c);
526 /// Might return a sugared-doc-attr
527 fn scan_block_comment(&mut self) -> Option<TokenAndSpan> {
528 // block comments starting with "/**" or "/*!" are doc-comments
529 let is_doc_comment = self.curr_is('*') || self.curr_is('!');
530 let start_bpos = self.last_pos - BytePos(2);
532 let mut level: isize = 1;
533 let mut has_cr = false;
536 let msg = if is_doc_comment {
537 "unterminated block doc-comment"
539 "unterminated block comment"
541 let last_bpos = self.last_pos;
542 panic!(self.fatal_span_(start_bpos, last_bpos, msg));
544 let n = self.curr.unwrap();
546 '/' if self.nextch_is('*') => {
550 '*' if self.nextch_is('/') => {
562 self.with_str_from(start_bpos, |string| {
563 // but comments with only "*"s between two "/"s are not
564 let tok = if is_block_doc_comment(string) {
565 let string = if has_cr {
566 self.translate_crlf(start_bpos, string,
567 "bare CR not allowed in block doc-comment")
568 } else { string.into() };
569 token::DocComment(token::intern(&string[..]))
576 sp: codemap::mk_sp(start_bpos, self.last_pos)
581 /// Scan through any digits (base `scan_radix`) or underscores,
582 /// and return how many digits there were.
584 /// `real_radix` represents the true radix of the number we're
585 /// interested in, and errors will be emitted for any digits
586 /// between `real_radix` and `scan_radix`.
587 fn scan_digits(&mut self, real_radix: u32, scan_radix: u32) -> usize {
588 assert!(real_radix <= scan_radix);
592 if c == Some('_') { debug!("skipping a _"); self.bump(); continue; }
593 match c.and_then(|cc| cc.to_digit(scan_radix)) {
595 debug!("{:?} in scan_digits", c);
596 // check that the hypothetical digit is actually
597 // in range for the true radix
598 if c.unwrap().to_digit(real_radix).is_none() {
599 self.err_span_(self.last_pos, self.pos,
600 &format!("invalid digit for a base {} literal",
611 /// Lex a LIT_INTEGER or a LIT_FLOAT
612 fn scan_number(&mut self, c: char) -> token::Lit {
615 let start_bpos = self.last_pos;
620 match self.curr.unwrap_or('\0') {
621 'b' => { self.bump(); base = 2; num_digits = self.scan_digits(2, 10); }
622 'o' => { self.bump(); base = 8; num_digits = self.scan_digits(8, 10); }
623 'x' => { self.bump(); base = 16; num_digits = self.scan_digits(16, 16); }
624 '0'...'9' | '_' | '.' => {
625 num_digits = self.scan_digits(10, 10) + 1;
629 return token::Integer(self.name_from(start_bpos));
632 } else if c.is_digit(10) {
633 num_digits = self.scan_digits(10, 10) + 1;
639 self.err_span_(start_bpos, self.last_pos, "no valid digits found for number");
640 return token::Integer(token::intern("0"));
643 // might be a float, but don't be greedy if this is actually an
644 // integer literal followed by field/method access or a range pattern
645 // (`0..2` and `12.foo()`)
646 if self.curr_is('.') && !self.nextch_is('.') && !self.nextch().unwrap_or('\0')
648 // might have stuff after the ., and if it does, it needs to start
651 if self.curr.unwrap_or('\0').is_digit(10) {
652 self.scan_digits(10, 10);
653 self.scan_float_exponent();
655 let last_pos = self.last_pos;
656 self.check_float_base(start_bpos, last_pos, base);
657 return token::Float(self.name_from(start_bpos));
659 // it might be a float if it has an exponent
660 if self.curr_is('e') || self.curr_is('E') {
661 self.scan_float_exponent();
662 let last_pos = self.last_pos;
663 self.check_float_base(start_bpos, last_pos, base);
664 return token::Float(self.name_from(start_bpos));
666 // but we certainly have an integer!
667 return token::Integer(self.name_from(start_bpos));
671 /// Scan over `n_digits` hex digits, stopping at `delim`, reporting an
672 /// error if too many or too few digits are encountered.
673 fn scan_hex_digits(&mut self,
676 below_0x7f_only: bool)
678 debug!("scanning {} digits until {:?}", n_digits, delim);
679 let start_bpos = self.last_pos;
680 let mut accum_int = 0;
682 let mut valid = true;
683 for _ in 0..n_digits {
685 let last_bpos = self.last_pos;
686 panic!(self.fatal_span_(start_bpos,
688 "unterminated numeric character escape"));
690 if self.curr_is(delim) {
691 let last_bpos = self.last_pos;
692 self.err_span_(start_bpos, last_bpos, "numeric character escape is too short");
696 let c = self.curr.unwrap_or('\x00');
698 accum_int += c.to_digit(16).unwrap_or_else(|| {
699 self.err_span_char(self.last_pos, self.pos,
700 "invalid character in numeric character escape", c);
708 if below_0x7f_only && accum_int >= 0x80 {
709 self.err_span_(start_bpos,
711 "this form of character escape may only be used \
712 with characters in the range [\\x00-\\x7f]");
716 match char::from_u32(accum_int) {
719 let last_bpos = self.last_pos;
720 self.err_span_(start_bpos, last_bpos, "invalid numeric character escape");
726 /// Scan for a single (possibly escaped) byte or char
727 /// in a byte, (non-raw) byte string, char, or (non-raw) string literal.
728 /// `start` is the position of `first_source_char`, which is already consumed.
730 /// Returns true if there was a valid char/byte, false otherwise.
731 fn scan_char_or_byte(&mut self, start: BytePos, first_source_char: char,
732 ascii_only: bool, delim: char) -> bool {
733 match first_source_char {
735 // '\X' for some X must be a character constant:
736 let escaped = self.curr;
737 let escaped_pos = self.last_pos;
740 None => {}, // EOF here is an error that will be checked later.
743 'n' | 'r' | 't' | '\\' | '\'' | '"' | '0' => true,
744 'x' => self.scan_byte_escape(delim, !ascii_only),
746 let valid = if self.curr_is('{') {
747 self.scan_unicode_escape(delim) && !ascii_only
749 self.err_span_(start, self.last_pos,
750 "incorrect unicode escape sequence");
751 self.help_span_(start, self.last_pos,
752 "format of unicode escape sequences is `\\u{…}`");
756 self.err_span_(start, self.last_pos,
757 "unicode escape sequences cannot be used as a byte or in \
764 '\n' if delim == '"' => {
765 self.consume_whitespace();
768 '\r' if delim == '"' && self.curr_is('\n') => {
769 self.consume_whitespace();
773 let last_pos = self.last_pos;
775 escaped_pos, last_pos,
776 if ascii_only { "unknown byte escape" }
777 else { "unknown character escape" },
780 self.help_span_(escaped_pos, last_pos,
781 "this is an isolated carriage return; consider checking \
782 your editor and version control settings")
784 if (e == '{' || e == '}') && !ascii_only {
785 self.help_span_(escaped_pos, last_pos,
786 "if used in a formatting string, \
787 curly braces are escaped with `{{` and `}}`")
795 '\t' | '\n' | '\r' | '\'' if delim == '\'' => {
796 let last_pos = self.last_pos;
799 if ascii_only { "byte constant must be escaped" }
800 else { "character constant must be escaped" },
805 if self.curr_is('\n') {
809 self.err_span_(start, self.last_pos,
810 "bare CR not allowed in string, use \\r instead");
814 _ => if ascii_only && first_source_char > '\x7F' {
815 let last_pos = self.last_pos;
818 "byte constant must be ASCII. \
819 Use a \\xHH escape for a non-ASCII byte", first_source_char);
826 /// Scan over a \u{...} escape
828 /// At this point, we have already seen the \ and the u, the { is the current character. We
829 /// will read at least one digit, and up to 6, and pass over the }.
830 fn scan_unicode_escape(&mut self, delim: char) -> bool {
831 self.bump(); // past the {
832 let start_bpos = self.last_pos;
834 let mut accum_int = 0;
835 let mut valid = true;
837 while !self.curr_is('}') && count <= 6 {
838 let c = match self.curr {
841 panic!(self.fatal_span_(start_bpos, self.last_pos,
842 "unterminated unicode escape (found EOF)"));
846 accum_int += c.to_digit(16).unwrap_or_else(|| {
848 panic!(self.fatal_span_(self.last_pos, self.pos,
849 "unterminated unicode escape (needed a `}`)"));
851 self.err_span_char(self.last_pos, self.pos,
852 "invalid character in unicode escape", c);
862 self.err_span_(start_bpos, self.last_pos,
863 "overlong unicode escape (can have at most 6 hex digits)");
867 if valid && (char::from_u32(accum_int).is_none() || count == 0) {
868 self.err_span_(start_bpos, self.last_pos, "invalid unicode character escape");
872 self.bump(); // past the ending }
876 /// Scan over a float exponent.
877 fn scan_float_exponent(&mut self) {
878 if self.curr_is('e') || self.curr_is('E') {
880 if self.curr_is('-') || self.curr_is('+') {
883 if self.scan_digits(10, 10) == 0 {
884 self.err_span_(self.last_pos, self.pos, "expected at least one digit in exponent")
889 /// Check that a base is valid for a floating literal, emitting a nice
890 /// error if it isn't.
891 fn check_float_base(&mut self, start_bpos: BytePos, last_bpos: BytePos, base: usize) {
893 16 => self.err_span_(start_bpos, last_bpos, "hexadecimal float literal is not \
895 8 => self.err_span_(start_bpos, last_bpos, "octal float literal is not supported"),
896 2 => self.err_span_(start_bpos, last_bpos, "binary float literal is not supported"),
901 fn binop(&mut self, op: token::BinOpToken) -> token::Token {
903 if self.curr_is('=') {
905 return token::BinOpEq(op);
907 return token::BinOp(op);
911 /// Return the next token from the string, advances the input past that
912 /// token, and updates the interner
913 fn next_token_inner(&mut self) -> token::Token {
915 if ident_start(c) && match (c.unwrap(), self.nextch(), self.nextnextch()) {
916 // Note: r as in r" or r#" is part of a raw string literal,
917 // b as in b' is part of a byte literal.
918 // They are not identifiers, and are handled further down.
919 ('r', Some('"'), _) | ('r', Some('#'), _) |
920 ('b', Some('"'), _) | ('b', Some('\''), _) |
921 ('b', Some('r'), Some('"')) | ('b', Some('r'), Some('#')) => false,
924 let start = self.last_pos;
925 while ident_continue(self.curr) {
929 return self.with_str_from(start, |string| {
933 // FIXME: perform NFKC normalization here. (Issue #2253)
934 if self.curr_is(':') && self.nextch_is(':') {
935 token::Ident(str_to_ident(string), token::ModName)
937 token::Ident(str_to_ident(string), token::Plain)
944 let num = self.scan_number(c.unwrap());
945 let suffix = self.scan_optional_raw_name();
946 debug!("next_token_inner: scanned number {:?}, {:?}", num, suffix);
947 return token::Literal(num, suffix)
950 match c.expect("next_token_inner called at EOF") {
952 ';' => { self.bump(); return token::Semi; }
953 ',' => { self.bump(); return token::Comma; }
956 return if self.curr_is('.') {
958 if self.curr_is('.') {
968 '(' => { self.bump(); return token::OpenDelim(token::Paren); }
969 ')' => { self.bump(); return token::CloseDelim(token::Paren); }
970 '{' => { self.bump(); return token::OpenDelim(token::Brace); }
971 '}' => { self.bump(); return token::CloseDelim(token::Brace); }
972 '[' => { self.bump(); return token::OpenDelim(token::Bracket); }
973 ']' => { self.bump(); return token::CloseDelim(token::Bracket); }
974 '@' => { self.bump(); return token::At; }
975 '#' => { self.bump(); return token::Pound; }
976 '~' => { self.bump(); return token::Tilde; }
977 '?' => { self.bump(); return token::Question; }
980 if self.curr_is(':') {
982 return token::ModSep;
988 '$' => { self.bump(); return token::Dollar; }
990 // Multi-byte tokens.
993 if self.curr_is('=') {
996 } else if self.curr_is('>') {
998 return token::FatArrow;
1005 if self.curr_is('=') {
1008 } else { return token::Not; }
1012 match self.curr.unwrap_or('\x00') {
1013 '=' => { self.bump(); return token::Le; }
1014 '<' => { return self.binop(token::Shl); }
1017 match self.curr.unwrap_or('\x00') {
1018 _ => { return token::LArrow; }
1021 _ => { return token::Lt; }
1026 match self.curr.unwrap_or('\x00') {
1027 '=' => { self.bump(); return token::Ge; }
1028 '>' => { return self.binop(token::Shr); }
1029 _ => { return token::Gt; }
1033 // Either a character constant 'a' OR a lifetime name 'abc
1035 let start = self.last_pos;
1037 // the eof will be picked up by the final `'` check below
1038 let c2 = self.curr.unwrap_or('\x00');
1041 // If the character is an ident start not followed by another single
1042 // quote, then this is a lifetime name:
1043 if ident_start(Some(c2)) && !self.curr_is('\'') {
1044 while ident_continue(self.curr) {
1048 // Include the leading `'` in the real identifier, for macro
1049 // expansion purposes. See #12512 for the gory details of why
1050 // this is necessary.
1051 let ident = self.with_str_from(start, |lifetime_name| {
1052 str_to_ident(&format!("'{}", lifetime_name))
1055 // Conjure up a "keyword checking ident" to make sure that
1056 // the lifetime name is not a keyword.
1057 let keyword_checking_ident =
1058 self.with_str_from(start, |lifetime_name| {
1059 str_to_ident(lifetime_name)
1061 let keyword_checking_token =
1062 &token::Ident(keyword_checking_ident, token::Plain);
1063 let last_bpos = self.last_pos;
1064 if keyword_checking_token.is_keyword(token::keywords::SelfValue) {
1065 self.err_span_(start,
1067 "invalid lifetime name: 'self \
1068 is no longer a special lifetime");
1069 } else if keyword_checking_token.is_any_keyword() &&
1070 !keyword_checking_token.is_keyword(token::keywords::Static)
1072 self.err_span_(start,
1074 "invalid lifetime name");
1076 return token::Lifetime(ident);
1079 // Otherwise it is a character constant:
1080 let valid = self.scan_char_or_byte(start, c2, /* ascii_only = */ false, '\'');
1081 if !self.curr_is('\'') {
1082 let last_bpos = self.last_pos;
1083 panic!(self.fatal_span_verbose(
1084 // Byte offsetting here is okay because the
1085 // character before position `start` is an
1086 // ascii single quote.
1087 start - BytePos(1), last_bpos,
1089 String::from("character literal may only contain one codepoint")));
1091 let id = if valid { self.name_from(start) } else { token::intern("0") };
1092 self.bump(); // advance curr past token
1093 let suffix = self.scan_optional_raw_name();
1094 return token::Literal(token::Char(id), suffix);
1098 let lit = match self.curr {
1099 Some('\'') => self.scan_byte(),
1100 Some('"') => self.scan_byte_string(),
1101 Some('r') => self.scan_raw_byte_string(),
1102 _ => unreachable!() // Should have been a token::Ident above.
1104 let suffix = self.scan_optional_raw_name();
1105 return token::Literal(lit, suffix);
1108 let start_bpos = self.last_pos;
1109 let mut valid = true;
1111 while !self.curr_is('"') {
1113 let last_bpos = self.last_pos;
1114 panic!(self.fatal_span_(start_bpos,
1116 "unterminated double quote string"));
1119 let ch_start = self.last_pos;
1120 let ch = self.curr.unwrap();
1122 valid &= self.scan_char_or_byte(ch_start, ch, /* ascii_only = */ false, '"');
1124 // adjust for the ASCII " at the start of the literal
1125 let id = if valid { self.name_from(start_bpos + BytePos(1)) }
1126 else { token::intern("??") };
1128 let suffix = self.scan_optional_raw_name();
1129 return token::Literal(token::Str_(id), suffix);
1132 let start_bpos = self.last_pos;
1134 let mut hash_count = 0;
1135 while self.curr_is('#') {
1141 let last_bpos = self.last_pos;
1142 panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
1143 } else if !self.curr_is('"') {
1144 let last_bpos = self.last_pos;
1145 let curr_char = self.curr.unwrap();
1146 panic!(self.fatal_span_char(start_bpos, last_bpos,
1147 "found invalid character; \
1148 only `#` is allowed in raw string delimitation",
1152 let content_start_bpos = self.last_pos;
1153 let mut content_end_bpos;
1154 let mut valid = true;
1157 let last_bpos = self.last_pos;
1158 panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
1160 //if self.curr_is('"') {
1161 //content_end_bpos = self.last_pos;
1162 //for _ in 0..hash_count {
1164 //if !self.curr_is('#') {
1166 let c = self.curr.unwrap();
1169 content_end_bpos = self.last_pos;
1170 for _ in 0..hash_count {
1172 if !self.curr_is('#') {
1179 if !self.nextch_is('\n') {
1180 let last_bpos = self.last_pos;
1181 self.err_span_(start_bpos, last_bpos, "bare CR not allowed in raw \
1182 string, use \\r instead");
1192 self.name_from_to(content_start_bpos, content_end_bpos)
1196 let suffix = self.scan_optional_raw_name();
1197 return token::Literal(token::StrRaw(id, hash_count), suffix);
1200 if self.nextch_is('>') {
1203 return token::RArrow;
1204 } else { return self.binop(token::Minus); }
1207 if self.nextch_is('&') {
1210 return token::AndAnd;
1211 } else { return self.binop(token::And); }
1214 match self.nextch() {
1215 Some('|') => { self.bump(); self.bump(); return token::OrOr; }
1216 _ => { return self.binop(token::Or); }
1219 '+' => { return self.binop(token::Plus); }
1220 '*' => { return self.binop(token::Star); }
1221 '/' => { return self.binop(token::Slash); }
1222 '^' => { return self.binop(token::Caret); }
1223 '%' => { return self.binop(token::Percent); }
1225 let last_bpos = self.last_pos;
1226 let bpos = self.pos;
1227 unicode_chars::check_for_substitution(&self, c);
1228 panic!(self.fatal_span_char(last_bpos, bpos, "unknown start of token", c))
1233 fn consume_whitespace(&mut self) {
1234 while is_whitespace(self.curr) && !self.is_eof() { self.bump(); }
1237 fn read_to_eol(&mut self) -> String {
1238 let mut val = String::new();
1239 while !self.curr_is('\n') && !self.is_eof() {
1240 val.push(self.curr.unwrap());
1243 if self.curr_is('\n') { self.bump(); }
1247 fn read_one_line_comment(&mut self) -> String {
1248 let val = self.read_to_eol();
1249 assert!((val.as_bytes()[0] == b'/' && val.as_bytes()[1] == b'/')
1250 || (val.as_bytes()[0] == b'#' && val.as_bytes()[1] == b'!'));
1254 fn consume_non_eol_whitespace(&mut self) {
1255 while is_whitespace(self.curr) && !self.curr_is('\n') && !self.is_eof() {
1260 fn peeking_at_comment(&self) -> bool {
1261 (self.curr_is('/') && self.nextch_is('/'))
1262 || (self.curr_is('/') && self.nextch_is('*'))
1263 // consider shebangs comments, but not inner attributes
1264 || (self.curr_is('#') && self.nextch_is('!') && !self.nextnextch_is('['))
1267 fn scan_byte(&mut self) -> token::Lit {
1269 let start = self.last_pos;
1271 // the eof will be picked up by the final `'` check below
1272 let c2 = self.curr.unwrap_or('\x00');
1275 let valid = self.scan_char_or_byte(start, c2, /* ascii_only = */ true, '\'');
1276 if !self.curr_is('\'') {
1277 // Byte offsetting here is okay because the
1278 // character before position `start` are an
1279 // ascii single quote and ascii 'b'.
1280 let last_pos = self.last_pos;
1281 panic!(self.fatal_span_verbose(
1282 start - BytePos(2), last_pos,
1283 "unterminated byte constant".to_string()));
1286 let id = if valid { self.name_from(start) } else { token::intern("?") };
1287 self.bump(); // advance curr past token
1288 return token::Byte(id);
1291 fn scan_byte_escape(&mut self, delim: char, below_0x7f_only: bool) -> bool {
1292 self.scan_hex_digits(2, delim, below_0x7f_only)
1295 fn scan_byte_string(&mut self) -> token::Lit {
1297 let start = self.last_pos;
1298 let mut valid = true;
1300 while !self.curr_is('"') {
1302 let last_pos = self.last_pos;
1303 panic!(self.fatal_span_(start, last_pos, "unterminated double quote byte string"));
1306 let ch_start = self.last_pos;
1307 let ch = self.curr.unwrap();
1309 valid &= self.scan_char_or_byte(ch_start, ch, /* ascii_only = */ true, '"');
1311 let id = if valid { self.name_from(start) } else { token::intern("??") };
1313 return token::ByteStr(id);
1316 fn scan_raw_byte_string(&mut self) -> token::Lit {
1317 let start_bpos = self.last_pos;
1319 let mut hash_count = 0;
1320 while self.curr_is('#') {
1326 let last_pos = self.last_pos;
1327 panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"));
1328 } else if !self.curr_is('"') {
1329 let last_pos = self.last_pos;
1330 let ch = self.curr.unwrap();
1331 panic!(self.fatal_span_char(start_bpos, last_pos,
1332 "found invalid character; \
1333 only `#` is allowed in raw string delimitation",
1337 let content_start_bpos = self.last_pos;
1338 let mut content_end_bpos;
1342 let last_pos = self.last_pos;
1343 panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"))
1346 content_end_bpos = self.last_pos;
1347 for _ in 0..hash_count {
1349 if !self.curr_is('#') {
1355 Some(c) => if c > '\x7F' {
1356 let last_pos = self.last_pos;
1358 last_pos, last_pos, "raw byte string must be ASCII", c);
1364 return token::ByteStrRaw(self.name_from_to(content_start_bpos,
1370 pub fn is_whitespace(c: Option<char>) -> bool {
1371 match c.unwrap_or('\x00') { // None can be null for now... it's not whitespace
1372 ' ' | '\n' | '\t' | '\r' => true,
1377 fn in_range(c: Option<char>, lo: char, hi: char) -> bool {
1379 Some(c) => lo <= c && c <= hi,
1384 fn is_dec_digit(c: Option<char>) -> bool { return in_range(c, '0', '9'); }
1386 pub fn is_doc_comment(s: &str) -> bool {
1387 let res = (s.starts_with("///") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'/')
1388 || s.starts_with("//!");
1389 debug!("is {:?} a doc comment? {}", s, res);
1393 pub fn is_block_doc_comment(s: &str) -> bool {
1394 let res = ((s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*')
1395 || s.starts_with("/*!"))
1396 && s.len() >= 5; // Prevent `/**/` from being parsed as a doc comment
1397 debug!("is {:?} a doc comment? {}", s, res);
1401 fn ident_start(c: Option<char>) -> bool {
1402 let c = match c { Some(c) => c, None => return false };
1404 (c >= 'a' && c <= 'z')
1405 || (c >= 'A' && c <= 'Z')
1407 || (c > '\x7f' && c.is_xid_start())
1410 fn ident_continue(c: Option<char>) -> bool {
1411 let c = match c { Some(c) => c, None => return false };
1413 (c >= 'a' && c <= 'z')
1414 || (c >= 'A' && c <= 'Z')
1415 || (c >= '0' && c <= '9')
1417 || (c > '\x7f' && c.is_xid_continue())
1424 use codemap::{BytePos, CodeMap, Span, NO_EXPANSION};
1427 use parse::token::{str_to_ident};
1430 fn mk_sh() -> diagnostic::Handler {
1431 // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
1432 let emitter = diagnostic::EmitterWriter::new(Box::new(io::sink()), None);
1433 let handler = diagnostic::Handler::with_emitter(true, Box::new(emitter));
1434 diagnostic::Handler::new(handler, CodeMap::new())
1437 // open a string reader for the given string
1438 fn setup<'a>(span_handler: &'a diagnostic::Handler,
1439 teststr: String) -> StringReader<'a> {
1440 let fm = span_handler.cm.new_filemap("zebra.rs".to_string(), teststr);
1441 StringReader::new(span_handler, fm)
1445 let span_handler = mk_sh();
1446 let mut string_reader = setup(&span_handler,
1447 "/* my source file */ \
1448 fn main() { println!(\"zebra\"); }\n".to_string());
1449 let id = str_to_ident("fn");
1450 assert_eq!(string_reader.next_token().tok, token::Comment);
1451 assert_eq!(string_reader.next_token().tok, token::Whitespace);
1452 let tok1 = string_reader.next_token();
1453 let tok2 = TokenAndSpan{
1454 tok:token::Ident(id, token::Plain),
1455 sp:Span {lo:BytePos(21),hi:BytePos(23),expn_id: NO_EXPANSION}};
1456 assert_eq!(tok1,tok2);
1457 assert_eq!(string_reader.next_token().tok, token::Whitespace);
1458 // the 'main' id is already read:
1459 assert_eq!(string_reader.last_pos.clone(), BytePos(28));
1460 // read another token:
1461 let tok3 = string_reader.next_token();
1462 let tok4 = TokenAndSpan{
1463 tok:token::Ident(str_to_ident("main"), token::Plain),
1464 sp:Span {lo:BytePos(24),hi:BytePos(28),expn_id: NO_EXPANSION}};
1465 assert_eq!(tok3,tok4);
1466 // the lparen is already read:
1467 assert_eq!(string_reader.last_pos.clone(), BytePos(29))
1470 // check that the given reader produces the desired stream
1471 // of tokens (stop checking after exhausting the expected vec)
1472 fn check_tokenization (mut string_reader: StringReader, expected: Vec<token::Token> ) {
1473 for expected_tok in &expected {
1474 assert_eq!(&string_reader.next_token().tok, expected_tok);
1478 // make the identifier by looking up the string in the interner
1479 fn mk_ident(id: &str, style: token::IdentStyle) -> token::Token {
1480 token::Ident(str_to_ident(id), style)
1483 #[test] fn doublecolonparsing () {
1484 check_tokenization(setup(&mk_sh(), "a b".to_string()),
1485 vec![mk_ident("a", token::Plain),
1487 mk_ident("b", token::Plain)]);
1490 #[test] fn dcparsing_2 () {
1491 check_tokenization(setup(&mk_sh(), "a::b".to_string()),
1492 vec![mk_ident("a",token::ModName),
1494 mk_ident("b", token::Plain)]);
1497 #[test] fn dcparsing_3 () {
1498 check_tokenization(setup(&mk_sh(), "a ::b".to_string()),
1499 vec![mk_ident("a", token::Plain),
1502 mk_ident("b", token::Plain)]);
1505 #[test] fn dcparsing_4 () {
1506 check_tokenization(setup(&mk_sh(), "a:: b".to_string()),
1507 vec![mk_ident("a",token::ModName),
1510 mk_ident("b", token::Plain)]);
1513 #[test] fn character_a() {
1514 assert_eq!(setup(&mk_sh(), "'a'".to_string()).next_token().tok,
1515 token::Literal(token::Char(token::intern("a")), None));
1518 #[test] fn character_space() {
1519 assert_eq!(setup(&mk_sh(), "' '".to_string()).next_token().tok,
1520 token::Literal(token::Char(token::intern(" ")), None));
1523 #[test] fn character_escaped() {
1524 assert_eq!(setup(&mk_sh(), "'\\n'".to_string()).next_token().tok,
1525 token::Literal(token::Char(token::intern("\\n")), None));
1528 #[test] fn lifetime_name() {
1529 assert_eq!(setup(&mk_sh(), "'abc".to_string()).next_token().tok,
1530 token::Lifetime(token::str_to_ident("'abc")));
1533 #[test] fn raw_string() {
1534 assert_eq!(setup(&mk_sh(),
1535 "r###\"\"#a\\b\x00c\"\"###".to_string()).next_token()
1537 token::Literal(token::StrRaw(token::intern("\"#a\\b\x00c\""), 3), None));
1540 #[test] fn literal_suffixes() {
1542 ($input: expr, $tok_type: ident, $tok_contents: expr) => {{
1543 assert_eq!(setup(&mk_sh(), format!("{}suffix", $input)).next_token().tok,
1544 token::Literal(token::$tok_type(token::intern($tok_contents)),
1545 Some(token::intern("suffix"))));
1546 // with a whitespace separator:
1547 assert_eq!(setup(&mk_sh(), format!("{} suffix", $input)).next_token().tok,
1548 token::Literal(token::$tok_type(token::intern($tok_contents)),
1553 test!("'a'", Char, "a");
1554 test!("b'a'", Byte, "a");
1555 test!("\"a\"", Str_, "a");
1556 test!("b\"a\"", ByteStr, "a");
1557 test!("1234", Integer, "1234");
1558 test!("0b101", Integer, "0b101");
1559 test!("0xABC", Integer, "0xABC");
1560 test!("1.0", Float, "1.0");
1561 test!("1.0e10", Float, "1.0e10");
1563 assert_eq!(setup(&mk_sh(), "2us".to_string()).next_token().tok,
1564 token::Literal(token::Integer(token::intern("2")),
1565 Some(token::intern("us"))));
1566 assert_eq!(setup(&mk_sh(), "r###\"raw\"###suffix".to_string()).next_token().tok,
1567 token::Literal(token::StrRaw(token::intern("raw"), 3),
1568 Some(token::intern("suffix"))));
1569 assert_eq!(setup(&mk_sh(), "br###\"raw\"###suffix".to_string()).next_token().tok,
1570 token::Literal(token::ByteStrRaw(token::intern("raw"), 3),
1571 Some(token::intern("suffix"))));
1574 #[test] fn line_doc_comments() {
1575 assert!(is_doc_comment("///"));
1576 assert!(is_doc_comment("/// blah"));
1577 assert!(!is_doc_comment("////"));
1580 #[test] fn nested_block_comments() {
1582 let mut lexer = setup(&sh, "/* /* */ */'a'".to_string());
1583 match lexer.next_token().tok {
1584 token::Comment => { },
1585 _ => panic!("expected a comment!")
1587 assert_eq!(lexer.next_token().tok, token::Literal(token::Char(token::intern("a")), None));
1590 #[test] fn crlf_comments() {
1592 let mut lexer = setup(&sh, "// test\r\n/// test\r\n".to_string());
1593 let comment = lexer.next_token();
1594 assert_eq!(comment.tok, token::Comment);
1595 assert_eq!(comment.sp, ::codemap::mk_sp(BytePos(0), BytePos(7)));
1596 assert_eq!(lexer.next_token().tok, token::Whitespace);
1597 assert_eq!(lexer.next_token().tok, token::DocComment(token::intern("/// test")));