Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/comp/driver/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impure fn pretty_print_input(session.session sess,
auto def = tup(0, 0);
auto p = front.parser.new_parser(sess, env, def, input);
auto crate = front.parser.parse_crate_from_source_file(p);
pretty.pprust.print_ast(crate.node.module, std.io.stdout());
pretty.pprust.print_file(crate.node.module, input, std.io.stdout());
}

fn warn_wrong_compiler() {
Expand Down
12 changes: 6 additions & 6 deletions src/comp/front/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ tag lit_ {
type mt = rec(@ty ty, mutability mut);
type ty_field = rec(ident ident, mt mt);
type ty_arg = rec(mode mode, @ty ty);
// TODO: effect
type ty_method = rec(proto proto, ident ident,
type ty_method = rec(effect effect, proto proto, ident ident,
vec[ty_arg] inputs, @ty output);
type ty = spanned[ty_];
tag ty_ {
Expand All @@ -324,7 +323,7 @@ tag ty_ {
ty_chan(@ty);
ty_tup(vec[mt]);
ty_rec(vec[ty_field]);
ty_fn(proto, vec[ty_arg], @ty); // TODO: effect
ty_fn(effect, proto, vec[ty_arg], @ty);
ty_obj(vec[ty_method]);
ty_path(path, option.t[def]);
ty_type;
Expand Down Expand Up @@ -386,7 +385,8 @@ type native_mod = rec(str native_name,
type native_mod_index = hashmap[ident,native_mod_index_entry];

type variant_arg = rec(@ty ty, def_id id);
type variant = rec(str name, vec[variant_arg] args, def_id id, ann ann);
type variant_ = rec(str name, vec[variant_arg] args, def_id id, ann ann);
type variant = spanned[variant_];

type view_item = spanned[view_item_];
tag view_item_ {
Expand Down Expand Up @@ -452,7 +452,7 @@ fn index_item(mod_index index, @item it) {
index.insert(id, ast.mie_item(it));
let uint variant_idx = 0u;
for (ast.variant v in variants) {
index.insert(v.name,
index.insert(v.node.name,
ast.mie_tag_variant(it, variant_idx));
variant_idx += 1u;
}
Expand Down Expand Up @@ -510,7 +510,7 @@ fn index_stmt(block_index index, @stmt s) {
let uint vid = 0u;
for (ast.variant v in variants) {
auto t = ast.bie_tag_variant(it, vid);
index.insert(v.name, t);
index.insert(v.node.name, t);
vid += 1u;
}
}
Expand Down
87 changes: 86 additions & 1 deletion src/comp/front/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import std.io;
import std._str;
import std._vec;
import std._int;
import std.map;
import std.map.hashmap;
Expand Down Expand Up @@ -281,7 +282,7 @@ impure fn consume_any_line_comment(reader rdr) {
if (rdr.curr() == '/') {
alt (rdr.next()) {
case ('/') {
while (rdr.curr() != '\n') {
while (rdr.curr() != '\n' && !rdr.is_eof()) {
rdr.bump();
}
// Restart whitespace munch.
Expand Down Expand Up @@ -316,6 +317,10 @@ impure fn consume_block_comment(reader rdr) {
rdr.bump();
}
}
if (rdr.is_eof()) {
log "unterminated block comment";
fail;
}
}
// restart whitespace munch.
be consume_any_whitespace(rdr);
Expand Down Expand Up @@ -781,6 +786,86 @@ impure fn next_token(reader rdr) -> token.token {
fail;
}

tag cmnt_ {
cmnt_line(str);
cmnt_block(vec[str]);
}
type cmnt = rec(cmnt_ val, common.pos pos, bool space_after);

impure fn consume_whitespace(reader rdr) -> uint {
auto lines = 0u;
while (is_whitespace(rdr.curr())) {
if (rdr.curr() == '\n') {lines += 1u;}
rdr.bump();
}
ret lines;
}

impure fn read_line_comment(reader rdr) -> cmnt {
auto p = rdr.get_curr_pos();
rdr.bump(); rdr.bump();
while (rdr.curr() == ' ') {rdr.bump();}
auto val = "";
while (rdr.curr() != '\n' && !rdr.is_eof()) {
_str.push_char(val, rdr.curr());
rdr.bump();
}
ret rec(val=cmnt_line(val),
pos=p,
space_after=consume_whitespace(rdr) > 1u);
}

impure fn read_block_comment(reader rdr) -> cmnt {
auto p = rdr.get_curr_pos();
rdr.bump(); rdr.bump();
while (rdr.curr() == ' ') {rdr.bump();}
let vec[str] lines = vec();
auto val = "";
auto level = 1;
while (true) {
if (rdr.curr() == '\n') {
_vec.push[str](lines, val);
val = "";
consume_whitespace(rdr);
} else {
if (rdr.curr() == '*' && rdr.next() == '/') {
level -= 1;
if (level == 0) {
rdr.bump(); rdr.bump();
_vec.push[str](lines, val);
break;
}
} else if (rdr.curr() == '/' && rdr.next() == '*') {
level += 1;
}
_str.push_char(val, rdr.curr());
rdr.bump();
}
if (rdr.is_eof()) {fail;}
}
ret rec(val=cmnt_block(lines),
pos=p,
space_after=consume_whitespace(rdr) > 1u);
}

impure fn gather_comments(str path) -> vec[cmnt] {
auto srdr = io.file_reader(path);
auto rdr = lexer.new_reader(srdr, path);
let vec[cmnt] comments = vec();
while (!rdr.is_eof()) {
while (true) {
consume_whitespace(rdr);
if (rdr.curr() == '/' && rdr.next() == '/') {
_vec.push[cmnt](comments, read_line_comment(rdr));
} else if (rdr.curr() == '/' && rdr.next() == '*') {
_vec.push[cmnt](comments, read_block_comment(rdr));
} else { break; }
}
next_token(rdr);
}
ret comments;
}


//
// Local Variables:
Expand Down
39 changes: 23 additions & 16 deletions src/comp/front/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ impure fn new_parser(session.session sess,
}
auto srdr = io.file_reader(path);
auto rdr = lexer.new_reader(srdr, path);
// Make sure npos points at first actual token.
lexer.consume_any_whitespace(rdr);
auto npos = rdr.get_curr_pos();
ret stdio_parser(sess, env, ftype, lexer.next_token(rdr),
npos, npos, initial_def._1, UNRESTRICTED, initial_def._0,
Expand Down Expand Up @@ -182,7 +184,7 @@ impure fn parse_str_lit_or_env_ident(parser p) -> ast.ident {
}


impure fn parse_ty_fn(ast.proto proto, parser p,
impure fn parse_ty_fn(ast.effect eff, ast.proto proto, parser p,
ast.span lo) -> ast.ty_ {
impure fn parse_fn_input_ty(parser p) -> rec(ast.mode mode, @ast.ty ty) {
auto mode;
Expand Down Expand Up @@ -226,7 +228,7 @@ impure fn parse_ty_fn(ast.proto proto, parser p,
output = @spanned(lo, inputs.span, ast.ty_nil);
}

ret ast.ty_fn(proto, inputs.node, output);
ret ast.ty_fn(eff, proto, inputs.node, output);
}

impure fn parse_proto(parser p) -> ast.proto {
Expand All @@ -243,15 +245,14 @@ impure fn parse_ty_obj(parser p, &mutable ast.span hi) -> ast.ty_ {
impure fn parse_method_sig(parser p) -> ast.ty_method {
auto flo = p.get_span();

// FIXME: do something with this, currently it's dropped on the floor.
let ast.effect eff = parse_effect(p);
let ast.proto proto = parse_proto(p);
auto ident = parse_ident(p);
auto f = parse_ty_fn(proto, p, flo);
auto f = parse_ty_fn(eff, proto, p, flo);
expect(p, token.SEMI);
alt (f) {
case (ast.ty_fn(?proto, ?inputs, ?output)) {
ret rec(proto=proto, ident=ident,
case (ast.ty_fn(?eff, ?proto, ?inputs, ?output)) {
ret rec(effect=eff, proto=proto, ident=ident,
inputs=inputs, output=output);
}
}
Expand Down Expand Up @@ -340,9 +341,9 @@ impure fn parse_ty(parser p) -> @ast.ty {
auto hi = lo;
let ast.ty_ t;

// FIXME: do something with these; currently they're
// dropped on the floor.
// FIXME: make sure these are only used when valid
let ast.effect eff = parse_effect(p);
// FIXME: do something with this
let ast.layer lyr = parse_layer(p);

alt (p.peek()) {
Expand Down Expand Up @@ -410,9 +411,9 @@ impure fn parse_ty(parser p) -> @ast.ty {
case (token.FN) {
auto flo = p.get_span();
p.bump();
t = parse_ty_fn(ast.proto_fn, p, flo);
t = parse_ty_fn(eff, ast.proto_fn, p, flo);
alt (t) {
case (ast.ty_fn(_, _, ?out)) {
case (ast.ty_fn(_, _, _, ?out)) {
hi = out.span;
}
}
Expand All @@ -421,9 +422,9 @@ impure fn parse_ty(parser p) -> @ast.ty {
case (token.ITER) {
auto flo = p.get_span();
p.bump();
t = parse_ty_fn(ast.proto_iter, p, flo);
t = parse_ty_fn(eff, ast.proto_iter, p, flo);
alt (t) {
case (ast.ty_fn(_, _, ?out)) {
case (ast.ty_fn(_, _, _, ?out)) {
hi = out.span;
}
}
Expand All @@ -450,7 +451,9 @@ impure fn parse_ty(parser p) -> @ast.ty {
}

case (token.IDENT(_)) {
t = ast.ty_path(parse_path(p, GREEDY), none[ast.def]);
auto path = parse_path(p, GREEDY);
t = ast.ty_path(path, none[ast.def]);
hi = path.span;
}

case (token.MUTABLE) {
Expand Down Expand Up @@ -1682,6 +1685,8 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
case (ast.expr_index(_,_,_)) { ret true; }
case (ast.expr_path(_,_,_)) { ret true; }
case (ast.expr_fail) { ret true; }
case (ast.expr_break) { ret true; }
case (ast.expr_cont) { ret true; }
case (ast.expr_ret(_)) { ret true; }
case (ast.expr_put(_)) { ret true; }
case (ast.expr_be(_)) { ret true; }
Expand Down Expand Up @@ -1748,8 +1753,8 @@ impure fn parse_block(parser p) -> ast.block {
}
}

p.bump();
auto hi = p.get_span();
p.bump();

auto bloc = index_block(stmts, expr);
ret spanned[ast.block_](lo, hi, bloc);
Expand Down Expand Up @@ -2064,6 +2069,7 @@ impure fn parse_item_tag(parser p) -> @ast.item {
auto tok = p.peek();
alt (tok) {
case (token.IDENT(?name)) {
auto vlo = p.get_span();
p.bump();

let vec[ast.variant_arg] args = vec();
Expand All @@ -2081,11 +2087,12 @@ impure fn parse_item_tag(parser p) -> @ast.item {
case (_) { /* empty */ }
}

auto vhi = p.get_span();
expect(p, token.SEMI);

auto id = p.next_def_id();
variants += vec(rec(name=name, args=args, id=id,
ann=ast.ann_none));
auto vr = rec(name=name, args=args, id=id, ann=ast.ann_none);
variants += vec(spanned[ast.variant_](vlo, vhi, vr));
}
case (token.RBRACE) { /* empty */ }
case (_) {
Expand Down
30 changes: 16 additions & 14 deletions src/comp/middle/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type ast_fold[ENV] =
vec[ast.ty_method] meths) -> @ty) fold_ty_obj,

(fn(&ENV e, &span sp,
ast.effect eff,
ast.proto proto,
vec[rec(ast.mode mode, @ty ty)] inputs,
@ty output) -> @ty) fold_ty_fn,
Expand Down Expand Up @@ -388,13 +389,13 @@ fn fold_ty[ENV](&ENV env, ast_fold[ENV] fld, @ty t) -> @ty {
case (ast.ty_obj(?meths)) {
let vec[ast.ty_method] meths_ = vec();
for (ast.ty_method m in meths) {
auto tfn = fold_ty_fn(env_, fld, t.span, m.proto,
auto tfn = fold_ty_fn(env_, fld, t.span, m.effect, m.proto,
m.inputs, m.output);
alt (tfn.node) {
case (ast.ty_fn(?p, ?ins, ?out)) {
case (ast.ty_fn(?eff, ?p, ?ins, ?out)) {
_vec.push[ast.ty_method]
(meths_, rec(proto=p, inputs=ins, output=out
with m));
(meths_, rec(effect=eff, proto=p, inputs=ins,
output=out with m));
}
}
}
Expand All @@ -406,8 +407,8 @@ fn fold_ty[ENV](&ENV env, ast_fold[ENV] fld, @ty t) -> @ty {
ret fld.fold_ty_path(env_, t.span, pth_, ref_opt);
}

case (ast.ty_fn(?proto, ?inputs, ?output)) {
ret fold_ty_fn(env_, fld, t.span, proto, inputs, output);
case (ast.ty_fn(?eff, ?proto, ?inputs, ?output)) {
ret fold_ty_fn(env_, fld, t.span, eff, proto, inputs, output);
}

case (ast.ty_chan(?ty)) {
Expand All @@ -423,7 +424,7 @@ fn fold_ty[ENV](&ENV env, ast_fold[ENV] fld, @ty t) -> @ty {
}

fn fold_ty_fn[ENV](&ENV env, ast_fold[ENV] fld, &span sp,
ast.proto proto,
ast.effect eff, ast.proto proto,
vec[rec(ast.mode mode, @ty ty)] inputs,
@ty output) -> @ty {
auto output_ = fold_ty(env, fld, output);
Expand All @@ -433,7 +434,7 @@ fn fold_ty_fn[ENV](&ENV env, ast_fold[ENV] fld, &span sp,
auto input_ = rec(ty=ty_ with input);
inputs_ += vec(input_);
}
ret fld.fold_ty_fn(env, sp, proto, inputs_, output_);
ret fld.fold_ty_fn(env, sp, eff, proto, inputs_, output_);
}

fn fold_decl[ENV](&ENV env, ast_fold[ENV] fld, @decl d) -> @decl {
Expand Down Expand Up @@ -964,12 +965,13 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
let vec[ast.variant] new_variants = vec();
for (ast.variant v in variants) {
let vec[ast.variant_arg] new_args = vec();
for (ast.variant_arg va in v.args) {
for (ast.variant_arg va in v.node.args) {
auto new_ty = fold_ty[ENV](env_, fld, va.ty);
new_args += vec(rec(ty=new_ty, id=va.id));
}
new_variants += vec(rec(name=v.name, args=new_args, id=v.id,
ann=v.ann));
auto new_v = rec(name=v.node.name, args=new_args,
id=v.node.id, ann=v.node.ann);
new_variants += vec(respan[ast.variant_](v.span, new_v));
}
ret fld.fold_item_tag(env_, i.span, ident, new_variants,
ty_params, id);
Expand Down Expand Up @@ -1131,10 +1133,10 @@ fn identity_fold_ty_obj[ENV](&ENV env, &span sp,
}

fn identity_fold_ty_fn[ENV](&ENV env, &span sp,
ast.proto proto,
ast.effect eff, ast.proto proto,
vec[rec(ast.mode mode, @ty ty)] inputs,
@ty output) -> @ty {
ret @respan(sp, ast.ty_fn(proto, inputs, output));
ret @respan(sp, ast.ty_fn(eff, proto, inputs, output));
}

fn identity_fold_ty_path[ENV](&ENV env, &span sp, ast.path p,
Expand Down Expand Up @@ -1569,7 +1571,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_ty_tup = bind identity_fold_ty_tup[ENV](_,_,_),
fold_ty_rec = bind identity_fold_ty_rec[ENV](_,_,_),
fold_ty_obj = bind identity_fold_ty_obj[ENV](_,_,_),
fold_ty_fn = bind identity_fold_ty_fn[ENV](_,_,_,_,_),
fold_ty_fn = bind identity_fold_ty_fn[ENV](_,_,_,_,_,_),
fold_ty_path = bind identity_fold_ty_path[ENV](_,_,_,_),
fold_ty_chan = bind identity_fold_ty_chan[ENV](_,_,_),
fold_ty_port = bind identity_fold_ty_port[ENV](_,_,_),
Expand Down
Loading