Skip to content

Commit

Permalink
Ranges (pegjs/pegjs#30): Add testcases for delimiter support in range…
Browse files Browse the repository at this point in the history
…s and regenerate parser and add documentation
  • Loading branch information
Mingun committed May 29, 2022
1 parent 49b2f5b commit 4ca6cfb
Show file tree
Hide file tree
Showing 12 changed files with 2,012 additions and 274 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,9 @@ Released: TBD

### Major Changes

- [#208](https://github.com/peggyjs/peggy/pull/208): Add support for repetition
operator `expression|min .. max, delimiter|`, from @Mingun

### Minor Changes

### Bug Fixes
Expand Down
33 changes: 33 additions & 0 deletions README.md
Expand Up @@ -517,6 +517,39 @@ Try to match the expression. If the match succeeds, return its match result,
otherwise return `null`. Unlike in regular expressions, there is no
backtracking.

#### _expression_ |count|<br> _expression_ |min..max|<br> _expression_ |count, delimiter|<br> _expression_ |min..max, delimiter|

Match exact `count` repetitions of `expression`. If the match succeeds, return
their match results in an array.

-or-

Match expression at least `min` but not more then `max` times. If the match
succeeds, return their match results in an array. Both `min` and `max` may
be omitted. If `min` is omitted, then it is assumed to be `0`. If `max` is
omitted, then it is assumed to be infinity. Hence `expression |..|` is an
equivalent of `expression |0..|` and `expression *`. `expression |1..|` is
equivalent of `expression +`.

Optionally, `delimiter` expression can be specified. Delimiter must appear
between expressions exactly once and it is not included in the final array.

`count`, `min` and `max` can be represented as:
- positive integers:
```peggy
start = "a"|2|;
```
- name of the preceding label:
```peggy
start = count:n1 "a"|count|;
n1 = n:$[0-9] { parseInt(n); };
```
- code block:
```peggy
start = 'a'|{ return options.count; }|;
```
Any non-number values will be interpreted as `0`.

#### & _expression_

Try to match the expression. If the match succeeds, just return `undefined` and
Expand Down
34 changes: 34 additions & 0 deletions docs/documentation.html
Expand Up @@ -662,6 +662,40 @@ <h3 id="grammar-syntax-and-semantics-parsing-expression-types">Parsing Expressio
result, otherwise return <code>null</code>. Unlike in regular expressions,
there is no backtracking.</p>
</dd>
<dt><code><em>expression</em> |count|<br> <em>expression</em> |min..max|<br> <em>expression</em> |count, delimiter|<br> <em>expression</em> |min..max, delimiter|</code></dt>

<dd>
<p>Match exact <code>count</code> repetitions of <code>expression</code>. If the match succeeds, return
their match results in an array.</p>

<p>-or-</p>

<p>Match expression at least <code>min</code> but not more then <code>max</code> times. If the match
succeeds, return their match results in an array. Both <code>min</code> and <code>max</code> may
be omitted. If <code>min</code> is omitted, then it is assumed to be <code>0</code>. If <code>max</code> is
omitted, then it is assumed to be infinity. Hence <code>expression |..|</code> is an
equivalent of <code>expression |0..|</code> and <code>expression *</code>. <code>expression |1..|</code> is
equivalent of <code>expression +</code>.</p>

<p>Optionally, <code>delimiter</code> expression can be specified. Delimiter must appear
between expressions exactly once and it is not included in the final array.</p>

<p><code>count</code>, <code>min</code> and <code>max</code> can be represented as:</p>

<ul>
<li>positive integers:
<pre><code class="language-peggy">start = "a"|2|;</code></pre>
</li>
<li>name of the preceding label:
<pre><code class="language-peggy">start = count:n1 "a"|count|;
n1 = n:$[0-9] { parseInt(n); };</code></pre>
</li>
<li>code block:
<pre><code class="language-peggy">start = "a"|{ return options.count; }|;</code></pre>
</li>
Any non-number values will be interpreted as <code>0</code>.
</ul>
</dd>

<dt><code>&amp; <em>expression</em></code></dt>

Expand Down
574 changes: 301 additions & 273 deletions lib/parser.js

Large diffs are not rendered by default.

537 changes: 537 additions & 0 deletions test/behavior/generated-parser-behavior.spec.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/types/peg.test-d.ts
Expand Up @@ -294,6 +294,7 @@ describe("peg.d.ts", () => {
expectType<peggy.LocationRange>(node.location);
expectType<peggy.ast.RepeatedBoundary | null>(node.min);
expectType<peggy.ast.RepeatedBoundary>(node.max);
expectType<peggy.ast.Expression | null>(node.delimiter);
expectType<peggy.ast.Primary>(node.expression);
visit(node.expression);
},
Expand Down

0 comments on commit 4ca6cfb

Please sign in to comment.