rust/src/libsyntax/ext/trace_macros.rs

45 lines
1.5 KiB
Rust
Raw Normal View History

// 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.
use ast;
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base;
use parse::lexer::{new_tt_reader, Reader};
use parse::parser::Parser;
use parse::token::keywords;
2012-08-15 12:45:10 -05:00
2013-12-28 23:06:22 -06:00
pub fn expand_trace_macros(cx: &mut ExtCtxt,
sp: Span,
tt: &[ast::TokenTree])
-> base::MacResult {
2012-08-15 12:45:10 -05:00
let sess = cx.parse_sess();
let cfg = cx.cfg();
2014-03-16 13:56:24 -05:00
let tt_rdr = new_tt_reader(&cx.parse_sess().span_diagnostic,
None,
tt.iter().map(|x| (*x).clone()).collect());
2014-02-06 16:38:33 -06:00
let mut rust_parser = Parser(sess, cfg.clone(), tt_rdr.dup());
2012-08-15 12:45:10 -05:00
if rust_parser.is_keyword(keywords::True) {
2012-11-26 21:12:31 -06:00
cx.set_trace_macros(true);
} else if rust_parser.is_keyword(keywords::False) {
2012-11-26 21:12:31 -06:00
cx.set_trace_macros(false);
} else {
cx.span_err(sp, "trace_macros! only accepts `true` or `false`");
return base::MacResult::dummy_expr(sp);
2012-08-15 12:45:10 -05:00
}
2012-11-26 21:12:31 -06:00
rust_parser.bump();
2014-02-06 16:38:33 -06:00
let mut rust_parser = Parser(sess, cfg, tt_rdr.dup());
2012-08-15 12:45:10 -05:00
let result = rust_parser.parse_expr();
2013-01-22 18:45:27 -06:00
base::MRExpr(result)
2012-08-15 12:45:10 -05:00
}