| 1 |
<!DOCTYPE html> |
| 2 |
<title>Tokenizers - nearley.js - JS Parsing Toolkit</title> |
| 3 |
<meta charset="utf-8"/> |
| 4 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 5 |
<meta name="description" content="nearley.js is a simple, fast, and powerful parser toolkit for JavaScript." /> |
| 6 |
<style> |
| 7 |
body { |
| 8 |
font-family: BlinkMacSystemFont, -apple-system, Helvetica Neue, Open Sans, Ubuntu, Arial, Helvetica, sans-serif; |
| 9 |
font-weight: 300; |
| 10 |
font-size: 15pt; |
| 11 |
margin: 0px; |
| 12 |
padding: 0px; |
| 13 |
} |
| 14 |
|
| 15 |
#main a { |
| 16 |
color: #55a; |
| 17 |
} |
| 18 |
#main a:visited { |
| 19 |
color: #55a; |
| 20 |
} |
| 21 |
#main a:hover { |
| 22 |
color: #259; |
| 23 |
} |
| 24 |
|
| 25 |
h1 { |
| 26 |
font-size: 1.2em; |
| 27 |
font-weight: normal; |
| 28 |
letter-spacing: 0.2em; |
| 29 |
text-align: center; |
| 30 |
margin: 0em; |
| 31 |
padding: 1em; |
| 32 |
} |
| 33 |
|
| 34 |
h1 a { |
| 35 |
color: #559; |
| 36 |
text-decoration: none; |
| 37 |
} |
| 38 |
|
| 39 |
h1 a:hover { |
| 40 |
color: orange; |
| 41 |
cursor: pointer; |
| 42 |
} |
| 43 |
|
| 44 |
#version { |
| 45 |
background: orange; |
| 46 |
border-radius: 50%; |
| 47 |
font-weight: bold; |
| 48 |
color: white; |
| 49 |
width: 3em; |
| 50 |
height: 3em; |
| 51 |
display: inline-block; |
| 52 |
text-align: center; |
| 53 |
line-height: 3em; |
| 54 |
letter-spacing: 0em; |
| 55 |
} |
| 56 |
|
| 57 |
h2 { |
| 58 |
font-size: 1.5em; |
| 59 |
margin-top: 2em; |
| 60 |
} |
| 61 |
h3 { |
| 62 |
margin-top: 3em; |
| 63 |
} |
| 64 |
h4 { |
| 65 |
margin-top: 3em; |
| 66 |
} |
| 67 |
|
| 68 |
.center { |
| 69 |
padding-left: 2em; |
| 70 |
padding-right: 2em; |
| 71 |
margin-left: auto; |
| 72 |
margin-right: auto; |
| 73 |
} |
| 74 |
#main { |
| 75 |
max-width: 35em; |
| 76 |
} |
| 77 |
|
| 78 |
#main p, ul, ol { |
| 79 |
line-height: 1.5em; |
| 80 |
margin-bottom: 1em; |
| 81 |
} |
| 82 |
|
| 83 |
#main li { |
| 84 |
margin-top: 1em; |
| 85 |
margin-bottom: 1em; |
| 86 |
} |
| 87 |
|
| 88 |
#main ul li { |
| 89 |
list-style: none; |
| 90 |
min-width: 10em; |
| 91 |
} |
| 92 |
#main ul li:before { |
| 93 |
display: inline-block; |
| 94 |
content: "\2014"; |
| 95 |
margin-left: -1.5em; |
| 96 |
width: 1.5em; |
| 97 |
position: absolute; |
| 98 |
} |
| 99 |
|
| 100 |
#main textarea { |
| 101 |
width: 100%; |
| 102 |
height: 20em; |
| 103 |
resize: none; |
| 104 |
margin-top: 5px; |
| 105 |
margin-bottom: 5px; |
| 106 |
|
| 107 |
font-family: menlo, monaco, monospace; |
| 108 |
font-size: 10pt; |
| 109 |
padding: 5px; |
| 110 |
|
| 111 |
border: none; |
| 112 |
border-left: solid 1px #aaa; |
| 113 |
} |
| 114 |
#main textarea:focus { |
| 115 |
outline: none; |
| 116 |
} |
| 117 |
|
| 118 |
#main img { |
| 119 |
display: block; |
| 120 |
margin-left: auto; |
| 121 |
margin-right: auto; |
| 122 |
max-width: 100%; |
| 123 |
} |
| 124 |
img + .caption { |
| 125 |
text-align: center; |
| 126 |
font-size: 0.7em; |
| 127 |
color: #aaa; |
| 128 |
} |
| 129 |
|
| 130 |
#footer { |
| 131 |
margin-top: 10em; |
| 132 |
color: #aaa; |
| 133 |
font-size: 0.7em; |
| 134 |
padding: 5em; |
| 135 |
} |
| 136 |
|
| 137 |
code { |
| 138 |
margin-left: 0.2em; |
| 139 |
margin-right: 0.2em; |
| 140 |
padding: 0.2em; |
| 141 |
border-radius: 0.2em; |
| 142 |
background-color: #eee; |
| 143 |
} |
| 144 |
|
| 145 |
pre code { |
| 146 |
border-radius: 0.5em; |
| 147 |
padding: 0.5em; |
| 148 |
display: block; |
| 149 |
overflow: auto; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
ol { |
| 154 |
counter-reset: tutorial-counter; |
| 155 |
position: relative; |
| 156 |
} |
| 157 |
|
| 158 |
ol li { |
| 159 |
list-style: none; |
| 160 |
} |
| 161 |
|
| 162 |
ol li:before { |
| 163 |
content: counter(tutorial-counter); |
| 164 |
counter-increment: tutorial-counter; |
| 165 |
|
| 166 |
font-weight: bold; |
| 167 |
border-radius: 50%; |
| 168 |
background-color: #559; |
| 169 |
width: 2em; |
| 170 |
height: 2em; |
| 171 |
display: inline-block; |
| 172 |
text-align: center; |
| 173 |
line-height: 2em; |
| 174 |
color: white; |
| 175 |
|
| 176 |
margin-left: -2em; |
| 177 |
|
| 178 |
position: absolute; |
| 179 |
left: 1em; |
| 180 |
} |
| 181 |
|
| 182 |
input[type="text"] { |
| 183 |
border: none; |
| 184 |
font-family: monospace; |
| 185 |
font-size: 1em; |
| 186 |
color: #559; |
| 187 |
} |
| 188 |
|
| 189 |
input[type="text"]:focus { |
| 190 |
outline: none; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
nav { |
| 195 |
border-bottom: 2px solid #559; |
| 196 |
background: #fafafa; |
| 197 |
} |
| 198 |
nav ul { |
| 199 |
padding: 0; |
| 200 |
margin: 0; |
| 201 |
} |
| 202 |
nav ul a { |
| 203 |
text-decoration: none; |
| 204 |
color: #334; |
| 205 |
display: block; |
| 206 |
overflow: hidden; |
| 207 |
text-overflow: ellipsis; |
| 208 |
padding: 0.25em 0.5em; */ |
| 209 |
} |
| 210 |
.page { |
| 211 |
display: block; |
| 212 |
} |
| 213 |
|
| 214 |
.page-title-active, |
| 215 |
.page-heading-active a { |
| 216 |
color: #66f; |
| 217 |
} |
| 218 |
|
| 219 |
.page-heading { |
| 220 |
display: block; |
| 221 |
padding-left: 1.5em; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
@media screen and (min-width: 36rem) { |
| 226 |
nav { |
| 227 |
position: fixed; |
| 228 |
width: 16rem; |
| 229 |
left: 0; |
| 230 |
top: 0; |
| 231 |
bottom: 0; |
| 232 |
white-space: nowrap; |
| 233 |
overflow-y: auto; |
| 234 |
font-size: 1rem; |
| 235 |
border-bottom: none; |
| 236 |
border-right: 2px solid #559; |
| 237 |
} |
| 238 |
body.docs-page { |
| 239 |
margin-left: 16rem; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/* highlight.js */ |
| 244 |
|
| 245 |
.hljs{display:block;overflow-x:auto;padding:0.5em;background:#F0F0F0}.hljs,.hljs-subst{color:#444}.hljs-comment{color:#888888}.hljs-keyword,.hljs-attribute,.hljs-selector-tag,.hljs-meta-keyword,.hljs-doctag,.hljs-name{font-weight:bold}.hljs-type,.hljs-string,.hljs-number,.hljs-selector-id,.hljs-selector-class,.hljs-quote,.hljs-template-tag,.hljs-deletion{color:#880000}.hljs-title,.hljs-section{color:#880000;font-weight:bold}.hljs-regexp,.hljs-symbol,.hljs-variable,.hljs-template-variable,.hljs-link,.hljs-selector-attr,.hljs-selector-pseudo{color:#BC6060}.hljs-literal{color:#78A960}.hljs-built_in,.hljs-bullet,.hljs-code,.hljs-addition{color:#397300}.hljs-meta{color:#1f7199}.hljs-meta-string{color:#4d99bf}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:bold} |
| 246 |
|
| 247 |
|
| 248 |
</style> |
| 249 |
|
| 250 |
<body class="docs-page"> |
| 251 |
|
| 252 |
<a href="https://github.com/kach/nearley" class="github-corner"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style> |
| 253 |
<nav id="nav"> |
| 254 |
<h1><a href="/">nearley<span style="color: #559;">.js</span><span id="version">2.20.0</span></a></h1> |
| 255 |
<ul> |
| 256 |
<li class="page"><a href="/docs/index">Home</a> |
| 257 |
<li class="page"><a href="/docs/getting-started">Getting started</a> |
| 258 |
<li class="page"><a href="/docs/grammar">Writing a parser</a> |
| 259 |
<li class="page"><a href="/docs/parser">Using a parser</a> |
| 260 |
<li class="page"> |
| 261 |
<a href="#" class="page-title-active">Tokenizers</a> |
| 262 |
<ul> |
| 263 |
<li class="page-heading"><a href="#lexing-with-moo">Lexing with Moo</a> |
| 264 |
<li class="page-heading"><a href="#custom-lexers">Custom lexers</a> |
| 265 |
<li class="page-heading"><a href="#custom-token-matchers">Custom token matchers</a> |
| 266 |
</ul> |
| 267 |
</li> |
| 268 |
<li class="page"><a href="/docs/tooling">Tooling</a> |
| 269 |
<li class="page"><a href="/docs/how-to-grammar-good">How to grammar good</a> |
| 270 |
<li class="page"><a href="/docs/using-in-frontend">Compiling in browsers</a> |
| 271 |
<li class="page"><a href="/docs/glossary">Glossary</a> |
| 272 |
</ul> |
| 273 |
</nav> |
| 274 |
|
| 275 |
<div id="main" class="center"> |
| 276 |
|
| 277 |
<h2>Tokenizers</h2> |
| 278 |
<p>By default, nearley splits the input into a stream of characters. This is |
| 279 |
called <em>scannerless</em> parsing.</p> |
| 280 |
<p>A tokenizer splits the input into a stream of larger units called <em>tokens</em>. |
| 281 |
This happens in a separate stage before parsing. For example, a tokenizer might |
| 282 |
convert <code>512 + 10</code> into <code>["512", "+", "10"]</code>: notice how it removed the |
| 283 |
whitespace, and combined multi-digit numbers into a single number.</p> |
| 284 |
<p>Using a tokenizer has many benefits. It…</p> |
| 285 |
<ul> |
| 286 |
<li>…often makes your parser faster by more than an order of magnitude.</li> |
| 287 |
<li>…allows you to write cleaner, more maintainable grammars.</li> |
| 288 |
<li>…helps avoid ambiguous grammars in some cases. For example, a tokenizer can |
| 289 |
easily tell you that <code>superclass</code> is a single keyword, not a sequence of |
| 290 |
<code>super</code> and <code>class</code> keywords.</li> |
| 291 |
<li>…gives you <em>lexical information</em> such as line numbers for each token. This |
| 292 |
lets you make better error messages.</li> |
| 293 |
</ul> |
| 294 |
<h3 id="lexing-with-moo">Lexing with Moo</h3> |
| 295 |
<p>The <code>@lexer</code> directive instructs Nearley to use a lexer you’ve defined inside a |
| 296 |
<a href="grammar#additional-js">Javascript block</a> in your grammar.</p> |
| 297 |
<p>nearley supports and recommends <a href="https://github.com/tjvr/moo">Moo</a>, a |
| 298 |
super-fast lexer. Construct a lexer using <code>moo.compile</code>.</p> |
| 299 |
<p>When using a lexer, there are two ways to match tokens:</p> |
| 300 |
<ul> |
| 301 |
<li><p>Use <code>%token</code> to match a token with <strong>type</strong> <code>token</code>.</p> |
| 302 |
<pre><code class="language-ne">line -> words %newline</code></pre> |
| 303 |
</li> |
| 304 |
<li><p>Use <code>"foo"</code> to match a token with <strong>text</strong> <code>foo</code>.</p> |
| 305 |
<p>This is convenient for matching keywords:</p> |
| 306 |
<pre><code class="language-ne">ifStatement -> "if" condition "then" block</code></pre> |
| 307 |
</li> |
| 308 |
</ul> |
| 309 |
<p>Here is an example of a simple grammar:</p> |
| 310 |
<pre><code class="language-coffeescript">@{% |
| 311 |
const moo = require("moo"); |
| 312 |
|
| 313 |
const lexer = moo.compile({ |
| 314 |
ws: /[ \t]+/, |
| 315 |
number: /[0-9]+/, |
| 316 |
word: /[a-z]+/, |
| 317 |
times: /\*|x/ |
| 318 |
}); |
| 319 |
%} |
| 320 |
|
| 321 |
# Pass your lexer object using the @lexer option: |
| 322 |
@lexer lexer |
| 323 |
|
| 324 |
# Use %token to match any token of that type instead of "token": |
| 325 |
multiplication -> %number %ws %times %ws %number {% ([first, , , , second]) => first * second %} |
| 326 |
|
| 327 |
# Literal strings now match tokens with that text: |
| 328 |
trig -> "sin" %number</code></pre> |
| 329 |
<p>Have a look at <a href="https://github.com/tjvr/moo#usage">the Moo documentation</a> to |
| 330 |
learn more about writing a tokenizer.</p> |
| 331 |
<p>You use the parser as usual: call <code>parser.feed(data)</code>, and nearley will give |
| 332 |
you the parsed results in return.</p> |
| 333 |
<h3 id="custom-lexers">Custom lexers</h3> |
| 334 |
<p>nearley recommends using a <a href="https://github.com/tjvr/moo">moo</a>-based lexer. |
| 335 |
However, you can use any lexer that conforms to the following interface:</p> |
| 336 |
<ul> |
| 337 |
<li><code>next()</code> returns a token object, which could have fields for line number, |
| 338 |
etc. Importantly, a token object <em>must</em> have a <code>value</code> attribute.</li> |
| 339 |
<li><code>save()</code> returns an info object that describes the current state of the |
| 340 |
lexer. nearley places no restrictions on this object.</li> |
| 341 |
<li><code>reset(chunk, info)</code> sets the internal buffer of the lexer to <code>chunk</code>, and |
| 342 |
restores its state to a state returned by <code>save()</code>.</li> |
| 343 |
<li><code>formatError(token)</code> returns a string with an error message describing a |
| 344 |
parse error at that token (for example, the string might contain the line and |
| 345 |
column where the error was found).</li> |
| 346 |
<li><code>has(name)</code> returns true if the lexer can emit tokens with that name. This is |
| 347 |
used to resolve <code>%</code>-specifiers in compiled nearley grammars.</li> |
| 348 |
</ul> |
| 349 |
<blockquote> |
| 350 |
<p>Note: if you are searching for a lexer that allows indentation-aware |
| 351 |
grammars (like in Python), you can still use moo. See <a href="https://gist.github.com/nathan/d8d1adea38a1ef3a6d6a06552da641aa">this |
| 352 |
example</a> or |
| 353 |
the |
| 354 |
<a href="https://www.npmjs.com/package/moo-indentation-lexer">moo-indentation-lexer</a> |
| 355 |
module.</p> |
| 356 |
</blockquote> |
| 357 |
<h3 id="custom-token-matchers">Custom token matchers</h3> |
| 358 |
<p>Aside from the lexer infrastructure, nearley provides a lightweight way to |
| 359 |
parse arbitrary streams.</p> |
| 360 |
<p>Custom matchers can be defined in two ways: <em>literal</em> tokens and <em>testable</em> |
| 361 |
tokens. A literal token matches a JS value exactly (with <code>===</code>), while a |
| 362 |
testable token runs a predicate that tests whether or not the value matches.</p> |
| 363 |
<p>Note that in this case, you would feed a <code>Parser</code> instance an <em>array</em> of |
| 364 |
objects rather than a string! Here is a simple example:</p> |
| 365 |
<pre><code class="language-ne">@{% |
| 366 |
const tokenPrint = { literal: "print" }; |
| 367 |
const tokenNumber = { test: x => Number.isInteger(x) }; |
| 368 |
%} |
| 369 |
|
| 370 |
main -> %tokenPrint %tokenNumber ";;" |
| 371 |
|
| 372 |
# parser.feed(["print", 12, ";;"]);</code></pre> |
| 373 |
|
| 374 |
|
| 375 |
<div id="footer"><p>nearley is MIT-licensed. It's maintained by <a |
| 376 |
href="http://hardmath123.github.io">Hardmath123</a> (<a |
| 377 |
href="http://github.com/Hardmath123">Github</a>). You're welcome to |
| 378 |
leave questions, comments, advice, or ideas as Github issues. And |
| 379 |
feel free to fork nearley with your own experiments!<br/> |
| 380 |
|
| 381 |
<a href="https://js.org" target="_blank" title="JS.ORG | JavaScript Community"> |
| 382 |
<img src="https://logo.js.org/dark_horz.png" width="102" alt="JS.ORG Logo"/></a> |
| 383 |
</div> |
| 384 |
|
| 385 |
<script id="highlight-js" async src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script> |
| 386 |
<script> |
| 387 |
document.querySelector('#highlight-js').addEventListener('load', function() { |
| 388 |
[].slice.apply(document.querySelectorAll('pre code')).forEach(function(b) { |
| 389 |
hljs.highlightBlock(b) |
| 390 |
}) |
| 391 |
}) |
| 392 |
</script> |
| 393 |
|
| 394 |
|
| 395 |
<script> |
| 396 |
window.addEventListener('scroll', onScroll) |
| 397 |
window.addEventListener('resize', onScroll) |
| 398 |
|
| 399 |
var timeout |
| 400 |
function onScroll(e) { |
| 401 |
if (timeout) clearTimeout(timeout) |
| 402 |
timeout = setTimeout(scrollEnd, 50) |
| 403 |
} |
| 404 |
|
| 405 |
function scrollEnd() { |
| 406 |
var headings = document.querySelectorAll('h3') |
| 407 |
var offset = 60 |
| 408 |
for (var i=headings.length; i--; ) { |
| 409 |
var h3 = headings[i] |
| 410 |
if (h3.offsetTop < window.scrollY + offset) { |
| 411 |
var cur = document.querySelector('.page-heading-active') |
| 412 |
if (cur) cur.className = 'page-heading' |
| 413 |
document.querySelector('a[href="#' + h3.id + '"]').parentNode.className = 'page-heading page-heading-active' |
| 414 |
return |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
</script> |
| 419 |
|
| 420 |
<script> |
| 421 |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| 422 |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| 423 |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| 424 |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); |
| 425 |
ga('create', 'UA-46120535-5', 'auto'); |
| 426 |
ga('send', 'pageview'); |
| 427 |
</script> |
| 428 |
|