Nested labels / fat arrow support

33 views
Skip to first unread message

Mike Austin

unread,
Oct 31, 2018, 10:09:07 PM10/31/18
to PEG.js: Parser Generator for JavaScript
I've used PEG.js for fun building a JavaScript dialect, and now I'm coming back to it after a year. I love that it's simple and easy to use.
I was messing around and learned it has support for nested labels and blocks. I didn't see any examples, but it does make sense now.

I never really liked the buildList() with array indices. It feels fragile and uses magic numbers:

Factor
  = "(" _ head:Expression _ tail:("," _ Expression)* ")" {
      return buildList(head, tail, 2)
    }

I recently learned you can do this, instead. I like that it doesn't need a helper function or indices, but it's not the prettiest with the inline block.

Factor
  = "(" _ head:Expression _ tail:("," _ e:Expression { return e })* ")" {
      return [head, ...tail]
    }

If fat arrow syntax is added, this would look fairly clean and look like modern JavaScript:

Factor
  = "(" _ head:Expression _ tail:("," _ expr:Expression => expr)* ")" {
      return [head, ...tail]
    }

Just some thoughts.
Maybe I'll take a look at the PEG source and see how difficult it would be to add.

Mike
Reply all
Reply to author
Forward
0 new messages