proc_macro2/
strnom.rs

1//! Adapted from [`nom`](https://github.com/Geal/nom).
2
3use crate::fallback::LexError;
4use std::str::{Bytes, CharIndices, Chars};
5use unicode_xid::UnicodeXID;
6
7#[derive(Copy, Clone, Eq, PartialEq)]
8pub struct Cursor<'a> {
9    pub rest: &'a str,
10    #[cfg(span_locations)]
11    pub off: u32,
12}
13
14impl<'a> Cursor<'a> {
15    #[cfg(not(span_locations))]
16    pub fn advance(&self, amt: usize) -> Cursor<'a> {
17        Cursor {
18            rest: &self.rest[amt..],
19        }
20    }
21    #[cfg(span_locations)]
22    pub fn advance(&self, amt: usize) -> Cursor<'a> {
23        Cursor {
24            rest: &self.rest[amt..],
25            off: self.off + (amt as u32),
26        }
27    }
28
29    pub fn find(&self, p: char) -> Option<usize> {
30        self.rest.find(p)
31    }
32
33    pub fn starts_with(&self, s: &str) -> bool {
34        self.rest.starts_with(s)
35    }
36
37    pub fn is_empty(&self) -> bool {
38        self.rest.is_empty()
39    }
40
41    pub fn len(&self) -> usize {
42        self.rest.len()
43    }
44
45    pub fn as_bytes(&self) -> &'a [u8] {
46        self.rest.as_bytes()
47    }
48
49    pub fn bytes(&self) -> Bytes<'a> {
50        self.rest.bytes()
51    }
52
53    pub fn chars(&self) -> Chars<'a> {
54        self.rest.chars()
55    }
56
57    pub fn char_indices(&self) -> CharIndices<'a> {
58        self.rest.char_indices()
59    }
60}
61
62pub type PResult<'a, O> = Result<(Cursor<'a>, O), LexError>;
63
64pub fn whitespace(input: Cursor) -> PResult<()> {
65    if input.is_empty() {
66        return Err(LexError);
67    }
68
69    let bytes = input.as_bytes();
70    let mut i = 0;
71    while i < bytes.len() {
72        let s = input.advance(i);
73        if bytes[i] == b'/' {
74            if s.starts_with("//")
75                && (!s.starts_with("///") || s.starts_with("////"))
76                && !s.starts_with("//!")
77            {
78                if let Some(len) = s.find('\n') {
79                    i += len + 1;
80                    continue;
81                }
82                break;
83            } else if s.starts_with("/**/") {
84                i += 4;
85                continue;
86            } else if s.starts_with("/*")
87                && (!s.starts_with("/**") || s.starts_with("/***"))
88                && !s.starts_with("/*!")
89            {
90                let (_, com) = block_comment(s)?;
91                i += com.len();
92                continue;
93            }
94        }
95        match bytes[i] {
96            b' ' | 0x09..=0x0d => {
97                i += 1;
98                continue;
99            }
100            b if b <= 0x7f => {}
101            _ => {
102                let ch = s.chars().next().unwrap();
103                if is_whitespace(ch) {
104                    i += ch.len_utf8();
105                    continue;
106                }
107            }
108        }
109        return if i > 0 { Ok((s, ())) } else { Err(LexError) };
110    }
111    Ok((input.advance(input.len()), ()))
112}
113
114pub fn block_comment(input: Cursor) -> PResult<&str> {
115    if !input.starts_with("/*") {
116        return Err(LexError);
117    }
118
119    let mut depth = 0;
120    let bytes = input.as_bytes();
121    let mut i = 0;
122    let upper = bytes.len() - 1;
123    while i < upper {
124        if bytes[i] == b'/' && bytes[i + 1] == b'*' {
125            depth += 1;
126            i += 1; // eat '*'
127        } else if bytes[i] == b'*' && bytes[i + 1] == b'/' {
128            depth -= 1;
129            if depth == 0 {
130                return Ok((input.advance(i + 2), &input.rest[..i + 2]));
131            }
132            i += 1; // eat '/'
133        }
134        i += 1;
135    }
136    Err(LexError)
137}
138
139pub fn skip_whitespace(input: Cursor) -> Cursor {
140    match whitespace(input) {
141        Ok((rest, _)) => rest,
142        Err(LexError) => input,
143    }
144}
145
146fn is_whitespace(ch: char) -> bool {
147    // Rust treats left-to-right mark and right-to-left mark as whitespace
148    ch.is_whitespace() || ch == '\u{200e}' || ch == '\u{200f}'
149}
150
151pub fn word_break(input: Cursor) -> PResult<()> {
152    match input.chars().next() {
153        Some(ch) if UnicodeXID::is_xid_continue(ch) => Err(LexError),
154        Some(_) | None => Ok((input, ())),
155    }
156}
157
158macro_rules! named {
159    ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
160        fn $name<'a>(i: Cursor<'a>) -> $crate::strnom::PResult<'a, $o> {
161            $submac!(i, $($args)*)
162        }
163    };
164}
165
166macro_rules! alt {
167    ($i:expr, $e:ident | $($rest:tt)*) => {
168        alt!($i, call!($e) | $($rest)*)
169    };
170
171    ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
172        match $subrule!($i, $($args)*) {
173            res @ Ok(_) => res,
174            _ => alt!($i, $($rest)*)
175        }
176    };
177
178    ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
179        match $subrule!($i, $($args)*) {
180            Ok((i, o)) => Ok((i, $gen(o))),
181            Err(LexError) => alt!($i, $($rest)*)
182        }
183    };
184
185    ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
186        alt!($i, call!($e) => { $gen } | $($rest)*)
187    };
188
189    ($i:expr, $e:ident => { $gen:expr }) => {
190        alt!($i, call!($e) => { $gen })
191    };
192
193    ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
194        match $subrule!($i, $($args)*) {
195            Ok((i, o)) => Ok((i, $gen(o))),
196            Err(LexError) => Err(LexError),
197        }
198    };
199
200    ($i:expr, $e:ident) => {
201        alt!($i, call!($e))
202    };
203
204    ($i:expr, $subrule:ident!( $($args:tt)*)) => {
205        $subrule!($i, $($args)*)
206    };
207}
208
209macro_rules! do_parse {
210    ($i:expr, ( $($rest:expr),* )) => {
211        Ok(($i, ( $($rest),* )))
212    };
213
214    ($i:expr, $e:ident >> $($rest:tt)*) => {
215        do_parse!($i, call!($e) >> $($rest)*)
216    };
217
218    ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
219        match $submac!($i, $($args)*) {
220            Err(LexError) => Err(LexError),
221            Ok((i, _)) => do_parse!(i, $($rest)*),
222        }
223    };
224
225    ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
226        do_parse!($i, $field: call!($e) >> $($rest)*)
227    };
228
229    ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
230        match $submac!($i, $($args)*) {
231            Err(LexError) => Err(LexError),
232            Ok((i, o)) => {
233                let $field = o;
234                do_parse!(i, $($rest)*)
235            },
236        }
237    };
238}
239
240macro_rules! peek {
241    ($i:expr, $submac:ident!( $($args:tt)* )) => {
242        match $submac!($i, $($args)*) {
243            Ok((_, o)) => Ok(($i, o)),
244            Err(LexError) => Err(LexError),
245        }
246    };
247}
248
249macro_rules! call {
250    ($i:expr, $fun:expr $(, $args:expr)*) => {
251        $fun($i $(, $args)*)
252    };
253}
254
255macro_rules! option {
256    ($i:expr, $f:expr) => {
257        match $f($i) {
258            Ok((i, o)) => Ok((i, Some(o))),
259            Err(LexError) => Ok(($i, None)),
260        }
261    };
262}
263
264macro_rules! take_until_newline_or_eof {
265    ($i:expr,) => {{
266        if $i.len() == 0 {
267            Ok(($i, ""))
268        } else {
269            match $i.find('\n') {
270                Some(i) => Ok(($i.advance(i), &$i.rest[..i])),
271                None => Ok(($i.advance($i.len()), &$i.rest[..$i.len()])),
272            }
273        }
274    }};
275}
276
277macro_rules! tuple {
278    ($i:expr, $($rest:tt)*) => {
279        tuple_parser!($i, (), $($rest)*)
280    };
281}
282
283/// Do not use directly. Use `tuple!`.
284macro_rules! tuple_parser {
285    ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
286        tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*)
287    };
288
289    ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
290        match $submac!($i, $($args)*) {
291            Err(LexError) => Err(LexError),
292            Ok((i, o)) => tuple_parser!(i, (o), $($rest)*),
293        }
294    };
295
296    ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
297        match $submac!($i, $($args)*) {
298            Err(LexError) => Err(LexError),
299            Ok((i, o)) => tuple_parser!(i, ($($parsed)* , o), $($rest)*),
300        }
301    };
302
303    ($i:expr, ($($parsed:tt),*), $e:ident) => {
304        tuple_parser!($i, ($($parsed),*), call!($e))
305    };
306
307    ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
308        $submac!($i, $($args)*)
309    };
310
311    ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
312        match $submac!($i, $($args)*) {
313            Err(LexError) => Err(LexError),
314            Ok((i, o)) => Ok((i, ($($parsed),*, o)))
315        }
316    };
317
318    ($i:expr, ($($parsed:expr),*)) => {
319        Ok(($i, ($($parsed),*)))
320    };
321}
322
323macro_rules! not {
324    ($i:expr, $submac:ident!( $($args:tt)* )) => {
325        match $submac!($i, $($args)*) {
326            Ok((_, _)) => Err(LexError),
327            Err(LexError) => Ok(($i, ())),
328        }
329    };
330}
331
332macro_rules! tag {
333    ($i:expr, $tag:expr) => {
334        if $i.starts_with($tag) {
335            Ok(($i.advance($tag.len()), &$i.rest[..$tag.len()]))
336        } else {
337            Err(LexError)
338        }
339    };
340}
341
342macro_rules! punct {
343    ($i:expr, $punct:expr) => {
344        $crate::strnom::punct($i, $punct)
345    };
346}
347
348/// Do not use directly. Use `punct!`.
349pub fn punct<'a>(input: Cursor<'a>, token: &'static str) -> PResult<'a, &'a str> {
350    let input = skip_whitespace(input);
351    if input.starts_with(token) {
352        Ok((input.advance(token.len()), token))
353    } else {
354        Err(LexError)
355    }
356}
357
358macro_rules! preceded {
359    ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
360        match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
361            Ok((remaining, (_, o))) => Ok((remaining, o)),
362            Err(LexError) => Err(LexError),
363        }
364    };
365
366    ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
367        preceded!($i, $submac!($($args)*), call!($g))
368    };
369}
370
371macro_rules! delimited {
372    ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
373        match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
374            Err(LexError) => Err(LexError),
375            Ok((i1, (_, o, _))) => Ok((i1, o))
376        }
377    };
378}
379
380macro_rules! map {
381    ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
382        match $submac!($i, $($args)*) {
383            Err(LexError) => Err(LexError),
384            Ok((i, o)) => Ok((i, call!(o, $g)))
385        }
386    };
387
388    ($i:expr, $f:expr, $g:expr) => {
389        map!($i, call!($f), $g)
390    };
391}