<p>Rust's macros require them to be delimited by either parentheses, braces, or brackets, such as <code>println!(arg)</code> or <code>foo! { bar, baz }</code>. 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</p>
<pre><code>python! {
    print(&quot;}&quot;)
}
</code></pre>
<p>While this can be solved by having the parser detect inner strings, what if my strings are delimited by <code>&lt;</code> and <code>&gt;</code>? 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?</p>

<parsing><rust><macro>
How does Rust parse ambiguous macros?
CC BY-SA 4.0
2023-11-12T05:41:40.650Z
Please see https://doc.rust-lang.org/beta/reference/macros.html `<` and `>` are not delimiters in Rust token trees.
alephalpha
<p>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.</p>

CC BY-SA 4.0
2023-11-12T00:24:37.803Z
2023-11-12T00:24:37.803Z
rpjohnst
CC BY-SA 4.0
2023-11-12T11:39:57.167Z
Would it be fair to state that the "input" to a macro must be *lexically* valid Rust code?
Jörg W Mittag
CC BY-SA 4.0
2023-11-12T12:40:38.333Z
@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.
Matthieu M.
CC BY-SA 4.0
2023-11-13T18:35:03.010Z
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.
Silvio Mayolo
# How does Rust parse ambiguous macros?
Posted by: username_0 on 2023-11-12
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! {
    print("}")
}

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?
**Tags:** parsing rust macro
> **Comments:**
> **username_1**: Please see https://doc.rust-lang.org/beta/reference/macros.html `<` and `>` are not delimiters in Rust token trees.  
---
## 1 answers
### Answer by username_2 on 2023-11-12. Score: 11 | Accepted answer
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.
> **Comments:**
> **username_3** (2023-11-12): Would it be fair to state that the "input" to a macro must be *lexically* valid Rust code?  
> **username_4** (2023-11-12): @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.  
> **username_5** (2023-11-13): 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.  
---
