]>
git.proxmox.com Git - rustc.git/blob - src/libsyntax_ext/proc_macro_impl.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use errors
::FatalError
;
15 use syntax
::codemap
::Span
;
16 use syntax
::ext
::base
::*;
17 use syntax
::tokenstream
::TokenStream
;
18 use syntax
::ext
::base
;
20 use proc_macro
::TokenStream
as TsShim
;
21 use proc_macro
::__internal
;
23 pub struct AttrProcMacro
{
24 pub inner
: fn(TsShim
, TsShim
) -> TsShim
,
27 impl base
::AttrProcMacro
for AttrProcMacro
{
29 ecx
: &'cx
mut ExtCtxt
,
31 annotation
: TokenStream
,
32 annotated
: TokenStream
)
34 let annotation
= __internal
::token_stream_wrap(annotation
);
35 let annotated
= __internal
::token_stream_wrap(annotated
);
37 let res
= __internal
::set_parse_sess(&ecx
.parse_sess
, || {
38 panic
::catch_unwind(panic
::AssertUnwindSafe(|| (self.inner
)(annotation
, annotated
)))
42 Ok(stream
) => __internal
::token_stream_inner(stream
),
44 let msg
= "custom attribute panicked";
45 let mut err
= ecx
.struct_span_fatal(span
, msg
);
46 if let Some(s
) = e
.downcast_ref
::<String
>() {
47 err
.help(&format
!("message: {}", s
));
49 if let Some(s
) = e
.downcast_ref
::<&'
static str>() {
50 err
.help(&format
!("message: {}", s
));
60 pub struct BangProcMacro
{
61 pub inner
: fn(TsShim
) -> TsShim
,
64 impl base
::ProcMacro
for BangProcMacro
{
66 ecx
: &'cx
mut ExtCtxt
,
70 let input
= __internal
::token_stream_wrap(input
);
72 let res
= __internal
::set_parse_sess(&ecx
.parse_sess
, || {
73 panic
::catch_unwind(panic
::AssertUnwindSafe(|| (self.inner
)(input
)))
77 Ok(stream
) => __internal
::token_stream_inner(stream
),
79 let msg
= "proc macro panicked";
80 let mut err
= ecx
.struct_span_fatal(span
, msg
);
81 if let Some(s
) = e
.downcast_ref
::<String
>() {
82 err
.help(&format
!("message: {}", s
));
84 if let Some(s
) = e
.downcast_ref
::<&'
static str>() {
85 err
.help(&format
!("message: {}", s
));