syntax: Add new values that can be used with the quasiquoter

This commit is contained in:
Erick Tryzelaar 2013-03-27 06:49:39 -07:00
parent 2888563510
commit 2f1ab3a7fa

View File

@ -42,8 +42,7 @@ pub mod rt {
pub use ast::*; pub use ast::*;
pub use parse::token::*; pub use parse::token::*;
pub use parse::new_parser_from_tts; pub use parse::new_parser_from_tts;
pub use codemap::BytePos; pub use codemap::{BytePos, span, dummy_spanned};
pub use codemap::span;
use print::pprust; use print::pprust;
use print::pprust::{item_to_str, ty_to_str}; use print::pprust::{item_to_str, ty_to_str};
@ -119,6 +118,90 @@ pub mod rt {
} }
} }
impl ToSource for ast::blk {
fn to_source(&self, cx: @ext_ctxt) -> ~str {
pprust::block_to_str(self, cx.parse_sess().interner)
}
}
impl<'self> ToSource for &'self str {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_str(@str::from_slice(*self)));
pprust::lit_to_str(@lit)
}
}
impl ToSource for int {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i));
pprust::lit_to_str(@lit)
}
}
impl ToSource for i8 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i8));
pprust::lit_to_str(@lit)
}
}
impl ToSource for i16 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i16));
pprust::lit_to_str(@lit)
}
}
impl ToSource for i32 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i32));
pprust::lit_to_str(@lit)
}
}
impl ToSource for i64 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i64));
pprust::lit_to_str(@lit)
}
}
impl ToSource for uint {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u));
pprust::lit_to_str(@lit)
}
}
impl ToSource for u8 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u8));
pprust::lit_to_str(@lit)
}
}
impl ToSource for u16 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u16));
pprust::lit_to_str(@lit)
}
}
impl ToSource for u32 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u32));
pprust::lit_to_str(@lit)
}
}
impl ToSource for u64 {
fn to_source(&self, _cx: @ext_ctxt) -> ~str {
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u64));
pprust::lit_to_str(@lit)
}
}
// Alas ... we write these out instead. All redundant. // Alas ... we write these out instead. All redundant.
impl ToTokens for ast::ident { impl ToTokens for ast::ident {
@ -163,6 +246,78 @@ pub mod rt {
} }
} }
impl ToTokens for ast::blk {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl<'self> ToTokens for &'self str {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for int {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for i8 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for i16 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for i32 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for i64 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for uint {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for u8 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for u16 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for u32 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
impl ToTokens for u64 {
fn to_tokens(&self, cx: @ext_ctxt) -> ~[token_tree] {
cx.parse_tts(self.to_source(cx))
}
}
pub trait ExtParseUtils { pub trait ExtParseUtils {
fn parse_item(&self, s: ~str) -> @ast::item; fn parse_item(&self, s: ~str) -> @ast::item;
fn parse_expr(&self, s: ~str) -> @ast::expr; fn parse_expr(&self, s: ~str) -> @ast::expr;