2014-01-04 20:16:57 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
2012-12-04 00:48:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2013-02-25 19:11:21 +00:00
|
|
|
use ast;
|
2013-08-31 16:13:04 +00:00
|
|
|
use codemap::{Pos, Span};
|
2014-10-21 06:04:16 +00:00
|
|
|
use codemap;
|
2012-12-23 22:41:37 +00:00
|
|
|
use ext::base::*;
|
|
|
|
use ext::base;
|
2013-05-17 14:19:28 +00:00
|
|
|
use ext::build::AstBuilder;
|
2014-01-10 22:02:36 +00:00
|
|
|
use parse::token;
|
2014-10-21 06:04:16 +00:00
|
|
|
use parse;
|
2012-12-23 22:41:37 +00:00
|
|
|
use print::pprust;
|
2014-10-21 06:04:16 +00:00
|
|
|
use ptr::P;
|
|
|
|
use util::small_vector::SmallVector;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2015-01-23 00:31:00 +00:00
|
|
|
use std::old_io::File;
|
2014-02-01 04:54:41 +00:00
|
|
|
use std::rc::Rc;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2013-02-26 18:15:29 +00:00
|
|
|
// These macros all relate to the file system; they either return
|
|
|
|
// the column/row/filename of the expression, or they include
|
|
|
|
// a given file into the current one.
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// line!(): expands to the current line number
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<base::MacResult+'static> {
|
2012-12-13 01:08:09 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "line!");
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2014-09-17 16:01:33 +00:00
|
|
|
let topmost = cx.original_span_in_file();
|
|
|
|
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2015-02-21 11:56:46 +00:00
|
|
|
base::MacExpr::new(cx.expr_u32(topmost, loc.line as u32))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 12:03:58 +00:00
|
|
|
/* column!(): expands to the current column number */
|
|
|
|
pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<base::MacResult+'static> {
|
2014-11-18 12:03:58 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "column!");
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2014-09-17 16:01:33 +00:00
|
|
|
let topmost = cx.original_span_in_file();
|
|
|
|
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
2015-02-21 11:56:46 +00:00
|
|
|
|
|
|
|
base::MacExpr::new(cx.expr_u32(topmost, loc.col.to_usize() as u32))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// file!(): expands to the current filename */
|
|
|
|
/// The filemap (`loc.file`) contains a bunch more information we could spit
|
|
|
|
/// out if we wanted.
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<base::MacResult+'static> {
|
2012-12-13 01:08:09 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "file!");
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2014-09-17 16:01:33 +00:00
|
|
|
let topmost = cx.original_span_in_file();
|
|
|
|
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
2015-02-20 19:08:14 +00:00
|
|
|
let filename = token::intern_and_get_ident(&loc.file.name);
|
2014-09-17 16:01:33 +00:00
|
|
|
base::MacExpr::new(cx.expr_str(topmost, filename))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<base::MacResult+'static> {
|
2014-06-21 10:39:03 +00:00
|
|
|
let s = pprust::tts_to_string(tts);
|
2014-05-07 23:33:43 +00:00
|
|
|
base::MacExpr::new(cx.expr_str(sp,
|
2015-02-18 19:48:57 +00:00
|
|
|
token::intern_and_get_ident(&s[..])))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<base::MacResult+'static> {
|
2012-12-13 01:08:09 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "module_path!");
|
2014-01-21 18:08:10 +00:00
|
|
|
let string = cx.mod_path()
|
2014-03-28 19:42:34 +00:00
|
|
|
.iter()
|
2015-02-03 22:31:06 +00:00
|
|
|
.map(|x| token::get_ident(*x).to_string())
|
2014-05-22 23:57:53 +00:00
|
|
|
.collect::<Vec<String>>()
|
2014-01-21 18:08:10 +00:00
|
|
|
.connect("::");
|
2014-05-20 00:23:26 +00:00
|
|
|
base::MacExpr::new(cx.expr_str(
|
|
|
|
sp,
|
2015-02-18 19:48:57 +00:00
|
|
|
token::intern_and_get_ident(&string[..])))
|
2012-05-18 17:02:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// include! : parse the given file as an expr
|
|
|
|
/// This is generally a bad idea because it's going to behave
|
|
|
|
/// unhygienically.
|
2014-10-21 06:04:16 +00:00
|
|
|
pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult+'cx> {
|
2014-01-17 14:53:10 +00:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
|
|
|
|
Some(f) => f,
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-11-28 03:05:12 +00:00
|
|
|
// The file will be added to the code map by the parser
|
2014-10-21 06:04:16 +00:00
|
|
|
let p =
|
2013-12-30 22:04:00 +00:00
|
|
|
parse::new_sub_parser_from_file(cx.parse_sess(),
|
|
|
|
cx.cfg(),
|
|
|
|
&res_rel_file(cx,
|
|
|
|
sp,
|
|
|
|
&Path::new(file)),
|
2014-05-16 21:23:04 +00:00
|
|
|
true,
|
|
|
|
None,
|
2013-12-30 22:04:00 +00:00
|
|
|
sp);
|
2014-10-21 06:04:16 +00:00
|
|
|
|
|
|
|
struct ExpandResult<'a> {
|
|
|
|
p: parse::parser::Parser<'a>,
|
|
|
|
}
|
|
|
|
impl<'a> base::MacResult for ExpandResult<'a> {
|
|
|
|
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
|
|
|
|
Some(self.p.parse_expr())
|
|
|
|
}
|
|
|
|
fn make_items(mut self: Box<ExpandResult<'a>>)
|
|
|
|
-> Option<SmallVector<P<ast::Item>>> {
|
|
|
|
let mut ret = SmallVector::zero();
|
2015-02-07 16:23:33 +00:00
|
|
|
while self.p.token != token::Eof {
|
2014-10-21 06:04:16 +00:00
|
|
|
match self.p.parse_item_with_outer_attributes() {
|
|
|
|
Some(item) => ret.push(item),
|
2015-02-07 16:23:33 +00:00
|
|
|
None => self.p.span_fatal(
|
|
|
|
self.p.span,
|
|
|
|
&format!("expected item, found `{}`",
|
2015-02-18 23:58:07 +00:00
|
|
|
self.p.this_token_to_string())
|
2015-02-07 16:23:33 +00:00
|
|
|
)
|
2014-10-21 06:04:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box ExpandResult { p: p }
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
2012-05-18 17:00:49 +00:00
|
|
|
|
2013-02-26 18:15:29 +00:00
|
|
|
// include_str! : read the given file, insert it as a literal string expr
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<base::MacResult+'static> {
|
2014-01-17 14:53:10 +00:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
|
|
|
|
Some(f) => f,
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp)
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-12-04 03:15:12 +00:00
|
|
|
let file = res_rel_file(cx, sp, &Path::new(file));
|
2014-01-30 01:39:21 +00:00
|
|
|
let bytes = match File::open(&file).read_to_end() {
|
2013-10-26 00:04:37 +00:00
|
|
|
Err(e) => {
|
2014-05-16 17:45:16 +00:00
|
|
|
cx.span_err(sp,
|
2015-01-25 06:43:11 +00:00
|
|
|
&format!("couldn't read {}: {}",
|
2014-05-16 17:45:16 +00:00
|
|
|
file.display(),
|
2015-02-18 23:58:07 +00:00
|
|
|
e));
|
2014-04-15 12:00:14 +00:00
|
|
|
return DummyResult::expr(sp);
|
2013-10-14 01:48:47 +00:00
|
|
|
}
|
2013-10-26 00:04:37 +00:00
|
|
|
Ok(bytes) => bytes,
|
|
|
|
};
|
2014-06-30 14:41:30 +00:00
|
|
|
match String::from_utf8(bytes) {
|
|
|
|
Ok(src) => {
|
2013-11-28 03:05:12 +00:00
|
|
|
// Add this input file to the code map to make it available as
|
|
|
|
// dependency information
|
2015-01-25 06:43:11 +00:00
|
|
|
let filename = format!("{}", file.display());
|
2015-02-18 19:48:57 +00:00
|
|
|
let interned = token::intern_and_get_ident(&src[..]);
|
2014-06-30 14:41:30 +00:00
|
|
|
cx.codemap().new_filemap(filename, src);
|
2014-01-08 08:59:11 +00:00
|
|
|
|
2014-04-15 12:00:14 +00:00
|
|
|
base::MacExpr::new(cx.expr_str(sp, interned))
|
2013-11-28 03:05:12 +00:00
|
|
|
}
|
2014-06-30 14:41:30 +00:00
|
|
|
Err(_) => {
|
2014-05-16 17:45:16 +00:00
|
|
|
cx.span_err(sp,
|
2015-01-25 06:43:11 +00:00
|
|
|
&format!("{} wasn't a utf-8 file",
|
2015-02-18 23:58:07 +00:00
|
|
|
file.display()));
|
2014-04-15 12:00:14 +00:00
|
|
|
return DummyResult::expr(sp);
|
2013-10-14 01:48:47 +00:00
|
|
|
}
|
2012-05-18 17:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 21:57:09 +00:00
|
|
|
pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult+'static> {
|
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") {
|
2014-01-17 14:53:10 +00:00
|
|
|
Some(f) => f,
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return DummyResult::expr(sp)
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-12-04 03:15:12 +00:00
|
|
|
let file = res_rel_file(cx, sp, &Path::new(file));
|
2014-01-30 01:39:21 +00:00
|
|
|
match File::open(&file).read_to_end() {
|
2013-10-26 00:04:37 +00:00
|
|
|
Err(e) => {
|
2014-05-16 17:45:16 +00:00
|
|
|
cx.span_err(sp,
|
2015-02-18 23:58:07 +00:00
|
|
|
&format!("couldn't read {}: {}", file.display(), e));
|
2014-04-15 12:00:14 +00:00
|
|
|
return DummyResult::expr(sp);
|
2013-10-14 15:24:17 +00:00
|
|
|
}
|
2013-10-26 00:04:37 +00:00
|
|
|
Ok(bytes) => {
|
2015-02-13 07:33:44 +00:00
|
|
|
let bytes = bytes.iter().cloned().collect();
|
2014-04-15 12:00:14 +00:00
|
|
|
base::MacExpr::new(cx.expr_lit(sp, ast::LitBinary(Rc::new(bytes))))
|
2013-10-14 15:24:17 +00:00
|
|
|
}
|
2012-05-18 17:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 18:15:29 +00:00
|
|
|
// resolve a file-system path to an absolute file-system path (if it
|
|
|
|
// isn't already)
|
2013-12-29 05:06:22 +00:00
|
|
|
fn res_rel_file(cx: &mut ExtCtxt, sp: codemap::Span, arg: &Path) -> Path {
|
2012-05-18 17:02:21 +00:00
|
|
|
// NB: relative paths are resolved relative to the compilation unit
|
2013-09-27 00:21:59 +00:00
|
|
|
if !arg.is_absolute() {
|
2013-12-04 03:15:12 +00:00
|
|
|
let mut cu = Path::new(cx.codemap().span_to_filename(sp));
|
2013-09-27 00:21:59 +00:00
|
|
|
cu.pop();
|
2013-10-08 02:16:58 +00:00
|
|
|
cu.push(arg);
|
2013-09-27 00:21:59 +00:00
|
|
|
cu
|
2012-05-18 17:02:21 +00:00
|
|
|
} else {
|
2013-09-27 00:21:59 +00:00
|
|
|
arg.clone()
|
2012-05-18 17:02:21 +00:00
|
|
|
}
|
2012-05-18 17:00:49 +00:00
|
|
|
}
|