Make the macro system more modular.
This commit is contained in:
parent
cfc75695a5
commit
79fcd51b46
4 changed files with 63 additions and 74 deletions
|
@ -14,9 +14,9 @@ import std::generic_os;
|
|||
export expand_syntax_ext;
|
||||
|
||||
// FIXME: Need to thread parser through here to handle errors correctly
|
||||
fn expand_syntax_ext(parser::parser p,
|
||||
fn expand_syntax_ext(&parser::parser p,
|
||||
common::span sp,
|
||||
vec[@ast::expr] args,
|
||||
&vec[@ast::expr] args,
|
||||
option::t[str] body) -> @ast::expr {
|
||||
|
||||
if (vec::len[@ast::expr](args) != 1u) {
|
||||
|
|
|
@ -13,41 +13,12 @@ import std::option;
|
|||
import std::option::none;
|
||||
import std::option::some;
|
||||
|
||||
import std::extfmt::ct::signedness;
|
||||
import std::extfmt::ct::signed;
|
||||
import std::extfmt::ct::unsigned;
|
||||
import std::extfmt::ct::caseness;
|
||||
import std::extfmt::ct::case_upper;
|
||||
import std::extfmt::ct::case_lower;
|
||||
import std::extfmt::ct::ty;
|
||||
import std::extfmt::ct::ty_bool;
|
||||
import std::extfmt::ct::ty_str;
|
||||
import std::extfmt::ct::ty_char;
|
||||
import std::extfmt::ct::ty_int;
|
||||
import std::extfmt::ct::ty_bits;
|
||||
import std::extfmt::ct::ty_hex;
|
||||
import std::extfmt::ct::ty_octal;
|
||||
import std::extfmt::ct::flag;
|
||||
import std::extfmt::ct::flag_left_justify;
|
||||
import std::extfmt::ct::flag_left_zero_pad;
|
||||
import std::extfmt::ct::flag_space_for_sign;
|
||||
import std::extfmt::ct::flag_sign_always;
|
||||
import std::extfmt::ct::flag_alternate;
|
||||
import std::extfmt::ct::count;
|
||||
import std::extfmt::ct::count_is;
|
||||
import std::extfmt::ct::count_is_param;
|
||||
import std::extfmt::ct::count_is_next_param;
|
||||
import std::extfmt::ct::count_implied;
|
||||
import std::extfmt::ct::conv;
|
||||
import std::extfmt::ct::piece;
|
||||
import std::extfmt::ct::piece_string;
|
||||
import std::extfmt::ct::piece_conv;
|
||||
import std::extfmt::ct::parse_fmt_string;
|
||||
import std::extfmt::ct::*;
|
||||
|
||||
export expand_syntax_ext;
|
||||
|
||||
fn expand_syntax_ext(parser p,
|
||||
vec[@ast::expr] args,
|
||||
fn expand_syntax_ext(&parser p, common::span sp,
|
||||
&vec[@ast::expr] args,
|
||||
option::t[str] body) -> @ast::expr {
|
||||
|
||||
if (vec::len[@ast::expr](args) == 0u) {
|
||||
|
|
|
@ -27,6 +27,11 @@ tag file_type {
|
|||
|
||||
type ty_or_bang = util::common::ty_or_bang[@ast::ty];
|
||||
|
||||
// Temporary: to introduce a tag in order to make a recursive type work
|
||||
tag xmacro {
|
||||
x(macro);
|
||||
}
|
||||
|
||||
state type parser =
|
||||
state obj {
|
||||
fn peek() -> token::token;
|
||||
|
@ -48,11 +53,15 @@ state type parser =
|
|||
fn get_reader() -> lexer::reader;
|
||||
fn get_filemap() -> codemap::filemap;
|
||||
fn get_bad_expr_words() -> std::map::hashmap[str, ()];
|
||||
fn get_macros() -> std::map::hashmap[str, xmacro];
|
||||
fn get_chpos() -> uint;
|
||||
fn get_ann() -> ast::ann;
|
||||
fn next_ann_num() -> uint;
|
||||
};
|
||||
|
||||
type macro = fn(&parser, common::span, &vec[@ast::expr], option::t[str])
|
||||
-> @ast::expr;
|
||||
|
||||
fn new_parser(session::session sess,
|
||||
eval::env env,
|
||||
ast::def_id initial_def,
|
||||
|
@ -70,7 +79,8 @@ fn new_parser(session::session sess,
|
|||
lexer::reader rdr,
|
||||
vec[op_spec] precs,
|
||||
mutable uint next_ann_var,
|
||||
std::map::hashmap[str, ()] bad_words)
|
||||
std::map::hashmap[str, ()] bad_words,
|
||||
std::map::hashmap[str, xmacro] macros)
|
||||
{
|
||||
fn peek() -> token::token {
|
||||
ret tok;
|
||||
|
@ -143,6 +153,10 @@ fn new_parser(session::session sess,
|
|||
ret bad_words;
|
||||
}
|
||||
|
||||
fn get_macros() -> std::map::hashmap[str, xmacro] {
|
||||
ret macros;
|
||||
}
|
||||
|
||||
fn get_chpos() -> uint {ret rdr.get_chpos();}
|
||||
|
||||
fn get_ann() -> ast::ann {
|
||||
|
@ -169,7 +183,7 @@ fn new_parser(session::session sess,
|
|||
ret stdio_parser(sess, env, ftype, lexer::next_token(rdr),
|
||||
npos, npos, npos, initial_def._1, UNRESTRICTED,
|
||||
initial_def._0, rdr, prec_table(), next_ann,
|
||||
bad_expr_word_table());
|
||||
bad_expr_word_table(), macro_table());
|
||||
}
|
||||
|
||||
// These are the words that shouldn't be allowed as value identifiers,
|
||||
|
@ -213,6 +227,13 @@ fn bad_expr_word_table() -> std::map::hashmap[str, ()] {
|
|||
ret words;
|
||||
}
|
||||
|
||||
fn macro_table() -> std::map::hashmap[str, xmacro] {
|
||||
auto macros = new_str_hash[xmacro]();
|
||||
macros.insert("fmt", x(extfmt::expand_syntax_ext));
|
||||
macros.insert("env", x(extenv::expand_syntax_ext));
|
||||
ret macros;
|
||||
}
|
||||
|
||||
fn unexpected(&parser p, token::token t) -> ! {
|
||||
let str s = "unexpected token: ";
|
||||
s += token::to_str(p.get_reader(), t);
|
||||
|
@ -1037,23 +1058,15 @@ fn expand_syntax_ext(&parser p, common::span sp,
|
|||
|
||||
assert (vec::len[ast::ident](path.node.idents) > 0u);
|
||||
auto extname = path.node.idents.(0);
|
||||
if (str::eq(extname, "fmt")) {
|
||||
auto expanded = extfmt::expand_syntax_ext(p, args, body);
|
||||
auto newexpr = ast::expr_ext(path, args, body,
|
||||
expanded,
|
||||
p.get_ann());
|
||||
|
||||
ret newexpr;
|
||||
} else if (str::eq(extname, "env")) {
|
||||
auto expanded = extenv::expand_syntax_ext(p, sp, args, body);
|
||||
auto newexpr = ast::expr_ext(path, args, body,
|
||||
expanded,
|
||||
alt (p.get_macros().find(extname)) {
|
||||
case (none[xmacro]) {
|
||||
p.err("unknown macro: '" + extname + "'");
|
||||
}
|
||||
case (some[xmacro](x(?ext))) {
|
||||
ret ast::expr_ext(path, args, body, ext(p, sp, args, body),
|
||||
p.get_ann());
|
||||
|
||||
ret newexpr;
|
||||
} else {
|
||||
p.err("unknown syntax extension");
|
||||
fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
5
src/test/compile-fail/ext-nonexistent.rs
Normal file
5
src/test/compile-fail/ext-nonexistent.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
|
||||
// error-pattern:unknown macro
|
||||
fn main() {
|
||||
#iamnotanextensionthatexists("");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue