Move everything syntax-related to syntax/, break deps on rest of compiler
src/comp/syntax is currently just a sub-module of rustc, but it will, in the near future, be its own crate. This includes: - The AST data structure - The parser - The pretty-printer - Visit, walk, and fold - The syntax extension system - Some utility stuff that should be in the stdlib* *) Stdlib extensions currently require a snapshot before they can be used, and the win build is very broken right now. This is temporary and will be cleaned up when one of those problems goes away. A lot of code was moved by this patch, mostly towards a more organized layout. Some package paths did get longer, and I guess the new layout will take some getting used to. Sorry about that! Please try not to re-introduce any dependencies in syntax/ on any of the other src/comp/ subdirs.
This commit is contained in:
124
src/comp/syntax/parse/eval.rs
Normal file
124
src/comp/syntax/parse/eval.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
|
||||
import std::vec;
|
||||
import std::str;
|
||||
import std::option;
|
||||
import std::option::some;
|
||||
import std::option::none;
|
||||
import syntax::ast;
|
||||
import syntax::parse::token;
|
||||
import syntax::parse::parser::parser;
|
||||
import syntax::parse::parser::new_parser;
|
||||
import syntax::parse::parser::parse_inner_attrs_and_next;
|
||||
import syntax::parse::parser::parse_mod_items;
|
||||
|
||||
export eval_crate_directives_to_mod;
|
||||
export mode_parse;
|
||||
|
||||
tag eval_mode { mode_depend; mode_parse; }
|
||||
|
||||
type ctx =
|
||||
@rec(parser p,
|
||||
eval_mode mode,
|
||||
mutable vec[str] deps,
|
||||
parser::parse_sess sess,
|
||||
mutable uint chpos,
|
||||
ast::crate_cfg cfg);
|
||||
|
||||
fn eval_crate_directives(ctx cx, vec[@ast::crate_directive] cdirs,
|
||||
str prefix, &mutable vec[@ast::view_item] view_items,
|
||||
&mutable vec[@ast::item] items) {
|
||||
for (@ast::crate_directive sub_cdir in cdirs) {
|
||||
eval_crate_directive(cx, sub_cdir, prefix, view_items, items);
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_crate_directives_to_mod(ctx cx,
|
||||
vec[@ast::crate_directive] cdirs, str prefix)
|
||||
-> ast::_mod {
|
||||
let vec[@ast::view_item] view_items = [];
|
||||
let vec[@ast::item] items = [];
|
||||
eval_crate_directives(cx, cdirs, prefix, view_items, items);
|
||||
ret rec(view_items=view_items, items=items);
|
||||
}
|
||||
|
||||
fn eval_crate_directive_block(ctx cx, &ast::block blk, str prefix,
|
||||
&mutable vec[@ast::view_item] view_items,
|
||||
&mutable vec[@ast::item] items) {
|
||||
for (@ast::stmt s in blk.node.stmts) {
|
||||
alt (s.node) {
|
||||
case (ast::stmt_crate_directive(?cdir)) {
|
||||
eval_crate_directive(cx, cdir, prefix, view_items, items);
|
||||
}
|
||||
case (_) {
|
||||
codemap::emit_warning
|
||||
(some(s.span), "unsupported stmt in crate-directive block",
|
||||
cx.sess.cm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_crate_directive(ctx cx, @ast::crate_directive cdir, str prefix,
|
||||
&mutable vec[@ast::view_item] view_items,
|
||||
&mutable vec[@ast::item] items) {
|
||||
alt (cdir.node) {
|
||||
case (ast::cdir_src_mod(?id, ?file_opt, ?attrs)) {
|
||||
auto file_path = id + ".rs";
|
||||
alt (file_opt) {
|
||||
case (some(?f)) { file_path = f; }
|
||||
case (none) { }
|
||||
}
|
||||
auto full_path = if (std::fs::path_is_absolute(file_path)) {
|
||||
file_path
|
||||
} else {
|
||||
prefix + std::fs::path_sep() + file_path
|
||||
};
|
||||
if (cx.mode == mode_depend) { cx.deps += [full_path]; ret; }
|
||||
auto p0 =
|
||||
new_parser(cx.sess, cx.cfg, full_path, cx.chpos);
|
||||
auto inner_attrs = parse_inner_attrs_and_next(p0);
|
||||
auto mod_attrs = attrs + inner_attrs._0;
|
||||
auto first_item_outer_attrs = inner_attrs._1;
|
||||
auto m0 = parse_mod_items(p0, token::EOF, first_item_outer_attrs);
|
||||
|
||||
auto i = syntax::parse::parser::mk_item
|
||||
(p0, cdir.span.lo, cdir.span.hi, id, ast::item_mod(m0),
|
||||
mod_attrs);
|
||||
// Thread defids and chpos through the parsers
|
||||
cx.chpos = p0.get_chpos();
|
||||
vec::push[@ast::item](items, i);
|
||||
}
|
||||
case (ast::cdir_dir_mod(?id, ?dir_opt, ?cdirs, ?attrs)) {
|
||||
auto path = id;
|
||||
alt (dir_opt) { case (some(?d)) { path = d; } case (none) { } }
|
||||
auto full_path = if (std::fs::path_is_absolute(path)) {
|
||||
path
|
||||
} else {
|
||||
prefix + std::fs::path_sep() + path
|
||||
};
|
||||
auto m0 = eval_crate_directives_to_mod(cx, cdirs, full_path);
|
||||
auto i = @rec(ident=id,
|
||||
attrs=attrs,
|
||||
id=cx.sess.next_id,
|
||||
node=ast::item_mod(m0),
|
||||
span=cdir.span);
|
||||
cx.sess.next_id += 1;
|
||||
vec::push[@ast::item](items, i);
|
||||
}
|
||||
case (ast::cdir_view_item(?vi)) {
|
||||
vec::push[@ast::view_item](view_items, vi);
|
||||
}
|
||||
case (ast::cdir_syntax(?pth)) { }
|
||||
case (ast::cdir_auth(?pth, ?eff)) { }
|
||||
}
|
||||
}
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
// fill-column: 78;
|
||||
// indent-tabs-mode: nil
|
||||
// c-basic-offset: 4
|
||||
// buffer-file-coding-system: utf-8-unix
|
||||
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
||||
// End:
|
||||
//
|
||||
Reference in New Issue
Block a user