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

Better repetition operator #23

Open
johndeighan opened this issue Oct 31, 2024 · 1 comment
Open

Better repetition operator #23

johndeighan opened this issue Oct 31, 2024 · 1 comment

Comments

@johndeighan
Copy link

I think that Peggy allows this. Instead of having to write:
Part ( "," Part )* for "one or more Parts separated by a comma", you could write, e.g.:
(Part / ",")+ Now, I'm sure you won't be able to use a slash there - you just need something to separate the item(s) from the separator. You'd also be allowed to use
(Part / ",")* to mean "zero or more Parts separated by a comma"
Where you see Part, you could, in fact, put any expression at all. Personally, I think it's much clearer what you're expecting to see. The separator is usually something with no semantic meaning - it's just there to make parsing easier - for both the software and the brain.

@STRd6
Copy link
Collaborator

STRd6 commented Nov 3, 2024

The direction I'd like to go with this is something closer to higher order functions / macros.

We could declare a parameterized meta rule like:

Repetition(Part, Separator)
  Part ( Separator Part )* ->
    $2.unshift($1)
    return $2

This way we get an unambiguous resolution to the structure of the repetition and it can be changed based on preference.

It will also help with things like:

InsertSpace
  "" ->
    return { $loc, token: " " }
 
 --- becomes something like ---
 
 Insert(Token)
   "" ->
     return { $loc, token: Token } // TODO: figure out what it means to reference the parameterized rule here, fine with a string literal but could get weird with another parsing rule
 
InsertSpace = Insert("") // TODO: Figure out how to name and assign these parameterizations

If we do end up adding this parameterization feature we could even ship with some default utilities to handle repetition and other common situations.

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

No branches or pull requests

2 participants