Post #2029200
2026-05-04 14:47 UTC
@jamey I think you're right, a macro match arm is pretty much limited to regular languages. I had considered mentioning regexes near the end, but omitted them because they can be ambiguous about ambiguity :p. But regular languages do explicitly allow for ambiguity, so that would work. I think the literature on regular languages could inform this design space further -- thanks for bringing that up!
However, meta-variables like $:expr do not fit within regular languages. It seems there is another case of ambiguity there -- a match arm like $(0 +)* $e:expr, given 0 + 0 + 0 + 0 + 0 + 0 + 0, could still result in asymptotic (this time, at worst quadratic) blowup. You could guard for this by supporting packrat parsing for meta-variables too (perhaps only enabling it after certain iterations of the match arm, so that you don't waste time caching stuff in easy cases).
I'll add a note to the blog post :)
Replies (1)
-
@bal4e@tech.lgbt 2026-05-04 15:27
@jamey A follow-up: I tried this example in the Rust Playground and apparently it is not allowed! rustc does not allow ambiguity around meta-variables; it will only parse $e:expr if no other meta-variables or tokens can be parsed at that point. There go my hopes of getting quartic macro expansion D: I'll continue exploring the possibilities here. I think this limitation can be handled by the packrat style I proposed, perhaps with some adjustment of cache keys.