9
$\begingroup$

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?

$\endgroup$
1

1 Answer 1

12
$\begingroup$

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.

$\endgroup$
3
  • 3
    $\begingroup$ Would it be fair to state that the "input" to a macro must be lexically valid Rust code? $\endgroup$ Commented Nov 12, 2023 at 11:39
  • 8
    $\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$ Commented 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$ Commented Nov 13, 2023 at 18:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.