blob: 541e07000df591b463e5caab344890884b358593 [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
18#[derive(Clone)]
19pub 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
84#[derive(Copy, Clone)]
85pub 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
99#[derive(Clone)]
100pub 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
111#[derive(Clone)]
112pub enum TokenKind {
113 Sequence(Delimiter, TokenStream),
114 Word(Symbol),
115 Op(char, OpKind),
116 Literal(Literal),
117}
118
119#[derive(Copy, Clone)]
120pub enum Delimiter {
121 Parenthesis,
122 Brace,
123 Bracket,
124 None,
125}
126
127#[derive(Copy, Clone)]
128pub 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 {
143 fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700144 &self.0
145 }
146}
147
148#[derive(Copy, Clone)]
149pub enum OpKind {
150 Alone,
151 Joint,
152}
153
154#[derive(Clone)]
155pub 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 Crichton852d53d2017-05-19 19:25:08 -0700183}
184
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700185macro_rules! froms {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700186 ($($t:ty,)*) => {$(
187 impl<'a> From<$t> for Literal {
188 fn from(t: $t) -> Literal {
189 Literal(t.into())
190 }
191 }
192 )*}
193}
194
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700195froms! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700196 u8, u16, u32, u64, usize,
197 i8, i16, i32, i64, isize,
Alex Crichton46060dc2017-05-25 07:41:30 -0700198 f32, f64, char, &'a str,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700199}
200
201pub struct TokenIter(imp::TokenIter);
202
203impl Iterator for TokenIter {
204 type Item = TokenTree;
205
206 fn next(&mut self) -> Option<TokenTree> {
207 self.0.next()
208 }
209}