Repeated nodes

Canopy offers the well-known * and + operators from regular expressions, meaning “zero or more” and “one or more” respectively. Placing them after a node allows the node to repeat the desired number of times.

Here’s a grammar that matches the word badger one or more times:

grammar Badger
  root  <-  "badger"+

When a repetition matches the input, you get one node for the repetition itself, and one child node for each instance in the repetition. If there aren’t enough repetitions (for example if there’s not at least one match for a + rule) you’ll get an error.

   == { text: 'badgerbadgerbadger',
        offset: 0,
         [ { text: 'badger', offset: 0, elements: [] },
           { text: 'badger', offset: 6, elements: [] },
           { text: 'badger', offset: 12, elements: [] } ] }

Error: Line 1: expected "badger"