Debian Codegraph @

libgrust/libproc_macro/lib.rs

raw
1 // Copyright (C) 2023-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU Proc Macro Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
17
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 // <http://www.gnu.org/licenses/>.
22
23 pub use group::{Delimiter, Group};
24 pub use ident::Ident;
25 pub use literal::Literal;
26 pub use punct::{Punct, Spacing};
27 pub use span::Span;
28 use std::error;
29 use std::{fmt, iter, str::FromStr};
30
31 mod bridge;
32 mod group;
33 mod ident;
34 mod literal;
35 mod punct;
36 mod span;
37 pub mod token_stream;
38
39 /// Determines whether proc_macro has been made accessible to the currently
40 /// running program.
41 ///
42 /// # Note
43 ///
44 /// This function provide a non panicking way to detect whether the API is
45 /// invoked from inside of a procedural macro.
46 pub fn is_available() -> bool {
47 bridge::is_available()
48 }
49
50 /// A single token or a delimited sequence of token trees.
51 #[derive(Clone)]
52 pub enum TokenTree {
53 Group(Group),
54 Ident(Ident),
55 Punct(Punct),
56 Literal(Literal),
57 }
58
59 type InternalTokenTree = bridge::token_stream::TokenTree;
60
61 impl From<InternalTokenTree> for TokenTree {
62 fn from(value: InternalTokenTree) -> Self {
63 match value {
64 InternalTokenTree::Group(g) => TokenTree::Group(Group(g)),
65 InternalTokenTree::Ident(i) => TokenTree::Ident(Ident(i)),
66 InternalTokenTree::Punct(p) => TokenTree::Punct(Punct(p)),
67 InternalTokenTree::Literal(l) => TokenTree::Literal(Literal(l)),
68 }
69 }
70 }
71
72 impl TokenTree {
73 /// Get the [`Span`] for this TokenTree.
74 pub fn span(&self) -> Span {
75 match self {
76 TokenTree::Group(group) => group.span(),
77 TokenTree::Ident(ident) => ident.span(),
78 TokenTree::Punct(punct) => punct.span(),
79 TokenTree::Literal(literal) => literal.span(),
80 }
81 }
82
83 /// Set the span for this TokenTree.
84 ///
85 /// # Arguments
86 ///
87 /// * `span` - The new span value.
88 pub fn set_span(&mut self, span: Span) {
89 match self {
90 TokenTree::Group(group) => group.set_span(span),
91 TokenTree::Ident(ident) => ident.set_span(span),
92 TokenTree::Punct(punct) => punct.set_span(span),
93 TokenTree::Literal(literal) => literal.set_span(span),
94 }
95 }
96 }
97
98 impl fmt::Debug for TokenTree {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 match self {
101 TokenTree::Group(group) => group.fmt(f),
102 TokenTree::Ident(ident) => ident.fmt(f),
103 TokenTree::Punct(punct) => punct.fmt(f),
104 TokenTree::Literal(literal) => literal.fmt(f),
105 }
106 }
107 }
108
109 impl fmt::Display for TokenTree {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 match self {
112 TokenTree::Group(group) => group.fmt(f),
113 TokenTree::Ident(ident) => ident.fmt(f),
114 TokenTree::Punct(punct) => punct.fmt(f),
115 TokenTree::Literal(literal) => literal.fmt(f),
116 }
117 }
118 }
119
120 impl From<Group> for TokenTree {
121 fn from(g: Group) -> Self {
122 TokenTree::Group(g)
123 }
124 }
125
126 impl From<Ident> for TokenTree {
127 fn from(i: Ident) -> Self {
128 TokenTree::Ident(i)
129 }
130 }
131
132 impl From<Punct> for TokenTree {
133 fn from(p: Punct) -> Self {
134 TokenTree::Punct(p)
135 }
136 }
137
138 impl From<Literal> for TokenTree {
139 fn from(l: Literal) -> Self {
140 TokenTree::Literal(l)
141 }
142 }
143
144 /// Error returned from `from_str` functions.
145 #[derive(Debug)]
146 pub struct LexError;
147
148 impl fmt::Display for LexError {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 f.write_str("cannot parse string into token stream")
151 }
152 }
153
154 impl error::Error for LexError {}
155
156 /// An abstract sequence of token trees.
157 ///
158 /// This type provides interfaces for iterating over those token trees. This
159 /// is both the input and the output of `#[proc_macro]`,
160 /// `#[proc_macro_attribute]` and `#[proc_macro_derive]` definitions.
161 #[derive(Clone)]
162 pub struct TokenStream(bridge::token_stream::TokenStream);
163
164 impl TokenStream {
165 // TODO: Add experimental API functions for this type
166
167 /// Creates an empty `TokenStream` containing no token trees.
168 pub fn new() -> Self {
169 TokenStream(bridge::token_stream::TokenStream::new())
170 }
171
172 /// Checks if this `TokenStream` is empty.
173 pub fn is_empty(&self) -> bool {
174 self.0.is_empty()
175 }
176 }
177
178 impl fmt::Display for TokenStream {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 self.0.fmt(f)
181 }
182 }
183
184 impl fmt::Debug for TokenStream {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 self.0.fmt(f)
187 }
188 }
189
190 impl FromStr for TokenStream {
191 type Err = LexError;
192
193 fn from_str(src: &str) -> Result<Self, LexError> {
194 bridge::token_stream::TokenStream::from_str(src).map(TokenStream)
195 }
196 }
197
198 impl iter::FromIterator<TokenTree> for TokenStream {
199 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
200 TokenStream(bridge::token_stream::TokenStream::from_tree_iterator(trees))
201 }
202 }
203
204 impl iter::FromIterator<TokenStream> for TokenStream {
205 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
206 TokenStream(bridge::token_stream::TokenStream::from_iterator(streams))
207 }
208 }
209
210 impl Extend<TokenTree> for TokenStream {
211 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) {
212 self.0.extend(trees);
213 }
214 }
215
216 impl Extend<TokenStream> for TokenStream {
217 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
218 self.0.extend(streams)
219 }
220 }