rust-lang / rust-lang/rfcs

Allow group capture, choice and optional arguments

Open
#2,170 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Currently I'm trying to parse following macro structure in Rust

<top>      ::= <prefix> <suffix>
<prefix>   ::= ε | <map> | <map>, <prefix>
<map>      ::= <token> "=>" <token>
<suffix>   ::= ε | ":" <metadata>
<metadata> ::= ...

Currently I'm doing (simplified):

macro_rules! top {
    ($a:ident => $b:ident, $(tail:tt)*) => {
        proc_map!($a => $b);
        top!($(tail)*)
    };
    ($a:ident => $b:ident($c:ident), $(tail:tt)*) => {
        proc_map!($a => $b($c));
        top!($(tail)*)
    };
    ($a:ident => $b:ident: $(tail:tt)*) => {
        proc_map!($a => $b);
        suffix!($(tail)*)
    };
    ($a:ident => $b:ident($c:ident): $(tail:tt)*) => {
        proc_map!($a => $b($c));
        suffix!($(tail)*)
    };
    (: $(tail:tt)*) => {
        suffix!($(tail)*)
    };
    ($a:ident => $b:ident) => {
        proc_map!($a => $b);
    };
    ($a:ident => $b:ident($c:ident)) => {
        proc_map!($a => $b($c));
        s
    () => {};
}

This has several disadvantages such as code repetition and exponential explosion of potential options. For more complex DSL it creates a spaghetti code. It could potentially be simplified - for example using this made up syntax:

macro_rules! top {
    ($a:ident => $b:ident$(($c:ident))? $suff:($|((,) | (:)) $($tail:tt)*)?) => {
        proc_map!($a => $b$(($c))*);
        $(top_hlp!($suff));
    };
    (: $(tail:tt)*) => {
        suffix!($($tail)*);
    };
    () => {};
}

macro_rules! top_hlp {
    (, $($tail:tt)*) => {top!($($tail)*)};
    (: $($tail:tt)*) => {suffix!($($tail)*)};
}

I've added following changes:

  • $(pattern)? - analogous to $(pattern)* and $(pattern)+ except it matches 0 or 1 time (analogous to regular expressions)
  • $name:(pattern) - $name captures all token trees captured by pattern to be passed somewhere else to avoid copy and pasting
  • $|((pattern1) | (pattern2) | ...) captures either of those patterns (choice). If pattern1/pattern2 have any name tokens it behaves analogically to optional group. So if full pattern is $|((type $typ:ty) | (expr $ex:expr)) and the expansion is $($typ)* then it will expand to passed type if the first branch was taken or to nothing if second was taken

While none of those options allows macros to do more they would simplify the writing of them.

I'm not insisting on particular syntax and probably someone more familiar with Rust internals/language design could come up with better ones.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the macro_rules! examples and the proposed optional-group, capture, and choice syntax in this issue. Compare the alternatives against the stated goal of reducing repetition and option combinations; done requires an agreed design for Rust macros rather than a narrowly scoped edit.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.