1 // SPDX-License-Identifier: GPL-2.0 2 3 use proc_macro::{token_stream, TokenTree}; 4 5 pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> { 6 if let Some(TokenTree::Ident(ident)) = it.next() { 7 Some(ident.to_string()) 8 } else { 9 None 10 } 11 } 12 13 pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> { 14 if let Some(TokenTree::Literal(literal)) = it.next() { 15 Some(literal.to_string()) 16 } else { 17 None 18 } 19 } 20 21 pub(crate) fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String> { 22 try_literal(it).and_then(|byte_string| { 23 if byte_string.starts_with("b\"") && byte_string.ends_with('\"') { 24 Some(byte_string[2..byte_string.len() - 1].to_string()) 25 } else { 26 None 27 } 28 }) 29 } 30 31 pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String { 32 try_ident(it).expect("Expected Ident") 33 } 34 35 pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char { 36 if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") { 37 punct.as_char() 38 } else { 39 panic!("Expected Punct"); 40 } 41 } 42 43 pub(crate) fn expect_byte_string(it: &mut token_stream::IntoIter) -> String { 44 try_byte_string(it).expect("Expected byte string") 45 } 46 47 pub(crate) fn expect_end(it: &mut token_stream::IntoIter) { 48 if it.next().is_some() { 49 panic!("Expected end"); 50 } 51 } 52