Skip to content

Commit

Permalink
Merge pull request #601 from samestep/fix-docs
Browse files Browse the repository at this point in the history
Fix errors in docs
  • Loading branch information
kach authored Jan 7, 2022
2 parents 6336c03 + 2605b5d commit 6e24450
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started.html
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ <h3 id="nearley-in-3-steps">nearley in 3 steps</h3>
parser.feed(&quot;foo\n&quot;);

// parser.results is an array of possible parsings.
console.log(parser.results); // [[[[ &quot;foo&quot; ],&quot;\n&quot; ]]]</code></pre>
console.log(JSON.stringify(parser.results)); // [[[[[&quot;foo&quot;],&quot;\n&quot;]]]]</code></pre>
<h3 id="whats-next">What’s next?</h3>
<p>Now that you have nearley installed, you can <a href="grammar">learn how to write a
grammar</a>!</p>
Expand Down
12 changes: 7 additions & 5 deletions docs/grammar.html
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ <h3 id="postprocessors">Postprocessors</h3>
rightOperand: data[2] // data[1] is &quot;+&quot;
};
}
%}</code></pre>
<p>The rule above will parse the string <code>5+10</code> into <code>{ operator: &quot;sum&quot;,
%}
number -&gt; [0-9]:+ {% d =&gt; parseInt(d[0].join(&quot;&quot;)) %}</code></pre>
<p>The rules above will parse the string <code>5+10</code> into <code>{ operator: &quot;sum&quot;,
leftOperand: 5, rightOperand: 10 }</code>.</p>
<p>The postprocessor can be any function with signature <code>function(data, location,
reject)</code>. Here,</p>
Expand Down Expand Up @@ -371,10 +372,11 @@ <h3 id="postprocessors">Postprocessors</h3>
rule may be</p>
<pre><code class="language-ne">variable -&gt; [a-z]:+ {%
function(d,l, reject) {
if (d[0] == &#39;if&#39;) {
const name = d[0].join(&#39;&#39;);
if (name === &#39;if&#39;) {
return reject;
} else {
return {&#39;name&#39;: d[0]};
return { name };
}
}
%}</code></pre>
Expand Down Expand Up @@ -429,7 +431,7 @@ <h3 id="macros">Macros</h3>
<p>Macros are expanded at compile time and inserted in places they are used. They
are not “real” rules. Therefore, macros <em>cannot</em> be recursive (<code>nearleyc</code> will
go into an infinite loop trying to expand the macro-loop). They must also be
defined <em>before</em> they are used.</p>
defined <em>before</em> they are used (except by other macros).</p>
<h3 id="additional-js">Additional JS</h3>
<p>For more intricate postprocessors, or any other functionality you may need, you
can include chunks of JavaScript code between production rules by surrounding
Expand Down
2 changes: 1 addition & 1 deletion docs/md/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed("foo\n");

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

### What's next?
Expand Down
10 changes: 6 additions & 4 deletions docs/md/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ expression -> number "+" number {%
};
}
%}
number -> [0-9]:+ {% d => parseInt(d[0].join("")) %}
```

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

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

### Additional JS

Expand Down
9 changes: 5 additions & 4 deletions docs/md/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ data.
```js
try {
parser.feed("Cow goes% moo.");
} catch (err) {
} catch (parseError) {
console.log("Error at character " + parseError.offset); // "Error at character 9"
}
```

Errors are nicely formatted for you:

```
Error: invalid syntax at line 1 col 9:
Error: Syntax error at line 1 col 9:
Cow goes% moo
1 Cow goes% moo.
^
Unexpected "%"
```

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

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


### Accessing the parse table
Expand Down
12 changes: 7 additions & 5 deletions docs/md/tokenizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,28 @@ When using a lexer, there are two ways to match tokens:
Here is an example of a simple grammar:
```coffeescript
```ne
@{%
const moo = require("moo");
const lexer = moo.compile({
ws: /[ \t]+/,
number: /[0-9]+/,
word: /[a-z]+/,
times: /\*|x/
word: { match: /[a-z]+/, type: moo.keywords({ times: "x" }) },
times: /\*/
});
%}
# Pass your lexer object using the @lexer option:
@lexer lexer
expr -> multiplication {% id %} | trig {% id %}
# Use %token to match any token of that type instead of "token":
multiplication -> %number %ws %times %ws %number {% ([first, , , , second]) => first * second %}
# Literal strings now match tokens with that text:
trig -> "sin" %number
trig -> "sin" %ws %number {% ([, , x]) => Math.sin(x) %}
```

Have a look at [the Moo documentation](https://github.com/tjvr/moo#usage) to
Expand Down Expand Up @@ -121,5 +123,5 @@ const tokenNumber = { test: x => Number.isInteger(x) };
main -> %tokenPrint %tokenNumber ";;"
# parser.feed(["print", 12, ";;"]);
# parser.feed(["print", 12, ";", ";"]);
```
2 changes: 1 addition & 1 deletion docs/md/tooling.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The Unparser takes a (compiled) parser and outputs a random string that would
be accepted by the parser.

```bash
$ nearley-unparse -s number <(nearleyc builtin/prims.ne)
$ nearley-unparse -s jsonfloat <(nearleyc builtin/number.ne)
-6.22E94
```

Expand Down
9 changes: 5 additions & 4 deletions docs/parser.html
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,15 @@ <h3 id="catching-errors">Catching errors</h3>
data.</p>
<pre><code class="language-js">try {
parser.feed(&quot;Cow goes% moo.&quot;);
} catch (err) {
} catch (parseError) {
console.log(&quot;Error at character &quot; + parseError.offset); // &quot;Error at character 9&quot;
}</code></pre>
<p>Errors are nicely formatted for you:</p>
<pre><code>Error: invalid syntax at line 1 col 9:
<pre><code>Error: Syntax error at line 1 col 9:

Cow goes% moo
1 Cow goes% moo.
^

Unexpected &quot;%&quot;</code></pre><p><strong>After <code>feed()</code> finishes</strong>, the <code>results</code> array will contain all possible
parsings.</p>
<p>If there are no possible parsings given the current input, but in the <em>future</em>
Expand All @@ -341,7 +342,7 @@ <h3 id="catching-errors">Catching errors</h3>
parser.feed(&quot;moo.&quot;); // parser.results is [&quot;yay!&quot;]</code></pre>
<p>If you’re done calling <code>feed()</code>, but the array is still empty, this indicates
“unexpected end of input”. Make sure to check there’s at least one result.</p>
<p>If there’s more than one result, that indicates ambiguity–read on.</p>
<p>If there’s more than one result, that indicates ambiguity–see above.</p>
<h3 id="accessing-the-parse-table">Accessing the parse table</h3>
<p>If you are familiar with the Earley parsing algorithm, you can access the
internal parse table using <code>Parser.table</code> (this, for example, is how
Expand Down
12 changes: 7 additions & 5 deletions docs/tokenizers.html
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,27 @@ <h3 id="lexing-with-moo">Lexing with Moo</h3>
</li>
</ul>
<p>Here is an example of a simple grammar:</p>
<pre><code class="language-coffeescript">@{%
<pre><code class="language-ne">@{%
const moo = require(&quot;moo&quot;);

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

# Pass your lexer object using the @lexer option:
@lexer lexer

expr -&gt; multiplication {% id %} | trig {% id %}

# Use %token to match any token of that type instead of &quot;token&quot;:
multiplication -&gt; %number %ws %times %ws %number {% ([first, , , , second]) =&gt; first * second %}

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

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

# parser.feed([&quot;print&quot;, 12, &quot;;;&quot;]);</code></pre>
# parser.feed([&quot;print&quot;, 12, &quot;;&quot;, &quot;;&quot;]);</code></pre>


<div id="footer"><p>nearley is MIT-licensed. It's maintained by <a
Expand Down
2 changes: 1 addition & 1 deletion docs/tooling.html
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ <h3 id="nearley-test-exploring-a-parser-interactively">nearley-test: Exploring a
<h3 id="nearley-unparse-the-unparser">nearley-unparse: The Unparser</h3>
<p>The Unparser takes a (compiled) parser and outputs a random string that would
be accepted by the parser.</p>
<pre><code class="language-bash">$ nearley-unparse -s number &lt;(nearleyc builtin/prims.ne)
<pre><code class="language-bash">$ nearley-unparse -s jsonfloat &lt;(nearleyc builtin/number.ne)
-6.22E94</code></pre>
<p>You can use the Unparser to…</p>
<ul>
Expand Down

0 comments on commit 6e24450

Please sign in to comment.