blob: 4ddc20800ef262f2a10329572a1335a8f0a1f6fc [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf4bbbd92016-09-23 14:41:55 -07009use super::*;
David Tolnayab919512017-12-30 23:31:51 -050010use proc_macro2::TokenStream;
David Tolnay61037c62018-01-05 16:21:03 -080011use token::{Brace, Bracket, Paren};
David Tolnay9c76bcb2017-12-26 23:14:59 -050012
13#[cfg(feature = "extra-traits")]
14use std::hash::{Hash, Hasher};
David Tolnayc43b44e2017-12-30 23:55:54 -050015#[cfg(feature = "extra-traits")]
16use tt::TokenStreamHelper;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070017
Alex Crichton62a0a592017-05-22 13:58:53 -070018ast_struct! {
David Tolnayab919512017-12-30 23:31:51 -050019 /// Represents a macro invocation. The Path indicates which macro is being
20 /// invoked, and the `TokenStream` contains the source of the macro
21 /// invocation.
David Tolnay9c76bcb2017-12-26 23:14:59 -050022 pub struct Macro #manual_extra_traits {
Alex Crichton62a0a592017-05-22 13:58:53 -070023 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -080024 pub bang_token: Token![!],
David Tolnayab919512017-12-30 23:31:51 -050025 pub delimiter: MacroDelimiter,
26 pub tts: TokenStream,
27 }
28}
29
30ast_enum! {
31 pub enum MacroDelimiter {
32 /// `macro!(...)`
33 Paren(Paren),
34 /// `macro!{...}`
35 Brace(Brace),
36 /// `macro![...]`
37 Bracket(Bracket),
Alex Crichton62a0a592017-05-22 13:58:53 -070038 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070039}
40
David Tolnay9c76bcb2017-12-26 23:14:59 -050041#[cfg(feature = "extra-traits")]
42impl Eq for Macro {}
43
44#[cfg(feature = "extra-traits")]
45impl PartialEq for Macro {
46 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -050047 self.path == other.path && self.bang_token == other.bang_token
David Tolnayab919512017-12-30 23:31:51 -050048 && self.delimiter == other.delimiter
49 && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
David Tolnay9c76bcb2017-12-26 23:14:59 -050050 }
51}
52
53#[cfg(feature = "extra-traits")]
54impl Hash for Macro {
55 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -050056 where
57 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -050058 {
59 self.path.hash(state);
60 self.bang_token.hash(state);
David Tolnayab919512017-12-30 23:31:51 -050061 self.delimiter.hash(state);
62 TokenStreamHelper(&self.tts).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -050063 }
64}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065
David Tolnay84aa0752016-10-02 23:01:13 -070066#[cfg(feature = "parsing")]
67pub mod parsing {
68 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070069
David Tolnayc5ab8c62017-12-26 16:43:39 -050070 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -070071
David Tolnaydecf28d2017-11-11 11:56:45 -080072 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -040073 named!(parse -> Self, do_parse!(
74 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -080075 bang: punct!(!) >>
David Tolnaye0824032017-12-27 15:25:56 -050076 body: call!(tt::delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -080077 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -040078 path: what,
79 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -050080 delimiter: body.0,
81 tts: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -040082 })
83 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -080084
85 fn description() -> Option<&'static str> {
86 Some("macro invocation")
87 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070088 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070089}
David Tolnayf8cf9632016-10-02 23:15:25 -070090
91#[cfg(feature = "printing")]
92mod printing {
93 use super::*;
David Tolnay51382052017-12-27 13:46:21 -050094 use quote::{ToTokens, Tokens};
David Tolnayf8cf9632016-10-02 23:15:25 -070095
David Tolnaydecf28d2017-11-11 11:56:45 -080096 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -070097 fn to_tokens(&self, tokens: &mut Tokens) {
98 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -070099 self.bang_token.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -0500100 match self.delimiter {
101 MacroDelimiter::Paren(ref paren) => {
102 paren.surround(tokens, |tokens| self.tts.to_tokens(tokens));
103 }
104 MacroDelimiter::Brace(ref brace) => {
105 brace.surround(tokens, |tokens| self.tts.to_tokens(tokens));
106 }
107 MacroDelimiter::Bracket(ref bracket) => {
108 bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens));
109 }
110 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700111 }
112 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700113}