Skip to content

Commit 6e24450

Browse files
authored
Merge pull request #601 from samestep/fix-docs
Fix errors in docs
2 parents 6336c03 + 2605b5d commit 6e24450

File tree

10 files changed

+41
-31
lines changed

10 files changed

+41
-31
lines changed

docs/getting-started.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ <h3 id="nearley-in-3-steps">nearley in 3 steps</h3>
330330
parser.feed(&quot;foo\n&quot;);
331331

332332
// parser.results is an array of possible parsings.
333-
console.log(parser.results); // [[[[ &quot;foo&quot; ],&quot;\n&quot; ]]]</code></pre>
333+
console.log(JSON.stringify(parser.results)); // [[[[[&quot;foo&quot;],&quot;\n&quot;]]]]</code></pre>
334334
<h3 id="whats-next">What’s next?</h3>
335335
<p>Now that you have nearley installed, you can <a href="grammar">learn how to write a
336336
grammar</a>!</p>

docs/grammar.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,9 @@ <h3 id="postprocessors">Postprocessors</h3>
336336
rightOperand: data[2] // data[1] is &quot;+&quot;
337337
};
338338
}
339-
%}</code></pre>
340-
<p>The rule above will parse the string <code>5+10</code> into <code>{ operator: &quot;sum&quot;,
339+
%}
340+
number -&gt; [0-9]:+ {% d =&gt; parseInt(d[0].join(&quot;&quot;)) %}</code></pre>
341+
<p>The rules above will parse the string <code>5+10</code> into <code>{ operator: &quot;sum&quot;,
341342
leftOperand: 5, rightOperand: 10 }</code>.</p>
342343
<p>The postprocessor can be any function with signature <code>function(data, location,
343344
reject)</code>. Here,</p>
@@ -371,10 +372,11 @@ <h3 id="postprocessors">Postprocessors</h3>
371372
rule may be</p>
372373
<pre><code class="language-ne">variable -&gt; [a-z]:+ {%
373374
function(d,l, reject) {
374-
if (d[0] == &#39;if&#39;) {
375+
const name = d[0].join(&#39;&#39;);
376+
if (name === &#39;if&#39;) {
375377
return reject;
376378
} else {
377-
return {&#39;name&#39;: d[0]};
379+
return { name };
378380
}
379381
}
380382
%}</code></pre>
@@ -429,7 +431,7 @@ <h3 id="macros">Macros</h3>
429431
<p>Macros are expanded at compile time and inserted in places they are used. They
430432
are not “real” rules. Therefore, macros <em>cannot</em> be recursive (<code>nearleyc</code> will
431433
go into an infinite loop trying to expand the macro-loop). They must also be
432-
defined <em>before</em> they are used.</p>
434+
defined <em>before</em> they are used (except by other macros).</p>
433435
<h3 id="additional-js">Additional JS</h3>
434436
<p>For more intricate postprocessors, or any other functionality you may need, you
435437
can include chunks of JavaScript code between production rules by surrounding

docs/md/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
8080
parser.feed("foo\n");
8181

8282
// parser.results is an array of possible parsings.
83-
console.log(parser.results); // [[[[ "foo" ],"\n" ]]]
83+
console.log(JSON.stringify(parser.results)); // [[[[["foo"],"\n"]]]]
8484
```
8585

8686
### What's next?

docs/md/grammar.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ expression -> number "+" number {%
8484
};
8585
}
8686
%}
87+
number -> [0-9]:+ {% d => parseInt(d[0].join("")) %}
8788
```
8889

89-
The rule above will parse the string `5+10` into `{ operator: "sum",
90+
The rules above will parse the string `5+10` into `{ operator: "sum",
9091
leftOperand: 5, rightOperand: 10 }`.
9192

9293
The postprocessor can be any function with signature `function(data, location,
@@ -125,10 +126,11 @@ reject)`. Here,
125126
```ne
126127
variable -> [a-z]:+ {%
127128
function(d,l, reject) {
128-
if (d[0] == 'if') {
129+
const name = d[0].join('');
130+
if (name === 'if') {
129131
return reject;
130132
} else {
131-
return {'name': d[0]};
133+
return { name };
132134
}
133135
}
134136
%}
@@ -216,7 +218,7 @@ main -> sentence["Cows", ("." | "!")]
216218
Macros are expanded at compile time and inserted in places they are used. They
217219
are not "real" rules. Therefore, macros *cannot* be recursive (`nearleyc` will
218220
go into an infinite loop trying to expand the macro-loop). They must also be
219-
defined *before* they are used.
221+
defined *before* they are used (except by other macros).
220222

221223
### Additional JS
222224

docs/md/parser.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,19 @@ data.
8484
```js
8585
try {
8686
parser.feed("Cow goes% moo.");
87-
} catch (err) {
87+
} catch (parseError) {
8888
console.log("Error at character " + parseError.offset); // "Error at character 9"
8989
}
9090
```
9191

9292
Errors are nicely formatted for you:
9393

9494
```
95-
Error: invalid syntax at line 1 col 9:
95+
Error: Syntax error at line 1 col 9:
9696
97-
Cow goes% moo
97+
1 Cow goes% moo.
9898
^
99+
99100
Unexpected "%"
100101
```
101102

@@ -115,7 +116,7 @@ parser.feed("moo."); // parser.results is ["yay!"]
115116
If you're done calling `feed()`, but the array is still empty, this indicates
116117
"unexpected end of input". Make sure to check there's at least one result.
117118

118-
If there's more than one result, that indicates ambiguity--read on.
119+
If there's more than one result, that indicates ambiguity--see above.
119120

120121

121122
### Accessing the parse table

docs/md/tokenizers.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,28 @@ When using a lexer, there are two ways to match tokens:
4747
4848
Here is an example of a simple grammar:
4949
50-
```coffeescript
50+
```ne
5151
@{%
5252
const moo = require("moo");
5353
5454
const lexer = moo.compile({
5555
ws: /[ \t]+/,
5656
number: /[0-9]+/,
57-
word: /[a-z]+/,
58-
times: /\*|x/
57+
word: { match: /[a-z]+/, type: moo.keywords({ times: "x" }) },
58+
times: /\*/
5959
});
6060
%}
6161
6262
# Pass your lexer object using the @lexer option:
6363
@lexer lexer
6464
65+
expr -> multiplication {% id %} | trig {% id %}
66+
6567
# Use %token to match any token of that type instead of "token":
6668
multiplication -> %number %ws %times %ws %number {% ([first, , , , second]) => first * second %}
6769
6870
# Literal strings now match tokens with that text:
69-
trig -> "sin" %number
71+
trig -> "sin" %ws %number {% ([, , x]) => Math.sin(x) %}
7072
```
7173

7274
Have a look at [the Moo documentation](https://github.com/tjvr/moo#usage) to
@@ -121,5 +123,5 @@ const tokenNumber = { test: x => Number.isInteger(x) };
121123
122124
main -> %tokenPrint %tokenNumber ";;"
123125
124-
# parser.feed(["print", 12, ";;"]);
126+
# parser.feed(["print", 12, ";", ";"]);
125127
```

docs/md/tooling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The Unparser takes a (compiled) parser and outputs a random string that would
2020
be accepted by the parser.
2121

2222
```bash
23-
$ nearley-unparse -s number <(nearleyc builtin/prims.ne)
23+
$ nearley-unparse -s jsonfloat <(nearleyc builtin/number.ne)
2424
-6.22E94
2525
```
2626

docs/parser.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,15 @@ <h3 id="catching-errors">Catching errors</h3>
323323
data.</p>
324324
<pre><code class="language-js">try {
325325
parser.feed(&quot;Cow goes% moo.&quot;);
326-
} catch (err) {
326+
} catch (parseError) {
327327
console.log(&quot;Error at character &quot; + parseError.offset); // &quot;Error at character 9&quot;
328328
}</code></pre>
329329
<p>Errors are nicely formatted for you:</p>
330-
<pre><code>Error: invalid syntax at line 1 col 9:
330+
<pre><code>Error: Syntax error at line 1 col 9:
331331

332-
Cow goes% moo
332+
1 Cow goes% moo.
333333
^
334+
334335
Unexpected &quot;%&quot;</code></pre><p><strong>After <code>feed()</code> finishes</strong>, the <code>results</code> array will contain all possible
335336
parsings.</p>
336337
<p>If there are no possible parsings given the current input, but in the <em>future</em>
@@ -341,7 +342,7 @@ <h3 id="catching-errors">Catching errors</h3>
341342
parser.feed(&quot;moo.&quot;); // parser.results is [&quot;yay!&quot;]</code></pre>
342343
<p>If you’re done calling <code>feed()</code>, but the array is still empty, this indicates
343344
“unexpected end of input”. Make sure to check there’s at least one result.</p>
344-
<p>If there’s more than one result, that indicates ambiguity–read on.</p>
345+
<p>If there’s more than one result, that indicates ambiguity–see above.</p>
345346
<h3 id="accessing-the-parse-table">Accessing the parse table</h3>
346347
<p>If you are familiar with the Earley parsing algorithm, you can access the
347348
internal parse table using <code>Parser.table</code> (this, for example, is how

docs/tokenizers.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,25 +307,27 @@ <h3 id="lexing-with-moo">Lexing with Moo</h3>
307307
</li>
308308
</ul>
309309
<p>Here is an example of a simple grammar:</p>
310-
<pre><code class="language-coffeescript">@{%
310+
<pre><code class="language-ne">@{%
311311
const moo = require(&quot;moo&quot;);
312312

313313
const lexer = moo.compile({
314314
ws: /[ \t]+/,
315315
number: /[0-9]+/,
316-
word: /[a-z]+/,
317-
times: /\*|x/
316+
word: { match: /[a-z]+/, type: moo.keywords({ times: &quot;x&quot; }) },
317+
times: /\*/
318318
});
319319
%}
320320

321321
# Pass your lexer object using the @lexer option:
322322
@lexer lexer
323323

324+
expr -&gt; multiplication {% id %} | trig {% id %}
325+
324326
# Use %token to match any token of that type instead of &quot;token&quot;:
325327
multiplication -&gt; %number %ws %times %ws %number {% ([first, , , , second]) =&gt; first * second %}
326328

327329
# Literal strings now match tokens with that text:
328-
trig -&gt; &quot;sin&quot; %number</code></pre>
330+
trig -&gt; &quot;sin&quot; %ws %number {% ([, , x]) =&gt; Math.sin(x) %}</code></pre>
329331
<p>Have a look at <a href="https://github.com/tjvr/moo#usage">the Moo documentation</a> to
330332
learn more about writing a tokenizer.</p>
331333
<p>You use the parser as usual: call <code>parser.feed(data)</code>, and nearley will give
@@ -369,7 +371,7 @@ <h3 id="custom-token-matchers">Custom token matchers</h3>
369371

370372
main -&gt; %tokenPrint %tokenNumber &quot;;;&quot;
371373

372-
# parser.feed([&quot;print&quot;, 12, &quot;;;&quot;]);</code></pre>
374+
# parser.feed([&quot;print&quot;, 12, &quot;;&quot;, &quot;;&quot;]);</code></pre>
373375

374376

375377
<div id="footer"><p>nearley is MIT-licensed. It's maintained by <a

docs/tooling.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ <h3 id="nearley-test-exploring-a-parser-interactively">nearley-test: Exploring a
288288
<h3 id="nearley-unparse-the-unparser">nearley-unparse: The Unparser</h3>
289289
<p>The Unparser takes a (compiled) parser and outputs a random string that would
290290
be accepted by the parser.</p>
291-
<pre><code class="language-bash">$ nearley-unparse -s number &lt;(nearleyc builtin/prims.ne)
291+
<pre><code class="language-bash">$ nearley-unparse -s jsonfloat &lt;(nearleyc builtin/number.ne)
292292
-6.22E94</code></pre>
293293
<p>You can use the Unparser to…</p>
294294
<ul>

0 commit comments

Comments
 (0)