blob: 1587d9f6f89bd2e05ce48e9cf303ec6dd781fd8f [file] [log] [blame]
Alex Crichton44bffbc2017-05-19 17:51:59 -07001extern crate proc_macro;
2
3#[macro_use]
Alex Crichtone14e8fd2017-05-23 07:02:54 -07004#[cfg(not(feature = "unstable"))]
Alex Crichton44bffbc2017-05-19 17:51:59 -07005extern crate synom;
6
7use std::fmt;
Alex Crichton44bffbc2017-05-19 17:51:59 -07008use std::str::FromStr;
9use std::iter::FromIterator;
10
11#[path = "stable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070012#[cfg(not(feature = "unstable"))]
Alex Crichtonb15c6352017-05-19 19:36:36 -070013mod imp;
14#[path = "unstable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070015#[cfg(feature = "unstable")]
Alex Crichton44bffbc2017-05-19 17:51:59 -070016mod imp;
17
David Tolnay977f8282017-05-31 17:41:33 -070018#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070019pub struct TokenStream(imp::TokenStream);
20
21#[derive(Debug)]
22pub struct LexError(imp::LexError);
23
24impl FromStr for TokenStream {
25 type Err = LexError;
26
27 fn from_str(src: &str) -> Result<TokenStream, LexError> {
28 match src.parse() {
29 Ok(e) => Ok(TokenStream(e)),
30 Err(e) => Err(LexError(e)),
31 }
32 }
33}
34
35impl fmt::Display for TokenStream {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 self.0.fmt(f)
38 }
39}
40
41impl From<proc_macro::TokenStream> for TokenStream {
42 fn from(inner: proc_macro::TokenStream) -> TokenStream {
43 TokenStream(inner.into())
44 }
45}
46
47impl From<TokenStream> for proc_macro::TokenStream {
48 fn from(inner: TokenStream) -> proc_macro::TokenStream {
49 inner.0.into()
50 }
51}
52
53impl From<TokenTree> for TokenStream {
54 fn from(tree: TokenTree) -> TokenStream {
55 TokenStream(tree.into())
56 }
57}
58
Alex Crichton44bffbc2017-05-19 17:51:59 -070059impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
60 fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
61 TokenStream(streams.into_iter().map(|t| t.into().0).collect())
62 }
63}
64
65impl IntoIterator for TokenStream {
66 type Item = TokenTree;
67 type IntoIter = TokenIter;
68
69 fn into_iter(self) -> TokenIter {
70 TokenIter(self.0.into_iter())
71 }
72}
73
74impl TokenStream {
75 pub fn empty() -> TokenStream {
76 TokenStream(imp::TokenStream::empty())
77 }
78
79 pub fn is_empty(&self) -> bool {
80 self.0.is_empty()
81 }
82}
83
David Tolnay977f8282017-05-31 17:41:33 -070084#[derive(Copy, Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070085pub struct Span(imp::Span);
86
87impl Default for Span {
88 fn default() -> Span {
89 Span(imp::Span::default())
90 }
91}
92
93impl Span {
94 pub fn call_site() -> Span {
95 Span(imp::Span::call_site())
96 }
97}
98
David Tolnay977f8282017-05-31 17:41:33 -070099#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700100pub struct TokenTree {
101 pub span: Span,
102 pub kind: TokenKind,
103}
104
Alex Crichton44bffbc2017-05-19 17:51:59 -0700105impl fmt::Display for TokenTree {
106 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107 TokenStream::from(self.clone()).fmt(f)
108 }
109}
110
David Tolnay977f8282017-05-31 17:41:33 -0700111#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700112pub enum TokenKind {
113 Sequence(Delimiter, TokenStream),
114 Word(Symbol),
115 Op(char, OpKind),
116 Literal(Literal),
117}
118
David Tolnay977f8282017-05-31 17:41:33 -0700119#[derive(Copy, Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700120pub enum Delimiter {
121 Parenthesis,
122 Brace,
123 Bracket,
124 None,
125}
126
David Tolnay977f8282017-05-31 17:41:33 -0700127#[derive(Copy, Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700128pub struct Symbol(imp::Symbol);
129
130impl<'a> From<&'a str> for Symbol {
131 fn from(string: &'a str) -> Symbol {
132 Symbol(string.into())
133 }
134}
135
Alex Crichton852d53d2017-05-19 19:25:08 -0700136impl From<String> for Symbol {
137 fn from(string: String) -> Symbol {
138 Symbol(string[..].into())
139 }
140}
141
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700142impl Symbol {
Alex Crichton43881252017-05-26 08:51:06 -0700143 pub fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700144 &self.0
145 }
146}
147
David Tolnay977f8282017-05-31 17:41:33 -0700148#[derive(Copy, Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700149pub enum OpKind {
150 Alone,
151 Joint,
152}
153
David Tolnay977f8282017-05-31 17:41:33 -0700154#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700155pub struct Literal(imp::Literal);
156
157impl fmt::Display for Literal {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159 self.0.fmt(f)
160 }
161}
162
Alex Crichton852d53d2017-05-19 19:25:08 -0700163impl Literal {
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700164 pub fn byte_char(b: u8) -> Literal {
165 Literal(imp::Literal::byte_char(b))
Alex Crichton76a5cc82017-05-23 07:01:44 -0700166 }
167
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700168 pub fn byte_string(s: &[u8]) -> Literal {
169 Literal(imp::Literal::byte_string(s))
Alex Crichton852d53d2017-05-19 19:25:08 -0700170 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700171
172 pub fn doccomment(s: &str) -> Literal {
173 Literal(imp::Literal::doccomment(s))
174 }
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700175
176 pub fn float(s: &str) -> Literal {
177 Literal(imp::Literal::float(s))
178 }
179
180 pub fn integer(s: &str) -> Literal {
181 Literal(imp::Literal::integer(s))
182 }
Alex Crichton31316622017-05-26 12:54:47 -0700183
184 pub fn raw_string(s: &str, pounds: usize) -> Literal {
185 Literal(imp::Literal::raw_string(s, pounds))
186 }
187
188 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
189 Literal(imp::Literal::raw_byte_string(s, pounds))
190 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700191}
192
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700193macro_rules! froms {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700194 ($($t:ty,)*) => {$(
195 impl<'a> From<$t> for Literal {
196 fn from(t: $t) -> Literal {
197 Literal(t.into())
198 }
199 }
200 )*}
201}
202
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700203froms! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700204 u8, u16, u32, u64, usize,
205 i8, i16, i32, i64, isize,
Alex Crichton46060dc2017-05-25 07:41:30 -0700206 f32, f64, char, &'a str,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700207}
208
David Tolnay977f8282017-05-31 17:41:33 -0700209#[derive(Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700210pub struct TokenIter(imp::TokenIter);
211
212impl Iterator for TokenIter {
213 type Item = TokenTree;
214
215 fn next(&mut self) -> Option<TokenTree> {
216 self.0.next()
217 }
218}