blob: a5e123aa662ddce8ef2da74ecfc0d4bb8f6eaf34 [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 Tolnay05658502018-01-07 09:56:37 -080019 /// A macro invocation: `println!("{}", mac)`.
David Tolnay9c76bcb2017-12-26 23:14:59 -050020 pub struct Macro #manual_extra_traits {
Alex Crichton62a0a592017-05-22 13:58:53 -070021 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -080022 pub bang_token: Token![!],
David Tolnayab919512017-12-30 23:31:51 -050023 pub delimiter: MacroDelimiter,
24 pub tts: TokenStream,
25 }
26}
27
28ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -080029 /// A grouping token that surrounds a macro body: `m!(...)` or `m!{...}` or `m![...]`.
David Tolnayab919512017-12-30 23:31:51 -050030 pub enum MacroDelimiter {
David Tolnayab919512017-12-30 23:31:51 -050031 Paren(Paren),
David Tolnayab919512017-12-30 23:31:51 -050032 Brace(Brace),
David Tolnayab919512017-12-30 23:31:51 -050033 Bracket(Bracket),
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070035}
36
David Tolnay9c76bcb2017-12-26 23:14:59 -050037#[cfg(feature = "extra-traits")]
38impl Eq for Macro {}
39
40#[cfg(feature = "extra-traits")]
41impl PartialEq for Macro {
42 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -050043 self.path == other.path && self.bang_token == other.bang_token
David Tolnayab919512017-12-30 23:31:51 -050044 && self.delimiter == other.delimiter
45 && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
David Tolnay9c76bcb2017-12-26 23:14:59 -050046 }
47}
48
49#[cfg(feature = "extra-traits")]
50impl Hash for Macro {
51 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -050052 where
53 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -050054 {
55 self.path.hash(state);
56 self.bang_token.hash(state);
David Tolnayab919512017-12-30 23:31:51 -050057 self.delimiter.hash(state);
58 TokenStreamHelper(&self.tts).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -050059 }
60}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070061
David Tolnay84aa0752016-10-02 23:01:13 -070062#[cfg(feature = "parsing")]
63pub mod parsing {
64 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065
David Tolnayc5ab8c62017-12-26 16:43:39 -050066 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -070067
David Tolnaydecf28d2017-11-11 11:56:45 -080068 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -040069 named!(parse -> Self, do_parse!(
70 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -080071 bang: punct!(!) >>
David Tolnaye0824032017-12-27 15:25:56 -050072 body: call!(tt::delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -080073 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -040074 path: what,
75 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -050076 delimiter: body.0,
77 tts: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -040078 })
79 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -080080
81 fn description() -> Option<&'static str> {
82 Some("macro invocation")
83 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070084 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070085}
David Tolnayf8cf9632016-10-02 23:15:25 -070086
87#[cfg(feature = "printing")]
88mod printing {
89 use super::*;
David Tolnay51382052017-12-27 13:46:21 -050090 use quote::{ToTokens, Tokens};
David Tolnayf8cf9632016-10-02 23:15:25 -070091
David Tolnaydecf28d2017-11-11 11:56:45 -080092 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -070093 fn to_tokens(&self, tokens: &mut Tokens) {
94 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -070095 self.bang_token.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -050096 match self.delimiter {
97 MacroDelimiter::Paren(ref paren) => {
98 paren.surround(tokens, |tokens| self.tts.to_tokens(tokens));
99 }
100 MacroDelimiter::Brace(ref brace) => {
101 brace.surround(tokens, |tokens| self.tts.to_tokens(tokens));
102 }
103 MacroDelimiter::Bracket(ref bracket) => {
104 bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens));
105 }
106 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700107 }
108 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700109}