-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: flexible syntax for macro invocations #2387
Comments
Minor note: I'm hoping to be able to write |
I would really like to be able to write macros in non-expression positions (particularly items). Is this as simple as renaming |
At least, I think I would like that. |
That would work. But the invocation syntax might have to be different in order to be unambiguous. |
One note on the
Also, I just realized that we have discussed a class literal syntax of |
Note that modifying the sugared closure syntax to This is dangerously close to coupling proposals, but I thought I'd get it out there in case we decide that we need macro bodies that can begin with |
Fortunately, the interesting work on this is independent of the specific invocation syntax. That said, I'd hate to special-case-forbid some syntaxes. |
A few comments:
|
I'm going to put off thinking about notational issues about delimiters for the moment.
|
|
Implemented. The extensions are still relevant, however. |
Hi, I'm new to rust and a bit contextually challenged. Are the macros under discussion here deprecated in favor of the foo!(a, b) style? I just learned that style tonight from this: https://gist.github.com/3421238 In either case, I have two wishlist items: hygienic macros - I assume everyone is on board with this? scoping definitions like other definitions - You can define a macro that is exported/imported across module boundaries and also you can define macros locally to functions or blocks. Is this the write venue for these kinds of wishlist items? |
@nejucomo, here's a talk by @paulstansifer that discusses implementing hygienic macros (at the 15:50 mark). As for your other questions, you can generally direct wishlist-style suggestions to the rust-dev mailing list, and file issues for them once you've gotten some positive feedback. |
update ui_test readme I forgot to do that when changing the ignore/only syntax.
Currently, macro invocations piggyback off an existing syntactic form, the array literal. We'd like more flexibility.
Macro invocation syntax
The proposed invocation syntax will extend the grammar roughly as follows (the exact syntax for identifying an invocation will be decided later. However, they will need to be distinguished from function invocations at parse-time.):
Parsing macro invocations
The tricky part is having macros consume
Balanced
s in a useful way. An example invocation to an example macro:Here's how we'd like to define
my_let
(rep()
is like Macro by Example's...
):Proposed implementation
I believe that this can be implemented in a minimally-invasive way.
pat_macro
will be a syntax extension which takes a BNF-like notation for the invocation parser on the inside of the<macro_name>{}
, and aBalanced
on the right side of the=>
. (The only reason not to parse it as an Expression is that rustc has no data structure for incomplete ASTs.) (It would be friendly to also parse it as an expression, using dummy values for interpolated syntax, to check that it will parse correctly.)At macro expansion time, the
Balanced
will need to be parsed (well, re-parsed) according the the grammar of the macro. We can do this by building a lexer that takes aBalanced
instead of a string as input. The parser will interpret the macro's BNF-like pattern, delegating to the Rust parser for things likeIdentifier
andExpr
.There are two ways for the shim lexer to deal with interpolated syntax. The bad one is to pretty-print the interpolated ASTs and re-lex them before sending them to the parser again. The better one is to use special tokens to hand the parser pre-parsed ASTs for it to return immediately.
Possible extensions
Syntax for lexer-skipping syntax extensions
If we remove
#
from ordinary macro invocation syntax, we can use it to provide quotation for un-lexed syntax. Delimiters would work in a Perl-like fashion:String-examining/lexer-skipping macros
Making macros look inside strings should be fairly simple, but most practical applications will probably require lots more power from the macro system.
Invocations at non-expression position
What if we want macros to generate non-expressions (especially items, types)? It seems like we need a separate invocation form for every nonterminal we want to extend. Fortunately, expressions cover a lot of the interesting territory.
The text was updated successfully, but these errors were encountered: