blob: 54cebd8c4637e4fe3fe527da60cefc85d9f0fc93 [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
121impl fmt::Display for TokenTree {
122 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123 TokenStream::from(self.clone()).fmt(f)
124 }
125}
126
127#[derive(Clone)]
128pub enum TokenKind {
129 Sequence(Delimiter, TokenStream),
130 Word(Symbol),
131 Op(char, OpKind),
132 Literal(Literal),
133}
134
135#[derive(Copy, Clone)]
136pub enum Delimiter {
137 Parenthesis,
138 Brace,
139 Bracket,
140 None,
141}
142
143#[derive(Copy, Clone)]
144pub struct Symbol(imp::Symbol);
145
146impl<'a> From<&'a str> for Symbol {
147 fn from(string: &'a str) -> Symbol {
148 Symbol(string.into())
149 }
150}
151
Alex Crichton852d53d2017-05-19 19:25:08 -0700152impl From<String> for Symbol {
153 fn from(string: String) -> Symbol {
154 Symbol(string[..].into())
155 }
156}
157
Alex Crichton44bffbc2017-05-19 17:51:59 -0700158impl ops::Deref for Symbol {
159 type Target = str;
160
161 fn deref(&self) -> &str {
162 &self.0
163 }
164}
165
166#[derive(Copy, Clone)]
167pub enum OpKind {
168 Alone,
169 Joint,
170}
171
172#[derive(Clone)]
173pub struct Literal(imp::Literal);
174
175impl fmt::Display for Literal {
176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
177 self.0.fmt(f)
178 }
179}
180
Alex Crichton852d53d2017-05-19 19:25:08 -0700181impl Literal {
182 pub fn bytestring(s: &[u8]) -> Literal {
183 Literal(imp::Literal::bytestring(s))
184 }
185}
186
Alex Crichton44bffbc2017-05-19 17:51:59 -0700187macro_rules! tys {
188 ($($t:ty,)*) => {$(
189 impl<'a> From<$t> for Literal {
190 fn from(t: $t) -> Literal {
191 Literal(t.into())
192 }
193 }
194 )*}
195}
196
197tys! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700198 u8, u16, u32, u64, usize,
199 i8, i16, i32, i64, isize,
200 f32, f64, char, &'a str, bool,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700201}
202
203pub struct TokenIter(imp::TokenIter);
204
205impl Iterator for TokenIter {
206 type Item = TokenTree;
207
208 fn next(&mut self) -> Option<TokenTree> {
209 self.0.next()
210 }
211}