Variable token order for rule

25 views
Skip to first unread message

setht...@gmail.com

unread,
Jan 28, 2018, 1:08:21 AM1/28/18
to PEG.js: Parser Generator for JavaScript
Hello all,

I am making a basic JS-like programming language as a side project, with the exception being that it is statically typed (rather similar to TypeScript). I am currently implementing classes and OOP inheritance; however, I have come across an mild issue as far as specifying attributes for member methods and properties. For example, say I want to have a method talk() as part of a basic class, and I want to make it const, abstract, and one of either private or public. If I were to implement these three keywords with the slash operator for public/private, like so:

const:'const'? _ abs:'abstract'? _ acc:('public' / 'private')?

Then it is all well and good, with the exception being that one would have to specify the keywords in exactly that order. The only way I can think of as of yet to make the order irrelevant would be to create separate subrules for each placement of the keywords... Obviously less than optimal.

Is there a PEGjs feature I'm missing here, or is there any other way of making dynamic keyword placement work that I'm just not thinking of? Thanks in advance!

Samuel Crow

unread,
Jan 28, 2018, 4:27:33 PM1/28/18
to setht...@gmail.com, PEG.js: Parser Generator for JavaScript
Have you considered a more general modifier rule that uses a star operation for multiple keywords? Otherwise you may just have to slash operation for each permutation of the keywords, placing each keyword parser into a separate rule.

Seth Traman

unread,
Jan 28, 2018, 5:25:12 PM1/28/18
to Samuel Crow, PEG.js: Parser Generator for JavaScript
Using a star operator actually would work, combined with a quick JS check to make sure no keyword is stated more than once... Certainly a step up from the rule permutation option :P Thank you!
Reply all
Reply to author
Forward
0 new messages