rust/src/libsyntax/parse/lexer.rs

708 lines
22 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2012-09-04 13:37:29 -05:00
use diagnostic::span_handler;
2012-11-16 16:35:43 -06:00
use codemap::{span, CodeMap, CharPos, BytePos};
2012-09-04 13:37:29 -05:00
use ext::tt::transcribe::{tt_reader, new_tt_reader, dup_tt_reader,
tt_next_token};
2012-05-30 13:36:30 -05:00
export reader, string_reader, new_string_reader, is_whitespace;
export tt_reader, new_tt_reader;
2012-06-15 11:32:17 -05:00
export nextch, is_eof, bump, get_str_from, new_low_level_string_reader;
export string_reader_as_reader, tt_reader_as_reader;
2012-05-30 13:36:30 -05:00
trait reader {
2012-05-30 13:36:30 -05:00
fn is_eof() -> bool;
fn next_token() -> {tok: token::Token, sp: span};
fn fatal(~str) -> !;
fn span_diag() -> span_handler;
pure fn interner() -> @token::ident_interner;
fn peek() -> {tok: token::Token, sp: span};
2012-06-12 12:59:50 -05:00
fn dup() -> reader;
2012-05-30 13:36:30 -05:00
}
2012-04-15 05:49:59 -05:00
2012-05-30 13:36:30 -05:00
type string_reader = @{
span_diagnostic: span_handler,
src: @~str,
2012-11-15 16:34:23 -06:00
// The absolute offset within the codemap of the next character to read
2012-11-16 16:10:17 -06:00
mut pos: BytePos,
// The absolute offset within the codemap of the last character read(curr)
2012-11-16 16:10:17 -06:00
mut last_pos: BytePos,
2012-11-15 16:34:23 -06:00
// The column of the next character to read
mut col: CharPos,
2012-11-15 16:34:23 -06:00
// The last character to be read
2012-03-26 20:35:18 -05:00
mut curr: char,
filemap: @codemap::FileMap,
interner: @token::ident_interner,
2012-06-12 12:59:50 -05:00
/* cached: */
mut peek_tok: token::Token,
2012-06-12 12:59:50 -05:00
mut peek_span: span
};
fn new_string_reader(span_diagnostic: span_handler,
filemap: @codemap::FileMap,
itr: @token::ident_interner) -> string_reader {
2012-06-15 11:32:17 -05:00
let r = new_low_level_string_reader(span_diagnostic, filemap, itr);
2012-06-12 12:59:50 -05:00
string_advance_token(r); /* fill in peek_* */
2012-08-01 19:30:05 -05:00
return r;
2012-06-15 11:32:17 -05:00
}
/* For comments.rs, which hackily pokes into 'pos' and 'curr' */
fn new_low_level_string_reader(span_diagnostic: span_handler,
filemap: @codemap::FileMap,
itr: @token::ident_interner)
2012-06-15 11:32:17 -05:00
-> string_reader {
// Force the initial reader bump to start on a fresh line
let initial_char = '\n';
let r = @{span_diagnostic: span_diagnostic, src: filemap.src,
mut pos: filemap.start_pos,
mut last_pos: filemap.start_pos,
mut col: CharPos(0),
mut curr: initial_char,
2012-06-12 12:59:50 -05:00
filemap: filemap, interner: itr,
/* dummy values; not read */
mut peek_tok: token::EOF,
mut peek_span: ast_util::dummy_sp()};
bump(r);
2012-08-01 19:30:05 -05:00
return r;
}
2012-06-12 12:59:50 -05:00
fn dup_string_reader(&&r: string_reader) -> string_reader {
@{span_diagnostic: r.span_diagnostic, src: r.src,
mut pos: r.pos,
mut last_pos: r.last_pos,
mut col: r.col, mut curr: r.curr,
2012-06-12 12:59:50 -05:00
filemap: r.filemap, interner: r.interner,
mut peek_tok: r.peek_tok, mut peek_span: r.peek_span}
}
2012-08-07 20:10:06 -05:00
impl string_reader: reader {
2012-05-30 13:36:30 -05:00
fn is_eof() -> bool { is_eof(self) }
fn next_token() -> {tok: token::Token, sp: span} {
2012-06-12 12:59:50 -05:00
let ret_val = {tok: self.peek_tok, sp: self.peek_span};
string_advance_token(self);
2012-08-01 19:30:05 -05:00
return ret_val;
}
fn fatal(m: ~str) -> ! {
2012-06-12 12:59:50 -05:00
self.span_diagnostic.span_fatal(copy self.peek_span, m)
}
fn span_diag() -> span_handler { self.span_diagnostic }
pure fn interner() -> @token::ident_interner { self.interner }
fn peek() -> {tok: token::Token, sp: span} {
2012-06-12 12:59:50 -05:00
{tok: self.peek_tok, sp: self.peek_span}
}
fn dup() -> reader { dup_string_reader(self) as reader }
2012-06-01 17:52:07 -05:00
}
2012-08-07 20:10:06 -05:00
impl tt_reader: reader {
2012-06-01 17:52:07 -05:00
fn is_eof() -> bool { self.cur_tok == token::EOF }
fn next_token() -> {tok: token::Token, sp: span} {
/* weird resolve bug: if the following `if`, or any of its
statements are removed, we get resolution errors */
if false {
let _ignore_me = 0;
let _me_too = self.cur.readme[self.cur.idx];
2012-06-01 17:52:07 -05:00
}
tt_next_token(self)
2012-06-01 17:52:07 -05:00
}
fn fatal(m: ~str) -> ! {
self.sp_diag.span_fatal(copy self.cur_span, m);
2012-06-01 17:52:07 -05:00
}
fn span_diag() -> span_handler { self.sp_diag }
pure fn interner() -> @token::ident_interner { self.interner }
fn peek() -> {tok: token::Token, sp: span} {
2012-06-12 12:59:50 -05:00
{ tok: self.cur_tok, sp: self.cur_span }
}
fn dup() -> reader { dup_tt_reader(self) as reader }
}
2012-06-15 11:32:17 -05:00
fn string_advance_token(&&r: string_reader) {
Merge remote-tracking branch 'Dretch/prettydocs' Conflicts: src/compiletest/errors.rs src/libsyntax/parse/attr.rs src/libsyntax/parse/comments.rs src/test/compile-fail/ambig_impl_unify.rs src/test/compile-fail/assign-super.rs src/test/compile-fail/bad-for-loop.rs src/test/compile-fail/bad-var-env-capture-in-block-arg.rs src/test/compile-fail/block-arg-as-stmt-with-value.rs src/test/compile-fail/borrowck-assign-comp-idx.rs src/test/compile-fail/borrowck-lend-flow.rs src/test/compile-fail/borrowck-loan-blocks-move-cc.rs src/test/compile-fail/borrowck-loan-blocks-mut-uniq.rs src/test/compile-fail/borrowck-loan-rcvr.rs src/test/compile-fail/borrowck-loan-vec-content.rs src/test/compile-fail/borrowck-mut-vec-as-imm-slice-bad.rs src/test/compile-fail/cap-clause-with-stack-closure.rs src/test/compile-fail/do1.rs src/test/compile-fail/do2.rs src/test/compile-fail/empty-vec-trailing-comma.rs src/test/compile-fail/evec-subtyping.rs src/test/compile-fail/issue-1896.rs src/test/compile-fail/issue-2149.rs src/test/compile-fail/issue-2150.rs src/test/compile-fail/issue-2487-b.rs src/test/compile-fail/kindck-implicit-close-over-mut-var.rs src/test/compile-fail/liveness-issue-2163.rs src/test/compile-fail/liveness-use-in-index-lvalue.rs src/test/compile-fail/no-reuse-move-arc.rs src/test/compile-fail/no-send-res-ports.rs src/test/compile-fail/non-const.rs src/test/compile-fail/pure-higher-order.rs src/test/compile-fail/pure-loop-body.rs src/test/compile-fail/regions-addr-of-upvar-self.rs src/test/compile-fail/regions-escape-loop-via-vec.rs src/test/compile-fail/regions-scoping.rs src/test/compile-fail/seq-args.rs src/test/compile-fail/tstate-unsat-in-called-fn-expr.rs src/test/compile-fail/tstate-unsat-in-fn-expr.rs src/test/compile-fail/vec-add.rs src/test/compile-fail/vec-concat-bug.rs src/test/compile-fail/vector-no-ann.rs
2012-07-02 16:44:31 -05:00
for consume_whitespace_and_comments(r).each |comment| {
r.peek_tok = comment.tok;
r.peek_span = comment.sp;
2012-08-01 19:30:05 -05:00
return;
}
2012-06-15 11:32:17 -05:00
2012-06-12 12:59:50 -05:00
if is_eof(r) {
r.peek_tok = token::EOF;
} else {
2012-11-16 16:10:17 -06:00
let start_bytepos = r.last_pos;
2012-06-12 12:59:50 -05:00
r.peek_tok = next_token_inner(r);
2012-11-16 16:10:17 -06:00
r.peek_span = ast_util::mk_sp(start_bytepos, r.last_pos);
2012-06-12 12:59:50 -05:00
};
2012-06-15 11:32:17 -05:00
}
fn byte_offset(rdr: string_reader) -> BytePos {
2012-11-16 16:10:17 -06:00
(rdr.pos - rdr.filemap.start_pos)
}
fn get_str_from(rdr: string_reader, start: BytePos) -> ~str unsafe {
2012-05-30 13:36:30 -05:00
// I'm pretty skeptical about this subtraction. What if there's a
// multi-byte character before the mark?
return str::slice(*rdr.src, start.to_uint() - 1u,
byte_offset(rdr).to_uint() - 1u);
2012-05-30 13:36:30 -05:00
}
fn bump(rdr: string_reader) {
rdr.last_pos = rdr.pos;
let current_byte_offset = byte_offset(rdr).to_uint();;
if current_byte_offset < (*rdr.src).len() {
2012-11-18 16:58:57 -06:00
assert rdr.curr != -1 as char;
let last_char = rdr.curr;
let next = str::char_range_at(*rdr.src, current_byte_offset);
let byte_offset_diff = next.next - current_byte_offset;
2012-11-16 16:10:17 -06:00
rdr.pos = rdr.pos + BytePos(byte_offset_diff);
2012-05-30 13:36:30 -05:00
rdr.curr = next.ch;
rdr.col += CharPos(1u);
if last_char == '\n' {
rdr.filemap.next_line(rdr.last_pos);
rdr.col = CharPos(0u);
2012-05-30 13:36:30 -05:00
}
if byte_offset_diff > 1 {
rdr.filemap.record_multibyte_char(
BytePos(current_byte_offset), byte_offset_diff);
}
2012-05-30 13:36:30 -05:00
} else {
2012-11-18 16:58:57 -06:00
rdr.curr = -1 as char;
2012-05-30 13:36:30 -05:00
}
}
fn is_eof(rdr: string_reader) -> bool {
rdr.curr == -1 as char
}
fn nextch(rdr: string_reader) -> char {
let offset = byte_offset(rdr).to_uint();
if offset < (*rdr.src).len() {
return str::char_at(*rdr.src, offset);
2012-08-01 19:30:05 -05:00
} else { return -1 as char; }
2012-05-30 13:36:30 -05:00
}
2012-08-01 19:30:05 -05:00
fn dec_digit_val(c: char) -> int { return (c as int) - ('0' as int); }
2011-07-27 07:19:39 -05:00
fn hex_digit_val(c: char) -> int {
2012-08-01 19:30:05 -05:00
if in_range(c, '0', '9') { return (c as int) - ('0' as int); }
if in_range(c, 'a', 'f') { return (c as int) - ('a' as int) + 10; }
if in_range(c, 'A', 'F') { return (c as int) - ('A' as int) + 10; }
fail;
}
2012-08-01 19:30:05 -05:00
fn bin_digit_value(c: char) -> int { if c == '0' { return 0; } return 1; }
2011-07-27 07:19:39 -05:00
fn is_whitespace(c: char) -> bool {
2012-08-01 19:30:05 -05:00
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
2010-08-19 20:42:17 -05:00
}
2012-08-01 19:30:05 -05:00
fn may_begin_ident(c: char) -> bool { return is_alpha(c) || c == '_'; }
2012-08-01 19:30:05 -05:00
fn in_range(c: char, lo: char, hi: char) -> bool {
return lo <= c && c <= hi
}
2011-07-27 07:19:39 -05:00
fn is_alpha(c: char) -> bool {
2012-08-01 19:30:05 -05:00
return in_range(c, 'a', 'z') || in_range(c, 'A', 'Z');
}
2012-08-01 19:30:05 -05:00
fn is_dec_digit(c: char) -> bool { return in_range(c, '0', '9'); }
2012-08-01 19:30:05 -05:00
fn is_alnum(c: char) -> bool { return is_alpha(c) || is_dec_digit(c); }
2011-07-27 07:19:39 -05:00
fn is_hex_digit(c: char) -> bool {
2012-08-01 19:30:05 -05:00
return in_range(c, '0', '9') || in_range(c, 'a', 'f') ||
in_range(c, 'A', 'F');
}
2012-08-01 19:30:05 -05:00
fn is_bin_digit(c: char) -> bool { return c == '0' || c == '1'; }
// might return a sugared-doc-attr
fn consume_whitespace_and_comments(rdr: string_reader)
-> Option<{tok: token::Token, sp: span}> {
2012-05-30 13:36:30 -05:00
while is_whitespace(rdr.curr) { bump(rdr); }
2012-08-01 19:30:05 -05:00
return consume_any_line_comment(rdr);
}
// might return a sugared-doc-attr
fn consume_any_line_comment(rdr: string_reader)
-> Option<{tok: token::Token, sp: span}> {
if rdr.curr == '/' {
2012-08-06 14:34:08 -05:00
match nextch(rdr) {
2012-08-03 21:59:04 -05:00
'/' => {
bump(rdr);
bump(rdr);
// line comments starting with "///" or "//!" are doc-comments
if rdr.curr == '/' || rdr.curr == '!' {
2012-11-16 16:10:17 -06:00
let start_bpos = rdr.pos - BytePos(2u);
let mut acc = ~"//";
while rdr.curr != '\n' && !is_eof(rdr) {
2012-09-21 20:36:32 -05:00
str::push_char(&mut acc, rdr.curr);
bump(rdr);
}
2012-08-20 14:23:37 -05:00
return Some({
tok: token::DOC_COMMENT(rdr.interner.intern(@acc)),
2012-11-16 16:10:17 -06:00
sp: ast_util::mk_sp(start_bpos, rdr.pos)
});
} else {
while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); }
// Restart whitespace munch.
2012-08-01 19:30:05 -05:00
return consume_whitespace_and_comments(rdr);
}
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'*' => { bump(rdr); bump(rdr); return consume_block_comment(rdr); }
_ => ()
}
} else if rdr.curr == '#' {
2012-05-30 13:36:30 -05:00
if nextch(rdr) == '!' {
2012-11-12 20:24:56 -06:00
let cmap = @CodeMap::new();
(*cmap).files.push(rdr.filemap);
2012-11-16 16:10:17 -06:00
let loc = cmap.lookup_char_pos_adj(rdr.last_pos);
if loc.line == 1u && loc.col == CharPos(0u) {
2012-05-30 13:36:30 -05:00
while rdr.curr != '\n' && !is_eof(rdr) { bump(rdr); }
2012-08-01 19:30:05 -05:00
return consume_whitespace_and_comments(rdr);
}
}
}
2012-08-20 14:23:37 -05:00
return None;
}
// might return a sugared-doc-attr
fn consume_block_comment(rdr: string_reader)
-> Option<{tok: token::Token, sp: span}> {
// block comments starting with "/**" or "/*!" are doc-comments
if rdr.curr == '*' || rdr.curr == '!' {
2012-11-16 16:10:17 -06:00
let start_bpos = rdr.pos - BytePos(2u);
let mut acc = ~"/*";
while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) {
2012-09-21 20:36:32 -05:00
str::push_char(&mut acc, rdr.curr);
bump(rdr);
}
if is_eof(rdr) {
rdr.fatal(~"unterminated block doc-comment");
} else {
acc += ~"*/";
bump(rdr);
bump(rdr);
2012-08-20 14:23:37 -05:00
return Some({
tok: token::DOC_COMMENT(rdr.interner.intern(@acc)),
2012-11-16 16:10:17 -06:00
sp: ast_util::mk_sp(start_bpos, rdr.pos)
});
}
} else {
loop {
if is_eof(rdr) { rdr.fatal(~"unterminated block comment"); }
2012-05-30 13:36:30 -05:00
if rdr.curr == '*' && nextch(rdr) == '/' {
bump(rdr);
bump(rdr);
break;
} else {
bump(rdr);
}
}
}
// restart whitespace munch.
2012-08-01 19:30:05 -05:00
return consume_whitespace_and_comments(rdr);
}
2012-08-20 14:23:37 -05:00
fn scan_exponent(rdr: string_reader) -> Option<~str> {
let mut c = rdr.curr;
let mut rslt = ~"";
2011-07-27 07:19:39 -05:00
if c == 'e' || c == 'E' {
2012-09-21 20:36:32 -05:00
str::push_char(&mut rslt, c);
2012-05-30 13:36:30 -05:00
bump(rdr);
c = rdr.curr;
2011-07-27 07:19:39 -05:00
if c == '-' || c == '+' {
2012-09-21 20:36:32 -05:00
str::push_char(&mut rslt, c);
2012-05-30 13:36:30 -05:00
bump(rdr);
}
let exponent = scan_digits(rdr, 10u);
2012-02-23 03:44:04 -06:00
if str::len(exponent) > 0u {
2012-08-20 14:23:37 -05:00
return Some(rslt + exponent);
} else { rdr.fatal(~"scan_exponent: bad fp literal"); }
2012-08-20 14:23:37 -05:00
} else { return None::<~str>; }
}
fn scan_digits(rdr: string_reader, radix: uint) -> ~str {
let mut rslt = ~"";
loop {
let c = rdr.curr;
if c == '_' { bump(rdr); loop; }
2012-08-06 14:34:08 -05:00
match char::to_digit(c, radix) {
2012-08-20 14:23:37 -05:00
Some(_) => {
2012-09-21 20:36:32 -05:00
str::push_char(&mut rslt, c);
2012-05-30 13:36:30 -05:00
bump(rdr);
}
2012-08-03 21:59:04 -05:00
_ => return rslt
}
};
}
fn scan_number(c: char, rdr: string_reader) -> token::Token {
2012-05-30 13:36:30 -05:00
let mut num_str, base = 10u, c = c, n = nextch(rdr);
2011-07-27 07:19:39 -05:00
if c == '0' && n == 'x' {
2012-05-30 13:36:30 -05:00
bump(rdr);
bump(rdr);
base = 16u;
} else if c == '0' && n == 'b' {
2012-05-30 13:36:30 -05:00
bump(rdr);
bump(rdr);
base = 2u;
2011-09-02 00:39:44 -05:00
}
num_str = scan_digits(rdr, base);
c = rdr.curr;
2012-05-30 13:36:30 -05:00
nextch(rdr);
2011-07-27 07:19:39 -05:00
if c == 'u' || c == 'i' {
let signed = c == 'i';
let mut tp = {
2012-08-14 18:54:13 -05:00
if signed { either::Left(ast::ty_i) }
else { either::Right(ast::ty_u) }
};
2012-05-30 13:36:30 -05:00
bump(rdr);
c = rdr.curr;
2011-07-27 07:19:39 -05:00
if c == '8' {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-14 18:54:13 -05:00
tp = if signed { either::Left(ast::ty_i8) }
else { either::Right(ast::ty_u8) };
}
2012-05-30 13:36:30 -05:00
n = nextch(rdr);
2011-07-27 07:19:39 -05:00
if c == '1' && n == '6' {
2012-05-30 13:36:30 -05:00
bump(rdr);
bump(rdr);
2012-08-14 18:54:13 -05:00
tp = if signed { either::Left(ast::ty_i16) }
else { either::Right(ast::ty_u16) };
} else if c == '3' && n == '2' {
2012-05-30 13:36:30 -05:00
bump(rdr);
bump(rdr);
2012-08-14 18:54:13 -05:00
tp = if signed { either::Left(ast::ty_i32) }
else { either::Right(ast::ty_u32) };
} else if c == '6' && n == '4' {
2012-05-30 13:36:30 -05:00
bump(rdr);
bump(rdr);
2012-08-14 18:54:13 -05:00
tp = if signed { either::Left(ast::ty_i64) }
else { either::Right(ast::ty_u64) };
}
2012-02-23 03:44:04 -06:00
if str::len(num_str) == 0u {
rdr.fatal(~"no valid digits found for number");
}
2012-09-21 21:37:57 -05:00
let parsed = u64::from_str_radix(num_str, base as u64).get();
2012-08-06 14:34:08 -05:00
match tp {
2012-08-14 18:54:13 -05:00
either::Left(t) => return token::LIT_INT(parsed as i64, t),
either::Right(t) => return token::LIT_UINT(parsed, t)
}
}
let mut is_float = false;
if rdr.curr == '.' && !(is_alpha(nextch(rdr)) || nextch(rdr) == '_' ||
nextch(rdr) == '.') {
is_float = true;
2012-05-30 13:36:30 -05:00
bump(rdr);
let dec_part = scan_digits(rdr, 10u);
num_str += ~"." + dec_part;
}
2012-08-06 14:34:08 -05:00
match scan_exponent(rdr) {
Some(ref s) => {
is_float = true;
num_str += (*s);
}
2012-08-20 14:23:37 -05:00
None => ()
}
let mut is_machine_float = false;
if rdr.curr == 'f' {
2012-05-30 13:36:30 -05:00
bump(rdr);
c = rdr.curr;
2012-05-30 13:36:30 -05:00
n = nextch(rdr);
if c == '3' && n == '2' {
2012-05-30 13:36:30 -05:00
bump(rdr);
bump(rdr);
return token::LIT_FLOAT(rdr.interner.intern(@num_str),
ast::ty_f32);
} else if c == '6' && n == '4' {
2012-05-30 13:36:30 -05:00
bump(rdr);
bump(rdr);
return token::LIT_FLOAT(rdr.interner.intern(@num_str),
ast::ty_f64);
/* FIXME (#2252): if this is out of range for either a
32-bit or 64-bit float, it won't be noticed till the
back-end. */
} else {
is_float = true;
is_machine_float = true;
}
}
if is_float {
if is_machine_float {
return token::LIT_FLOAT(rdr.interner.intern(@num_str), ast::ty_f);
}
return token::LIT_FLOAT_UNSUFFIXED(rdr.interner.intern(@num_str));
} else {
2012-02-23 03:44:04 -06:00
if str::len(num_str) == 0u {
rdr.fatal(~"no valid digits found for number");
}
2012-09-21 21:37:57 -05:00
let parsed = u64::from_str_radix(num_str, base as u64).get();
2012-08-22 19:24:52 -05:00
debug!("lexing %s as an unsuffixed integer literal",
num_str);
2012-08-01 19:30:05 -05:00
return token::LIT_INT_UNSUFFIXED(parsed as i64);
}
}
2012-05-30 13:36:30 -05:00
fn scan_numeric_escape(rdr: string_reader, n_hex_digits: uint) -> char {
let mut accum_int = 0, i = n_hex_digits;
while i != 0u {
let n = rdr.curr;
2012-05-30 13:36:30 -05:00
bump(rdr);
2011-07-27 07:19:39 -05:00
if !is_hex_digit(n) {
2012-08-22 19:24:52 -05:00
rdr.fatal(fmt!("illegal numeric character escape: %d", n as int));
}
accum_int *= 16;
accum_int += hex_digit_val(n);
i -= 1u;
}
2012-08-01 19:30:05 -05:00
return accum_int as char;
}
fn next_token_inner(rdr: string_reader) -> token::Token {
let mut accum_str = ~"";
let mut c = rdr.curr;
2012-03-02 20:45:10 -06:00
if (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == '_'
|| (c > 'z' && char::is_XID_start(c)) {
while (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '_'
|| (c > 'z' && char::is_XID_continue(c)) {
2012-09-21 20:36:32 -05:00
str::push_char(&mut accum_str, c);
2012-05-30 13:36:30 -05:00
bump(rdr);
c = rdr.curr;
}
if accum_str == ~"_" { return token::UNDERSCORE; }
2012-05-30 13:36:30 -05:00
let is_mod_name = c == ':' && nextch(rdr) == ':';
// FIXME: perform NFKC normalization here. (Issue #2253)
return token::IDENT(rdr.interner.intern(@accum_str), is_mod_name);
2010-08-20 12:03:31 -05:00
}
if is_dec_digit(c) {
2012-08-01 19:30:05 -05:00
return scan_number(c, rdr);
2011-09-02 00:39:44 -05:00
}
fn binop(rdr: string_reader, op: token::binop) -> token::Token {
2012-05-30 13:36:30 -05:00
bump(rdr);
if rdr.curr == '=' {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::BINOPEQ(op);
} else { return token::BINOP(op); }
}
2012-08-06 14:34:08 -05:00
match c {
2011-07-27 07:19:39 -05:00
2011-09-02 17:34:58 -05:00
2011-09-12 05:39:38 -05:00
2011-07-27 07:19:39 -05:00
// One-byte tokens.
2012-08-03 21:59:04 -05:00
';' => { bump(rdr); return token::SEMI; }
',' => { bump(rdr); return token::COMMA; }
'.' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
if rdr.curr == '.' && nextch(rdr) != '.' {
bump(rdr);
return token::DOTDOT;
}
2012-05-30 13:36:30 -05:00
if rdr.curr == '.' && nextch(rdr) == '.' {
bump(rdr);
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::ELLIPSIS;
2011-07-27 07:19:39 -05:00
}
2012-08-01 19:30:05 -05:00
return token::DOT;
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'(' => { bump(rdr); return token::LPAREN; }
')' => { bump(rdr); return token::RPAREN; }
'{' => { bump(rdr); return token::LBRACE; }
'}' => { bump(rdr); return token::RBRACE; }
'[' => { bump(rdr); return token::LBRACKET; }
']' => { bump(rdr); return token::RBRACKET; }
'@' => { bump(rdr); return token::AT; }
'#' => { bump(rdr); return token::POUND; }
'~' => { bump(rdr); return token::TILDE; }
':' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
if rdr.curr == ':' {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::MOD_SEP;
} else { return token::COLON; }
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'$' => { bump(rdr); return token::DOLLAR; }
2011-09-02 17:34:58 -05:00
2011-09-12 05:39:38 -05:00
2011-07-27 07:19:39 -05:00
// Multi-byte tokens.
2012-08-03 21:59:04 -05:00
'=' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
if rdr.curr == '=' {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::EQEQ;
} else if rdr.curr == '>' {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::FAT_ARROW;
} else {
2012-08-01 19:30:05 -05:00
return token::EQ;
}
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'!' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
if rdr.curr == '=' {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::NE;
} else { return token::NOT; }
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'<' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-06 14:34:08 -05:00
match rdr.curr {
2012-08-03 21:59:04 -05:00
'=' => { bump(rdr); return token::LE; }
'<' => { return binop(rdr, token::SHL); }
'-' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-06 14:34:08 -05:00
match rdr.curr {
2012-08-03 21:59:04 -05:00
'>' => { bump(rdr); return token::DARROW; }
_ => { return token::LARROW; }
}
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
_ => { return token::LT; }
}
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'>' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-06 14:34:08 -05:00
match rdr.curr {
2012-08-03 21:59:04 -05:00
'=' => { bump(rdr); return token::GE; }
'>' => { return binop(rdr, token::SHR); }
_ => { return token::GT; }
}
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'\'' => {
2012-05-30 13:36:30 -05:00
bump(rdr);
let mut c2 = rdr.curr;
2012-05-30 13:36:30 -05:00
bump(rdr);
2011-07-27 07:19:39 -05:00
if c2 == '\\' {
let escaped = rdr.curr;
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-06 14:34:08 -05:00
match escaped {
2012-08-03 21:59:04 -05:00
'n' => { c2 = '\n'; }
'r' => { c2 = '\r'; }
't' => { c2 = '\t'; }
'\\' => { c2 = '\\'; }
'\'' => { c2 = '\''; }
'"' => { c2 = '"'; }
'x' => { c2 = scan_numeric_escape(rdr, 2u); }
'u' => { c2 = scan_numeric_escape(rdr, 4u); }
'U' => { c2 = scan_numeric_escape(rdr, 8u); }
c2 => {
2012-08-22 19:24:52 -05:00
rdr.fatal(fmt!("unknown character escape: %d", c2 as int));
2011-07-27 07:19:39 -05:00
}
}
}
if rdr.curr != '\'' {
rdr.fatal(~"unterminated character constant");
2011-07-27 07:19:39 -05:00
}
2012-05-30 13:36:30 -05:00
bump(rdr); // advance curr past token
2012-08-01 19:30:05 -05:00
return token::LIT_INT(c2 as i64, ast::ty_char);
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'"' => {
let n = byte_offset(rdr);
2012-05-30 13:36:30 -05:00
bump(rdr);
while rdr.curr != '"' {
2012-05-30 13:36:30 -05:00
if is_eof(rdr) {
2012-08-22 19:24:52 -05:00
rdr.fatal(fmt!("unterminated double quote string: %s",
get_str_from(rdr, n)));
}
let ch = rdr.curr;
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-06 14:34:08 -05:00
match ch {
2012-08-03 21:59:04 -05:00
'\\' => {
let escaped = rdr.curr;
2012-05-30 13:36:30 -05:00
bump(rdr);
2012-08-06 14:34:08 -05:00
match escaped {
2012-09-21 20:36:32 -05:00
'n' => str::push_char(&mut accum_str, '\n'),
'r' => str::push_char(&mut accum_str, '\r'),
't' => str::push_char(&mut accum_str, '\t'),
'\\' => str::push_char(&mut accum_str, '\\'),
'\'' => str::push_char(&mut accum_str, '\''),
'"' => str::push_char(&mut accum_str, '"'),
2012-08-03 21:59:04 -05:00
'\n' => consume_whitespace(rdr),
'x' => {
2012-09-21 20:36:32 -05:00
str::push_char(&mut accum_str,
scan_numeric_escape(rdr, 2u));
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'u' => {
2012-09-21 20:36:32 -05:00
str::push_char(&mut accum_str,
scan_numeric_escape(rdr, 4u));
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'U' => {
2012-09-21 20:36:32 -05:00
str::push_char(&mut accum_str,
scan_numeric_escape(rdr, 8u));
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
c2 => {
2012-08-22 19:24:52 -05:00
rdr.fatal(fmt!("unknown string escape: %d", c2 as int));
2011-07-27 07:19:39 -05:00
}
}
2011-07-27 07:19:39 -05:00
}
2012-09-21 20:36:32 -05:00
_ => str::push_char(&mut accum_str, ch)
}
}
2012-05-30 13:36:30 -05:00
bump(rdr);
return token::LIT_STR(rdr.interner.intern(@accum_str));
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'-' => {
2012-05-30 13:36:30 -05:00
if nextch(rdr) == '>' {
bump(rdr);
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::RARROW;
} else { return binop(rdr, token::MINUS); }
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'&' => {
2012-05-30 13:36:30 -05:00
if nextch(rdr) == '&' {
bump(rdr);
bump(rdr);
2012-08-01 19:30:05 -05:00
return token::ANDAND;
} else { return binop(rdr, token::AND); }
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'|' => {
2012-08-06 14:34:08 -05:00
match nextch(rdr) {
2012-08-03 21:59:04 -05:00
'|' => { bump(rdr); bump(rdr); return token::OROR; }
_ => { return binop(rdr, token::OR); }
2011-07-27 07:19:39 -05:00
}
}
2012-08-03 21:59:04 -05:00
'+' => { return binop(rdr, token::PLUS); }
'*' => { return binop(rdr, token::STAR); }
'/' => { return binop(rdr, token::SLASH); }
'^' => { return binop(rdr, token::CARET); }
'%' => { return binop(rdr, token::PERCENT); }
2012-08-22 19:24:52 -05:00
c => { rdr.fatal(fmt!("unknown start of token: %d", c as int)); }
}
}
2012-05-30 13:36:30 -05:00
fn consume_whitespace(rdr: string_reader) {
while is_whitespace(rdr.curr) && !is_eof(rdr) { bump(rdr); }
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//