blob: 269cf73b4c7fd3ddca80438c29c235e3ba6daa9c [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 Tolnay977f8282017-05-31 17:41:33 -070023#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070024pub struct TokenStream(imp::TokenStream);
25
26#[derive(Debug)]
27pub struct LexError(imp::LexError);
28
29impl FromStr for TokenStream {
30 type Err = LexError;
31
32 fn from_str(src: &str) -> Result<TokenStream, LexError> {
33 match src.parse() {
34 Ok(e) => Ok(TokenStream(e)),
35 Err(e) => Err(LexError(e)),
36 }
37 }
38}
39
40impl fmt::Display for TokenStream {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 self.0.fmt(f)
43 }
44}
45
46impl From<proc_macro::TokenStream> for TokenStream {
47 fn from(inner: proc_macro::TokenStream) -> TokenStream {
48 TokenStream(inner.into())
49 }
50}
51
52impl From<TokenStream> for proc_macro::TokenStream {
53 fn from(inner: TokenStream) -> proc_macro::TokenStream {
54 inner.0.into()
55 }
56}
57
58impl From<TokenTree> for TokenStream {
59 fn from(tree: TokenTree) -> TokenStream {
60 TokenStream(tree.into())
61 }
62}
63
Alex Crichton44bffbc2017-05-19 17:51:59 -070064impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
65 fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
66 TokenStream(streams.into_iter().map(|t| t.into().0).collect())
67 }
68}
69
70impl IntoIterator for TokenStream {
71 type Item = TokenTree;
72 type IntoIter = TokenIter;
73
74 fn into_iter(self) -> TokenIter {
75 TokenIter(self.0.into_iter())
76 }
77}
78
79impl TokenStream {
80 pub fn empty() -> TokenStream {
81 TokenStream(imp::TokenStream::empty())
82 }
83
84 pub fn is_empty(&self) -> bool {
85 self.0.is_empty()
86 }
87}
88
David Tolnay977f8282017-05-31 17:41:33 -070089#[derive(Copy, Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070090pub struct Span(imp::Span);
91
92impl Default for Span {
93 fn default() -> Span {
94 Span(imp::Span::default())
95 }
96}
97
98impl Span {
99 pub fn call_site() -> Span {
100 Span(imp::Span::call_site())
101 }
102}
103
David Tolnay977f8282017-05-31 17:41:33 -0700104#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700105pub struct TokenTree {
106 pub span: Span,
107 pub kind: TokenKind,
108}
109
Alex Crichton44bffbc2017-05-19 17:51:59 -0700110impl fmt::Display for TokenTree {
111 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112 TokenStream::from(self.clone()).fmt(f)
113 }
114}
115
David Tolnay977f8282017-05-31 17:41:33 -0700116#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700117pub enum TokenKind {
118 Sequence(Delimiter, TokenStream),
119 Word(Symbol),
120 Op(char, OpKind),
121 Literal(Literal),
122}
123
Michael Layzell5372f4b2017-06-02 10:29:31 -0400124#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700125pub enum Delimiter {
126 Parenthesis,
127 Brace,
128 Bracket,
129 None,
130}
131
David Tolnay977f8282017-05-31 17:41:33 -0700132#[derive(Copy, Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700133pub struct Symbol(imp::Symbol);
134
135impl<'a> From<&'a str> for Symbol {
136 fn from(string: &'a str) -> Symbol {
137 Symbol(string.into())
138 }
139}
140
Alex Crichton852d53d2017-05-19 19:25:08 -0700141impl From<String> for Symbol {
142 fn from(string: String) -> Symbol {
143 Symbol(string[..].into())
144 }
145}
146
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700147impl Symbol {
Alex Crichton43881252017-05-26 08:51:06 -0700148 pub fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700149 &self.0
150 }
151}
152
Michael Layzell5372f4b2017-06-02 10:29:31 -0400153#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700154pub enum OpKind {
155 Alone,
156 Joint,
157}
158
David Tolnay977f8282017-05-31 17:41:33 -0700159#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700160pub struct Literal(imp::Literal);
161
162impl fmt::Display for Literal {
163 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164 self.0.fmt(f)
165 }
166}
167
Alex Crichton852d53d2017-05-19 19:25:08 -0700168impl Literal {
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700169 pub fn byte_char(b: u8) -> Literal {
170 Literal(imp::Literal::byte_char(b))
Alex Crichton76a5cc82017-05-23 07:01:44 -0700171 }
172
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700173 pub fn byte_string(s: &[u8]) -> Literal {
174 Literal(imp::Literal::byte_string(s))
Alex Crichton852d53d2017-05-19 19:25:08 -0700175 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700176
177 pub fn doccomment(s: &str) -> Literal {
178 Literal(imp::Literal::doccomment(s))
179 }
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700180
181 pub fn float(s: &str) -> Literal {
182 Literal(imp::Literal::float(s))
183 }
184
185 pub fn integer(s: &str) -> Literal {
186 Literal(imp::Literal::integer(s))
187 }
Alex Crichton31316622017-05-26 12:54:47 -0700188
189 pub fn raw_string(s: &str, pounds: usize) -> Literal {
190 Literal(imp::Literal::raw_string(s, pounds))
191 }
192
193 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
194 Literal(imp::Literal::raw_byte_string(s, pounds))
195 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700196}
197
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700198macro_rules! froms {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700199 ($($t:ty,)*) => {$(
200 impl<'a> From<$t> for Literal {
201 fn from(t: $t) -> Literal {
202 Literal(t.into())
203 }
204 }
205 )*}
206}
207
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700208froms! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700209 u8, u16, u32, u64, usize,
210 i8, i16, i32, i64, isize,
Alex Crichton46060dc2017-05-25 07:41:30 -0700211 f32, f64, char, &'a str,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700212}
213
David Tolnay977f8282017-05-31 17:41:33 -0700214#[derive(Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700215pub struct TokenIter(imp::TokenIter);
216
217impl Iterator for TokenIter {
218 type Item = TokenTree;
219
220 fn next(&mut self) -> Option<TokenTree> {
221 self.0.next()
222 }
223}