Skip to main content

moxy_token/
lib.rs

1#![cfg_attr(nightly, feature(proc_macro_diagnostic, proc_macro_span, proc_macro_def_site,))]
2
3//! # Moxy tokens
4//!
5//! Token types, lexing, spans, and compiler-token conversions for Rust syntax
6//! tooling. [`TokenStream`] is an owned sequence of [`TokenTree`] values that
7//! preserves token text, punctuation spacing, and source spans.
8//!
9//! ## Quick start
10//!
11//! Parse source into a token stream, or construct individual token types with
12//! the provided macros:
13//!
14//! ```ignore
15//! use moxy::token::{ident, TokenStream};
16//! use moxy::Token;
17//!
18//! let stream: TokenStream = "fn generated() {}".parse().unwrap();
19//! let name = ident!(generated);
20//! let semi: Token![;] = Default::default();
21//! assert_eq!(name.to_string(), "generated");
22//! assert_eq!(semi.to_string(), ";");
23//! ```
24//!
25//! ## Integrations
26//!
27//! Enable `serde` to serialize supported token types. Enable `proc-macro2` for
28//! conversions with `proc_macro2`;
29
30extern crate proc_macro;
31
32mod bridge;
33mod delim;
34mod group;
35mod ident;
36mod keyword;
37mod lex;
38mod lit;
39mod punct;
40/// Source files, locations, and source maps used by spans.
41pub mod source;
42mod spacing;
43mod span;
44mod stream;
45mod tree;
46
47#[doc(inline)]
48pub use delim::*;
49#[doc(inline)]
50pub use group::*;
51#[doc(inline)]
52pub use ident::*;
53#[doc(inline)]
54pub use keyword::*;
55#[doc(inline)]
56pub use lex::*;
57#[doc(inline)]
58pub use lit::*;
59#[doc(inline)]
60pub use punct::*;
61#[doc(inline)]
62pub use source::Location;
63#[doc(inline)]
64pub use spacing::*;
65#[doc(inline)]
66pub use span::{DelimSpan, RangeSpan, Span, Spanner};
67#[doc(inline)]
68pub use stream::*;
69#[doc(inline)]
70pub use tree::*;
71
72/// Emits a value as tokens into a destination token stream.
73/// A public Rust token API type for to tokens<t.
74pub trait ToTokens<T = TokenStream> {
75    fn to_tokens(&self, tokens: &mut T);
76}
77
78/// Convenience methods for values that can emit a [`TokenStream`].
79/// A public Rust token API type for to token stream:.
80pub trait ToTokenStream: ToTokens<TokenStream> {
81    fn to_token_stream(&self) -> TokenStream {
82        let mut tokens = TokenStream::new();
83        self.to_tokens(&mut tokens);
84        tokens
85    }
86
87    fn into_token_stream(self) -> TokenStream
88    where
89        Self: Sized,
90    {
91        self.to_token_stream()
92    }
93}
94
95impl<X: ToTokens<TokenStream> + ?Sized> ToTokenStream for X {}
96
97impl<T: ToTokens> ToTokens for ::std::boxed::Box<T> {
98    fn to_tokens(&self, tokens: &mut TokenStream) {
99        (**self).to_tokens(tokens);
100    }
101}
102
103impl<T: ToTokens> ToTokens for &T {
104    fn to_tokens(&self, tokens: &mut TokenStream) {
105        ToTokens::to_tokens(*self, tokens);
106    }
107}
108
109impl<T: ToTokens> ToTokens for Option<T> {
110    fn to_tokens(&self, tokens: &mut TokenStream) {
111        if let Some(v) = self {
112            v.to_tokens(tokens);
113        }
114    }
115}
116
117impl<T: ToTokens> ToTokens for Vec<T> {
118    fn to_tokens(&self, tokens: &mut TokenStream) {
119        for v in self {
120            v.to_tokens(tokens);
121        }
122    }
123}
124
125impl<T: ToTokens, E: ToTokens> ToTokens for Result<T, E> {
126    fn to_tokens(&self, tokens: &mut TokenStream) {
127        match self {
128            Self::Ok(v) => v.to_tokens(tokens),
129            Self::Err(err) => err.to_tokens(tokens),
130        }
131    }
132}
133
134/// Map a Rust punctuation or keyword symbol to its [`crate`] token type.
135#[macro_export]
136macro_rules! Token {
137    // --- punctuation: single char ---
138    [&]     => { $crate::And };
139    [|]     => { $crate::Or };
140    [!]     => { $crate::Not };
141    [~]     => { $crate::Tilde };
142    [+]     => { $crate::Plus };
143    [-]     => { $crate::Minus };
144    [_]     => { $crate::Underscore };
145    [*]     => { $crate::Star };
146    [/]     => { $crate::Slash };
147    [%]     => { $crate::Percent };
148    [^]     => { $crate::Caret };
149    [=]     => { $crate::Eq };
150    [<]     => { $crate::Lt };
151    [>]     => { $crate::Gt };
152    [@]     => { $crate::At };
153    [.]     => { $crate::Dot };
154    [,]     => { $crate::Comma };
155    [;]     => { $crate::Semi };
156    [:]     => { $crate::Colon };
157    [#]     => { $crate::Pound };
158    [$]     => { $crate::Dollar };
159    [?]     => { $crate::Question };
160
161    // --- keywords ---
162    [as]          => { $crate::As };
163    [async]       => { $crate::Async };
164    [auto]        => { $crate::Auto };
165    [await]       => { $crate::Await };
166    [become]      => { $crate::Become };
167    [box]         => { $crate::Box };
168    [break]       => { $crate::Break };
169    [const]       => { $crate::Const };
170    [continue]    => { $crate::Continue };
171    [crate]       => { $crate::Crate };
172    [default]     => { $crate::Default };
173    [do]          => { $crate::Do };
174    [dyn]         => { $crate::Dyn };
175    [else]        => { $crate::Else };
176    [enum]        => { $crate::Enum };
177    [extern]      => { $crate::Extern };
178    [final]       => { $crate::Final };
179    [fn]          => { $crate::Fn };
180    [for]         => { $crate::For };
181    [if]          => { $crate::If };
182    [impl]        => { $crate::Impl };
183    [in]          => { $crate::In };
184    [let]         => { $crate::Let };
185    [loop]        => { $crate::Loop };
186    [macro]       => { $crate::Macro };
187    [macro_rules] => { $crate::MacroRules };
188    [match]       => { $crate::Match };
189    [mod]         => { $crate::Mod };
190    [move]        => { $crate::Move };
191    [mut]         => { $crate::Mut };
192    [override]    => { $crate::Override };
193    [priv]        => { $crate::Priv };
194    [pub]         => { $crate::Pub };
195    [raw]         => { $crate::Raw };
196    [ref]         => { $crate::Ref };
197    [return]      => { $crate::Return };
198    [safe]        => { $crate::Safe };
199    [Self]        => { $crate::SelfType };
200    [self]        => { $crate::SelfValue };
201    [static]      => { $crate::Static };
202    [struct]      => { $crate::Struct };
203    [super]       => { $crate::Super };
204    [trait]       => { $crate::Trait };
205    [try]         => { $crate::Try };
206    [type]        => { $crate::Type };
207    [typeof]      => { $crate::Typeof };
208    [union]       => { $crate::Union };
209    [unsafe]      => { $crate::Unsafe };
210    [unsized]     => { $crate::Unsized };
211    [use]         => { $crate::Use };
212    [virtual]     => { $crate::Virtual };
213    [where]       => { $crate::Where };
214    [while]       => { $crate::While };
215    [yield]       => { $crate::Yield };
216}