Skip to content
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

Support parsing multi-character Punct separators #20

Closed
wants to merge 1 commit into from
Closed

Support parsing multi-character Punct separators #20

wants to merge 1 commit into from

Conversation

jmhain
Copy link

@jmhain jmhain commented Oct 18, 2018

Fixes #8

src/parser.rs Outdated
TokenTree::Punct(punct) => Separator::Punct(punct),
TokenTree::Punct(punct) => {
let mut puncts = vec![punct];
while !Repetition::peek(input) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This accepts macro_rules! m { ($($tt:tt):::*) => {} } which is not permitted by Rust.

error: expected `*` or `+`
 --> src/lib.rs:1:30
  |
1 | macro_rules! m { ($($tt:tt):::*) => {} }
  |                              ^

If people use macro_railroad for experimenting and building their understanding of macro_rules, accepting invalid input would be misleading.

Copy link
Author

@jmhain jmhain Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. Is there a cleaner way to fix this than calling input.peek() with each multi-character punctuation token? Also do you have any recommendations for making tokens that include repetition characters like *= or += work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sort of thing is the best way:

if input.parse::<Option<Token![*]>>()?.is_some() {
Ok(Repetition::Repeated)
} else if input.parse::<Option<Token![+]>>()?.is_some() {
Ok(Repetition::AtLeastOnce)
} else if input.parse::<Option<Token![?]>>()?.is_some() {
Ok(Repetition::AtMostOnce)

*= and += work the same as any other multicharacter separator.

macro_rules! m {
    ($($tt:tt)*) => {
        println!(stringify!($($tt) *= *));
    };
}

fn main() {
    m!(a b c); // a *= b *= c
}

@jmhain jmhain closed this Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multi-character repetition separators not parseable
2 participants