Give the lexer a session so that it can fail more informatively

This commit is contained in:
Brian Anderson 2011-05-11 23:04:16 -04:00
parent 531515d7ae
commit a459bcd127
4 changed files with 34 additions and 22 deletions

View File

@ -123,7 +123,7 @@ fn pretty_print_input(session.session sess,
auto def = tup(ast.local_crate, 0);
auto p = front.parser.new_parser(sess, env, def, input, 0u);
auto crate = front.parser.parse_crate_from_source_file(p);
pretty.pprust.print_file(crate.node.module, input, std.IO.stdout());
pretty.pprust.print_file(sess, crate.node.module, input, std.IO.stdout());
}
fn version(str argv0) {

View File

@ -7,6 +7,7 @@
import std.Option;
import std.Option.some;
import std.Option.none;
import driver.session.session;
import util.common;
import util.common.new_str_hash;
@ -24,11 +25,14 @@
fn get_keywords() -> hashmap[str,token.token];
fn get_reserved() -> hashmap[str,()];
fn get_filemap() -> codemap.filemap;
fn err(str m);
};
fn new_reader(IO.reader rdr, str filename, codemap.filemap filemap)
-> reader {
state obj reader(str file,
fn new_reader(session sess, IO.reader rdr, str filename,
codemap.filemap filemap) -> reader {
state obj reader(session sess,
str file,
uint len,
mutable uint pos,
mutable char ch,
@ -98,10 +102,14 @@ fn get_reserved() -> hashmap[str,()] {
fn get_filemap() -> codemap.filemap {
ret fm;
}
fn err(str m) {
sess.span_err(rec(lo=chpos, hi=chpos), m);
}
}
auto file = Str.unsafe_from_bytes(rdr.read_whole_stream());
let vec[str] strs = vec();
auto rd = reader(file, Str.byte_len(file), 0u, -1 as char,
auto rd = reader(sess, file, Str.byte_len(file), 0u, -1 as char,
filemap.start_pos, filemap.start_pos,
strs, keyword_table(),
reserved_word_table(),
@ -333,7 +341,7 @@ fn consume_block_comment(reader rdr) {
}
}
if (rdr.is_eof()) {
log_err "unterminated block comment";
rdr.err("unterminated block comment");
fail;
}
}
@ -371,7 +379,7 @@ fn scan_exponent(reader rdr) -> Option.t[str] {
ret(some(res + exponent));
}
else {
log_err ("scan_exponent: bad fp literal");
rdr.err("scan_exponent: bad fp literal");
fail;
}
}
@ -550,7 +558,7 @@ fn scan_numeric_escape(reader rdr) -> char {
case ('u') { n_hex_digits = 4; }
case ('U') { n_hex_digits = 8; }
case (?c) {
log_err #fmt("unknown numeric character escape: %d", c as int);
rdr.err(#fmt("unknown numeric character escape: %d", c as int));
fail;
}
}
@ -562,7 +570,7 @@ fn scan_numeric_escape(reader rdr) -> char {
while (n_hex_digits != 0) {
if (!is_hex_digit(n)) {
log_err #fmt("illegal numeric character escape: %d", n as int);
rdr.err(#fmt("illegal numeric character escape: %d", n as int));
fail;
}
accum_int *= 16;
@ -603,7 +611,7 @@ fn next_token(reader rdr) -> token.token {
auto rsvd = rdr.get_reserved();
if (rsvd.contains_key(accum_str)) {
log_err #fmt("reserved keyword: %s", accum_str);
rdr.err(#fmt("reserved keyword: %s", accum_str));
fail;
}
@ -726,8 +734,8 @@ fn binop(reader rdr, token.binop op) -> token.token {
case ('U') { c2 = scan_numeric_escape(rdr); }
case (?c2) {
log_err #fmt("unknown character escape: %d",
c2 as int);
rdr.err(#fmt("unknown character escape: %d",
c2 as int));
fail;
}
}
@ -735,7 +743,7 @@ fn binop(reader rdr, token.binop op) -> token.token {
}
if (rdr.next() != '\'') {
log_err "unterminated character constant";
rdr.err("unterminated character constant");
fail;
}
rdr.bump(); // advance curr to closing '
@ -786,8 +794,8 @@ fn binop(reader rdr, token.binop op) -> token.token {
}
case (?c2) {
log_err #fmt("unknown string escape: %d",
c2 as int);
rdr.err(#fmt("unknown string escape: %d",
c2 as int));
fail;
}
}
@ -853,7 +861,7 @@ fn binop(reader rdr, token.binop op) -> token.token {
}
case (?c) {
log_err #fmt("unkown start of token: %d", c as int);
rdr.err(#fmt("unkown start of token: %d", c as int));
fail;
}
}
@ -917,16 +925,19 @@ fn read_block_comment(reader rdr) -> cmnt {
Str.push_char(val, rdr.curr());
rdr.bump();
}
if (rdr.is_eof()) {fail;}
if (rdr.is_eof()) {
rdr.err("Unexpected end of file in block comment");
fail;
}
}
ret rec(val=cmnt_block(lines),
pos=p,
space_after=consume_whitespace(rdr) > 1u);
}
fn gather_comments(str path) -> vec[cmnt] {
fn gather_comments(session sess, str path) -> vec[cmnt] {
auto srdr = IO.file_reader(path);
auto rdr = new_reader(srdr, path, codemap.new_filemap(path, 0u));
auto rdr = new_reader(sess, srdr, path, codemap.new_filemap(path, 0u));
let vec[cmnt] comments = vec();
while (!rdr.is_eof()) {
while (true) {

View File

@ -142,7 +142,7 @@ fn get_ann() -> ast.ann {
auto srdr = IO.file_reader(path);
auto filemap = codemap.new_filemap(path, pos);
Vec.push[codemap.filemap](sess.get_codemap().files, filemap);
auto rdr = lexer.new_reader(srdr, path, filemap);
auto rdr = lexer.new_reader(sess, srdr, path, filemap);
// Make sure npos points at first actual token.
lexer.consume_any_whitespace(rdr);
auto npos = rdr.get_chpos();

View File

@ -2,6 +2,7 @@
import std.Str;
import std.IO;
import std.Option;
import driver.session.session;
import front.ast;
import front.lexer;
import util.common;
@ -15,8 +16,8 @@
Option.t[vec[lexer.cmnt]] comments,
mutable uint cur_cmnt);
fn print_file(ast._mod _mod, str filename, IO.writer out) {
auto cmnts = lexer.gather_comments(filename);
fn print_file(session sess, ast._mod _mod, str filename, IO.writer out) {
auto cmnts = lexer.gather_comments(sess, filename);
auto s = @rec(s=pp.mkstate(out, default_columns),
comments=Option.some[vec[lexer.cmnt]](cmnts),
mutable cur_cmnt=0u);