remove the old auto_serialize syntax extension

This commit is contained in:
Erick Tryzelaar 2012-10-07 14:56:18 -07:00
parent e1c517ca48
commit d301dd3686
5 changed files with 146 additions and 316 deletions

View File

@ -122,34 +122,7 @@ fn expand(cx: ext_ctxt,
}
}
trait ext_ctxt_helpers {
fn helper_path(base_path: @ast::path, helper_name: ~str) -> @ast::path;
fn path(span: span, strs: ~[ast::ident]) -> @ast::path;
fn path_tps(span: span, strs: ~[ast::ident],
tps: ~[@ast::ty]) -> @ast::path;
fn ty_path(span: span, strs: ~[ast::ident], tps: ~[@ast::ty]) -> @ast::ty;
fn ty_fn(span: span,
-input_tys: ~[@ast::ty],
-output: @ast::ty) -> @ast::ty;
fn ty_nil(span: span) -> @ast::ty;
fn expr(span: span, node: ast::expr_) -> @ast::expr;
fn var_ref(span: span, name: ast::ident) -> @ast::expr;
fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk;
fn expr_blk(expr: @ast::expr) -> ast::blk;
fn binder_pat(span: span, nm: ast::ident) -> @ast::pat;
fn stmt(expr: @ast::expr) -> @ast::stmt;
fn alt_stmt(arms: ~[ast::arm], span: span, -v: @ast::expr) -> @ast::stmt;
fn lit_str(span: span, s: @~str) -> @ast::expr;
fn lit_uint(span: span, i: uint) -> @ast::expr;
fn lambda(blk: ast::blk) -> @ast::expr;
fn clone_folder() -> fold::ast_fold;
fn clone(v: @ast::expr) -> @ast::expr;
fn clone_ty(v: @ast::ty) -> @ast::ty;
fn clone_ty_param(v: ast::ty_param) -> ast::ty_param;
fn at(span: span, expr: @ast::expr) -> @ast::expr;
}
impl ext_ctxt: ext_ctxt_helpers {
priv impl ext_ctxt {
fn helper_path(base_path: @ast::path,
helper_name: ~str) -> @ast::path {
let head = vec::init(base_path.idents);
@ -162,22 +135,6 @@ impl ext_ctxt: ext_ctxt_helpers {
tail)))]))
}
fn path(span: span, strs: ~[ast::ident]) -> @ast::path {
@{span: span, global: false, idents: strs, rp: None, types: ~[]}
}
fn path_tps(span: span, strs: ~[ast::ident],
tps: ~[@ast::ty]) -> @ast::path {
@{span: span, global: false, idents: strs, rp: None, types: tps}
}
fn ty_path(span: span, strs: ~[ast::ident],
tps: ~[@ast::ty]) -> @ast::ty {
@{id: self.next_id(),
node: ast::ty_path(self.path_tps(span, strs, tps), self.next_id()),
span: span}
}
fn ty_fn(span: span,
-input_tys: ~[@ast::ty],
-output: @ast::ty) -> @ast::ty {
@ -202,48 +159,10 @@ impl ext_ctxt: ext_ctxt_helpers {
@{id: self.next_id(), node: ast::ty_nil, span: span}
}
fn expr(span: span, node: ast::expr_) -> @ast::expr {
@{id: self.next_id(), callee_id: self.next_id(),
node: node, span: span}
}
fn var_ref(span: span, name: ast::ident) -> @ast::expr {
self.expr(span, ast::expr_path(self.path(span, ~[name])))
}
fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk {
{node: {view_items: ~[],
stmts: stmts,
expr: None,
id: self.next_id(),
rules: ast::default_blk},
span: span}
}
fn expr_blk(expr: @ast::expr) -> ast::blk {
{node: {view_items: ~[],
stmts: ~[],
expr: Some(expr),
id: self.next_id(),
rules: ast::default_blk},
span: expr.span}
}
fn binder_pat(span: span, nm: ast::ident) -> @ast::pat {
let path = @{span: span, global: false, idents: ~[nm],
rp: None, types: ~[]};
@{id: self.next_id(),
node: ast::pat_ident(ast::bind_by_implicit_ref,
path,
None),
span: span}
}
fn stmt(expr: @ast::expr) -> @ast::stmt {
@{node: ast::stmt_semi(expr, self.next_id()),
span: expr.span}
}
fn alt_stmt(arms: ~[ast::arm],
span: span, -v: @ast::expr) -> @ast::stmt {
self.stmt(
@ -252,32 +171,6 @@ impl ext_ctxt: ext_ctxt_helpers {
ast::expr_match(v, arms)))
}
fn lit_str(span: span, s: @~str) -> @ast::expr {
self.expr(
span,
ast::expr_vstore(
self.expr(
span,
ast::expr_lit(
@{node: ast::lit_str(s),
span: span})),
ast::expr_vstore_uniq))
}
fn lit_uint(span: span, i: uint) -> @ast::expr {
self.expr(
span,
ast::expr_lit(
@{node: ast::lit_uint(i as u64, ast::ty_u),
span: span}))
}
fn lambda(blk: ast::blk) -> @ast::expr {
let ext_cx = self;
let blk_e = self.expr(blk.span, ast::expr_block(blk));
#ast{ || $(blk_e) }
}
fn clone_folder() -> fold::ast_fold {
fold::make_fold(@{
new_id: |_id| self.next_id(),

View File

@ -215,6 +215,86 @@ fn expand_auto_deserialize(
}
priv impl ext_ctxt {
fn expr(span: span, node: ast::expr_) -> @ast::expr {
@{id: self.next_id(), callee_id: self.next_id(),
node: node, span: span}
}
fn path(span: span, strs: ~[ast::ident]) -> @ast::path {
@{span: span, global: false, idents: strs, rp: None, types: ~[]}
}
fn path_tps(span: span, strs: ~[ast::ident],
tps: ~[@ast::ty]) -> @ast::path {
@{span: span, global: false, idents: strs, rp: None, types: tps}
}
fn ty_path(span: span, strs: ~[ast::ident],
tps: ~[@ast::ty]) -> @ast::ty {
@{id: self.next_id(),
node: ast::ty_path(self.path_tps(span, strs, tps), self.next_id()),
span: span}
}
fn binder_pat(span: span, nm: ast::ident) -> @ast::pat {
let path = @{span: span, global: false, idents: ~[nm],
rp: None, types: ~[]};
@{id: self.next_id(),
node: ast::pat_ident(ast::bind_by_implicit_ref,
path,
None),
span: span}
}
fn stmt(expr: @ast::expr) -> @ast::stmt {
@{node: ast::stmt_semi(expr, self.next_id()),
span: expr.span}
}
fn lit_str(span: span, s: @~str) -> @ast::expr {
self.expr(
span,
ast::expr_vstore(
self.expr(
span,
ast::expr_lit(
@{node: ast::lit_str(s),
span: span})),
ast::expr_vstore_uniq))
}
fn lit_uint(span: span, i: uint) -> @ast::expr {
self.expr(
span,
ast::expr_lit(
@{node: ast::lit_uint(i as u64, ast::ty_u),
span: span}))
}
fn lambda(blk: ast::blk) -> @ast::expr {
let ext_cx = self;
let blk_e = self.expr(blk.span, ast::expr_block(blk));
#ast{ || $(blk_e) }
}
fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk {
{node: {view_items: ~[],
stmts: stmts,
expr: None,
id: self.next_id(),
rules: ast::default_blk},
span: span}
}
fn expr_blk(expr: @ast::expr) -> ast::blk {
{node: {view_items: ~[],
stmts: ~[],
expr: Some(expr),
id: self.next_id(),
rules: ast::default_blk},
span: expr.span}
}
fn expr_path(span: span, strs: ~[ast::ident]) -> @ast::expr {
self.expr(span, ast::expr_path(self.path(span, strs)))
}

View File

@ -64,6 +64,7 @@ enum syntax_extension {
// A temporary hard-coded map of methods for expanding syntax extension
// AST nodes into full ASTs
#[cfg(stage0)]
fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
fn builtin(f: syntax_expander_) -> syntax_extension
{normal({expander: f, span: None})}
@ -122,6 +123,69 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
return syntax_expanders;
}
#[cfg(stage1)]
#[cfg(stage2)]
fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
fn builtin(f: syntax_expander_) -> syntax_extension
{normal({expander: f, span: None})}
fn builtin_expr_tt(f: syntax_expander_tt_) -> syntax_extension {
expr_tt({expander: f, span: None})
}
fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension {
item_tt({expander: f, span: None})
}
let syntax_expanders = HashMap();
syntax_expanders.insert(~"macro",
macro_defining(ext::simplext::add_new_extension));
syntax_expanders.insert(~"macro_rules",
builtin_item_tt(
ext::tt::macro_rules::add_new_extension));
syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext));
syntax_expanders.insert(
~"auto_serialize",
item_decorator(ext::auto_serialize2::expand_auto_serialize));
syntax_expanders.insert(
~"auto_deserialize",
item_decorator(ext::auto_serialize2::expand_auto_deserialize));
syntax_expanders.insert(
~"auto_serialize2",
item_decorator(ext::auto_serialize2::expand_auto_serialize));
syntax_expanders.insert(
~"auto_deserialize2",
item_decorator(ext::auto_serialize2::expand_auto_deserialize));
syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext));
syntax_expanders.insert(~"concat_idents",
builtin(ext::concat_idents::expand_syntax_ext));
syntax_expanders.insert(~"ident_to_str",
builtin(ext::ident_to_str::expand_syntax_ext));
syntax_expanders.insert(~"log_syntax",
builtin_expr_tt(
ext::log_syntax::expand_syntax_ext));
syntax_expanders.insert(~"ast",
builtin(ext::qquote::expand_ast));
syntax_expanders.insert(~"line",
builtin(ext::source_util::expand_line));
syntax_expanders.insert(~"col",
builtin(ext::source_util::expand_col));
syntax_expanders.insert(~"file",
builtin(ext::source_util::expand_file));
syntax_expanders.insert(~"stringify",
builtin(ext::source_util::expand_stringify));
syntax_expanders.insert(~"include",
builtin(ext::source_util::expand_include));
syntax_expanders.insert(~"include_str",
builtin(ext::source_util::expand_include_str));
syntax_expanders.insert(~"include_bin",
builtin(ext::source_util::expand_include_bin));
syntax_expanders.insert(~"module_path",
builtin(ext::source_util::expand_mod));
syntax_expanders.insert(~"proto",
builtin_item_tt(ext::pipes::expand_proto));
syntax_expanders.insert(
~"trace_macros",
builtin_expr_tt(ext::trace_macros::expand_trace_macros));
return syntax_expanders;
}
// One of these is made during expansion and incrementally updated as we go;
// when a macro expansion occurs, the resulting nodes have the backtrace()

