blob: 4667e7f845d407859568994a67e5ffd8dffc97d6 [file] [log] [blame]
Alex Crichton954046c2017-05-30 21:49:42 -07001//! Discrete tokens that can be parsed out by synom.
2//!
3//! This module contains a number of useful tokens like `+=` and `/` along with
4//! keywords like `crate` and such. These structures are used to track the spans
5//! of these tokens and all implment the `ToTokens` and `Synom` traits when the
6//! corresponding feature is activated.
7
Alex Crichton7b9e02f2017-05-30 15:54:33 -07008use span::Span;
9
10macro_rules! tokens {
11 (
12 ops: {
13 $(($($op:tt)*),)*
14 }
15 delim: {
16 $(($($delim:tt)*),)*
17 }
18 syms: {
19 $(($($sym:tt)*),)*
20 }
21 ) => (
22 $(op! { $($op)* })*
23 $(delim! { $($delim)* })*
24 $(sym! { $($sym)* })*
25 )
26}
27
28macro_rules! op {
29 (pub struct $name:ident($($contents:tt)*) => $s:expr) => {
30 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
31 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
32 #[derive(Default)]
33 pub struct $name(pub $($contents)*);
34
35 #[cfg(feature = "printing")]
36 impl ::quote::ToTokens for $name {
37 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
38 printing::op($s, &self.0, tokens);
39 }
40 }
41
42 #[cfg(feature = "parsing")]
43 impl ::Synom for $name {
Michael Layzell760fd662017-05-31 22:46:05 -040044 fn parse(tokens: $crate::Cursor) -> $crate::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070045 parsing::op($s, tokens, $name)
46 }
47 }
48 }
49}
50
51macro_rules! sym {
52 (pub struct $name:ident => $s:expr) => {
53 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
54 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
55 #[derive(Default)]
56 pub struct $name(pub Span);
57
58 #[cfg(feature = "printing")]
59 impl ::quote::ToTokens for $name {
60 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
61 printing::sym($s, &self.0, tokens);
62 }
63 }
64
65 #[cfg(feature = "parsing")]
66 impl ::Synom for $name {
Michael Layzell760fd662017-05-31 22:46:05 -040067 fn parse(tokens: $crate::Cursor) -> $crate::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070068 parsing::sym($s, tokens, $name)
69 }
70 }
71 }
72}
73
74macro_rules! delim {
75 (pub struct $name:ident => $s:expr) => {
76 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
77 #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
78 #[derive(Default)]
79 pub struct $name(pub Span);
80
81 impl $name {
82 #[cfg(feature = "printing")]
83 pub fn surround<F>(&self,
84 tokens: &mut ::quote::Tokens,
85 f: F)
86 where F: FnOnce(&mut ::quote::Tokens)
87 {
88 printing::delim($s, &self.0, tokens, f);
89 }
90
91 #[cfg(feature = "parsing")]
Michael Layzell760fd662017-05-31 22:46:05 -040092 pub fn parse<F, R>(tokens: $crate::Cursor, f: F) -> $crate::PResult<(R, $name)>
93 where F: FnOnce($crate::Cursor) -> $crate::PResult<R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -070094 {
95 parsing::delim($s, tokens, $name, f)
96 }
97 }
98 }
99}
100
101tokens! {
102 ops: {
103 (pub struct Add([Span; 1]) => "+"),
104 (pub struct AddEq([Span; 2]) => "+="),
105 (pub struct And([Span; 1]) => "&"),
106 (pub struct AndAnd([Span; 2]) => "&&"),
107 (pub struct AndEq([Span; 2]) => "&="),
108 (pub struct At([Span; 1]) => "@"),
109 (pub struct Bang([Span; 1]) => "!"),
110 (pub struct Caret([Span; 1]) => "^"),
111 (pub struct CaretEq([Span; 2]) => "^="),
112 (pub struct Colon([Span; 1]) => ":"),
113 (pub struct Colon2([Span; 2]) => "::"),
114 (pub struct Comma([Span; 1]) => ","),
115 (pub struct Div([Span; 1]) => "/"),
116 (pub struct DivEq([Span; 2]) => "/="),
117 (pub struct Dot([Span; 1]) => "."),
118 (pub struct Dot2([Span; 2]) => ".."),
119 (pub struct Dot3([Span; 3]) => "..."),
120 (pub struct Eq([Span; 1]) => "="),
121 (pub struct EqEq([Span; 2]) => "=="),
122 (pub struct Ge([Span; 2]) => ">="),
123 (pub struct Gt([Span; 1]) => ">"),
124 (pub struct Le([Span; 2]) => "<="),
125 (pub struct Lt([Span; 1]) => "<"),
126 (pub struct MulEq([Span; 2]) => "*="),
127 (pub struct Ne([Span; 2]) => "!="),
128 (pub struct Or([Span; 1]) => "|"),
129 (pub struct OrEq([Span; 2]) => "|="),
130 (pub struct OrOr([Span; 2]) => "||"),
131 (pub struct Pound([Span; 1]) => "#"),
132 (pub struct Question([Span; 1]) => "?"),
133 (pub struct RArrow([Span; 2]) => "->"),
134 (pub struct Rem([Span; 1]) => "%"),
135 (pub struct RemEq([Span; 2]) => "%="),
136 (pub struct Rocket([Span; 2]) => "=>"),
137 (pub struct Semi([Span; 1]) => ";"),
138 (pub struct Shl([Span; 2]) => "<<"),
139 (pub struct ShlEq([Span; 3]) => "<<="),
140 (pub struct Shr([Span; 2]) => ">>"),
141 (pub struct ShrEq([Span; 3]) => ">>="),
142 (pub struct Star([Span; 1]) => "*"),
143 (pub struct Sub([Span; 1]) => "-"),
144 (pub struct SubEq([Span; 2]) => "-="),
145 (pub struct Underscore([Span; 1]) => "_"),
146 }
147 delim: {
148 (pub struct Brace => "{"),
149 (pub struct Bracket => "["),
150 (pub struct Paren => "("),
151 }
152 syms: {
153 (pub struct As => "as"),
Alex Crichton954046c2017-05-30 21:49:42 -0700154 (pub struct Box_ => "box"),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700155 (pub struct Break => "break"),
Alex Crichton954046c2017-05-30 21:49:42 -0700156 (pub struct CapSelf => "Self"),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700157 (pub struct Catch => "catch"),
158 (pub struct Const => "const"),
159 (pub struct Continue => "continue"),
160 (pub struct Crate => "crate"),
Alex Crichton954046c2017-05-30 21:49:42 -0700161 (pub struct Default_ => "default"),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700162 (pub struct Do => "do"),
163 (pub struct Else => "else"),
164 (pub struct Enum => "enum"),
165 (pub struct Extern => "extern"),
Alex Crichton954046c2017-05-30 21:49:42 -0700166 (pub struct Fn_ => "fn"),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700167 (pub struct For => "for"),
168 (pub struct If => "if"),
169 (pub struct Impl => "impl"),
170 (pub struct In => "in"),
171 (pub struct Let => "let"),
172 (pub struct Loop => "loop"),
173 (pub struct Match => "match"),
174 (pub struct Mod => "mod"),
175 (pub struct Move => "move"),
176 (pub struct Mut => "mut"),
177 (pub struct Pub => "pub"),
178 (pub struct Ref => "ref"),
179 (pub struct Return => "return"),
180 (pub struct Self_ => "self"),
181 (pub struct Static => "static"),
182 (pub struct Struct => "struct"),
Alex Crichton954046c2017-05-30 21:49:42 -0700183 (pub struct Super => "super"),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700184 (pub struct Trait => "trait"),
185 (pub struct Type => "type"),
186 (pub struct Union => "union"),
187 (pub struct Unsafe => "unsafe"),
188 (pub struct Use => "use"),
189 (pub struct Where => "where"),
190 (pub struct While => "while"),
191 }
192}
193
194#[cfg(feature = "parsing")]
195mod parsing {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400196 use proc_macro2::{Delimiter, OpKind};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700197
Michael Layzell760fd662017-05-31 22:46:05 -0400198 use {PResult, Cursor, parse_error};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700199 use span::Span;
200
201 pub trait FromSpans: Sized {
202 fn from_spans(spans: &[Span]) -> Self;
203 }
204
205 impl FromSpans for [Span; 1] {
206 fn from_spans(spans: &[Span]) -> Self {
207 [spans[0]]
208 }
209 }
210
211 impl FromSpans for [Span; 2] {
212 fn from_spans(spans: &[Span]) -> Self {
213 [spans[0], spans[1]]
214 }
215 }
216
217 impl FromSpans for [Span; 3] {
218 fn from_spans(spans: &[Span]) -> Self {
219 [spans[0], spans[1], spans[2]]
220 }
221 }
222
223 pub fn op<'a, T, R>(s: &str,
Michael Layzell0a1a6632017-06-02 18:07:43 -0400224 mut tokens: Cursor<'a>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700225 new: fn(T) -> R)
Michael Layzell760fd662017-05-31 22:46:05 -0400226 -> PResult<'a, R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700227 where T: FromSpans,
228 {
229 let mut spans = [Span::default(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700230 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700231 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700232
Alex Crichton954046c2017-05-30 21:49:42 -0700233 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400234 match tokens.op() {
235 Some((rest, span, c, kind)) if c == ch => {
236 if i != s.len() - 1 {
237 if kind != OpKind::Joint {
238 return parse_error();
239 }
240 }
241 *slot = Span(span);
242 tokens = rest;
Alex Crichton954046c2017-05-30 21:49:42 -0700243 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400244 _ => return parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700245 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700246 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400247 Ok((tokens, new(T::from_spans(&spans))))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700248 }
249
250 pub fn sym<'a, T>(sym: &str,
Michael Layzell760fd662017-05-31 22:46:05 -0400251 tokens: Cursor<'a>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700252 new: fn(Span) -> T)
Michael Layzell760fd662017-05-31 22:46:05 -0400253 -> PResult<'a, T>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700254 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400255 if let Some((rest, span, s)) = tokens.word() {
256 if s.as_str() == sym {
257 return Ok((rest, new(Span(span))));
258 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700259 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400260 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700261 }
262
263 pub fn delim<'a, F, R, T>(delim: &str,
Michael Layzell760fd662017-05-31 22:46:05 -0400264 tokens: Cursor<'a>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700265 new: fn(Span) -> T,
266 f: F)
Michael Layzell760fd662017-05-31 22:46:05 -0400267 -> PResult<'a, (R, T)>
268 where F: FnOnce(Cursor) -> PResult<R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700269 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400270 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700271 let delim = match delim {
272 "(" => Delimiter::Parenthesis,
273 "{" => Delimiter::Brace,
274 "[" => Delimiter::Bracket,
275 _ => panic!("unknown delimiter: {}", delim),
276 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700277
Michael Layzell0a1a6632017-06-02 18:07:43 -0400278 if let Some(seqinfo) = tokens.seq(delim) {
279 match f(seqinfo.inside) {
280 Ok((remaining, ret)) => {
281 if remaining.eof() {
282 return Ok((seqinfo.outside, (ret, new(Span(seqinfo.span)))));
283 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700284 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400285 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700286 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700287 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400288 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700289 }
290}
291
292#[cfg(feature = "printing")]
293mod printing {
294 use proc_macro2::{TokenTree, TokenKind, OpKind};
295 use quote::Tokens;
296
297 use span::Span;
298
299 pub fn op(s: &str, spans: &[Span], tokens: &mut Tokens) {
300 assert_eq!(s.len(), spans.len());
301
302 let mut chars = s.chars();
303 let mut spans = spans.iter();
304 let ch = chars.next_back().unwrap();
305 let span = spans.next_back().unwrap();
306 for (ch, span) in chars.zip(spans) {
307 tokens.append(TokenTree {
308 span: span.0,
309 kind: TokenKind::Op(ch, OpKind::Joint),
310 });
311 }
312
313 tokens.append(TokenTree {
314 span: span.0,
315 kind: TokenKind::Op(ch, OpKind::Alone),
316 });
317 }
318
319 pub fn sym(s: &str, span: &Span, tokens: &mut Tokens) {
320 tokens.append(TokenTree {
321 span: span.0,
322 kind: TokenKind::Word(s.into()),
323 });
324 }
325
326 pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F)
327 where F: FnOnce(&mut Tokens)
328 {
329 tokens.append_delimited(s, span.0, f)
330 }
331}