Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a small syntax cheatsheet #587

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions docs/grammar.html
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ <h1><a href="/">nearley<span style="color: #559;">.js</span><span id="version">2
<a href="#" class="page-title-active">Writing a parser</a>
<ul>
<li class="page-heading"><a href="#vocabulary">Vocabulary</a>
<li class="page-heading"><a href="#matching-rules">Matching Rules</a>
<li class="page-heading"><a href="#postprocessors">Postprocessors</a>
<li class="page-heading"><a href="#more-syntax-tips-and-tricks">More syntax: tips and tricks</a>
<li class="page-heading"><a href="#macros">Macros</a>
Expand Down Expand Up @@ -319,6 +320,22 @@ <h3 id="vocabulary">Vocabulary</h3>
following nonterminal matches zero or more <code>cow</code>s in a row, such as
<code>cowcowcow</code>:</p>
<pre><code class="language-ne">a -&gt; null | a &quot;cow&quot;</code></pre>
<h3 id="matching-rules">Matching Rules</h3>
<p>Syntax for matching input if you’re not using a tokenizer.</p>
<ul>
<li><code>&quot;if&quot;</code> - match <code>if</code> literally</li>
<li><code>&quot;if&quot;i</code> - match <code>if</code> case-insensitive (1)</li>
<li><code>.</code> - match any symbol except newline</li>
<li><code>[^]</code> - match anything including newline</li>
<li><code>[a-zA-Z]</code> - match symbols in range (2)</li>
<li><code>[^a-zA-Z]</code> - match symbols not in range</li>
<li><code>:+</code> - match previous construction one or more time</li>
<li><code>:*</code> - match zero times or more</li>
<li><code>:?</code> - zero time or one time</li>
</ul>
<p>(1) <code>i</code> suffix will <em>not</em> work with a lexer if you’re using one. Your lexer
should use the <code>i</code> flag in its regexes instead.</p>
<p>(2) RegExp charsets will not work with <a href="tokenizers">tokenizer</a>.</p>
<h3 id="postprocessors">Postprocessors</h3>
<p>By default, nearley wraps everything matched by a rule into an array. For
example, when <code>rule -&gt; &quot;tick&quot; &quot;tock&quot;</code> matches the string <code>&quot;ticktock&quot;</code>, it
Expand Down Expand Up @@ -395,16 +412,6 @@ <h4 id="comments">Comments</h4>
<p>Comments are marked with ‘#’. Everything from <code>#</code> to the end of a line is
ignored:</p>
<pre><code class="language-ne">expression -&gt; number &quot;+&quot; number # sum of two numbers</code></pre>
<h4 id="charsets">Charsets</h4>
<p>You can use valid RegExp charsets in a rule (unless you’re using a
<a href="tokenizers">tokenizer</a>):</p>
<pre><code>not_a_letter -&gt; [^a-zA-Z]</code></pre><p>The <code>.</code> character can be used to represent any character.</p>
<h4 id="case-insensitive-string-literals">Case-insensitive string literals</h4>
<p>You can create case-insensitive string literals by adding an <code>i</code> after the
string literal:</p>
<pre><code>cow -&gt; &quot;cow&quot;i # matches CoW, COW, and so on.</code></pre><p>Note that if you are using a lexer, your lexer should use the <code>i</code> flag in its
regexes instead. That is, if you are using a lexer, you should <em>not</em> use the
<code>i</code> suffix in nearley.</p>
<h4 id="ebnf">EBNF</h4>
<p>nearley supports the <code>*</code>, <code>?</code>, and <code>+</code> operators from
<a href="https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form">EBNF</a> as shown:</p>
Expand Down
40 changes: 20 additions & 20 deletions docs/md/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ following nonterminal matches zero or more `cow`s in a row, such as
a -> null | a "cow"
```

### Matching Rules

Syntax for matching input if you're not using a tokenizer.

* `"if"` - match `if` literally
* `"if"i` - match `if` case-insensitive (1)
* `.` - match any symbol except newline
* `[^]` - match anything including newline
* `[a-zA-Z]` - match symbols in range (2)
* `[^a-zA-Z]` - match symbols not in range
* `:+` - match previous construction one or more time
* `:*` - match zero times or more
* `:?` - zero time or one time

(1) `i` suffix will *not* work with a lexer if you're using one. Your lexer
should use the `i` flag in its regexes instead.

(2) RegExp charsets will not work with [tokenizer](tokenizers).


### Postprocessors

By default, nearley wraps everything matched by a rule into an array. For
Expand Down Expand Up @@ -154,26 +174,6 @@ ignored:
expression -> number "+" number # sum of two numbers
```

#### Charsets

You can use valid RegExp charsets in a rule (unless you're using a
[tokenizer](tokenizers)):

not_a_letter -> [^a-zA-Z]

The `.` character can be used to represent any character.

#### Case-insensitive string literals

You can create case-insensitive string literals by adding an `i` after the
string literal:

cow -> "cow"i # matches CoW, COW, and so on.

Note that if you are using a lexer, your lexer should use the `i` flag in its
regexes instead. That is, if you are using a lexer, you should *not* use the
`i` suffix in nearley.

#### EBNF

nearley supports the `*`, `?`, and `+` operators from
Expand Down
1 change: 0 additions & 1 deletion docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.