blob: 7a2f5b61e15e6c99cafdd57dcba5b8d7426079a2 [file] [log] [blame]
Alex Crichton44bffbc2017-05-19 17:51:59 -07001extern crate proc_macro;
2
Alex Crichtone14e8fd2017-05-23 07:02:54 -07003#[cfg(not(feature = "unstable"))]
David Tolnayb1032662017-05-31 15:52:28 -07004extern crate unicode_xid;
Alex Crichton44bffbc2017-05-19 17:51:59 -07005
6use std::fmt;
Alex Crichton44bffbc2017-05-19 17:51:59 -07007use std::str::FromStr;
8use std::iter::FromIterator;
9
David Tolnayb1032662017-05-31 15:52:28 -070010#[macro_use]
11#[cfg(not(feature = "unstable"))]
12mod strnom;
13
Alex Crichton44bffbc2017-05-19 17:51:59 -070014#[path = "stable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070015#[cfg(not(feature = "unstable"))]
Alex Crichtonb15c6352017-05-19 19:36:36 -070016mod imp;
17#[path = "unstable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070018#[cfg(feature = "unstable")]
Alex Crichton44bffbc2017-05-19 17:51:59 -070019mod imp;
20
21#[derive(Clone)]
22pub struct TokenStream(imp::TokenStream);
23
24#[derive(Debug)]
25pub struct LexError(imp::LexError);
26
27impl FromStr for TokenStream {
28 type Err = LexError;
29
30 fn from_str(src: &str) -> Result<TokenStream, LexError> {
31 match src.parse() {
32 Ok(e) => Ok(TokenStream(e)),
33 Err(e) => Err(LexError(e)),
34 }
35 }
36}
37
38impl fmt::Display for TokenStream {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 self.0.fmt(f)
41 }
42}
43
44impl From<proc_macro::TokenStream> for TokenStream {
45 fn from(inner: proc_macro::TokenStream) -> TokenStream {
46 TokenStream(inner.into())
47 }
48}
49
50impl From<TokenStream> for proc_macro::TokenStream {
51 fn from(inner: TokenStream) -> proc_macro::TokenStream {
52 inner.0.into()
53 }
54}
55
56impl From<TokenTree> for TokenStream {
57 fn from(tree: TokenTree) -> TokenStream {
58 TokenStream(tree.into())
59 }
60}
61
Alex Crichton44bffbc2017-05-19 17:51:59 -070062impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
63 fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
64 TokenStream(streams.into_iter().map(|t| t.into().0).collect())
65 }
66}
67
68impl IntoIterator for TokenStream {
69 type Item = TokenTree;
70 type IntoIter = TokenIter;
71
72 fn into_iter(self) -> TokenIter {
73 TokenIter(self.0.into_iter())
74 }
75}
76
77impl TokenStream {
78 pub fn empty() -> TokenStream {
79 TokenStream(imp::TokenStream::empty())
80 }
81
82 pub fn is_empty(&self) -> bool {
83 self.0.is_empty()
84 }
85}
86
87#[derive(Copy, Clone)]
88pub struct Span(imp::Span);
89
90impl Default for Span {
91 fn default() -> Span {
92 Span(imp::Span::default())
93 }
94}
95
96impl Span {
97 pub fn call_site() -> Span {
98 Span(imp::Span::call_site())
99 }
100}
101
102#[derive(Clone)]
103pub struct TokenTree {
104 pub span: Span,
105 pub kind: TokenKind,
106}
107
Alex Crichton44bffbc2017-05-19 17:51:59 -0700108impl fmt::Display for TokenTree {
109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 TokenStream::from(self.clone()).fmt(f)
111 }
112}
113
114#[derive(Clone)]
115pub enum TokenKind {
116 Sequence(Delimiter, TokenStream),
117 Word(Symbol),
118 Op(char, OpKind),
119 Literal(Literal),
120}
121
122#[derive(Copy, Clone)]
123pub enum Delimiter {
124 Parenthesis,
125 Brace,
126 Bracket,
127 None,
128}
129
130#[derive(Copy, Clone)]
131pub struct Symbol(imp::Symbol);
132
133impl<'a> From<&'a str> for Symbol {
134 fn from(string: &'a str) -> Symbol {
135 Symbol(string.into())
136 }
137}
138
Alex Crichton852d53d2017-05-19 19:25:08 -0700139impl From<String> for Symbol {
140 fn from(string: String) -> Symbol {
141 Symbol(string[..].into())
142 }
143}
144
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700145impl Symbol {
Alex Crichton43881252017-05-26 08:51:06 -0700146 pub fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700147 &self.0
148 }
149}
150
151#[derive(Copy, Clone)]
152pub enum OpKind {
153 Alone,
154 Joint,
155}
156
157#[derive(Clone)]
158pub struct Literal(imp::Literal);
159
160impl fmt::Display for Literal {
161 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162 self.0.fmt(f)
163 }
164}
165
Alex Crichton852d53d2017-05-19 19:25:08 -0700166impl Literal {
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700167 pub fn byte_char(b: u8) -> Literal {
168 Literal(imp::Literal::byte_char(b))
Alex Crichton76a5cc82017-05-23 07:01:44 -0700169 }
170
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700171 pub fn byte_string(s: &[u8]) -> Literal {
172 Literal(imp::Literal::byte_string(s))
Alex Crichton852d53d2017-05-19 19:25:08 -0700173 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700174
175 pub fn doccomment(s: &str) -> Literal {
176 Literal(imp::Literal::doccomment(s))
177 }
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700178
179 pub fn float(s: &str) -> Literal {
180 Literal(imp::Literal::float(s))
181 }
182
183 pub fn integer(s: &str) -> Literal {
184 Literal(imp::Literal::integer(s))
185 }
Alex Crichton31316622017-05-26 12:54:47 -0700186
187 pub fn raw_string(s: &str, pounds: usize) -> Literal {
188 Literal(imp::Literal::raw_string(s, pounds))
189 }
190
191 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
192 Literal(imp::Literal::raw_byte_string(s, pounds))
193 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700194}
195
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700196macro_rules! froms {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700197 ($($t:ty,)*) => {$(
198 impl<'a> From<$t> for Literal {
199 fn from(t: $t) -> Literal {
200 Literal(t.into())
201 }
202 }
203 )*}
204}
205
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700206froms! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700207 u8, u16, u32, u64, usize,
208 i8, i16, i32, i64, isize,
Alex Crichton46060dc2017-05-25 07:41:30 -0700209 f32, f64, char, &'a str,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700210}
211
212pub struct TokenIter(imp::TokenIter);
213
214impl Iterator for TokenIter {
215 type Item = TokenTree;
216
217 fn next(&mut self) -> Option<TokenTree> {
218 self.0.next()
219 }
220}