verbatim.rs 1.2 KB

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