Chevrotain / chevrotain

Parser Building Toolkit for JavaScript
https://chevrotain.io
Apache License 2.0
2.48k stars 204 forks source link

How to say "one or more of a few options" in parser? #1854

Closed dhowe closed 2 years ago

dhowe commented 2 years ago

I'm sure this is super obvious (I'm a new user), but how to define a parser rule that says one or more of a few options:

element: 
  (chars | choice | symbol)+;

Is the proper way to create a sub-rule for each option (as below,) or can I simply say AT_LEAST_ONE(OR [{ALT: ..., }])

   $.RULE("element-part", () => {
      $.OR([
        { ALT: () => $.SUBRULE($.chars) },
        { ALT: () => $.SUBRULE($.choice) },
        { ALT: () => $.SUBRULE($.symbol) },
      ])
    })

    $.RULE("element", () => {
      $.AT_LEAST_ONE(() => $.SUBRULE($.element-part"));
    });