blob: 746897de5cd5bd4afd0217da290c27e2cdc8a644 [file] [log] [blame]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07001#![cfg_attr(feature = "unstable", feature(proc_macro))]
2
Alex Crichton44bffbc2017-05-19 17:51:59 -07003extern crate proc_macro;
4
Alex Crichtone14e8fd2017-05-23 07:02:54 -07005#[cfg(not(feature = "unstable"))]
David Tolnayb1032662017-05-31 15:52:28 -07006extern crate unicode_xid;
Alex Crichton44bffbc2017-05-19 17:51:59 -07007
8use std::fmt;
Alex Crichton44bffbc2017-05-19 17:51:59 -07009use std::str::FromStr;
10use std::iter::FromIterator;
11
David Tolnayb1032662017-05-31 15:52:28 -070012#[macro_use]
13#[cfg(not(feature = "unstable"))]
14mod strnom;
15
Alex Crichton44bffbc2017-05-19 17:51:59 -070016#[path = "stable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070017#[cfg(not(feature = "unstable"))]
Alex Crichtonb15c6352017-05-19 19:36:36 -070018mod imp;
19#[path = "unstable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070020#[cfg(feature = "unstable")]
Alex Crichton44bffbc2017-05-19 17:51:59 -070021mod imp;
22
David Tolnaycb1b85f2017-06-03 16:40:35 -070023#[macro_use]
24mod macros;
25
26#[derive(Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070027pub struct TokenStream(imp::TokenStream);
28
Alex Crichton44bffbc2017-05-19 17:51:59 -070029pub struct LexError(imp::LexError);
30
31impl FromStr for TokenStream {
32 type Err = LexError;
33
34 fn from_str(src: &str) -> Result<TokenStream, LexError> {
35 match src.parse() {
36 Ok(e) => Ok(TokenStream(e)),
37 Err(e) => Err(LexError(e)),
38 }
39 }
40}
41
Alex Crichton44bffbc2017-05-19 17:51:59 -070042impl From<proc_macro::TokenStream> for TokenStream {
43 fn from(inner: proc_macro::TokenStream) -> TokenStream {
44 TokenStream(inner.into())
45 }
46}
47
48impl From<TokenStream> for proc_macro::TokenStream {
49 fn from(inner: TokenStream) -> proc_macro::TokenStream {
50 inner.0.into()
51 }
52}
53
54impl From<TokenTree> for TokenStream {
55 fn from(tree: TokenTree) -> TokenStream {
56 TokenStream(tree.into())
57 }
58}
59
Alex Crichton44bffbc2017-05-19 17:51:59 -070060impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
61 fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
62 TokenStream(streams.into_iter().map(|t| t.into().0).collect())
63 }
64}
65
66impl IntoIterator for TokenStream {
67 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -070068 type IntoIter = TokenTreeIter;
Alex Crichton44bffbc2017-05-19 17:51:59 -070069
Alex Crichton1a7f7622017-07-05 17:47:15 -070070 fn into_iter(self) -> TokenTreeIter {
71 TokenTreeIter(self.0.into_iter())
Alex Crichton44bffbc2017-05-19 17:51:59 -070072 }
73}
74
75impl TokenStream {
76 pub fn empty() -> TokenStream {
77 TokenStream(imp::TokenStream::empty())
78 }
79
80 pub fn is_empty(&self) -> bool {
81 self.0.is_empty()
82 }
83}
84
David Tolnaycb1b85f2017-06-03 16:40:35 -070085#[derive(Copy, Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070086pub struct Span(imp::Span);
87
88impl Default for Span {
89 fn default() -> Span {
90 Span(imp::Span::default())
91 }
92}
93
94impl Span {
95 pub fn call_site() -> Span {
96 Span(imp::Span::call_site())
97 }
98}
99
David Tolnay977f8282017-05-31 17:41:33 -0700100#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700101pub struct TokenTree {
102 pub span: Span,
Alex Crichton1a7f7622017-07-05 17:47:15 -0700103 pub kind: TokenNode,
104}
105
106impl From<TokenNode> for TokenTree {
107 fn from(kind: TokenNode) -> TokenTree {
108 TokenTree { span: Span::default(), kind: kind }
109 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700110}
111
Alex Crichton44bffbc2017-05-19 17:51:59 -0700112impl fmt::Display for TokenTree {
113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 TokenStream::from(self.clone()).fmt(f)
115 }
116}
117
David Tolnay977f8282017-05-31 17:41:33 -0700118#[derive(Clone, Debug)]
Alex Crichton1a7f7622017-07-05 17:47:15 -0700119pub enum TokenNode {
120 Group(Delimiter, TokenStream),
121 Term(Term),
122 Op(char, Spacing),
Alex Crichton44bffbc2017-05-19 17:51:59 -0700123 Literal(Literal),
124}
125
Michael Layzell5372f4b2017-06-02 10:29:31 -0400126#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700127pub enum Delimiter {
128 Parenthesis,
129 Brace,
130 Bracket,
131 None,
132}
133
David Tolnaycb1b85f2017-06-03 16:40:35 -0700134#[derive(Copy, Clone)]
Alex Crichton1a7f7622017-07-05 17:47:15 -0700135pub struct Term(imp::Term);
Alex Crichton44bffbc2017-05-19 17:51:59 -0700136
Alex Crichton1a7f7622017-07-05 17:47:15 -0700137impl Term {
138 pub fn intern(string: &str) -> Term {
139 Term(string.into())
Alex Crichton44bffbc2017-05-19 17:51:59 -0700140 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700141
Alex Crichton43881252017-05-26 08:51:06 -0700142 pub fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700143 &self.0
144 }
145}
146
Alex Crichton1a7f7622017-07-05 17:47:15 -0700147#[derive(Copy, Clone, Debug)]
148pub enum Spacing {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700149 Alone,
150 Joint,
151}
152
David Tolnaycb1b85f2017-06-03 16:40:35 -0700153#[derive(Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700154pub struct Literal(imp::Literal);
155
Alex Crichton1a7f7622017-07-05 17:47:15 -0700156macro_rules! int_literals {
157 ($($kind:ident,)*) => ($(
158 pub fn $kind(n: $kind) -> Literal {
159 Literal(n.into())
160 }
161 )*)
162}
163
Alex Crichton852d53d2017-05-19 19:25:08 -0700164impl Literal {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700165 pub fn integer(s: i64) -> Literal {
166 Literal(imp::Literal::integer(s))
167 }
168
169 int_literals! {
170 u8, u16, u32, u64, /*usize*/
171 i8, i16, i32, i64, /*isize,*/
172 }
173
174 pub fn float(f: f64) -> Literal {
175 Literal(imp::Literal::float(f))
176 }
177
178 pub fn f64(f: f64) -> Literal {
179 Literal(f.into())
180 }
181
182 pub fn f32(f: f32) -> Literal {
183 Literal(f.into())
184 }
185
186 pub fn string(string: &str) -> Literal {
187 Literal(string.into())
188 }
189
190 pub fn character(ch: char) -> Literal {
191 Literal(ch.into())
Alex Crichton76a5cc82017-05-23 07:01:44 -0700192 }
193
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700194 pub fn byte_string(s: &[u8]) -> Literal {
195 Literal(imp::Literal::byte_string(s))
Alex Crichton852d53d2017-05-19 19:25:08 -0700196 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700197
Alex Crichton1a7f7622017-07-05 17:47:15 -0700198 // =======================================================================
199 // Not present upstream in proc_macro yet
200
201 pub fn byte_char(b: u8) -> Literal {
202 Literal(imp::Literal::byte_char(b))
203 }
204
Alex Crichton76a5cc82017-05-23 07:01:44 -0700205 pub fn doccomment(s: &str) -> Literal {
206 Literal(imp::Literal::doccomment(s))
207 }
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700208
Alex Crichton31316622017-05-26 12:54:47 -0700209 pub fn raw_string(s: &str, pounds: usize) -> Literal {
210 Literal(imp::Literal::raw_string(s, pounds))
211 }
212
213 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
214 Literal(imp::Literal::raw_byte_string(s, pounds))
215 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700216}
217
Alex Crichton1a7f7622017-07-05 17:47:15 -0700218pub struct TokenTreeIter(imp::TokenTreeIter);
Alex Crichton44bffbc2017-05-19 17:51:59 -0700219
Alex Crichton1a7f7622017-07-05 17:47:15 -0700220impl Iterator for TokenTreeIter {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700221 type Item = TokenTree;
222
223 fn next(&mut self) -> Option<TokenTree> {
224 self.0.next()
225 }
226}
David Tolnaycb1b85f2017-06-03 16:40:35 -0700227
228forward_fmt!(Debug for LexError);
229forward_fmt!(Debug for Literal);
230forward_fmt!(Debug for Span);
Alex Crichton1a7f7622017-07-05 17:47:15 -0700231forward_fmt!(Debug for Term);
232forward_fmt!(Debug for TokenTreeIter);
David Tolnaycb1b85f2017-06-03 16:40:35 -0700233forward_fmt!(Debug for TokenStream);
234forward_fmt!(Display for Literal);
235forward_fmt!(Display for TokenStream);