1 // Copyright (C) 2023-2024 Free Software Foundation, Inc.
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)
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.
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.
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/>.
23 pub use group::{Delimiter, Group};
25 pub use literal::Literal;
26 pub use punct::{Punct, Spacing};
29 use std::{fmt, iter, str::FromStr};
39 /// Determines whether proc_macro has been made accessible to the currently
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()
50 /// A single token or a delimited sequence of token trees.
59 type InternalTokenTree = bridge::token_stream::TokenTree;
61 impl From<InternalTokenTree> for TokenTree {
62 fn from(value: InternalTokenTree) -> Self {
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)),
73 /// Get the [`Span`] for this TokenTree.
74 pub fn span(&self) -> Span {
76 TokenTree::Group(group) => group.span(),
77 TokenTree::Ident(ident) => ident.span(),
78 TokenTree::Punct(punct) => punct.span(),
79 TokenTree::Literal(literal) => literal.span(),
83 /// Set the span for this TokenTree.
87 /// * `span` - The new span value.
88 pub fn set_span(&mut self, span: Span) {
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),
98 impl fmt::Debug for TokenTree {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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),
109 impl fmt::Display for TokenTree {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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),
120 impl From<Group> for TokenTree {
121 fn from(g: Group) -> Self {
126 impl From<Ident> for TokenTree {
127 fn from(i: Ident) -> Self {
132 impl From<Punct> for TokenTree {
133 fn from(p: Punct) -> Self {
138 impl From<Literal> for TokenTree {
139 fn from(l: Literal) -> Self {
140 TokenTree::Literal(l)
144 /// Error returned from `from_str` functions.
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")
154 impl error::Error for LexError {}
156 /// An abstract sequence of token trees.
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.
162 pub struct TokenStream(bridge::token_stream::TokenStream);
165 // TODO: Add experimental API functions for this type
167 /// Creates an empty `TokenStream` containing no token trees.
168 pub fn new() -> Self {
169 TokenStream(bridge::token_stream::TokenStream::new())
172 /// Checks if this `TokenStream` is empty.
173 pub fn is_empty(&self) -> bool {
178 impl fmt::Display for TokenStream {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184 impl fmt::Debug for TokenStream {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190 impl FromStr for TokenStream {
193 fn from_str(src: &str) -> Result<Self, LexError> {
194 bridge::token_stream::TokenStream::from_str(src).map(TokenStream)
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))
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))
210 impl Extend<TokenTree> for TokenStream {
211 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) {
212 self.0.extend(trees);
216 impl Extend<TokenStream> for TokenStream {
217 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
218 self.0.extend(streams)