Implement basic quasi-quoter. No anti-quotes yet.

This commit is contained in:
Kevin Atkinson 2012-01-26 17:03:20 -07:00
parent 98450d0dad
commit 485e489ba2
3 changed files with 40 additions and 7 deletions

View file

@ -74,6 +74,7 @@ mod syntax {
mod ext { mod ext {
mod base; mod base;
mod expand; mod expand;
mod qquote;
mod build; mod build;
mod fmt; mod fmt;

View file

@ -8,6 +8,7 @@ import vec;
import syntax::ast::{crate, expr_, expr_mac, mac_invoc, mac_qq}; import syntax::ast::{crate, expr_, expr_mac, mac_invoc, mac_qq};
import syntax::fold::*; import syntax::fold::*;
import syntax::ext::base::*; import syntax::ext::base::*;
import syntax::ext::qquote::expand_qquote;
import syntax::parse::parser::parse_expr_from_source_str; import syntax::parse::parser::parse_expr_from_source_str;
import codemap::span; import codemap::span;
@ -53,13 +54,6 @@ fn expand_expr(exts: hashmap<str, syntax_extension>, cx: ext_ctxt,
}; };
} }
fn expand_qquote(cx: ext_ctxt, sp: span, e: @ast::expr) -> ast::expr_ {
import syntax::ext::build::*;
let str = codemap::span_to_snippet(sp, cx.session().parse_sess.cm);
let expr = mk_str(cx, e.span, str);
ret expr.node;
}
// FIXME: this is a terrible kludge to inject some macros into the default // FIXME: this is a terrible kludge to inject some macros into the default
// compilation environment. When the macro-definition system is substantially // compilation environment. When the macro-definition system is substantially
// more mature, these should move from here, into a compiled part of libcore // more mature, these should move from here, into a compiled part of libcore

View file

@ -0,0 +1,38 @@
import driver::session;
import option::{none, some};
import syntax::ast::{crate, expr_, expr_mac, mac_invoc, mac_qq};
import syntax::fold::*;
import syntax::ext::base::*;
import syntax::ext::build::*;
import syntax::parse::parser::parse_expr_from_source_str;
import codemap::span;
fn expand_qquote(cx: ext_ctxt, sp: span, _e: @ast::expr) -> ast::expr_ {
let str = codemap::span_to_snippet(sp, cx.session().parse_sess.cm);
let session_call = bind mk_call_(cx,sp,
mk_access(cx,sp,["ext_cx"], "session"),
[]);
let call = mk_call(cx,sp,
["syntax", "parse", "parser",
"parse_expr_from_source_str"],
[mk_str(cx,sp, "<anon>"),
mk_unary(cx,sp, ast::box(ast::imm),
mk_str(cx,sp, str)),
mk_access_(cx,sp,
mk_access_(cx,sp, session_call(), "opts"),
"cfg"),
mk_access_(cx,sp, session_call(), "parse_sess")]
);
ret call.node;
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: