blob: e1305eb70705a38fdda816b581001eea16d1e8d1 [file] [log] [blame]
Alex Crichton44bffbc2017-05-19 17:51:59 -07001extern crate proc_macro;
2
3#[macro_use]
Alex Crichtonb15c6352017-05-19 19:36:36 -07004#[cfg(feature = "stable")]
Alex Crichton44bffbc2017-05-19 17:51:59 -07005extern crate synom;
6
7use std::fmt;
8use std::ops;
9use std::str::FromStr;
10use std::iter::FromIterator;
11
12#[path = "stable.rs"]
Alex Crichtonb15c6352017-05-19 19:36:36 -070013#[cfg(feature = "stable")]
14mod imp;
15#[path = "unstable.rs"]
16#[cfg(not(feature = "stable"))]
Alex Crichton44bffbc2017-05-19 17:51:59 -070017mod imp;
18
19#[derive(Clone)]
20pub struct TokenStream(imp::TokenStream);
21
22#[derive(Debug)]
23pub struct LexError(imp::LexError);
24
25impl FromStr for TokenStream {
26 type Err = LexError;
27
28 fn from_str(src: &str) -> Result<TokenStream, LexError> {
29 match src.parse() {
30 Ok(e) => Ok(TokenStream(e)),
31 Err(e) => Err(LexError(e)),
32 }
33 }
34}
35
36impl fmt::Display for TokenStream {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 self.0.fmt(f)
39 }
40}
41
42impl 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
60impl From<TokenKind> for TokenStream {
61 fn from(kind: TokenKind) -> TokenStream {
62 TokenTree::from(kind).into()
63 }
64}
65
66impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
67 fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
68 TokenStream(streams.into_iter().map(|t| t.into().0).collect())
69 }
70}
71
72impl IntoIterator for TokenStream {
73 type Item = TokenTree;
74 type IntoIter = TokenIter;
75
76 fn into_iter(self) -> TokenIter {
77 TokenIter(self.0.into_iter())
78 }
79}
80
81impl TokenStream {
82 pub fn empty() -> TokenStream {
83 TokenStream(imp::TokenStream::empty())
84 }
85
86 pub fn is_empty(&self) -> bool {
87 self.0.is_empty()
88 }
89}
90
91#[derive(Copy, Clone)]
92pub struct Span(imp::Span);
93
94impl Default for Span {
95 fn default() -> Span {
96 Span(imp::Span::default())
97 }
98}
99
100impl Span {
101 pub fn call_site() -> Span {
102 Span(imp::Span::call_site())
103 }
104}
105
106#[derive(Clone)]
107pub struct TokenTree {
108 pub span: Span,
109 pub kind: TokenKind,
110}
111
112impl From<TokenKind> for TokenTree {
113 fn from(kind: TokenKind) -> TokenTree {
114 TokenTree {
115 span: Span::default(),
116 kind: kind,
117 }
118 }
119}
120
Alex Crichton3d3e3d72017-05-19 19:50:09 -0700121impl From<Symbol> for TokenTree {
122 fn from(sym: Symbol) -> TokenTree {
123 TokenKind::Word(sym).into()
124 }
125}
126
127impl From<Literal> for TokenTree {
128 fn from(lit: Literal) -> TokenTree {
129 TokenKind::Literal(lit).into()
130 }
131}
132
Alex Crichton44bffbc2017-05-19 17:51:59 -0700133impl fmt::Display for TokenTree {
134 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135 TokenStream::from(self.clone()).fmt(f)
136 }
137}
138
139#[derive(Clone)]
140pub enum TokenKind {
141 Sequence(Delimiter, TokenStream),
142 Word(Symbol),
143 Op(char, OpKind),
144 Literal(Literal),
145}
146
147#[derive(Copy, Clone)]
148pub enum Delimiter {
149 Parenthesis,
150 Brace,
151 Bracket,
152 None,
153}
154
155#[derive(Copy, Clone)]
156pub struct Symbol(imp::Symbol);
157
158impl<'a> From<&'a str> for Symbol {
159 fn from(string: &'a str) -> Symbol {
160 Symbol(string.into())
161 }
162}
163
Alex Crichton852d53d2017-05-19 19:25:08 -0700164impl From<String> for Symbol {
165 fn from(string: String) -> Symbol {
166 Symbol(string[..].into())
167 }
168}
169
Alex Crichton44bffbc2017-05-19 17:51:59 -0700170impl ops::Deref for Symbol {
171 type Target = str;
172
173 fn deref(&self) -> &str {
174 &self.0
175 }
176}
177
178#[derive(Copy, Clone)]
179pub enum OpKind {
180 Alone,
181 Joint,
182}
183
184#[derive(Clone)]
185pub struct Literal(imp::Literal);
186
187impl fmt::Display for Literal {
188 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189 self.0.fmt(f)
190 }
191}
192
Alex Crichton852d53d2017-05-19 19:25:08 -0700193impl Literal {
Alex Crichton76a5cc82017-05-23 07:01:44 -0700194 pub fn bytechar(b: u8) -> Literal {
195 Literal(imp::Literal::bytechar(b))
196 }
197
Alex Crichton852d53d2017-05-19 19:25:08 -0700198 pub fn bytestring(s: &[u8]) -> Literal {
199 Literal(imp::Literal::bytestring(s))
200 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700201
202 pub fn doccomment(s: &str) -> Literal {
203 Literal(imp::Literal::doccomment(s))
204 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700205}
206
Alex Crichton44bffbc2017-05-19 17:51:59 -0700207macro_rules! tys {
208 ($($t:ty,)*) => {$(
209 impl<'a> From<$t> for Literal {
210 fn from(t: $t) -> Literal {
211 Literal(t.into())
212 }
213 }
214 )*}
215}
216
217tys! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700218 u8, u16, u32, u64, usize,
219 i8, i16, i32, i64, isize,
220 f32, f64, char, &'a str, bool,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700221}
222
223pub struct TokenIter(imp::TokenIter);
224
225impl Iterator for TokenIter {
226 type Item = TokenTree;
227
228 fn next(&mut self) -> Option<TokenTree> {
229 self.0.next()
230 }
231}