1 // Copyright (C) 2023 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 use bridge::{ffistring::FFIString, group::Group, ident::Ident, literal::Literal, punct::Punct};
24 use std::convert::TryInto;
27 use std::str::FromStr;
30 type ExternalTokenTree = crate::TokenTree;
31 type ExternalTokenStream = crate::TokenStream;
34 fn TokenStream__new() -> TokenStream;
35 fn TokenStream__with_capacity(capacity: u64) -> TokenStream;
36 fn TokenStream__push(stream: *mut TokenStream, tree: TokenTree);
37 fn TokenStream__from_string(str: FFIString, ts: *mut TokenStream) -> bool;
38 fn TokenStream__clone(ts: *const TokenStream) -> TokenStream;
39 fn TokenStream__drop(stream: *mut TokenStream);
51 impl fmt::Display for TokenTree {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 TokenTree::Group(group) => group.fmt(f),
55 TokenTree::Ident(ident) => ident.fmt(f),
56 TokenTree::Punct(punct) => punct.fmt(f),
57 TokenTree::Literal(literal) => literal.fmt(f),
62 impl From<ExternalTokenTree> for TokenTree {
63 fn from(value: ExternalTokenTree) -> Self {
65 ExternalTokenTree::Group(g) => TokenTree::Group(g.0),
66 ExternalTokenTree::Ident(i) => TokenTree::Ident(i.0),
67 ExternalTokenTree::Punct(p) => TokenTree::Punct(p.0),
68 ExternalTokenTree::Literal(l) => TokenTree::Literal(l.0),
75 pub struct TokenStream {
76 pub(crate) data: *const TokenTree,
82 pub fn new() -> Self {
83 unsafe { TokenStream__new() }
86 fn with_capacity(capacity: u64) -> Self {
87 unsafe { TokenStream__with_capacity(capacity) }
90 fn push(&mut self, tree: TokenTree) {
91 unsafe { TokenStream__push(self as *mut TokenStream, tree) }
94 pub fn is_empty(&self) -> bool {
98 pub fn from_iterator<I>(it: I) -> Self
100 I: IntoIterator<Item = ExternalTokenStream>,
102 let it = it.into_iter();
103 let mut result = TokenStream::with_capacity(it.size_hint().0.try_into().unwrap());
105 for item in stream.into_iter() {
106 result.push(item.into());
112 pub fn from_tree_iterator<I>(it: I) -> Self
114 I: IntoIterator<Item = ExternalTokenTree>,
116 let it = it.into_iter();
117 let mut result = TokenStream::with_capacity(it.size_hint().0.try_into().unwrap());
119 result.push(item.into());
125 impl Extend<ExternalTokenTree> for TokenStream {
126 fn extend<I: IntoIterator<Item = ExternalTokenTree>>(&mut self, trees: I) {
133 impl Extend<ExternalTokenStream> for TokenStream {
134 fn extend<I: IntoIterator<Item = ExternalTokenStream>>(&mut self, streams: I) {
135 for stream in streams {
137 self.push(tt.into());
143 impl fmt::Display for TokenStream {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 for i in unsafe { slice::from_raw_parts(self.data, self.size.try_into().unwrap()) } {
148 TokenTree::Punct(_) => (),
149 _ => f.write_str(" ")?,
156 impl FromStr for TokenStream {
158 fn from_str(string: &str) -> Result<Self, LexError> {
159 let mut ts = TokenStream::new();
160 if unsafe { TokenStream__from_string(string.into(), &mut ts as *mut TokenStream) } {
168 impl Clone for TokenStream {
169 fn clone(&self) -> Self {
170 unsafe { TokenStream__clone(self as *const TokenStream) }
174 impl Drop for TokenStream {
176 unsafe { TokenStream__drop(self as *mut TokenStream) }