blob: dbac5c9ce0676f8ac8e775146c0a056c0a641625 [file] [log] [blame]
David Tolnay1218e122017-06-01 11:13:45 -07001//! Adapted from [`nom`](https://github.com/Geal/nom).
David Tolnayb1032662017-05-31 15:52:28 -07002
Nika Layzellf8d5f212017-12-11 14:07:02 -05003use std::str::{Chars, CharIndices, Bytes};
4
David Tolnayb1032662017-05-31 15:52:28 -07005use unicode_xid::UnicodeXID;
6
David Tolnay1218e122017-06-01 11:13:45 -07007use imp::LexError;
David Tolnayb1032662017-05-31 15:52:28 -07008
Nika Layzellf8d5f212017-12-11 14:07:02 -05009#[derive(Copy, Clone, Eq, PartialEq)]
10pub struct Cursor<'a> {
11 pub rest: &'a str,
David Tolnay1ebe3972018-01-02 20:14:20 -080012 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -050013 pub off: u32,
14}
David Tolnay1218e122017-06-01 11:13:45 -070015
Nika Layzellf8d5f212017-12-11 14:07:02 -050016impl<'a> Cursor<'a> {
David Tolnay1ebe3972018-01-02 20:14:20 -080017 #[cfg(not(procmacro2_semver_exempt))]
Nika Layzellf8d5f212017-12-11 14:07:02 -050018 pub fn advance(&self, amt: usize) -> Cursor<'a> {
19 Cursor {
20 rest: &self.rest[amt..],
David Tolnay79105e52017-12-31 11:03:04 -050021 }
22 }
David Tolnay1ebe3972018-01-02 20:14:20 -080023 #[cfg(procmacro2_semver_exempt)]
David Tolnay79105e52017-12-31 11:03:04 -050024 pub fn advance(&self, amt: usize) -> Cursor<'a> {
25 Cursor {
26 rest: &self.rest[amt..],
Nika Layzellf8d5f212017-12-11 14:07:02 -050027 off: self.off + (amt as u32),
28 }
29 }
30
31 pub fn find(&self, p: char) -> Option<usize> {
32 self.rest.find(p)
33 }
34
35 pub fn starts_with(&self, s: &str) -> bool {
36 self.rest.starts_with(s)
37 }
38
39 pub fn is_empty(&self) -> bool {
40 self.rest.is_empty()
41 }
42
43 pub fn len(&self) -> usize {
44 self.rest.len()
45 }
46
47 pub fn as_bytes(&self) -> &'a [u8] {
48 self.rest.as_bytes()
49 }
50
51 pub fn bytes(&self) -> Bytes<'a> {
52 self.rest.bytes()
53 }
54
55 pub fn chars(&self) -> Chars<'a> {
56 self.rest.chars()
57 }
58
59 pub fn char_indices(&self) -> CharIndices<'a> {
60 self.rest.char_indices()
61 }
62}
63
64pub type PResult<'a, O> = Result<(Cursor<'a>, O), LexError>;
65
66pub fn whitespace(input: Cursor) -> PResult<()> {
David Tolnayb1032662017-05-31 15:52:28 -070067 if input.is_empty() {
David Tolnay1218e122017-06-01 11:13:45 -070068 return Err(LexError);
David Tolnayb1032662017-05-31 15:52:28 -070069 }
70
71 let bytes = input.as_bytes();
72 let mut i = 0;
73 while i < bytes.len() {
Nika Layzellf8d5f212017-12-11 14:07:02 -050074 let s = input.advance(i);
David Tolnayb1032662017-05-31 15:52:28 -070075 if bytes[i] == b'/' {
76 if s.starts_with("//") && (!s.starts_with("///") || s.starts_with("////")) &&
77 !s.starts_with("//!") {
78 if let Some(len) = s.find('\n') {
79 i += len + 1;
80 continue;
81 }
82 break;
Alex Crichtonf7df57c2018-01-21 21:05:11 -080083 } else if s.starts_with("/**/") {
84 i += 4;
85 continue
David Tolnayb1032662017-05-31 15:52:28 -070086 } else if s.starts_with("/*") && (!s.starts_with("/**") || s.starts_with("/***")) &&
87 !s.starts_with("/*!") {
David Tolnay1218e122017-06-01 11:13:45 -070088 let (_, com) = block_comment(s)?;
89 i += com.len();
90 continue;
David Tolnayb1032662017-05-31 15:52:28 -070091 }
92 }
93 match bytes[i] {
94 b' ' | 0x09...0x0d => {
95 i += 1;
96 continue;
97 }
98 b if b <= 0x7f => {}
99 _ => {
100 let ch = s.chars().next().unwrap();
101 if is_whitespace(ch) {
102 i += ch.len_utf8();
103 continue;
104 }
105 }
106 }
107 return if i > 0 {
David Tolnay1218e122017-06-01 11:13:45 -0700108 Ok((s, ()))
David Tolnayb1032662017-05-31 15:52:28 -0700109 } else {
David Tolnay1218e122017-06-01 11:13:45 -0700110 Err(LexError)
David Tolnayb1032662017-05-31 15:52:28 -0700111 };
112 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500113 Ok((input.advance(input.len()), ()))
David Tolnayb1032662017-05-31 15:52:28 -0700114}
115
Nika Layzellf8d5f212017-12-11 14:07:02 -0500116pub fn block_comment(input: Cursor) -> PResult<&str> {
David Tolnayb1032662017-05-31 15:52:28 -0700117 if !input.starts_with("/*") {
David Tolnay1218e122017-06-01 11:13:45 -0700118 return Err(LexError);
David Tolnayb1032662017-05-31 15:52:28 -0700119 }
120
121 let mut depth = 0;
122 let bytes = input.as_bytes();
123 let mut i = 0;
124 let upper = bytes.len() - 1;
125 while i < upper {
126 if bytes[i] == b'/' && bytes[i + 1] == b'*' {
127 depth += 1;
128 i += 1; // eat '*'
129 } else if bytes[i] == b'*' && bytes[i + 1] == b'/' {
130 depth -= 1;
131 if depth == 0 {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500132 return Ok((input.advance(i + 2), &input.rest[..i + 2]));
David Tolnayb1032662017-05-31 15:52:28 -0700133 }
134 i += 1; // eat '/'
135 }
136 i += 1;
137 }
David Tolnay1218e122017-06-01 11:13:45 -0700138 Err(LexError)
David Tolnayb1032662017-05-31 15:52:28 -0700139}
140
Nika Layzellf8d5f212017-12-11 14:07:02 -0500141pub fn skip_whitespace(input: Cursor) -> Cursor {
David Tolnayb1032662017-05-31 15:52:28 -0700142 match whitespace(input) {
David Tolnay1218e122017-06-01 11:13:45 -0700143 Ok((rest, _)) => rest,
144 Err(LexError) => input,
David Tolnayb1032662017-05-31 15:52:28 -0700145 }
146}
147
148fn is_whitespace(ch: char) -> bool {
149 // Rust treats left-to-right mark and right-to-left mark as whitespace
150 ch.is_whitespace() || ch == '\u{200e}' || ch == '\u{200f}'
151}
152
Nika Layzellf8d5f212017-12-11 14:07:02 -0500153pub fn word_break(input: Cursor) -> PResult<()> {
David Tolnayb1032662017-05-31 15:52:28 -0700154 match input.chars().next() {
David Tolnay1218e122017-06-01 11:13:45 -0700155 Some(ch) if UnicodeXID::is_xid_continue(ch) => Err(LexError),
156 Some(_) | None => Ok((input, ())),
David Tolnayb1032662017-05-31 15:52:28 -0700157 }
158}
159
160macro_rules! named {
161 ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500162 fn $name<'a>(i: Cursor<'a>) -> $crate::strnom::PResult<'a, $o> {
David Tolnayb1032662017-05-31 15:52:28 -0700163 $submac!(i, $($args)*)
164 }
165 };
166}
167
168macro_rules! alt {
169 ($i:expr, $e:ident | $($rest:tt)*) => {
170 alt!($i, call!($e) | $($rest)*)
171 };
172
173 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
174 match $subrule!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700175 res @ Ok(_) => res,
David Tolnayb1032662017-05-31 15:52:28 -0700176 _ => alt!($i, $($rest)*)
177 }
178 };
179
180 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
181 match $subrule!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700182 Ok((i, o)) => Ok((i, $gen(o))),
183 Err(LexError) => alt!($i, $($rest)*)
David Tolnayb1032662017-05-31 15:52:28 -0700184 }
185 };
186
187 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
188 alt!($i, call!($e) => { $gen } | $($rest)*)
189 };
190
191 ($i:expr, $e:ident => { $gen:expr }) => {
192 alt!($i, call!($e) => { $gen })
193 };
194
195 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
196 match $subrule!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700197 Ok((i, o)) => Ok((i, $gen(o))),
198 Err(LexError) => Err(LexError),
David Tolnayb1032662017-05-31 15:52:28 -0700199 }
200 };
201
202 ($i:expr, $e:ident) => {
203 alt!($i, call!($e))
204 };
205
206 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
207 $subrule!($i, $($args)*)
208 };
209}
210
211macro_rules! do_parse {
212 ($i:expr, ( $($rest:expr),* )) => {
David Tolnay1218e122017-06-01 11:13:45 -0700213 Ok(($i, ( $($rest),* )))
David Tolnayb1032662017-05-31 15:52:28 -0700214 };
215
216 ($i:expr, $e:ident >> $($rest:tt)*) => {
217 do_parse!($i, call!($e) >> $($rest)*)
218 };
219
220 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
221 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700222 Err(LexError) => Err(LexError),
223 Ok((i, _)) => do_parse!(i, $($rest)*),
David Tolnayb1032662017-05-31 15:52:28 -0700224 }
225 };
226
227 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
228 do_parse!($i, $field: call!($e) >> $($rest)*)
229 };
230
231 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
232 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700233 Err(LexError) => Err(LexError),
234 Ok((i, o)) => {
David Tolnayb1032662017-05-31 15:52:28 -0700235 let $field = o;
236 do_parse!(i, $($rest)*)
237 },
238 }
239 };
240}
241
242macro_rules! peek {
243 ($i:expr, $submac:ident!( $($args:tt)* )) => {
244 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700245 Ok((_, o)) => Ok(($i, o)),
246 Err(LexError) => Err(LexError),
David Tolnayb1032662017-05-31 15:52:28 -0700247 }
248 };
249}
250
251macro_rules! call {
252 ($i:expr, $fun:expr $(, $args:expr)*) => {
253 $fun($i $(, $args)*)
254 };
255}
256
257macro_rules! option {
258 ($i:expr, $f:expr) => {
259 match $f($i) {
David Tolnay1218e122017-06-01 11:13:45 -0700260 Ok((i, o)) => Ok((i, Some(o))),
261 Err(LexError) => Ok(($i, None)),
David Tolnayb1032662017-05-31 15:52:28 -0700262 }
263 };
264}
265
Alex Crichtond7904e52018-01-23 11:08:45 -0800266macro_rules! take_until_newline_or_eof {
267 ($i:expr,) => {{
268 if $i.len() == 0 {
269 Ok(($i, ""))
David Tolnayb1032662017-05-31 15:52:28 -0700270 } else {
Alex Crichtond7904e52018-01-23 11:08:45 -0800271 match $i.find('\n') {
272 Some(i) => Ok(($i.advance(i), &$i.rest[..i])),
273 None => Ok(($i.advance($i.len()), ""))
David Tolnayb1032662017-05-31 15:52:28 -0700274 }
275 }
276 }};
277}
278
279macro_rules! tuple {
280 ($i:expr, $($rest:tt)*) => {
281 tuple_parser!($i, (), $($rest)*)
282 };
283}
284
285/// Do not use directly. Use `tuple!`.
286macro_rules! tuple_parser {
287 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
288 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*)
289 };
290
291 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
292 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700293 Err(LexError) => Err(LexError),
294 Ok((i, o)) => tuple_parser!(i, (o), $($rest)*),
David Tolnayb1032662017-05-31 15:52:28 -0700295 }
296 };
297
298 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
299 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700300 Err(LexError) => Err(LexError),
301 Ok((i, o)) => tuple_parser!(i, ($($parsed)* , o), $($rest)*),
David Tolnayb1032662017-05-31 15:52:28 -0700302 }
303 };
304
305 ($i:expr, ($($parsed:tt),*), $e:ident) => {
306 tuple_parser!($i, ($($parsed),*), call!($e))
307 };
308
309 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
310 $submac!($i, $($args)*)
311 };
312
313 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
314 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700315 Err(LexError) => Err(LexError),
316 Ok((i, o)) => Ok((i, ($($parsed),*, o)))
David Tolnayb1032662017-05-31 15:52:28 -0700317 }
318 };
319
320 ($i:expr, ($($parsed:expr),*)) => {
David Tolnay1218e122017-06-01 11:13:45 -0700321 Ok(($i, ($($parsed),*)))
David Tolnayb1032662017-05-31 15:52:28 -0700322 };
323}
324
325macro_rules! not {
326 ($i:expr, $submac:ident!( $($args:tt)* )) => {
327 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700328 Ok((_, _)) => Err(LexError),
329 Err(LexError) => Ok(($i, ())),
David Tolnayb1032662017-05-31 15:52:28 -0700330 }
331 };
332}
333
334macro_rules! tag {
335 ($i:expr, $tag:expr) => {
336 if $i.starts_with($tag) {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500337 Ok(($i.advance($tag.len()), &$i.rest[..$tag.len()]))
David Tolnayb1032662017-05-31 15:52:28 -0700338 } else {
David Tolnay1218e122017-06-01 11:13:45 -0700339 Err(LexError)
David Tolnayb1032662017-05-31 15:52:28 -0700340 }
341 };
342}
343
344macro_rules! punct {
345 ($i:expr, $punct:expr) => {
346 $crate::strnom::punct($i, $punct)
347 };
348}
349
350/// Do not use directly. Use `punct!`.
Nika Layzellf8d5f212017-12-11 14:07:02 -0500351pub fn punct<'a>(input: Cursor<'a>, token: &'static str) -> PResult<'a, &'a str> {
David Tolnayb1032662017-05-31 15:52:28 -0700352 let input = skip_whitespace(input);
353 if input.starts_with(token) {
Nika Layzellf8d5f212017-12-11 14:07:02 -0500354 Ok((input.advance(token.len()), token))
David Tolnayb1032662017-05-31 15:52:28 -0700355 } else {
David Tolnay1218e122017-06-01 11:13:45 -0700356 Err(LexError)
David Tolnayb1032662017-05-31 15:52:28 -0700357 }
358}
359
David Tolnayb1032662017-05-31 15:52:28 -0700360macro_rules! preceded {
361 ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
362 match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
David Tolnay1218e122017-06-01 11:13:45 -0700363 Ok((remaining, (_, o))) => Ok((remaining, o)),
364 Err(LexError) => Err(LexError),
David Tolnayb1032662017-05-31 15:52:28 -0700365 }
366 };
367
368 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
369 preceded!($i, $submac!($($args)*), call!($g))
370 };
371}
372
373macro_rules! delimited {
374 ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
375 match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700376 Err(LexError) => Err(LexError),
377 Ok((i1, (_, o, _))) => Ok((i1, o))
David Tolnayb1032662017-05-31 15:52:28 -0700378 }
379 };
380}
381
382macro_rules! map {
383 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
384 match $submac!($i, $($args)*) {
David Tolnay1218e122017-06-01 11:13:45 -0700385 Err(LexError) => Err(LexError),
386 Ok((i, o)) => Ok((i, call!(o, $g)))
David Tolnayb1032662017-05-31 15:52:28 -0700387 }
388 };
389
390 ($i:expr, $f:expr, $g:expr) => {
391 map!($i, call!($f), $g)
392 };
393}
394
395macro_rules! many0 {
396 ($i:expr, $f:expr) => {{
397 let ret;
398 let mut res = ::std::vec::Vec::new();
399 let mut input = $i;
400
401 loop {
402 if input.is_empty() {
David Tolnay1218e122017-06-01 11:13:45 -0700403 ret = Ok((input, res));
David Tolnayb1032662017-05-31 15:52:28 -0700404 break;
405 }
406
407 match $f(input) {
David Tolnay1218e122017-06-01 11:13:45 -0700408 Err(LexError) => {
409 ret = Ok((input, res));
David Tolnayb1032662017-05-31 15:52:28 -0700410 break;
411 }
David Tolnay1218e122017-06-01 11:13:45 -0700412 Ok((i, o)) => {
David Tolnayb1032662017-05-31 15:52:28 -0700413 // loop trip must always consume (otherwise infinite loops)
414 if i.len() == input.len() {
David Tolnay1218e122017-06-01 11:13:45 -0700415 ret = Err(LexError);
David Tolnayb1032662017-05-31 15:52:28 -0700416 break;
417 }
418
419 res.push(o);
420 input = i;
421 }
422 }
423 }
424
425 ret
426 }};
427}