blob: d0195559d1945851f1991236fe46b79a92865b1c [file] [log] [blame]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07001#![cfg_attr(feature = "unstable", feature(proc_macro))]
2
Alex Crichton44bffbc2017-05-19 17:51:59 -07003extern crate proc_macro;
4
Alex Crichtone14e8fd2017-05-23 07:02:54 -07005#[cfg(not(feature = "unstable"))]
David Tolnayb1032662017-05-31 15:52:28 -07006extern crate unicode_xid;
Alex Crichton44bffbc2017-05-19 17:51:59 -07007
8use std::fmt;
Alex Crichton44bffbc2017-05-19 17:51:59 -07009use std::str::FromStr;
10use std::iter::FromIterator;
11
David Tolnayb1032662017-05-31 15:52:28 -070012#[macro_use]
13#[cfg(not(feature = "unstable"))]
14mod strnom;
15
Alex Crichton44bffbc2017-05-19 17:51:59 -070016#[path = "stable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070017#[cfg(not(feature = "unstable"))]
Alex Crichtonb15c6352017-05-19 19:36:36 -070018mod imp;
19#[path = "unstable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070020#[cfg(feature = "unstable")]
Alex Crichton44bffbc2017-05-19 17:51:59 -070021mod imp;
22
David Tolnaycb1b85f2017-06-03 16:40:35 -070023#[macro_use]
24mod macros;
25
26#[derive(Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070027pub struct TokenStream(imp::TokenStream);
28
Alex Crichton44bffbc2017-05-19 17:51:59 -070029pub struct LexError(imp::LexError);
30
31impl FromStr for TokenStream {
32 type Err = LexError;
33
34 fn from_str(src: &str) -> Result<TokenStream, LexError> {
35 match src.parse() {
36 Ok(e) => Ok(TokenStream(e)),
37 Err(e) => Err(LexError(e)),
38 }
39 }
40}
41
Alex Crichton44bffbc2017-05-19 17:51:59 -070042impl 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
David Tolnaycb1b85f2017-06-03 16:40:35 -070085#[derive(Copy, Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070086pub 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
David Tolnay977f8282017-05-31 17:41:33 -0700100#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700101pub 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
David Tolnay977f8282017-05-31 17:41:33 -0700112#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700113pub enum TokenKind {
114 Sequence(Delimiter, TokenStream),
115 Word(Symbol),
116 Op(char, OpKind),
117 Literal(Literal),
118}
119
Michael Layzell5372f4b2017-06-02 10:29:31 -0400120#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700121pub enum Delimiter {
122 Parenthesis,
123 Brace,
124 Bracket,
125 None,
126}
127
David Tolnaycb1b85f2017-06-03 16:40:35 -0700128#[derive(Copy, Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700129pub 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 Crichton9c2fb0a2017-05-26 08:49:31 -0700143impl Symbol {
Alex Crichton43881252017-05-26 08:51:06 -0700144 pub fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700145 &self.0
146 }
147}
148
Michael Layzell5372f4b2017-06-02 10:29:31 -0400149#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700150pub enum OpKind {
151 Alone,
152 Joint,
153}
154
David Tolnaycb1b85f2017-06-03 16:40:35 -0700155#[derive(Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700156pub struct Literal(imp::Literal);
157
Alex Crichton852d53d2017-05-19 19:25:08 -0700158impl Literal {
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700159 pub fn byte_char(b: u8) -> Literal {
160 Literal(imp::Literal::byte_char(b))
Alex Crichton76a5cc82017-05-23 07:01:44 -0700161 }
162
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700163 pub fn byte_string(s: &[u8]) -> Literal {
164 Literal(imp::Literal::byte_string(s))
Alex Crichton852d53d2017-05-19 19:25:08 -0700165 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700166
167 pub fn doccomment(s: &str) -> Literal {
168 Literal(imp::Literal::doccomment(s))
169 }
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700170
171 pub fn float(s: &str) -> Literal {
172 Literal(imp::Literal::float(s))
173 }
174
175 pub fn integer(s: &str) -> Literal {
176 Literal(imp::Literal::integer(s))
177 }
Alex Crichton31316622017-05-26 12:54:47 -0700178
179 pub fn raw_string(s: &str, pounds: usize) -> Literal {
180 Literal(imp::Literal::raw_string(s, pounds))
181 }
182
183 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
184 Literal(imp::Literal::raw_byte_string(s, pounds))
185 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700186}
187
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700188macro_rules! froms {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700189 ($($t:ty,)*) => {$(
190 impl<'a> From<$t> for Literal {
191 fn from(t: $t) -> Literal {
192 Literal(t.into())
193 }
194 }
195 )*}
196}
197
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700198froms! {
Alex Crichton852d53d2017-05-19 19:25:08 -0700199 u8, u16, u32, u64, usize,
200 i8, i16, i32, i64, isize,
Alex Crichton46060dc2017-05-25 07:41:30 -0700201 f32, f64, char, &'a str,
Alex Crichton44bffbc2017-05-19 17:51:59 -0700202}
203
Alex Crichton44bffbc2017-05-19 17:51:59 -0700204pub struct TokenIter(imp::TokenIter);
205
206impl Iterator for TokenIter {
207 type Item = TokenTree;
208
209 fn next(&mut self) -> Option<TokenTree> {
210 self.0.next()
211 }
212}
David Tolnaycb1b85f2017-06-03 16:40:35 -0700213
214forward_fmt!(Debug for LexError);
215forward_fmt!(Debug for Literal);
216forward_fmt!(Debug for Span);
217forward_fmt!(Debug for Symbol);
218forward_fmt!(Debug for TokenIter);
219forward_fmt!(Debug for TokenStream);
220forward_fmt!(Display for Literal);
221forward_fmt!(Display for TokenStream);