2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-11-08 19:00:55 -06:00
|
|
|
use mod ast;
|
|
|
|
use mod parse::token;
|
|
|
|
|
2012-11-16 20:54:48 -06:00
|
|
|
use codemap::{span, BytePos};
|
2012-11-08 19:00:55 -06:00
|
|
|
use ext::base::ext_ctxt;
|
|
|
|
use token::*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Quasiquoting works via token trees.
|
|
|
|
*
|
|
|
|
* This is registered as a expression syntax extension called quote! that lifts
|
|
|
|
* its argument token-tree to an AST representing the construction of the same
|
|
|
|
* token tree, with ast::tt_nonterminal nodes interpreted as antiquotes
|
|
|
|
* (splices).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-11-16 16:50:35 -06:00
|
|
|
pub mod rt {
|
|
|
|
pub use ast::*;
|
|
|
|
pub use parse::token::*;
|
2012-11-20 18:07:57 -06:00
|
|
|
pub use parse::new_parser_from_tts;
|
2012-11-16 20:54:48 -06:00
|
|
|
pub use codemap::BytePos;
|
|
|
|
pub use codemap::span;
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2012-11-16 16:50:35 -06:00
|
|
|
pub fn expand_quote_tokens(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
tts: ~[ast::token_tree]) -> base::mac_result {
|
2012-11-20 18:07:57 -06:00
|
|
|
base::mr_expr(expand_tts(cx, sp, tts))
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2012-11-16 16:50:35 -06:00
|
|
|
pub fn expand_quote_expr(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
tts: ~[ast::token_tree]) -> base::mac_result {
|
|
|
|
base::mr_expr(expand_parse_call(cx, sp, ~"parse_expr", ~[], tts))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand_quote_item(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
tts: ~[ast::token_tree]) -> base::mac_result {
|
|
|
|
let e_attrs = build::mk_uniq_vec_e(cx, sp, ~[]);
|
|
|
|
base::mr_expr(expand_parse_call(cx, sp, ~"parse_item",
|
|
|
|
~[e_attrs], tts))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand_quote_pat(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
tts: ~[ast::token_tree]) -> base::mac_result {
|
|
|
|
let e_refutable = build::mk_lit(cx, sp, ast::lit_bool(true));
|
|
|
|
base::mr_expr(expand_parse_call(cx, sp, ~"parse_pat",
|
|
|
|
~[e_refutable], tts))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand_quote_type(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
tts: ~[ast::token_tree]) -> base::mac_result {
|
|
|
|
let e_param_colons = build::mk_lit(cx, sp, ast::lit_bool(false));
|
|
|
|
base::mr_expr(expand_parse_call(cx, sp, ~"parse_type",
|
|
|
|
~[e_param_colons], tts))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand_quote_stmt(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
tts: ~[ast::token_tree]) -> base::mac_result {
|
|
|
|
let e_attrs = build::mk_uniq_vec_e(cx, sp, ~[]);
|
|
|
|
base::mr_expr(expand_parse_call(cx, sp, ~"parse_stmt",
|
|
|
|
~[e_attrs], tts))
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ids_ext(cx: ext_ctxt, strs: ~[~str]) -> ~[ast::ident] {
|
|
|
|
strs.map(|str| cx.parse_sess().interner.intern(@*str))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn id_ext(cx: ext_ctxt, str: ~str) -> ast::ident {
|
|
|
|
cx.parse_sess().interner.intern(@str)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_option_span(cx: ext_ctxt,
|
|
|
|
qsp: span,
|
|
|
|
sp: Option<span>) -> @ast::expr {
|
|
|
|
match sp {
|
|
|
|
None => build::mk_path(cx, qsp, ids_ext(cx, ~[~"None"])),
|
|
|
|
Some(sp) => {
|
|
|
|
build::mk_call(cx, qsp,
|
|
|
|
ids_ext(cx, ~[~"Some"]),
|
|
|
|
~[build::mk_managed(cx, qsp,
|
|
|
|
mk_span(cx, qsp, sp))])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_span(cx: ext_ctxt, qsp: span, sp: span) -> @ast::expr {
|
|
|
|
|
|
|
|
let e_expn_info = match sp.expn_info {
|
|
|
|
None => build::mk_path(cx, qsp, ids_ext(cx, ~[~"None"])),
|
2012-12-04 12:50:00 -06:00
|
|
|
Some(@codemap::ExpandedFrom(ref cr)) => {
|
2012-11-08 19:00:55 -06:00
|
|
|
let e_callee =
|
|
|
|
build::mk_rec_e(
|
|
|
|
cx, qsp,
|
|
|
|
~[{ident: id_ext(cx, ~"name"),
|
|
|
|
ex: build::mk_uniq_str(cx, qsp,
|
2012-12-04 12:50:00 -06:00
|
|
|
(*cr).callie.name)},
|
2012-11-08 19:00:55 -06:00
|
|
|
{ident: id_ext(cx, ~"span"),
|
2012-12-04 12:50:00 -06:00
|
|
|
ex: mk_option_span(cx, qsp, (*cr).callie.span)}]);
|
2012-11-08 19:00:55 -06:00
|
|
|
|
|
|
|
let e_expn_info_ =
|
|
|
|
build::mk_call(
|
|
|
|
cx, qsp,
|
|
|
|
ids_ext(cx, ~[~"expanded_from"]),
|
|
|
|
~[build::mk_rec_e(
|
|
|
|
cx, qsp,
|
|
|
|
~[{ident: id_ext(cx, ~"call_site"),
|
2012-12-04 12:50:00 -06:00
|
|
|
ex: mk_span(cx, qsp, (*cr).call_site)},
|
2012-11-08 19:00:55 -06:00
|
|
|
{ident: id_ext(cx, ~"callie"),
|
|
|
|
ex: e_callee}])]);
|
|
|
|
|
|
|
|
build::mk_call(cx, qsp,
|
|
|
|
ids_ext(cx, ~[~"Some"]),
|
|
|
|
~[build::mk_managed(cx, qsp, e_expn_info_)])
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-20 18:07:57 -06:00
|
|
|
let span_path = ids_ext(cx, ~[~"span"]);
|
2012-11-16 20:54:48 -06:00
|
|
|
|
|
|
|
build::mk_struct_e(cx, qsp,
|
|
|
|
span_path,
|
2012-11-08 19:00:55 -06:00
|
|
|
~[{ident: id_ext(cx, ~"lo"),
|
2012-11-16 20:54:48 -06:00
|
|
|
ex: mk_bytepos(cx, qsp, sp.lo) },
|
2012-11-08 19:00:55 -06:00
|
|
|
|
|
|
|
{ident: id_ext(cx, ~"hi"),
|
2012-11-16 20:54:48 -06:00
|
|
|
ex: mk_bytepos(cx, qsp, sp.hi) },
|
2012-11-08 19:00:55 -06:00
|
|
|
|
|
|
|
{ident: id_ext(cx, ~"expn_info"),
|
|
|
|
ex: e_expn_info}])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lift an ident to the expr that evaluates to that ident.
|
|
|
|
//
|
|
|
|
// NB: this identifies the interner used when re-parsing the token tree
|
|
|
|
// with the interner used during initial parse. This is _wrong_ and we
|
|
|
|
// should be emitting a &str here and the token type should be ok with
|
|
|
|
// &static/str or &session/str. Longer-term issue.
|
|
|
|
fn mk_ident(cx: ext_ctxt, sp: span, ident: ast::ident) -> @ast::expr {
|
|
|
|
build::mk_struct_e(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"ident"]),
|
|
|
|
~[{ident: id_ext(cx, ~"repr"),
|
|
|
|
ex: build::mk_uint(cx, sp, ident.repr) }])
|
|
|
|
}
|
|
|
|
|
2012-11-16 20:54:48 -06:00
|
|
|
fn mk_bytepos(cx: ext_ctxt, sp: span, bpos: BytePos) -> @ast::expr {
|
2012-11-20 18:07:57 -06:00
|
|
|
let path = ids_ext(cx, ~[~"BytePos"]);
|
2012-11-16 20:54:48 -06:00
|
|
|
let arg = build::mk_uint(cx, sp, bpos.to_uint());
|
|
|
|
build::mk_call(cx, sp, path, ~[arg])
|
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
|
|
|
|
fn mk_binop(cx: ext_ctxt, sp: span, bop: token::binop) -> @ast::expr {
|
|
|
|
let name = match bop {
|
|
|
|
PLUS => "PLUS",
|
|
|
|
MINUS => "MINUS",
|
|
|
|
STAR => "STAR",
|
|
|
|
SLASH => "SLASH",
|
|
|
|
PERCENT => "PERCENT",
|
|
|
|
CARET => "CARET",
|
|
|
|
AND => "AND",
|
|
|
|
OR => "OR",
|
|
|
|
SHL => "SHL",
|
|
|
|
SHR => "SHR"
|
|
|
|
};
|
|
|
|
build::mk_path(cx, sp,
|
|
|
|
ids_ext(cx, ~[name.to_owned()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_token(cx: ext_ctxt, sp: span, tok: token::Token) -> @ast::expr {
|
|
|
|
|
|
|
|
match tok {
|
|
|
|
BINOP(binop) => {
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"BINOP"]),
|
|
|
|
~[mk_binop(cx, sp, binop)]);
|
|
|
|
}
|
|
|
|
BINOPEQ(binop) => {
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"BINOPEQ"]),
|
|
|
|
~[mk_binop(cx, sp, binop)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIT_INT(i, ity) => {
|
|
|
|
let s_ity = match ity {
|
|
|
|
ast::ty_i => ~"ty_i",
|
|
|
|
ast::ty_char => ~"ty_char",
|
|
|
|
ast::ty_i8 => ~"ty_i8",
|
|
|
|
ast::ty_i16 => ~"ty_i16",
|
|
|
|
ast::ty_i32 => ~"ty_i32",
|
|
|
|
ast::ty_i64 => ~"ty_i64"
|
|
|
|
};
|
|
|
|
let e_ity =
|
|
|
|
build::mk_path(cx, sp,
|
|
|
|
ids_ext(cx, ~[s_ity]));
|
|
|
|
|
|
|
|
let e_i64 = build::mk_lit(cx, sp, ast::lit_int(i, ast::ty_i64));
|
|
|
|
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"LIT_INT"]),
|
|
|
|
~[e_i64, e_ity]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIT_UINT(u, uty) => {
|
|
|
|
let s_uty = match uty {
|
|
|
|
ast::ty_u => ~"ty_u",
|
|
|
|
ast::ty_u8 => ~"ty_u8",
|
|
|
|
ast::ty_u16 => ~"ty_u16",
|
|
|
|
ast::ty_u32 => ~"ty_u32",
|
|
|
|
ast::ty_u64 => ~"ty_u64"
|
|
|
|
};
|
|
|
|
let e_uty =
|
|
|
|
build::mk_path(cx, sp,
|
|
|
|
ids_ext(cx, ~[s_uty]));
|
|
|
|
|
|
|
|
let e_u64 = build::mk_lit(cx, sp, ast::lit_uint(u, ast::ty_u64));
|
|
|
|
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"LIT_UINT"]),
|
|
|
|
~[e_u64, e_uty]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIT_INT_UNSUFFIXED(i) => {
|
|
|
|
let e_i64 = build::mk_lit(cx, sp,
|
|
|
|
ast::lit_int(i, ast::ty_i64));
|
|
|
|
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"LIT_INT_UNSUFFIXED"]),
|
|
|
|
~[e_i64]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIT_FLOAT(fident, fty) => {
|
|
|
|
let s_fty = match fty {
|
|
|
|
ast::ty_f => ~"ty_f",
|
|
|
|
ast::ty_f32 => ~"ty_f32",
|
|
|
|
ast::ty_f64 => ~"ty_f64"
|
|
|
|
};
|
|
|
|
let e_fty =
|
|
|
|
build::mk_path(cx, sp,
|
|
|
|
ids_ext(cx, ~[s_fty]));
|
|
|
|
|
|
|
|
let e_fident = mk_ident(cx, sp, fident);
|
|
|
|
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"LIT_FLOAT"]),
|
|
|
|
~[e_fident, e_fty]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIT_STR(ident) => {
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"LIT_STR"]),
|
|
|
|
~[mk_ident(cx, sp, ident)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
IDENT(ident, b) => {
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"IDENT"]),
|
|
|
|
~[mk_ident(cx, sp, ident),
|
|
|
|
build::mk_lit(cx, sp, ast::lit_bool(b))]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DOC_COMMENT(ident) => {
|
|
|
|
return build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"DOC_COMMENT"]),
|
|
|
|
~[mk_ident(cx, sp, ident)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERPOLATED(_) => fail ~"quote! with interpolated token",
|
|
|
|
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = match tok {
|
|
|
|
EQ => "EQ",
|
|
|
|
LT => "LT",
|
|
|
|
LE => "LE",
|
|
|
|
EQEQ => "EQEQ",
|
|
|
|
NE => "NE",
|
|
|
|
GE => "GE",
|
|
|
|
GT => "GT",
|
|
|
|
ANDAND => "ANDAND",
|
|
|
|
OROR => "OROR",
|
|
|
|
NOT => "NOT",
|
|
|
|
TILDE => "TILDE",
|
|
|
|
AT => "AT",
|
|
|
|
DOT => "DOT",
|
|
|
|
DOTDOT => "DOTDOT",
|
|
|
|
ELLIPSIS => "ELLIPSIS",
|
|
|
|
COMMA => "COMMA",
|
|
|
|
SEMI => "SEMI",
|
|
|
|
COLON => "COLON",
|
|
|
|
MOD_SEP => "MOD_SEP",
|
|
|
|
RARROW => "RARROW",
|
|
|
|
LARROW => "LARROW",
|
|
|
|
DARROW => "DARROW",
|
|
|
|
FAT_ARROW => "FAT_ARROW",
|
|
|
|
LPAREN => "LPAREN",
|
|
|
|
RPAREN => "RPAREN",
|
|
|
|
LBRACKET => "LBRACKET",
|
|
|
|
RBRACKET => "RBRACKET",
|
|
|
|
LBRACE => "LBRACE",
|
|
|
|
RBRACE => "RBRACE",
|
|
|
|
POUND => "POUND",
|
|
|
|
DOLLAR => "DOLLAR",
|
|
|
|
UNDERSCORE => "UNDERSCORE",
|
|
|
|
EOF => "EOF",
|
|
|
|
_ => fail
|
|
|
|
};
|
|
|
|
build::mk_path(cx, sp,
|
|
|
|
ids_ext(cx, ~[name.to_owned()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn mk_tt(cx: ext_ctxt, sp: span, tt: &ast::token_tree) -> @ast::expr {
|
|
|
|
match *tt {
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::tt_tok(sp, ref tok) => {
|
2012-11-20 18:07:57 -06:00
|
|
|
let e_tok =
|
|
|
|
build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"tt_tok"]),
|
|
|
|
~[mk_span(cx, sp, sp),
|
2012-12-04 12:50:00 -06:00
|
|
|
mk_token(cx, sp, (*tok))]);
|
2012-11-20 18:07:57 -06:00
|
|
|
build::mk_uniq_vec_e(cx, sp, ~[e_tok])
|
|
|
|
}
|
2012-11-08 19:00:55 -06:00
|
|
|
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::tt_delim(ref tts) => {
|
2012-11-20 18:07:57 -06:00
|
|
|
let e_delim =
|
|
|
|
build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"tt_delim"]),
|
2012-12-04 12:50:00 -06:00
|
|
|
~[mk_tts(cx, sp, (*tts))]);
|
2012-11-20 18:07:57 -06:00
|
|
|
build::mk_uniq_vec_e(cx, sp, ~[e_delim])
|
2012-11-08 19:00:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ast::tt_seq(*) => fail ~"tt_seq in quote!",
|
|
|
|
|
|
|
|
ast::tt_nonterminal(sp, ident) =>
|
|
|
|
build::mk_copy(cx, sp, build::mk_path(cx, sp, ~[ident]))
|
|
|
|
}
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
|
|
|
|
2012-11-20 18:07:57 -06:00
|
|
|
fn mk_tts(cx: ext_ctxt, sp: span, tts: &[ast::token_tree]) -> @ast::expr {
|
|
|
|
let e_tts = tts.map(|tt| mk_tt(cx, sp, tt));
|
|
|
|
build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"vec", ~"concat"]),
|
|
|
|
~[build::mk_slice_vec_e(cx, sp, e_tts)])
|
|
|
|
}
|
2012-11-16 16:50:35 -06:00
|
|
|
|
2012-11-20 18:07:57 -06:00
|
|
|
fn expand_tts(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
tts: ~[ast::token_tree]) -> @ast::expr {
|
2012-11-16 16:50:35 -06:00
|
|
|
// NB: It appears that the main parser loses its mind if we consider
|
|
|
|
// $foo as a tt_nonterminal during the main parse, so we have to re-parse
|
|
|
|
// under quote_depth > 0. This is silly and should go away; the _guess_ is
|
|
|
|
// it has to do with transition away from supporting old-style macros, so
|
|
|
|
// try removing it when enough of them are gone.
|
2012-11-20 18:07:57 -06:00
|
|
|
let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts);
|
2012-11-16 16:50:35 -06:00
|
|
|
p.quote_depth += 1u;
|
2012-11-20 18:07:57 -06:00
|
|
|
let tts = p.parse_all_token_trees();
|
|
|
|
p.abort_if_errors();
|
2012-11-16 16:50:35 -06:00
|
|
|
|
|
|
|
// We want to emit a block expression that does a sequence of 'use's to
|
|
|
|
// import the runtime module, followed by a tt expression.
|
|
|
|
let uses = ~[ build::mk_glob_use(cx, sp, ids_ext(cx, ~[~"syntax",
|
|
|
|
~"ext",
|
|
|
|
~"quote",
|
|
|
|
~"rt"])) ];
|
2012-11-20 18:07:57 -06:00
|
|
|
build::mk_block(cx, sp, uses, ~[], Some(mk_tts(cx, sp, tts)))
|
2012-11-16 16:50:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_parse_call(cx: ext_ctxt,
|
|
|
|
sp: span,
|
|
|
|
parse_method: ~str,
|
|
|
|
arg_exprs: ~[@ast::expr],
|
|
|
|
tts: ~[ast::token_tree]) -> @ast::expr {
|
2012-11-20 18:07:57 -06:00
|
|
|
let tts_expr = expand_tts(cx, sp, tts);
|
2012-11-16 16:50:35 -06:00
|
|
|
|
|
|
|
let cfg_call = || build::mk_call_(
|
|
|
|
cx, sp, build::mk_access(cx, sp, ids_ext(cx, ~[~"ext_cx"]),
|
|
|
|
id_ext(cx, ~"cfg")), ~[]);
|
|
|
|
|
|
|
|
let parse_sess_call = || build::mk_call_(
|
|
|
|
cx, sp, build::mk_access(cx, sp, ids_ext(cx, ~[~"ext_cx"]),
|
|
|
|
id_ext(cx, ~"parse_sess")), ~[]);
|
|
|
|
|
|
|
|
let new_parser_call =
|
|
|
|
build::mk_call(cx, sp,
|
|
|
|
ids_ext(cx, ~[~"syntax",
|
|
|
|
~"ext",
|
|
|
|
~"quote",
|
|
|
|
~"rt",
|
2012-11-20 18:07:57 -06:00
|
|
|
~"new_parser_from_tts"]),
|
2012-11-16 16:50:35 -06:00
|
|
|
~[parse_sess_call(),
|
|
|
|
cfg_call(),
|
2012-11-20 18:07:57 -06:00
|
|
|
tts_expr]);
|
2012-11-16 16:50:35 -06:00
|
|
|
|
|
|
|
build::mk_call_(cx, sp,
|
|
|
|
build::mk_access_(cx, sp, new_parser_call,
|
|
|
|
id_ext(cx, parse_method)),
|
|
|
|
arg_exprs)
|
|
|
|
}
|
|
|
|
|