Rust's macros require them to be delimited by either parentheses, braces, or brackets, such as println!(arg) or foo! { bar, baz }. However, I have seen many examples of embedded scripting languages implemented via a macro. How does the Rust parser figure out where the macro ends? The language might have rogue braces or something in it, such as

python! {

While this can be solved by having the parser detect inner strings, what if my strings are delimited by < and >? Or what if my language does not require pairing brackets/braces, like many esolangs? Obviously pair matching cannot be used here, so how does Rust parse such macros?


1 Answer 1


The input to a Rust macro is a tree of Rust tokens and groups delimited by {}, (), or []. If your language does not conform to this structure, it cannot be parsed by a Rust macro.

  • 3
    $\begingroup$ Would it be fair to state that the "input" to a macro must be lexically valid Rust code? $\endgroup$ Nov 12, 2023 at 11:39
  • 7
    $\begingroup$ @JörgWMittag: I guess it would be one way to express it, though most people would likely not realize this doesn't mean syntactically valid Rust code. I prefer to express it as balanced token tree, with emphasis on balanced here. $\endgroup$ Nov 12, 2023 at 12:40
  • $\begingroup$ Lexically valid and with matching delimiters. Or at least the delimiter used to start the macro must be correctly matched. I'm not sure if you can have a boatload of unclosed ( in a macro delimited by { } or not, actually. $\endgroup$ Nov 13, 2023 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .