]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgrust/libproc_macro/bridge/token_stream.rs
42cba6264ce6349709929afbc20ba0d5cc732498
[thirdparty/gcc.git] / libgrust / libproc_macro / bridge / token_stream.rs
1 // Copyright (C) 2023 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 use bridge::{ffistring::FFIString, group::Group, ident::Ident, literal::Literal, punct::Punct};
24 use std::convert::TryInto;
25 use std::fmt;
26 use std::slice;
27 use std::str::FromStr;
28 use LexError;
29
30 type ExternalTokenTree = crate::TokenTree;
31 type ExternalTokenStream = crate::TokenStream;
32
33 extern "C" {
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);
40 }
41
42 #[repr(C)]
43 #[derive(Clone)]
44 pub enum TokenTree {
45 Group(Group),
46 Ident(Ident),
47 Punct(Punct),
48 Literal(Literal),
49 }
50
51 impl fmt::Display for TokenTree {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
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),
58 }
59 }
60 }
61
62 impl From<ExternalTokenTree> for TokenTree {
63 fn from(value: ExternalTokenTree) -> Self {
64 match value {
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),
69 }
70 }
71 }
72
73 #[repr(C)]
74 #[derive(Debug)]
75 pub struct TokenStream {
76 pub(crate) data: *const TokenTree,
77 pub(crate) size: u64,
78 capacity: u64,
79 }
80
81 impl TokenStream {
82 pub fn new() -> Self {
83 unsafe { TokenStream__new() }
84 }
85
86 fn with_capacity(capacity: u64) -> Self {
87 unsafe { TokenStream__with_capacity(capacity) }
88 }
89
90 fn push(&mut self, tree: TokenTree) {
91 unsafe { TokenStream__push(self as *mut TokenStream, tree) }
92 }
93
94 pub fn is_empty(&self) -> bool {
95 0 == self.size
96 }
97
98 pub fn from_iterator<I>(it: I) -> Self
99 where
100 I: IntoIterator<Item = ExternalTokenStream>,
101 {
102 let it = it.into_iter();
103 let mut result = TokenStream::with_capacity(it.size_hint().0.try_into().unwrap());
104 for stream in it {
105 for item in stream.into_iter() {
106 result.push(item.into());
107 }
108 }
109 result
110 }
111
112 pub fn from_tree_iterator<I>(it: I) -> Self
113 where
114 I: IntoIterator<Item = ExternalTokenTree>,
115 {
116 let it = it.into_iter();
117 let mut result = TokenStream::with_capacity(it.size_hint().0.try_into().unwrap());
118 for item in it {
119 result.push(item.into());
120 }
121 result
122 }
123 }
124
125 impl Extend<ExternalTokenTree> for TokenStream {
126 fn extend<I: IntoIterator<Item = ExternalTokenTree>>(&mut self, trees: I) {
127 for tt in trees {
128 self.push(tt.into())
129 }
130 }
131 }
132
133 impl Extend<ExternalTokenStream> for TokenStream {
134 fn extend<I: IntoIterator<Item = ExternalTokenStream>>(&mut self, streams: I) {
135 for stream in streams {
136 for tt in stream {
137 self.push(tt.into());
138 }
139 }
140 }
141 }
142
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()) } {
146 i.fmt(f)?;
147 match i {
148 TokenTree::Punct(_) => (),
149 _ => f.write_str(" ")?,
150 }
151 }
152 Ok(())
153 }
154 }
155
156 impl FromStr for TokenStream {
157 type Err = LexError;
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) } {
161 Err(LexError)
162 } else {
163 Ok(ts)
164 }
165 }
166 }
167
168 impl Clone for TokenStream {
169 fn clone(&self) -> Self {
170 unsafe { TokenStream__clone(self as *const TokenStream) }
171 }
172 }
173
174 impl Drop for TokenStream {
175 fn drop(&mut self) {
176 unsafe { TokenStream__drop(self as *mut TokenStream) }
177 }
178 }