ignore groups and commas

This commit is contained in:
Intege-rs
2025-12-27 19:54:39 -05:00
parent 6af236e002
commit 19721fb94e
2 changed files with 35 additions and 12 deletions

View File

@@ -18,6 +18,10 @@ pub fn derive_from_repr(input: proc_macro::TokenStream) -> proc_macro::TokenStre
#[proc_macro]
pub fn debug(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = input.into_iter().collect::<Vec<_>>();
format!("r#\"{input:#?}\"#").parse().unwrap()
}

View File

@@ -1,5 +1,5 @@
use core::{cmp, fmt, mem, str};
use proc_macro::{Literal, TokenStream, TokenTree};
use proc_macro::{Delimiter, Literal, TokenStream, TokenTree};
mod atoms {
@@ -16,12 +16,8 @@ mod parser {
/// const PATTERN: &[pelite::pattern::Atom] = pattern!("pattern string");
/// ```
pub fn proc_pattern(input: TokenStream) -> TokenStream {
let input = input.into_iter().collect::<Vec<_>>();
let string = match &input[..] {
[TokenTree::Literal(lit)] => parse_str_literal(&lit),
_e => panic!("expected a single string literal to parse, got: {_e:?}"),
};
let mut string = String::new();
parse_tokens(input, &mut string);
let pattern = match parser::parse(&string) {
Ok(pattern) => pattern,
@@ -31,10 +27,34 @@ pub fn proc_pattern(input: TokenStream) -> TokenStream {
format!("{{ use x::xpat::Atom::*; &{:?} as x::Pattern }}", pattern).parse().unwrap()
}
fn parse_str_literal(input: &Literal) -> String {
pub fn parse_tokens(input: TokenStream, out: &mut String) {
let input = input.into_iter().collect::<Vec<_>>();
for token in input {
match token {
TokenTree::Group(g) if g.delimiter() == Delimiter::None => {
parse_tokens(g.stream(), out);
}
TokenTree::Punct(p) if p.as_char() == ',' => {}
TokenTree::Literal(lit) => {
parse_str_literal(&lit, out);
}
TokenTree::Group(g) => { panic!("Unexpected Delimiter group") }
TokenTree::Ident(_) => { panic!("Unexpected identifier") }
TokenTree::Punct(p) => { panic!("Unexpected punctuation") }
}
}
}
fn parse_str_literal(input: &Literal, out: &mut String) {
let input = input.to_string();
let mut chars = input.chars();
let mut string = String::new();
if chars.next() != Some('"') {
panic!("expected string literal starting with a `\"` and no extraneous whitespace");
}
@@ -57,7 +77,6 @@ fn parse_str_literal(input: &Literal) -> String {
Some(chr) => chr,
None => panic!("unexpected end of string literal, missing `\"` terminator?"),
};
string.push(chr);
out.push(chr);
}
string
}