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 punct;
21pub mod source;
22mod spacing;
23pub mod span;
24mod stream;
25mod tree;
26
27#[doc(inline)]
28pub use delim::*;
29#[doc(inline)]
30pub use group::*;
31#[doc(inline)]
32pub use ident::*;
33#[doc(inline)]
34pub use keyword::*;
35#[doc(inline)]
36pub use lex::{LexError, Scan};
37#[doc(inline)]
38pub use lit::*;
39#[doc(inline)]
40pub use punct::*;
41#[doc(inline)]
42pub use spacing::*;
43#[doc(inline)]
44pub use span::{Span, Spanner};
45#[doc(inline)]
46pub use stream::*;
47#[doc(inline)]
48pub use tree::*;
49
50pub trait ToTokens<T = TokenStream> {
51    fn to_tokens(&self, tokens: &mut T);
52}
53
54pub trait ToTokenStream: ToTokens<TokenStream> {
55    fn to_token_stream(&self) -> TokenStream {
56        let mut tokens = TokenStream::new();
57        self.to_tokens(&mut tokens);
58        tokens
59    }
60
61    fn into_token_stream(self) -> TokenStream
62    where
63        Self: Sized,
64    {
65        self.to_token_stream()
66    }
67}
68
69impl<X: ToTokens<TokenStream> + ?Sized> ToTokenStream for X {}
70
71impl<T: ToTokens> ToTokens for ::std::boxed::Box<T> {
72    fn to_tokens(&self, tokens: &mut TokenStream) {
73        (**self).to_tokens(tokens);
74    }
75}
76
77impl<T: ToTokens> ToTokens for &T {
78    fn to_tokens(&self, tokens: &mut TokenStream) {
79        ToTokens::to_tokens(*self, tokens);
80    }
81}
82
83impl<T: ToTokens> ToTokens for Option<T> {
84    fn to_tokens(&self, tokens: &mut TokenStream) {
85        if let Some(v) = self {
86            v.to_tokens(tokens);
87        }
88    }
89}
90
91impl<T: ToTokens> ToTokens for Vec<T> {
92    fn to_tokens(&self, tokens: &mut TokenStream) {
93        for v in self {
94            v.to_tokens(tokens);
95        }
96    }
97}
98
99impl<T: ToTokens, E: ToTokens> ToTokens for Result<T, E> {
100    fn to_tokens(&self, tokens: &mut TokenStream) {
101        match self {
102            Self::Ok(v) => v.to_tokens(tokens),
103            Self::Err(err) => err.to_tokens(tokens),
104        }
105    }
106}
107
108/// Map a Rust punctuation or keyword symbol to its [`crate`] token type.
109#[macro_export]
110macro_rules! Token {
111    // --- punctuation: single char ---
112    [&]     => { $crate::And };
113    [|]     => { $crate::Or };
114    [!]     => { $crate::Not };
115    [~]     => { $crate::Tilde };
116    [+]     => { $crate::Plus };
117    [-]     => { $crate::Minus };
118    [_]     => { $crate::Underscore };
119    [*]     => { $crate::Star };
120    [/]     => { $crate::Slash };
121    [%]     => { $crate::Percent };
122    [^]     => { $crate::Caret };
123    [=]     => { $crate::Eq };
124    [<]     => { $crate::Lt };
125    [>]     => { $crate::Gt };
126    [@]     => { $crate::At };
127    [.]     => { $crate::Dot };
128    [,]     => { $crate::Comma };
129    [;]     => { $crate::Semi };
130    [:]     => { $crate::Colon };
131    [#]     => { $crate::Pound };
132    [$]     => { $crate::Dollar };
133    [?]     => { $crate::Question };
134
135    // --- keywords ---
136    [as]          => { $crate::As };
137    [async]       => { $crate::Async };
138    [auto]        => { $crate::Auto };
139    [await]       => { $crate::Await };
140    [become]      => { $crate::Become };
141    [box]         => { $crate::Box };
142    [break]       => { $crate::Break };
143    [const]       => { $crate::Const };
144    [continue]    => { $crate::Continue };
145    [crate]       => { $crate::Crate };
146    [default]     => { $crate::Default };
147    [do]          => { $crate::Do };
148    [dyn]         => { $crate::Dyn };
149    [else]        => { $crate::Else };
150    [enum]        => { $crate::Enum };
151    [extern]      => { $crate::Extern };
152    [final]       => { $crate::Final };
153    [fn]          => { $crate::Fn };
154    [for]         => { $crate::For };
155    [if]          => { $crate::If };
156    [impl]        => { $crate::Impl };
157    [in]          => { $crate::In };
158    [let]         => { $crate::Let };
159    [loop]        => { $crate::Loop };
160    [macro]       => { $crate::Macro };
161    [macro_rules] => { $crate::MacroRules };
162    [match]       => { $crate::Match };
163    [mod]         => { $crate::Mod };
164    [move]        => { $crate::Move };
165    [mut]         => { $crate::Mut };
166    [override]    => { $crate::Override };
167    [priv]        => { $crate::Priv };
168    [pub]         => { $crate::Pub };
169    [raw]         => { $crate::Raw };
170    [ref]         => { $crate::Ref };
171    [return]      => { $crate::Return };
172    [Self]        => { $crate::SelfType };
173    [self]        => { $crate::SelfValue };
174    [static]      => { $crate::Static };
175    [struct]      => { $crate::Struct };
176    [super]       => { $crate::Super };
177    [trait]       => { $crate::Trait };
178    [try]         => { $crate::Try };
179    [type]        => { $crate::Type };
180    [typeof]      => { $crate::Typeof };
181    [union]       => { $crate::Union };
182    [unsafe]      => { $crate::Unsafe };
183    [unsized]     => { $crate::Unsized };
184    [use]         => { $crate::Use };
185    [virtual]     => { $crate::Virtual };
186    [where]       => { $crate::Where };
187    [while]       => { $crate::While };
188    [yield]       => { $crate::Yield };
189}