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.
11 use ast::{self, Ident};
12 use syntax_pos::{self, BytePos, CharPos, Pos, Span, NO_EXPANSION};
13 use codemap::{CodeMap, FilePathMapping};
14 use errors::{FatalError, DiagnosticBuilder};
15 use parse::{token, ParseSess};
17 use symbol::{Symbol, keywords};
18 use std_unicode::property::Pattern_White_Space;
22 use std::mem::replace;
29 #[derive(Clone, PartialEq, Eq, Debug)]
30 pub struct TokenAndSpan {
31 pub tok: token::Token,
35 impl Default for TokenAndSpan {
36 fn default() -> Self {
37 TokenAndSpan { tok: token::Underscore, sp: syntax_pos::DUMMY_SP }
41 pub struct StringReader<'a> {
42 pub sess: &'a ParseSess,
43 /// The absolute offset within the codemap of the next character to read
44 pub next_pos: BytePos,
45 /// The absolute offset within the codemap of the current character
47 /// The column of the next character to read
49 /// The current character (which has been read from self.pos)
51 pub filemap: Rc<syntax_pos::FileMap>,
52 /// If Some, stop reading the source at this position (inclusive).
53 pub terminator: Option<BytePos>,
54 /// Whether to record new-lines and multibyte chars in filemap.
55 /// This is only necessary the first time a filemap is lexed.
56 /// If part of a filemap is being re-lexed, this should be set to false.
57 pub save_new_lines_and_multibyte: bool,
59 pub peek_tok: token::Token,
61 pub fatal_errs: Vec<DiagnosticBuilder<'a>>,
62 // cache a direct reference to the source text, so that we don't have to
63 // retrieve it via `self.filemap.src.as_ref().unwrap()` all the time.
64 source_text: Rc<String>,
65 /// Stack of open delimiters and their spans. Used for error message.
68 open_braces: Vec<(token::DelimToken, Span)>,
71 fn mk_sp(lo: BytePos, hi: BytePos) -> Span {
72 Span { lo: lo, hi: hi, ctxt: NO_EXPANSION }
75 impl<'a> StringReader<'a> {
76 fn next_token(&mut self) -> TokenAndSpan {
77 let res = self.try_next_token();
78 self.unwrap_or_abort(res)
80 fn unwrap_or_abort(&mut self, res: Result<TokenAndSpan, ()>) -> TokenAndSpan {
84 self.emit_fatal_errors();
89 fn try_real_token(&mut self) -> Result<TokenAndSpan, ()> {
90 let mut t = self.try_next_token()?;
93 token::Whitespace | token::Comment | token::Shebang(_) => {
94 t = self.try_next_token()?;
99 self.token = t.tok.clone();
103 pub fn real_token(&mut self) -> TokenAndSpan {
104 let res = self.try_real_token();
105 self.unwrap_or_abort(res)
107 fn is_eof(&self) -> bool {
108 if self.ch.is_none() {
112 match self.terminator {
113 Some(t) => self.next_pos > t,
117 /// Return the next token. EFFECT: advances the string_reader.
118 pub fn try_next_token(&mut self) -> Result<TokenAndSpan, ()> {
119 assert!(self.fatal_errs.is_empty());
120 let ret_val = TokenAndSpan {
121 tok: replace(&mut self.peek_tok, token::Underscore),
124 self.advance_token()?;
127 fn fatal(&self, m: &str) -> FatalError {
128 self.fatal_span(self.peek_span, m)
130 pub fn emit_fatal_errors(&mut self) {
131 for err in &mut self.fatal_errs {
134 self.fatal_errs.clear();
136 pub fn peek(&self) -> TokenAndSpan {
137 // FIXME(pcwalton): Bad copy!
139 tok: self.peek_tok.clone(),
145 impl<'a> StringReader<'a> {
146 /// For comments.rs, which hackily pokes into next_pos and ch
147 pub fn new_raw<'b>(sess: &'a ParseSess, filemap: Rc<syntax_pos::FileMap>) -> Self {
148 let mut sr = StringReader::new_raw_internal(sess, filemap);
153 fn new_raw_internal(sess: &'a ParseSess, filemap: Rc<syntax_pos::FileMap>) -> Self {
154 if filemap.src.is_none() {
155 sess.span_diagnostic.bug(&format!("Cannot lex filemap without source: {}",
159 let source_text = (*filemap.src.as_ref().unwrap()).clone();
163 next_pos: filemap.start_pos,
164 pos: filemap.start_pos,
169 save_new_lines_and_multibyte: true,
170 // dummy values; not read
171 peek_tok: token::Eof,
172 peek_span: syntax_pos::DUMMY_SP,
173 source_text: source_text,
174 fatal_errs: Vec::new(),
176 span: syntax_pos::DUMMY_SP,
177 open_braces: Vec::new(),
181 pub fn new(sess: &'a ParseSess, filemap: Rc<syntax_pos::FileMap>) -> Self {
182 let mut sr = StringReader::new_raw(sess, filemap);
183 if let Err(_) = sr.advance_token() {
184 sr.emit_fatal_errors();
190 pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
191 let begin = sess.codemap().lookup_byte_offset(span.lo);
192 let end = sess.codemap().lookup_byte_offset(span.hi);
194 // Make the range zero-length if the span is invalid.
195 if span.lo > span.hi || begin.fm.start_pos != end.fm.start_pos {
199 let mut sr = StringReader::new_raw_internal(sess, begin.fm);
201 // Seek the lexer to the right byte range.
202 sr.save_new_lines_and_multibyte = false;
203 sr.next_pos = span.lo;
204 sr.terminator = Some(span.hi);
208 if let Err(_) = sr.advance_token() {
209 sr.emit_fatal_errors();
215 pub fn ch_is(&self, c: char) -> bool {
219 /// Report a fatal lexical error with a given span.
220 pub fn fatal_span(&self, sp: Span, m: &str) -> FatalError {
221 self.sess.span_diagnostic.span_fatal(sp, m)
224 /// Report a lexical error with a given span.
225 pub fn err_span(&self, sp: Span, m: &str) {
226 self.sess.span_diagnostic.span_err(sp, m)
230 /// Report a fatal error spanning [`from_pos`, `to_pos`).
231 fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> FatalError {
232 self.fatal_span(mk_sp(from_pos, to_pos), m)
235 /// Report a lexical error spanning [`from_pos`, `to_pos`).
236 fn err_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
237 self.err_span(mk_sp(from_pos, to_pos), m)
240 /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
241 /// escaped character to the error message
242 fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> FatalError {
243 let mut m = m.to_string();
245 for c in c.escape_default() {
248 self.fatal_span_(from_pos, to_pos, &m[..])
250 fn struct_fatal_span_char(&self,
255 -> DiagnosticBuilder<'a> {
256 let mut m = m.to_string();
258 for c in c.escape_default() {
261 self.sess.span_diagnostic.struct_span_fatal(mk_sp(from_pos, to_pos), &m[..])
264 /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
265 /// escaped character to the error message
266 fn err_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) {
267 let mut m = m.to_string();
269 for c in c.escape_default() {
272 self.err_span_(from_pos, to_pos, &m[..]);
274 fn struct_err_span_char(&self,
279 -> DiagnosticBuilder<'a> {
280 let mut m = m.to_string();
282 for c in c.escape_default() {
285 self.sess.span_diagnostic.struct_span_err(mk_sp(from_pos, to_pos), &m[..])
288 /// Report a lexical error spanning [`from_pos`, `to_pos`), appending the
289 /// offending string to the error message
290 fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> FatalError {
292 let from = self.byte_offset(from_pos).to_usize();
293 let to = self.byte_offset(to_pos).to_usize();
294 m.push_str(&self.source_text[from..to]);
295 self.fatal_span_(from_pos, to_pos, &m[..])
298 /// Advance peek_tok and peek_span to refer to the next token, and
299 /// possibly update the interner.
300 fn advance_token(&mut self) -> Result<(), ()> {
301 match self.scan_whitespace_or_comment() {
303 self.peek_span = comment.sp;
304 self.peek_tok = comment.tok;
308 self.peek_tok = token::Eof;
309 self.peek_span = mk_sp(self.filemap.end_pos, self.filemap.end_pos);
311 let start_bytepos = self.pos;
312 self.peek_tok = self.next_token_inner()?;
313 self.peek_span = mk_sp(start_bytepos, self.pos);
320 fn byte_offset(&self, pos: BytePos) -> BytePos {
321 (pos - self.filemap.start_pos)
324 /// Calls `f` with a string slice of the source text spanning from `start`
325 /// up to but excluding `self.pos`, meaning the slice does not include
326 /// the character `self.ch`.
327 pub fn with_str_from<T, F>(&self, start: BytePos, f: F) -> T
328 where F: FnOnce(&str) -> T
330 self.with_str_from_to(start, self.pos, f)
333 /// Create a Name from a given offset to the current offset, each
334 /// adjusted 1 towards each other (assumes that on either side there is a
335 /// single-byte delimiter).
336 pub fn name_from(&self, start: BytePos) -> ast::Name {
337 debug!("taking an ident from {:?} to {:?}", start, self.pos);
338 self.with_str_from(start, Symbol::intern)
341 /// As name_from, with an explicit endpoint.
342 pub fn name_from_to(&self, start: BytePos, end: BytePos) -> ast::Name {
343 debug!("taking an ident from {:?} to {:?}", start, end);
344 self.with_str_from_to(start, end, Symbol::intern)
347 /// Calls `f` with a string slice of the source text spanning from `start`
348 /// up to but excluding `end`.
349 fn with_str_from_to<T, F>(&self, start: BytePos, end: BytePos, f: F) -> T
350 where F: FnOnce(&str) -> T
352 f(&self.source_text[self.byte_offset(start).to_usize()..self.byte_offset(end).to_usize()])
355 /// Converts CRLF to LF in the given string, raising an error on bare CR.
356 fn translate_crlf<'b>(&self, start: BytePos, s: &'b str, errmsg: &'b str) -> Cow<'b, str> {
359 let ch = char_at(s, i);
360 let next = i + ch.len_utf8();
362 if next < s.len() && char_at(s, next) == '\n' {
363 return translate_crlf_(self, start, s, errmsg, i).into();
365 let pos = start + BytePos(i as u32);
366 let end_pos = start + BytePos(next as u32);
367 self.err_span_(pos, end_pos, errmsg);
373 fn translate_crlf_(rdr: &StringReader,
379 let mut buf = String::with_capacity(s.len());
382 let ch = char_at(s, i);
383 let next = i + ch.len_utf8();
386 buf.push_str(&s[j..i]);
389 if next >= s.len() || char_at(s, next) != '\n' {
390 let pos = start + BytePos(i as u32);
391 let end_pos = start + BytePos(next as u32);
392 rdr.err_span_(pos, end_pos, errmsg);
398 buf.push_str(&s[j..]);
405 /// Advance the StringReader by one character. If a newline is
406 /// discovered, add it to the FileMap's list of line start offsets.
407 pub fn bump(&mut self) {
408 let new_pos = self.next_pos;
409 let new_byte_offset = self.byte_offset(new_pos).to_usize();
410 let end = self.terminator.map_or(self.source_text.len(), |t| {
411 self.byte_offset(t).to_usize()
413 if new_byte_offset < end {
414 let old_ch_is_newline = self.ch.unwrap() == '\n';
415 let new_ch = char_at(&self.source_text, new_byte_offset);
416 let new_ch_len = new_ch.len_utf8();
418 self.ch = Some(new_ch);
420 self.next_pos = new_pos + Pos::from_usize(new_ch_len);
421 if old_ch_is_newline {
422 if self.save_new_lines_and_multibyte {
423 self.filemap.next_line(self.pos);
425 self.col = CharPos(0);
427 self.col = self.col + CharPos(1);
430 if self.save_new_lines_and_multibyte {
431 self.filemap.record_multibyte_char(self.pos, new_ch_len);
440 pub fn nextch(&self) -> Option<char> {
441 let offset = self.byte_offset(self.next_pos).to_usize();
442 if offset < self.source_text.len() {
443 Some(char_at(&self.source_text, offset))
449 pub fn nextch_is(&self, c: char) -> bool {
450 self.nextch() == Some(c)
453 pub fn nextnextch(&self) -> Option<char> {
454 let offset = self.byte_offset(self.next_pos).to_usize();
455 let s = &self.source_text[..];
456 if offset >= s.len() {
459 let next = offset + char_at(s, offset).len_utf8();
461 Some(char_at(s, next))
467 pub fn nextnextch_is(&self, c: char) -> bool {
468 self.nextnextch() == Some(c)
471 /// Eats <XID_start><XID_continue>*, if possible.
472 fn scan_optional_raw_name(&mut self) -> Option<ast::Name> {
473 if !ident_start(self.ch) {
476 let start = self.pos;
477 while ident_continue(self.ch) {
481 self.with_str_from(start, |string| {
485 Some(Symbol::intern(string))
490 /// PRECONDITION: self.ch is not whitespace
491 /// Eats any kind of comment.
492 fn scan_comment(&mut self) -> Option<TokenAndSpan> {
493 if let Some(c) = self.ch {
494 if c.is_whitespace() {
495 let msg = "called consume_any_line_comment, but there was whitespace";
496 self.sess.span_diagnostic.span_err(mk_sp(self.pos, self.pos), msg);
501 match self.nextch() {
506 // line comments starting with "///" or "//!" are doc-comments
507 let doc_comment = (self.ch_is('/') && !self.nextch_is('/')) || self.ch_is('!');
508 let start_bpos = self.pos - BytePos(2);
510 while !self.is_eof() {
511 match self.ch.unwrap() {
514 if self.nextch_is('\n') {
517 } else if doc_comment {
518 self.err_span_(self.pos,
520 "bare CR not allowed in doc-comment");
528 return if doc_comment {
529 self.with_str_from(start_bpos, |string| {
530 // comments with only more "/"s are not doc comments
531 let tok = if is_doc_comment(string) {
532 token::DocComment(Symbol::intern(string))
539 sp: mk_sp(start_bpos, self.pos),
545 sp: mk_sp(start_bpos, self.pos),
552 self.scan_block_comment()
556 } else if self.ch_is('#') {
557 if self.nextch_is('!') {
559 // Parse an inner attribute.
560 if self.nextnextch_is('[') {
564 // I guess this is the only way to figure out if
565 // we're at the beginning of the file...
566 let cmap = CodeMap::new(FilePathMapping::empty());
567 cmap.files.borrow_mut().push(self.filemap.clone());
568 let loc = cmap.lookup_char_pos_adj(self.pos);
569 debug!("Skipping a shebang");
570 if loc.line == 1 && loc.col == CharPos(0) {
571 // FIXME: Add shebang "token", return it
572 let start = self.pos;
573 while !self.ch_is('\n') && !self.is_eof() {
576 return Some(TokenAndSpan {
577 tok: token::Shebang(self.name_from(start)),
578 sp: mk_sp(start, self.pos),
588 /// If there is whitespace, shebang, or a comment, scan it. Otherwise,
590 fn scan_whitespace_or_comment(&mut self) -> Option<TokenAndSpan> {
591 match self.ch.unwrap_or('\0') {
592 // # to handle shebang at start of file -- this is the entry point
593 // for skipping over all "junk"
595 let c = self.scan_comment();
596 debug!("scanning a comment {:?}", c);
599 c if is_pattern_whitespace(Some(c)) => {
600 let start_bpos = self.pos;
601 while is_pattern_whitespace(self.ch) {
604 let c = Some(TokenAndSpan {
605 tok: token::Whitespace,
606 sp: mk_sp(start_bpos, self.pos),
608 debug!("scanning whitespace: {:?}", c);
615 /// Might return a sugared-doc-attr
616 fn scan_block_comment(&mut self) -> Option<TokenAndSpan> {
617 // block comments starting with "/**" or "/*!" are doc-comments
618 let is_doc_comment = self.ch_is('*') || self.ch_is('!');
619 let start_bpos = self.pos - BytePos(2);
621 let mut level: isize = 1;
622 let mut has_cr = false;
625 let msg = if is_doc_comment {
626 "unterminated block doc-comment"
628 "unterminated block comment"
630 let last_bpos = self.pos;
631 panic!(self.fatal_span_(start_bpos, last_bpos, msg));
633 let n = self.ch.unwrap();
635 '/' if self.nextch_is('*') => {
639 '*' if self.nextch_is('/') => {
651 self.with_str_from(start_bpos, |string| {
652 // but comments with only "*"s between two "/"s are not
653 let tok = if is_block_doc_comment(string) {
654 let string = if has_cr {
655 self.translate_crlf(start_bpos,
657 "bare CR not allowed in block doc-comment")
661 token::DocComment(Symbol::intern(&string[..]))
668 sp: mk_sp(start_bpos, self.pos),
673 /// Scan through any digits (base `scan_radix`) or underscores,
674 /// and return how many digits there were.
676 /// `real_radix` represents the true radix of the number we're
677 /// interested in, and errors will be emitted for any digits
678 /// between `real_radix` and `scan_radix`.
679 fn scan_digits(&mut self, real_radix: u32, scan_radix: u32) -> usize {
680 assert!(real_radix <= scan_radix);
685 debug!("skipping a _");
689 match c.and_then(|cc| cc.to_digit(scan_radix)) {
691 debug!("{:?} in scan_digits", c);
692 // check that the hypothetical digit is actually
693 // in range for the true radix
694 if c.unwrap().to_digit(real_radix).is_none() {
695 self.err_span_(self.pos,
697 &format!("invalid digit for a base {} literal", real_radix));
707 /// Lex a LIT_INTEGER or a LIT_FLOAT
708 fn scan_number(&mut self, c: char) -> token::Lit {
711 let start_bpos = self.pos;
716 match self.ch.unwrap_or('\0') {
720 num_digits = self.scan_digits(2, 10);
725 num_digits = self.scan_digits(8, 10);
730 num_digits = self.scan_digits(16, 16);
732 '0'...'9' | '_' | '.' | 'e' | 'E' => {
733 num_digits = self.scan_digits(10, 10) + 1;
737 return token::Integer(self.name_from(start_bpos));
740 } else if c.is_digit(10) {
741 num_digits = self.scan_digits(10, 10) + 1;
747 self.err_span_(start_bpos,
749 "no valid digits found for number");
750 return token::Integer(Symbol::intern("0"));
753 // might be a float, but don't be greedy if this is actually an
754 // integer literal followed by field/method access or a range pattern
755 // (`0..2` and `12.foo()`)
756 if self.ch_is('.') && !self.nextch_is('.') &&
760 // might have stuff after the ., and if it does, it needs to start
763 if self.ch.unwrap_or('\0').is_digit(10) {
764 self.scan_digits(10, 10);
765 self.scan_float_exponent();
768 self.check_float_base(start_bpos, pos, base);
769 return token::Float(self.name_from(start_bpos));
771 // it might be a float if it has an exponent
772 if self.ch_is('e') || self.ch_is('E') {
773 self.scan_float_exponent();
775 self.check_float_base(start_bpos, pos, base);
776 return token::Float(self.name_from(start_bpos));
778 // but we certainly have an integer!
779 return token::Integer(self.name_from(start_bpos));
783 /// Scan over `n_digits` hex digits, stopping at `delim`, reporting an
784 /// error if too many or too few digits are encountered.
785 fn scan_hex_digits(&mut self, n_digits: usize, delim: char, below_0x7f_only: bool) -> bool {
786 debug!("scanning {} digits until {:?}", n_digits, delim);
787 let start_bpos = self.pos;
788 let mut accum_int = 0;
790 let mut valid = true;
791 for _ in 0..n_digits {
793 let last_bpos = self.pos;
794 panic!(self.fatal_span_(start_bpos,
796 "unterminated numeric character escape"));
798 if self.ch_is(delim) {
799 let last_bpos = self.pos;
800 self.err_span_(start_bpos,
802 "numeric character escape is too short");
806 let c = self.ch.unwrap_or('\x00');
808 accum_int += c.to_digit(16).unwrap_or_else(|| {
809 self.err_span_char(self.pos,
811 "invalid character in numeric character escape",
820 if below_0x7f_only && accum_int >= 0x80 {
821 self.err_span_(start_bpos,
823 "this form of character escape may only be used with characters in \
824 the range [\\x00-\\x7f]");
828 match char::from_u32(accum_int) {
831 let last_bpos = self.pos;
832 self.err_span_(start_bpos, last_bpos, "invalid numeric character escape");
838 /// Scan for a single (possibly escaped) byte or char
839 /// in a byte, (non-raw) byte string, char, or (non-raw) string literal.
840 /// `start` is the position of `first_source_char`, which is already consumed.
842 /// Returns true if there was a valid char/byte, false otherwise.
843 fn scan_char_or_byte(&mut self,
845 first_source_char: char,
849 match first_source_char {
851 // '\X' for some X must be a character constant:
852 let escaped = self.ch;
853 let escaped_pos = self.pos;
856 None => {} // EOF here is an error that will be checked later.
859 'n' | 'r' | 't' | '\\' | '\'' | '"' | '0' => true,
860 'x' => self.scan_byte_escape(delim, !ascii_only),
862 let valid = if self.ch_is('{') {
863 self.scan_unicode_escape(delim) && !ascii_only
865 let span = mk_sp(start, self.pos);
866 self.sess.span_diagnostic
867 .struct_span_err(span, "incorrect unicode escape sequence")
869 "format of unicode escape sequences is \
875 self.err_span_(start,
877 "unicode escape sequences cannot be used as a \
878 byte or in a byte string");
883 '\n' if delim == '"' => {
884 self.consume_whitespace();
887 '\r' if delim == '"' && self.ch_is('\n') => {
888 self.consume_whitespace();
893 let mut err = self.struct_err_span_char(escaped_pos,
896 "unknown byte escape"
903 err.span_help(mk_sp(escaped_pos, pos),
904 "this is an isolated carriage return; consider \
905 checking your editor and version control \
908 if (e == '{' || e == '}') && !ascii_only {
909 err.span_help(mk_sp(escaped_pos, pos),
910 "if used in a formatting string, curly braces \
911 are escaped with `{{` and `}}`");
920 '\t' | '\n' | '\r' | '\'' if delim == '\'' => {
922 self.err_span_char(start,
925 "byte constant must be escaped"
927 "character constant must be escaped"
933 if self.ch_is('\n') {
937 self.err_span_(start,
939 "bare CR not allowed in string, use \\r instead");
944 if ascii_only && first_source_char > '\x7F' {
946 self.err_span_(start,
948 "byte constant must be ASCII. Use a \\xHH escape for a \
957 /// Scan over a \u{...} escape
959 /// At this point, we have already seen the \ and the u, the { is the current character. We
960 /// will read at least one digit, and up to 6, and pass over the }.
961 fn scan_unicode_escape(&mut self, delim: char) -> bool {
962 self.bump(); // past the {
963 let start_bpos = self.pos;
965 let mut accum_int = 0;
966 let mut valid = true;
968 while !self.ch_is('}') && count <= 6 {
969 let c = match self.ch {
972 panic!(self.fatal_span_(start_bpos,
974 "unterminated unicode escape (found EOF)"));
978 accum_int += c.to_digit(16).unwrap_or_else(|| {
980 panic!(self.fatal_span_(self.pos,
982 "unterminated unicode escape (needed a `}`)"));
984 self.err_span_char(self.pos,
986 "invalid character in unicode escape",
997 self.err_span_(start_bpos,
999 "overlong unicode escape (can have at most 6 hex digits)");
1003 if valid && (char::from_u32(accum_int).is_none() || count == 0) {
1004 self.err_span_(start_bpos,
1006 "invalid unicode character escape");
1010 self.bump(); // past the ending }
1014 /// Scan over a float exponent.
1015 fn scan_float_exponent(&mut self) {
1016 if self.ch_is('e') || self.ch_is('E') {
1018 if self.ch_is('-') || self.ch_is('+') {
1021 if self.scan_digits(10, 10) == 0 {
1022 self.err_span_(self.pos,
1024 "expected at least one digit in exponent")
1029 /// Check that a base is valid for a floating literal, emitting a nice
1030 /// error if it isn't.
1031 fn check_float_base(&mut self, start_bpos: BytePos, last_bpos: BytePos, base: usize) {
1034 self.err_span_(start_bpos,
1036 "hexadecimal float literal is not supported")
1039 self.err_span_(start_bpos,
1041 "octal float literal is not supported")
1044 self.err_span_(start_bpos,
1046 "binary float literal is not supported")
1052 fn binop(&mut self, op: token::BinOpToken) -> token::Token {
1054 if self.ch_is('=') {
1056 return token::BinOpEq(op);
1058 return token::BinOp(op);
1062 /// Return the next token from the string, advances the input past that
1063 /// token, and updates the interner
1064 fn next_token_inner(&mut self) -> Result<token::Token, ()> {
1066 if ident_start(c) &&
1067 match (c.unwrap(), self.nextch(), self.nextnextch()) {
1068 // Note: r as in r" or r#" is part of a raw string literal,
1069 // b as in b' is part of a byte literal.
1070 // They are not identifiers, and are handled further down.
1071 ('r', Some('"'), _) |
1072 ('r', Some('#'), _) |
1073 ('b', Some('"'), _) |
1074 ('b', Some('\''), _) |
1075 ('b', Some('r'), Some('"')) |
1076 ('b', Some('r'), Some('#')) => false,
1079 let start = self.pos;
1080 while ident_continue(self.ch) {
1084 return Ok(self.with_str_from(start, |string| {
1088 // FIXME: perform NFKC normalization here. (Issue #2253)
1089 token::Ident(Ident::from_str(string))
1094 if is_dec_digit(c) {
1095 let num = self.scan_number(c.unwrap());
1096 let suffix = self.scan_optional_raw_name();
1097 debug!("next_token_inner: scanned number {:?}, {:?}", num, suffix);
1098 return Ok(token::Literal(num, suffix));
1101 match c.expect("next_token_inner called at EOF") {
1105 return Ok(token::Semi);
1109 return Ok(token::Comma);
1113 return if self.ch_is('.') {
1115 if self.ch_is('.') {
1117 Ok(token::DotDotDot)
1127 return Ok(token::OpenDelim(token::Paren));
1131 return Ok(token::CloseDelim(token::Paren));
1135 return Ok(token::OpenDelim(token::Brace));
1139 return Ok(token::CloseDelim(token::Brace));
1143 return Ok(token::OpenDelim(token::Bracket));
1147 return Ok(token::CloseDelim(token::Bracket));
1151 return Ok(token::At);
1155 return Ok(token::Pound);
1159 return Ok(token::Tilde);
1163 return Ok(token::Question);
1167 if self.ch_is(':') {
1169 return Ok(token::ModSep);
1171 return Ok(token::Colon);
1177 return Ok(token::Dollar);
1180 // Multi-byte tokens.
1183 if self.ch_is('=') {
1185 return Ok(token::EqEq);
1186 } else if self.ch_is('>') {
1188 return Ok(token::FatArrow);
1190 return Ok(token::Eq);
1195 if self.ch_is('=') {
1197 return Ok(token::Ne);
1199 return Ok(token::Not);
1204 match self.ch.unwrap_or('\x00') {
1207 return Ok(token::Le);
1210 return Ok(self.binop(token::Shl));
1214 match self.ch.unwrap_or('\x00') {
1216 return Ok(token::LArrow);
1221 return Ok(token::Lt);
1227 match self.ch.unwrap_or('\x00') {
1230 return Ok(token::Ge);
1233 return Ok(self.binop(token::Shr));
1236 return Ok(token::Gt);
1241 // Either a character constant 'a' OR a lifetime name 'abc
1242 let start_with_quote = self.pos;
1244 let start = self.pos;
1246 // the eof will be picked up by the final `'` check below
1247 let c2 = self.ch.unwrap_or('\x00');
1250 // If the character is an ident start not followed by another single
1251 // quote, then this is a lifetime name:
1252 if ident_start(Some(c2)) && !self.ch_is('\'') {
1253 while ident_continue(self.ch) {
1256 // lifetimes shouldn't end with a single quote
1257 // if we find one, then this is an invalid character literal
1258 if self.ch_is('\'') {
1259 panic!(self.fatal_span_verbose(
1260 start_with_quote, self.next_pos,
1261 String::from("character literal may only contain one codepoint")));
1265 // Include the leading `'` in the real identifier, for macro
1266 // expansion purposes. See #12512 for the gory details of why
1267 // this is necessary.
1268 let ident = self.with_str_from(start, |lifetime_name| {
1269 Ident::from_str(&format!("'{}", lifetime_name))
1272 // Conjure up a "keyword checking ident" to make sure that
1273 // the lifetime name is not a keyword.
1274 let keyword_checking_ident = self.with_str_from(start, |lifetime_name| {
1275 Ident::from_str(lifetime_name)
1277 let keyword_checking_token = &token::Ident(keyword_checking_ident);
1278 let last_bpos = self.pos;
1279 if keyword_checking_token.is_any_keyword() &&
1280 !keyword_checking_token.is_keyword(keywords::Static) {
1281 self.err_span_(start, last_bpos, "lifetimes cannot use keyword names");
1284 return Ok(token::Lifetime(ident));
1287 let valid = self.scan_char_or_byte(start,
1293 if !self.ch_is('\'') {
1294 panic!(self.fatal_span_verbose(
1295 start_with_quote, self.pos,
1296 String::from("character literal may only contain one codepoint")));
1300 self.name_from(start)
1304 self.bump(); // advance ch past token
1305 let suffix = self.scan_optional_raw_name();
1306 return Ok(token::Literal(token::Char(id), suffix));
1310 let lit = match self.ch {
1311 Some('\'') => self.scan_byte(),
1312 Some('"') => self.scan_byte_string(),
1313 Some('r') => self.scan_raw_byte_string(),
1314 _ => unreachable!(), // Should have been a token::Ident above.
1316 let suffix = self.scan_optional_raw_name();
1317 return Ok(token::Literal(lit, suffix));
1320 let start_bpos = self.pos;
1321 let mut valid = true;
1323 while !self.ch_is('"') {
1325 let last_bpos = self.pos;
1326 panic!(self.fatal_span_(start_bpos,
1328 "unterminated double quote string"));
1331 let ch_start = self.pos;
1332 let ch = self.ch.unwrap();
1334 valid &= self.scan_char_or_byte(ch_start,
1340 // adjust for the ASCII " at the start of the literal
1342 self.name_from(start_bpos + BytePos(1))
1344 Symbol::intern("??")
1347 let suffix = self.scan_optional_raw_name();
1348 return Ok(token::Literal(token::Str_(id), suffix));
1351 let start_bpos = self.pos;
1353 let mut hash_count = 0;
1354 while self.ch_is('#') {
1360 let last_bpos = self.pos;
1361 panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
1362 } else if !self.ch_is('"') {
1363 let last_bpos = self.pos;
1364 let curr_char = self.ch.unwrap();
1365 panic!(self.fatal_span_char(start_bpos,
1367 "found invalid character; only `#` is allowed \
1368 in raw string delimitation",
1372 let content_start_bpos = self.pos;
1373 let mut content_end_bpos;
1374 let mut valid = true;
1377 let last_bpos = self.pos;
1378 panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
1380 // if self.ch_is('"') {
1381 // content_end_bpos = self.pos;
1382 // for _ in 0..hash_count {
1384 // if !self.ch_is('#') {
1386 let c = self.ch.unwrap();
1389 content_end_bpos = self.pos;
1390 for _ in 0..hash_count {
1392 if !self.ch_is('#') {
1399 if !self.nextch_is('\n') {
1400 let last_bpos = self.pos;
1401 self.err_span_(start_bpos,
1403 "bare CR not allowed in raw string, use \\r \
1414 self.name_from_to(content_start_bpos, content_end_bpos)
1416 Symbol::intern("??")
1418 let suffix = self.scan_optional_raw_name();
1419 return Ok(token::Literal(token::StrRaw(id, hash_count), suffix));
1422 if self.nextch_is('>') {
1425 return Ok(token::RArrow);
1427 return Ok(self.binop(token::Minus));
1431 if self.nextch_is('&') {
1434 return Ok(token::AndAnd);
1436 return Ok(self.binop(token::And));
1440 match self.nextch() {
1444 return Ok(token::OrOr);
1447 return Ok(self.binop(token::Or));
1452 return Ok(self.binop(token::Plus));
1455 return Ok(self.binop(token::Star));
1458 return Ok(self.binop(token::Slash));
1461 return Ok(self.binop(token::Caret));
1464 return Ok(self.binop(token::Percent));
1467 let last_bpos = self.pos;
1468 let bpos = self.next_pos;
1469 let mut err = self.struct_fatal_span_char(last_bpos,
1471 "unknown start of token",
1473 unicode_chars::check_for_substitution(&self, c, &mut err);
1474 self.fatal_errs.push(err);
1480 fn consume_whitespace(&mut self) {
1481 while is_pattern_whitespace(self.ch) && !self.is_eof() {
1486 fn read_to_eol(&mut self) -> String {
1487 let mut val = String::new();
1488 while !self.ch_is('\n') && !self.is_eof() {
1489 val.push(self.ch.unwrap());
1492 if self.ch_is('\n') {
1498 fn read_one_line_comment(&mut self) -> String {
1499 let val = self.read_to_eol();
1500 assert!((val.as_bytes()[0] == b'/' && val.as_bytes()[1] == b'/') ||
1501 (val.as_bytes()[0] == b'#' && val.as_bytes()[1] == b'!'));
1505 fn consume_non_eol_whitespace(&mut self) {
1506 while is_pattern_whitespace(self.ch) && !self.ch_is('\n') && !self.is_eof() {
1511 fn peeking_at_comment(&self) -> bool {
1512 (self.ch_is('/') && self.nextch_is('/')) || (self.ch_is('/') && self.nextch_is('*')) ||
1513 // consider shebangs comments, but not inner attributes
1514 (self.ch_is('#') && self.nextch_is('!') && !self.nextnextch_is('['))
1517 fn scan_byte(&mut self) -> token::Lit {
1519 let start = self.pos;
1521 // the eof will be picked up by the final `'` check below
1522 let c2 = self.ch.unwrap_or('\x00');
1525 let valid = self.scan_char_or_byte(start,
1530 if !self.ch_is('\'') {
1531 // Byte offsetting here is okay because the
1532 // character before position `start` are an
1533 // ascii single quote and ascii 'b'.
1535 panic!(self.fatal_span_verbose(start - BytePos(2),
1537 "unterminated byte constant".to_string()));
1541 self.name_from(start)
1545 self.bump(); // advance ch past token
1546 return token::Byte(id);
1549 fn scan_byte_escape(&mut self, delim: char, below_0x7f_only: bool) -> bool {
1550 self.scan_hex_digits(2, delim, below_0x7f_only)
1553 fn scan_byte_string(&mut self) -> token::Lit {
1555 let start = self.pos;
1556 let mut valid = true;
1558 while !self.ch_is('"') {
1561 panic!(self.fatal_span_(start, pos, "unterminated double quote byte string"));
1564 let ch_start = self.pos;
1565 let ch = self.ch.unwrap();
1567 valid &= self.scan_char_or_byte(ch_start,
1574 self.name_from(start)
1576 Symbol::intern("??")
1579 return token::ByteStr(id);
1582 fn scan_raw_byte_string(&mut self) -> token::Lit {
1583 let start_bpos = self.pos;
1585 let mut hash_count = 0;
1586 while self.ch_is('#') {
1593 panic!(self.fatal_span_(start_bpos, pos, "unterminated raw string"));
1594 } else if !self.ch_is('"') {
1596 let ch = self.ch.unwrap();
1597 panic!(self.fatal_span_char(start_bpos,
1599 "found invalid character; only `#` is allowed in raw \
1600 string delimitation",
1604 let content_start_bpos = self.pos;
1605 let mut content_end_bpos;
1610 panic!(self.fatal_span_(start_bpos, pos, "unterminated raw string"))
1613 content_end_bpos = self.pos;
1614 for _ in 0..hash_count {
1616 if !self.ch_is('#') {
1625 self.err_span_char(pos, pos, "raw byte string must be ASCII", c);
1632 return token::ByteStrRaw(self.name_from_to(content_start_bpos, content_end_bpos),
1637 // This tests the character for the unicode property 'PATTERN_WHITE_SPACE' which
1638 // is guaranteed to be forward compatible. http://unicode.org/reports/tr31/#R3
1639 pub fn is_pattern_whitespace(c: Option<char>) -> bool {
1640 c.map_or(false, Pattern_White_Space)
1643 fn in_range(c: Option<char>, lo: char, hi: char) -> bool {
1645 Some(c) => lo <= c && c <= hi,
1650 fn is_dec_digit(c: Option<char>) -> bool {
1651 return in_range(c, '0', '9');
1654 pub fn is_doc_comment(s: &str) -> bool {
1655 let res = (s.starts_with("///") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'/') ||
1656 s.starts_with("//!");
1657 debug!("is {:?} a doc comment? {}", s, res);
1661 pub fn is_block_doc_comment(s: &str) -> bool {
1662 // Prevent `/**/` from being parsed as a doc comment
1663 let res = ((s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*') ||
1664 s.starts_with("/*!")) && s.len() >= 5;
1665 debug!("is {:?} a doc comment? {}", s, res);
1669 fn ident_start(c: Option<char>) -> bool {
1672 None => return false,
1675 (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c > '\x7f' && c.is_xid_start())
1678 fn ident_continue(c: Option<char>) -> bool {
1681 None => return false,
1684 (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' ||
1685 (c > '\x7f' && c.is_xid_continue())
1692 use ast::{Ident, CrateConfig};
1694 use syntax_pos::{BytePos, Span, NO_EXPANSION};
1695 use codemap::CodeMap;
1697 use feature_gate::UnstableFeatures;
1699 use std::cell::RefCell;
1700 use std::collections::HashSet;
1704 fn mk_sess(cm: Rc<CodeMap>) -> ParseSess {
1705 let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()), Some(cm.clone()));
1707 span_diagnostic: errors::Handler::with_emitter(true, false, Box::new(emitter)),
1708 unstable_features: UnstableFeatures::from_environment(),
1709 config: CrateConfig::new(),
1710 included_mod_stack: RefCell::new(Vec::new()),
1712 missing_fragment_specifiers: RefCell::new(HashSet::new()),
1716 // open a string reader for the given string
1717 fn setup<'a>(cm: &CodeMap,
1718 sess: &'a ParseSess,
1720 -> StringReader<'a> {
1721 let fm = cm.new_filemap("zebra.rs".to_string(), teststr);
1722 StringReader::new(sess, fm)
1727 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1728 let sh = mk_sess(cm.clone());
1729 let mut string_reader = setup(&cm,
1731 "/* my source file */ fn main() { println!(\"zebra\"); }\n"
1733 let id = Ident::from_str("fn");
1734 assert_eq!(string_reader.next_token().tok, token::Comment);
1735 assert_eq!(string_reader.next_token().tok, token::Whitespace);
1736 let tok1 = string_reader.next_token();
1737 let tok2 = TokenAndSpan {
1738 tok: token::Ident(id),
1745 assert_eq!(tok1, tok2);
1746 assert_eq!(string_reader.next_token().tok, token::Whitespace);
1747 // the 'main' id is already read:
1748 assert_eq!(string_reader.pos.clone(), BytePos(28));
1749 // read another token:
1750 let tok3 = string_reader.next_token();
1751 let tok4 = TokenAndSpan {
1752 tok: token::Ident(Ident::from_str("main")),
1759 assert_eq!(tok3, tok4);
1760 // the lparen is already read:
1761 assert_eq!(string_reader.pos.clone(), BytePos(29))
1764 // check that the given reader produces the desired stream
1765 // of tokens (stop checking after exhausting the expected vec)
1766 fn check_tokenization(mut string_reader: StringReader, expected: Vec<token::Token>) {
1767 for expected_tok in &expected {
1768 assert_eq!(&string_reader.next_token().tok, expected_tok);
1772 // make the identifier by looking up the string in the interner
1773 fn mk_ident(id: &str) -> token::Token {
1774 token::Ident(Ident::from_str(id))
1778 fn doublecolonparsing() {
1779 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1780 let sh = mk_sess(cm.clone());
1781 check_tokenization(setup(&cm, &sh, "a b".to_string()),
1782 vec![mk_ident("a"), token::Whitespace, mk_ident("b")]);
1787 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1788 let sh = mk_sess(cm.clone());
1789 check_tokenization(setup(&cm, &sh, "a::b".to_string()),
1790 vec![mk_ident("a"), token::ModSep, mk_ident("b")]);
1795 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1796 let sh = mk_sess(cm.clone());
1797 check_tokenization(setup(&cm, &sh, "a ::b".to_string()),
1798 vec![mk_ident("a"), token::Whitespace, token::ModSep, mk_ident("b")]);
1803 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1804 let sh = mk_sess(cm.clone());
1805 check_tokenization(setup(&cm, &sh, "a:: b".to_string()),
1806 vec![mk_ident("a"), token::ModSep, token::Whitespace, mk_ident("b")]);
1811 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1812 let sh = mk_sess(cm.clone());
1813 assert_eq!(setup(&cm, &sh, "'a'".to_string()).next_token().tok,
1814 token::Literal(token::Char(Symbol::intern("a")), None));
1818 fn character_space() {
1819 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1820 let sh = mk_sess(cm.clone());
1821 assert_eq!(setup(&cm, &sh, "' '".to_string()).next_token().tok,
1822 token::Literal(token::Char(Symbol::intern(" ")), None));
1826 fn character_escaped() {
1827 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1828 let sh = mk_sess(cm.clone());
1829 assert_eq!(setup(&cm, &sh, "'\\n'".to_string()).next_token().tok,
1830 token::Literal(token::Char(Symbol::intern("\\n")), None));
1834 fn lifetime_name() {
1835 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1836 let sh = mk_sess(cm.clone());
1837 assert_eq!(setup(&cm, &sh, "'abc".to_string()).next_token().tok,
1838 token::Lifetime(Ident::from_str("'abc")));
1843 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1844 let sh = mk_sess(cm.clone());
1845 assert_eq!(setup(&cm, &sh, "r###\"\"#a\\b\x00c\"\"###".to_string())
1848 token::Literal(token::StrRaw(Symbol::intern("\"#a\\b\x00c\""), 3), None));
1852 fn literal_suffixes() {
1853 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1854 let sh = mk_sess(cm.clone());
1856 ($input: expr, $tok_type: ident, $tok_contents: expr) => {{
1857 assert_eq!(setup(&cm, &sh, format!("{}suffix", $input)).next_token().tok,
1858 token::Literal(token::$tok_type(Symbol::intern($tok_contents)),
1859 Some(Symbol::intern("suffix"))));
1860 // with a whitespace separator:
1861 assert_eq!(setup(&cm, &sh, format!("{} suffix", $input)).next_token().tok,
1862 token::Literal(token::$tok_type(Symbol::intern($tok_contents)),
1867 test!("'a'", Char, "a");
1868 test!("b'a'", Byte, "a");
1869 test!("\"a\"", Str_, "a");
1870 test!("b\"a\"", ByteStr, "a");
1871 test!("1234", Integer, "1234");
1872 test!("0b101", Integer, "0b101");
1873 test!("0xABC", Integer, "0xABC");
1874 test!("1.0", Float, "1.0");
1875 test!("1.0e10", Float, "1.0e10");
1877 assert_eq!(setup(&cm, &sh, "2us".to_string()).next_token().tok,
1878 token::Literal(token::Integer(Symbol::intern("2")),
1879 Some(Symbol::intern("us"))));
1880 assert_eq!(setup(&cm, &sh, "r###\"raw\"###suffix".to_string()).next_token().tok,
1881 token::Literal(token::StrRaw(Symbol::intern("raw"), 3),
1882 Some(Symbol::intern("suffix"))));
1883 assert_eq!(setup(&cm, &sh, "br###\"raw\"###suffix".to_string()).next_token().tok,
1884 token::Literal(token::ByteStrRaw(Symbol::intern("raw"), 3),
1885 Some(Symbol::intern("suffix"))));
1889 fn line_doc_comments() {
1890 assert!(is_doc_comment("///"));
1891 assert!(is_doc_comment("/// blah"));
1892 assert!(!is_doc_comment("////"));
1896 fn nested_block_comments() {
1897 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1898 let sh = mk_sess(cm.clone());
1899 let mut lexer = setup(&cm, &sh, "/* /* */ */'a'".to_string());
1900 match lexer.next_token().tok {
1901 token::Comment => {}
1902 _ => panic!("expected a comment!"),
1904 assert_eq!(lexer.next_token().tok,
1905 token::Literal(token::Char(Symbol::intern("a")), None));
1909 fn crlf_comments() {
1910 let cm = Rc::new(CodeMap::new(FilePathMapping::empty()));
1911 let sh = mk_sess(cm.clone());
1912 let mut lexer = setup(&cm, &sh, "// test\r\n/// test\r\n".to_string());
1913 let comment = lexer.next_token();
1914 assert_eq!(comment.tok, token::Comment);
1915 assert_eq!((comment.sp.lo, comment.sp.hi), (BytePos(0), BytePos(7)));
1916 assert_eq!(lexer.next_token().tok, token::Whitespace);
1917 assert_eq!(lexer.next_token().tok,
1918 token::DocComment(Symbol::intern("/// test")));