blob: f504069f0a2db92c737adde3f603837b003af8b8 [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;
8use std::ops;
9use std::str::FromStr;
10use std::iter::FromIterator;
11
12#[path = "stable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070013#[cfg(not(feature = "unstable"))]
Alex Crichtonb15c6352017-05-19 19:36:36 -070014mod imp;
15#[path = "unstable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070016#[cfg(feature = "unstable")]
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
Alex Crichton44bffbc2017-05-19 17:51:59 -070060impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
61 fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
62 TokenStream(streams.into_iter().map(|t| t.into().0).collect())
63 }
64}
65
66impl IntoIterator for TokenStream {
67 type Item = TokenTree;
68 type IntoIter = TokenIter;
69
70 fn into_iter(self) -> TokenIter {
71 TokenIter(self.0.into_iter())
72 }
73}
74
75impl TokenStream {
76 pub fn empty() -> TokenStream {
77 TokenStream(imp::TokenStream::empty())
78 }
79
80 pub fn is_empty(&self) -> bool {
81 self.0.is_empty()
82 }
83}
84
85#[derive(Copy, Clone)]
86pub struct Span(imp::Span);
87
88impl Default for Span {
89 fn default() -> Span {
90 Span(imp::Span::default())
91 }
92}
93
94impl Span {
95 pub fn call_site() -> Span {
96 Span(imp::Span::call_site())
97 }
98}
99
100#[derive(Clone)]
101pub struct TokenTree {
102 pub span: Span,
103 pub kind: TokenKind,
104}
105
Alex Crichton44bffbc2017-05-19 17:51:59 -0700106impl fmt::Display for TokenTree {
107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108 TokenStream::from(self.clone()).fmt(f)
109 }
110}
111
112#[derive(Clone)]
113pub enum TokenKind {
114 Sequence(Delimiter, TokenStream),
115 Word(Symbol),
116 Op(char, OpKind),
117 Literal(Literal),
118}
119
120#[derive(Copy, Clone)]
121pub enum Delimiter {
122 Parenthesis,
123 Brace,
124 Bracket,
125 None,
126}
127
128#[derive(Copy, Clone)]
129pub struct Symbol(imp::Symbol);
130
131impl<'a> From<&'a str> for Symbol {
132 fn from(string: &'a str) -> Symbol {
133 Symbol(string.into())
134 }
135}
136
Alex Crichton852d53d2017-05-19 19:25:08 -0700137impl From<String> for Symbol {
138 fn from(string: String) -> Symbol {
139 Symbol(string[..].into())
140 }
141}
142
Alex Crichton44bffbc2017-05-19 17:51:59 -0700143impl ops::Deref for Symbol {
144 type Target = str;
145
146 fn deref(&self) -> &str {
147 &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 Crichton76a5cc82017-05-23 07:01:44 -0700167 pub fn bytechar(b: u8) -> Literal {
168 Literal(imp::Literal::bytechar(b))
169 }
170
Alex Crichton852d53d2017-05-19 19:25:08 -0700171 pub fn bytestring(s: &[u8]) -> Literal {
172 Literal(imp::Literal::bytestring(s))
173 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700174
175 pub fn doccomment(s: &str) -> Literal {
176 Literal(imp::Literal::doccomment(s))
177 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700178}
179
Alex Crichton44bffbc2017-05-19 17:51:59 -0700180macro_rules! tys {
181 ($($t:ty,)*) => {$(
182 impl<'a> From<$t> for Literal {
183 fn from(t: $t) -> Literal {
184 Literal(t.into())
185 }
186 }
187 )*}
188}
189
190tys! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700191 u8, u16, u32, u64, usize,
192 i8, i16, i32, i64, isize,
193 f32, f64, char, &'a str, bool,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700194}
195
196pub struct TokenIter(imp::TokenIter);
197
198impl Iterator for TokenIter {
199 type Item = TokenTree;
200
201 fn next(&mut self) -> Option<TokenTree> {
202 self.0.next()
203 }
204}