2019-07-17 22:49:10 +00:00
|
|
|
use syntax::{ast, panictry};
|
|
|
|
use syntax::ext::base::{self, *};
|
|
|
|
use syntax::parse::{self, token, DirectoryOwnership};
|
|
|
|
use syntax::print::pprust;
|
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax::symbol::Symbol;
|
|
|
|
use syntax::tokenstream;
|
2019-02-06 17:33:01 +00:00
|
|
|
|
2018-08-30 09:42:16 +00:00
|
|
|
use smallvec::SmallVec;
|
2019-05-14 19:50:39 +00:00
|
|
|
use syntax_pos::{self, Pos, Span};
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2018-11-16 21:22:06 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::io::ErrorKind;
|
2018-02-27 16:11:14 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
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
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_line(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn 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
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 10:14:09 +00:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2013-02-11 16:13:18 +00:00
|
|
|
|
2015-02-27 19:14:42 +00:00
|
|
|
base::MacEager::expr(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 */
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn 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
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 10:14:09 +00:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2015-02-21 11:56:46 +00:00
|
|
|
|
2017-12-24 01:20:06 +00:00
|
|
|
base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 20:12:30 +00:00
|
|
|
/// file!(): expands to the current filename */
|
2018-08-18 10:13:56 +00:00
|
|
|
/// The source_file (`loc.file`) contains a bunch more information we could spit
|
2014-06-09 20:12:30 +00:00
|
|
|
/// out if we wanted.
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_file(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn 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
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 10:14:09 +00:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2017-12-14 07:09:19 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string())))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_stringify(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2014-06-21 10:39:03 +00:00
|
|
|
let s = pprust::tts_to_string(tts);
|
2016-11-16 10:52:37 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
|
2012-05-15 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_mod(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2012-12-13 01:08:09 +00:00
|
|
|
base::check_zero_tts(cx, sp, tts, "module_path!");
|
2016-09-07 23:21:59 +00:00
|
|
|
let mod_path = &cx.current_expansion.module.mod_path;
|
|
|
|
let string = mod_path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("::");
|
2016-09-01 06:44:54 +00:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&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.
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn 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,
|
2018-12-29 21:56:55 +00:00
|
|
|
None => return DummyResult::any(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
|
2019-06-22 19:51:51 +00:00
|
|
|
let file = cx.resolve_path(file, sp);
|
2017-11-28 02:14:24 +00:00
|
|
|
let directory_ownership = DirectoryOwnership::Owned { relative: None };
|
2019-05-14 19:50:39 +00:00
|
|
|
let p = parse::new_sub_parser_from_file(cx.parse_sess(), &file, directory_ownership, None, 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>> {
|
2015-11-11 00:08:26 +00:00
|
|
|
Some(panictry!(self.p.parse_expr()))
|
2014-10-21 06:04:16 +00:00
|
|
|
}
|
2018-08-30 09:42:16 +00:00
|
|
|
|
|
|
|
fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
|
|
|
|
let mut ret = SmallVec::new();
|
2015-02-07 16:23:33 +00:00
|
|
|
while self.p.token != token::Eof {
|
2015-11-11 00:08:26 +00:00
|
|
|
match panictry!(self.p.parse_item()) {
|
2014-10-21 06:04:16 +00:00
|
|
|
Some(item) => ret.push(item),
|
2019-07-17 22:49:10 +00:00
|
|
|
None => self.p.sess.span_diagnostic.span_fatal(self.p.token.span,
|
2015-12-20 21:00:43 +00:00
|
|
|
&format!("expected item, found `{}`",
|
2018-01-21 11:47:58 +00:00
|
|
|
self.p.this_token_to_string()))
|
|
|
|
.raise()
|
2014-10-21 06:04:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 20:05:44 +00:00
|
|
|
Box::new(ExpandResult { 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
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn 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
|
|
|
};
|
2019-06-22 19:51:51 +00:00
|
|
|
let file = cx.resolve_path(file, sp);
|
2018-11-16 21:22:06 +00:00
|
|
|
match fs::read_to_string(&file) {
|
2014-06-30 14:41:30 +00:00
|
|
|
Ok(src) => {
|
2018-05-23 14:19:20 +00:00
|
|
|
let interned_src = Symbol::intern(&src);
|
|
|
|
|
2013-11-28 03:05:12 +00:00
|
|
|
// Add this input file to the code map to make it available as
|
|
|
|
// dependency information
|
2018-08-18 10:14:09 +00:00
|
|
|
cx.source_map().new_source_file(file.into(), src);
|
2014-01-08 08:59:11 +00:00
|
|
|
|
2018-05-23 14:19:20 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, interned_src))
|
2018-11-16 21:22:06 +00:00
|
|
|
},
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::InvalidData => {
|
|
|
|
cx.span_err(sp, &format!("{} wasn't a utf-8 file", file.display()));
|
|
|
|
DummyResult::expr(sp)
|
2013-11-28 03:05:12 +00:00
|
|
|
}
|
2018-11-16 21:22:06 +00:00
|
|
|
Err(e) => {
|
|
|
|
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
|
2017-05-12 18:05:39 +00:00
|
|
|
DummyResult::expr(sp)
|
2013-10-14 01:48:47 +00:00
|
|
|
}
|
2012-05-18 17:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
|
2018-07-10 19:06:26 +00:00
|
|
|
-> Box<dyn base::MacResult+'static> {
|
2014-12-21 21:57:09 +00:00
|
|
|
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
|
|
|
};
|
2019-06-22 19:51:51 +00:00
|
|
|
let file = cx.resolve_path(file, sp);
|
2018-11-16 21:22:06 +00:00
|
|
|
match fs::read(&file) {
|
|
|
|
Ok(bytes) => {
|
|
|
|
// Add the contents to the source map if it contains UTF-8.
|
|
|
|
let (contents, bytes) = match String::from_utf8(bytes) {
|
|
|
|
Ok(s) => {
|
|
|
|
let bytes = s.as_bytes().to_owned();
|
|
|
|
(s, bytes)
|
|
|
|
},
|
|
|
|
Err(e) => (String::new(), e.into_bytes()),
|
2018-10-30 14:10:42 +00:00
|
|
|
};
|
2018-11-16 21:22:06 +00:00
|
|
|
cx.source_map().new_source_file(file.into(), contents);
|
2015-04-14 17:53:23 +00:00
|
|
|
|
2018-02-27 16:11:14 +00:00
|
|
|
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes))))
|
2018-11-16 21:22:06 +00:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
|
|
|
|
DummyResult::expr(sp)
|
2013-10-14 15:24:17 +00:00
|
|
|
}
|
2012-05-18 17:03:27 +00:00
|
|
|
}
|
|
|
|
}
|