Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make macros operate on token streams instead of AST nodes (#5301)
# Description ## Problem\* Resolves #5284 ## Summary\* Adds a new `Quoted` type which is the new default output of a `quote { ... }` expression. In addition, a `quote` expression now quotes an unparsed, raw vector of tokens terminated by the matching closing brace. This has a number of advantages: - Quoting code fragments: Since these token streams are not parsed, there is almost no restriction on their grammar. Code fragments such as `quote { ) ( }` are valid as long as they're grammatically valid by the time the macro is unquoted at its call site. The only restriction on the grammar of these is that they cannot have an unmatched `}` since that would close the `quote` expression itself. Matched `{` and `}` paris however, are fine: `quote { look, a block: { foo } }`. - Since we don't need to parse to a specific grammar rule, we don't need a new quoted type for each grammar rule we quote (see the existing `Expr`, `Type`, and `TopLevelItem` types). - The implementation details are simpler (e.g. this PR net removes lines). The current plan is to keep the other macro types (`Expr`, `Type`, `TopLevelItem`, etc) as optional outputs of `quote` only if they're specified after the keyword, e.g. `quote Type { Foo<A, B> }`. Since the new approach of using token streams by default is much more flexible, there's less of a need for these however. ## Additional Context The formatting of quote expressions has also been fixed. This was a necessary change since these expressions hold token streams now instead of ASTs and thus can't be formatted the old way. Edit: This does not fix the existing issue with formatting comptime blocks, only `quote` blocks which were similarly affected. ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [x] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Tom French <[email protected]>
- Loading branch information