Skip to main content

moxy_token/
lib.rs

1#![cfg_attr(
2    nightly,
3    feature(
4        proc_macro_diagnostic,
5        proc_macro_span,
6        // proc_macro_totokens,
7        proc_macro_def_site,
8    )
9)]
10
11extern crate proc_macro;
12
13pub mod bridge;
14mod delim;
15mod group;
16mod ident;
17pub mod keyword;
18pub mod lex;
19mod lit;
20pub mod parser;
21pub mod punct;
22pub mod source;
23mod spacing;
24pub mod span;
25mod stream;
26mod tree;
27
28#[doc(inline)]
29pub use delim::*;
30#[doc(inline)]
31pub use group::*;
32#[doc(inline)]
33pub use ident::*;
34#[doc(inline)]
35pub use keyword::*;
36#[doc(inline)]
37pub use lex::{LexError, Scan};
38#[doc(inline)]
39pub use lit::*;
40#[doc(inline)]
41pub use parser::Parse;
42#[doc(inline)]
43pub use punct::*;
44#[doc(inline)]
45pub use spacing::*;
46#[doc(inline)]
47pub use span::{Span, Spanner};
48#[doc(inline)]
49pub use stream::*;
50#[doc(inline)]
51pub use tree::*;
52
53pub trait ToTokens<T = TokenStream> {
54    fn to_tokens(&self, tokens: &mut T);
55}
56
57pub trait ToTokenStream: ToTokens<TokenStream> {
58    fn to_token_stream(&self) -> TokenStream {
59        let mut tokens = TokenStream::new();
60        self.to_tokens(&mut tokens);
61        tokens
62    }
63
64    fn into_token_stream(self) -> TokenStream
65    where
66        Self: Sized,
67    {
68        self.to_token_stream()
69    }
70}
71
72impl<X: ToTokens<TokenStream> + ?Sized> ToTokenStream for X {}
73
74impl<T: ToTokens> ToTokens for ::std::boxed::Box<T> {
75    fn to_tokens(&self, tokens: &mut TokenStream) {
76        (**self).to_tokens(tokens);
77    }
78}
79
80impl<T: ToTokens> ToTokens for &T {
81    fn to_tokens(&self, tokens: &mut TokenStream) {
82        ToTokens::to_tokens(*self, tokens);
83    }
84}
85
86impl<T: ToTokens> ToTokens for Option<T> {
87    fn to_tokens(&self, tokens: &mut TokenStream) {
88        if let Some(v) = self {
89            v.to_tokens(tokens);
90        }
91    }
92}
93
94impl<T: ToTokens> ToTokens for Vec<T> {
95    fn to_tokens(&self, tokens: &mut TokenStream) {
96        for v in self {
97            v.to_tokens(tokens);
98        }
99    }
100}
101
102/// Map a Rust punctuation or keyword symbol to its [`crate`] token type.
103#[macro_export]
104macro_rules! Token {
105    // --- punctuation: single char ---
106    [&]     => { $crate::And };
107    [|]     => { $crate::Or };
108    [!]     => { $crate::Not };
109    [~]     => { $crate::Tilde };
110    [+]     => { $crate::Plus };
111    [-]     => { $crate::Minus };
112    [*]     => { $crate::Star };
113    [/]     => { $crate::Slash };
114    [%]     => { $crate::Percent };
115    [^]     => { $crate::Caret };
116    [=]     => { $crate::Eq };
117    [<]     => { $crate::Lt };
118    [>]     => { $crate::Gt };
119    [@]     => { $crate::At };
120    [.]     => { $crate::Dot };
121    [,]     => { $crate::Comma };
122    [;]     => { $crate::Semi };
123    [:]     => { $crate::Colon };
124    [#]     => { $crate::Pound };
125    [$]     => { $crate::Dollar };
126    [?]     => { $crate::Question };
127
128    // --- punctuation: multi char ---
129    [&&]    => { $crate::AndAnd };
130    [||]    => { $crate::OrOr };
131    [<<]    => { $crate::Shl };
132    [>>]    => { $crate::Shr };
133    [==]    => { $crate::EqEq };
134    [!=]    => { $crate::Ne };
135    [<=]    => { $crate::Le };
136    [>=]    => { $crate::Ge };
137    [&=]    => { $crate::AndEq };
138    [|=]    => { $crate::OrEq };
139    [+=]    => { $crate::PlusEq };
140    [-=]    => { $crate::MinusEq };
141    [*=]    => { $crate::StarEq };
142    [/=]    => { $crate::SlashEq };
143    [%=]    => { $crate::PercentEq };
144    [^=]    => { $crate::CaretEq };
145    [=>]    => { $crate::FatArrow };
146    [->]    => { $crate::RArrow };
147    [<-]    => { $crate::LArrow };
148    [::]    => { $crate::PathSep };
149    [..]    => { $crate::DotDot };
150    [<<=]   => { $crate::ShlEq };
151    [>>=]   => { $crate::ShrEq };
152    [...]   => { $crate::DotDotDot };
153    [..=]   => { $crate::DotDotEq };
154
155    // --- keywords ---
156    [as]          => { $crate::As };
157    [async]       => { $crate::Async };
158    [auto]        => { $crate::Auto };
159    [await]       => { $crate::Await };
160    [become]      => { $crate::Become };
161    [box]         => { $crate::Box };
162    [break]       => { $crate::Break };
163    [const]       => { $crate::Const };
164    [continue]    => { $crate::Continue };
165    [crate]       => { $crate::Crate };
166    [default]     => { $crate::Default };
167    [do]          => { $crate::Do };
168    [dyn]         => { $crate::Dyn };
169    [else]        => { $crate::Else };
170    [enum]        => { $crate::Enum };
171    [extern]      => { $crate::Extern };
172    [final]       => { $crate::Final };
173    [fn]          => { $crate::Fn };
174    [for]         => { $crate::For };
175    [if]          => { $crate::If };
176    [impl]        => { $crate::Impl };
177    [in]          => { $crate::In };
178    [let]         => { $crate::Let };
179    [loop]        => { $crate::Loop };
180    [macro]       => { $crate::Macro };
181    [macro_rules] => { $crate::MacroRules };
182    [match]       => { $crate::Match };
183    [mod]         => { $crate::Mod };
184    [move]        => { $crate::Move };
185    [mut]         => { $crate::Mut };
186    [override]    => { $crate::Override };
187    [priv]        => { $crate::Priv };
188    [pub]         => { $crate::Pub };
189    [raw]         => { $crate::Raw };
190    [ref]         => { $crate::Ref };
191    [return]      => { $crate::Return };
192    [Self]        => { $crate::SelfType };
193    [self]        => { $crate::SelfValue };
194    [static]      => { $crate::Static };
195    [struct]      => { $crate::Struct };
196    [super]       => { $crate::Super };
197    [trait]       => { $crate::Trait };
198    [try]         => { $crate::Try };
199    [type]        => { $crate::Type };
200    [typeof]      => { $crate::Typeof };
201    [union]       => { $crate::Union };
202    [unsafe]      => { $crate::Unsafe };
203    [unsized]     => { $crate::Unsized };
204    [use]         => { $crate::Use };
205    [virtual]     => { $crate::Virtual };
206    [where]       => { $crate::Where };
207    [while]       => { $crate::While };
208    [yield]       => { $crate::Yield };
209}
210
211#[cfg(test)]
212mod tests {
213    #[test]
214    fn token_macro_maps_dollar_and_macro_rules() {
215        let dollar: Token![$] = Default::default();
216        let macro_rules: Token![macro_rules] = Default::default();
217
218        assert_eq!(dollar.as_str(), "$");
219        assert_eq!(macro_rules.as_str(), "macro_rules");
220    }
221}