Skip to main content

syn/
verbatim.rs

1use crate::buffer::Cursor;
2use crate::ext::TokenStreamExt as _;
3use core::cmp::Ordering;
4use proc_macro2::{Delimiter, TokenStream};
5
6pub(crate) fn between(begin: Cursor, end: Cursor) -> TokenStream {
7    let mut cursor = begin;
8    if !crate::buffer::same_buffer(end, cursor) {
    ::core::panicking::panic("assertion failed: crate::buffer::same_buffer(end, cursor)")
};assert!(crate::buffer::same_buffer(end, cursor));
9
10    let mut tokens = TokenStream::new();
11    while cursor != end {
12        let (tt, next) = cursor.token_tree().unwrap();
13
14        if crate::buffer::cmp_assuming_same_buffer(end, next) == Ordering::Less {
15            // A syntax node can cross the boundary of a None-delimited group
16            // due to such groups being transparent to the parser in most cases.
17            // Any time this occurs the group is known to be semantically
18            // irrelevant. https://github.com/dtolnay/syn/issues/1235
19            if let Some((inside, _span, after)) = cursor.group(Delimiter::None) {
20                if !(next == after) {
    ::core::panicking::panic("assertion failed: next == after")
};assert!(next == after);
21                cursor = inside;
22                continue;
23            } else {
24                {
    ::core::panicking::panic_fmt(format_args!("verbatim end must not be inside a delimited group"));
};panic!("verbatim end must not be inside a delimited group");
25            }
26        }
27
28        tokens.append(tt);
29        cursor = next;
30    }
31    tokens
32}