View File

@ -128,6 +128,7 @@ mod ext {
mod ident_to_str;
#[legacy_exports]
mod log_syntax;
#[cfg(stage0)]
#[legacy_exports]
mod auto_serialize;
#[legacy_exports]

View File

@ -1,208 +0,0 @@
// xfail-fast
#[legacy_modes];
extern mod std;
// These tests used to be separate files, but I wanted to refactor all
// the common code.
use cmp::Eq;
use std::ebml;
use io::Writer;
use std::serialization::{serialize_uint, deserialize_uint};
fn test_ser_and_deser<A:Eq>(a1: A,
expected: ~str,
ebml_ser_fn: fn(ebml::Writer, A),
ebml_deser_fn: fn(ebml::EbmlDeserializer) -> A,
io_ser_fn: fn(io::Writer, A)) {
// check the pretty printer:
io_ser_fn(io::stdout(), a1);
let s = io::with_str_writer(|w| io_ser_fn(w, a1) );
debug!("s == %?", s);
assert s == expected;
// check the EBML serializer:
let bytes = do io::with_bytes_writer |wr| {
let w = ebml::Writer(wr);
ebml_ser_fn(w, a1);
};
let d = ebml::Doc(@bytes);
let a2 = ebml_deser_fn(ebml::ebml_deserializer(d));
io::print(~"\na1 = ");
io_ser_fn(io::stdout(), a1);
io::print(~"\na2 = ");
io_ser_fn(io::stdout(), a2);
io::print(~"\n");
assert a1 == a2;
}
#[auto_serialize]
enum expr {
val(uint),
plus(@expr, @expr),
minus(@expr, @expr)
}
impl an_enum : cmp::Eq {
pure fn eq(other: &an_enum) -> bool {
self.v == (*other).v
}
pure fn ne(other: &an_enum) -> bool { !self.eq(other) }
}
impl point : cmp::Eq {
pure fn eq(other: &point) -> bool {
self.x == (*other).x && self.y == (*other).y
}
pure fn ne(other: &point) -> bool { !self.eq(other) }
}
impl<T:cmp::Eq> quark<T> : cmp::Eq {
pure fn eq(other: &quark<T>) -> bool {
match self {
top(ref q) => match (*other) {
top(ref r) => q == r,
bottom(_) => false
},
bottom(ref q) => match (*other) {
top(_) => false,
bottom(ref r) => q == r
}
}
}
pure fn ne(other: &quark<T>) -> bool { !self.eq(other) }
}
impl c_like : cmp::Eq {
pure fn eq(other: &c_like) -> bool {
self as int == (*other) as int
}
pure fn ne(other: &c_like) -> bool { !self.eq(other) }
}
impl expr : cmp::Eq {
pure fn eq(other: &expr) -> bool {
match self {
val(e0a) => {
match (*other) {
val(e0b) => e0a == e0b,
_ => false
}
}
plus(e0a, e1a) => {
match (*other) {
plus(e0b, e1b) => e0a == e0b && e1a == e1b,
_ => false
}
}
minus(e0a, e1a) => {
match (*other) {
minus(e0b, e1b) => e0a == e0b && e1a == e1b,
_ => false
}
}
}
}
pure fn ne(other: &expr) -> bool { !self.eq(other) }
}
#[auto_serialize]
type spanned<T> = {lo: uint, hi: uint, node: T};
impl<T:cmp::Eq> spanned<T> : cmp::Eq {
pure fn eq(other: &spanned<T>) -> bool {
self.lo == (*other).lo && self.hi == (*other).hi &&
self.node.eq(&(*other).node)
}
pure fn ne(other: &spanned<T>) -> bool { !self.eq(other) }
}
#[auto_serialize]
type spanned_uint = spanned<uint>;
#[auto_serialize]
type some_rec = {v: uint_vec};
#[auto_serialize]
enum an_enum = some_rec;
#[auto_serialize]
type uint_vec = ~[uint];
#[auto_serialize]
type point = {x: uint, y: uint};
#[auto_serialize]
enum quark<T> {
top(T),
bottom(T)
}
#[auto_serialize]
type uint_quark = quark<uint>;
#[auto_serialize]
enum c_like { a, b, c }
fn main() {
test_ser_and_deser(plus(@minus(@val(3u), @val(10u)),
@plus(@val(22u), @val(5u))),
~"plus(@minus(@val(3u), @val(10u)), \
@plus(@val(22u), @val(5u)))",
serialize_expr,
deserialize_expr,
serialize_expr);
test_ser_and_deser({lo: 0u, hi: 5u, node: 22u},
~"{lo: 0u, hi: 5u, node: 22u}",
serialize_spanned_uint,
deserialize_spanned_uint,
serialize_spanned_uint);
test_ser_and_deser(an_enum({v: ~[1u, 2u, 3u]}),
~"an_enum({v: [1u, 2u, 3u]})",
serialize_an_enum,
deserialize_an_enum,
serialize_an_enum);
test_ser_and_deser({x: 3u, y: 5u},
~"{x: 3u, y: 5u}",
serialize_point,
deserialize_point,
serialize_point);
test_ser_and_deser(~[1u, 2u, 3u],
~"[1u, 2u, 3u]",
serialize_uint_vec,
deserialize_uint_vec,
serialize_uint_vec);
test_ser_and_deser(top(22u),
~"top(22u)",
serialize_uint_quark,
deserialize_uint_quark,
serialize_uint_quark);
test_ser_and_deser(bottom(222u),
~"bottom(222u)",
serialize_uint_quark,
deserialize_uint_quark,
serialize_uint_quark);
test_ser_and_deser(a,
~"a",
serialize_c_like,
deserialize_c_like,
serialize_c_like);
test_ser_and_deser(b,
~"b",
serialize_c_like,
deserialize_c_like,
serialize_c_like);
}