forked from kach/nearley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
368 lines (311 loc) · 16.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<html>
<head>
<title>nearley</title>
<link href='http://fonts.googleapis.com/css?family=Roboto:300,700'
rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="www/main.css"/>
<script charset="utf-8" src="lib/nearley.js"></script>
<script charset="utf-8" src="examples/js/csscolor.js"></script>
<!--
<script charset="utf-8" src="lib/compile.js"></script>
<script charset="utf-8" src="lib/generate.js"></script>
<script charset="utf-8" src="lib/nearley-language-bootstrapped.js"></script>
<script charset="utf-8" src="www/cytoscape.min.js"></script>
<script charset="utf-8" src="www/main.js"></script>
-->
</head>
<body>
<!--<a href="https://github.com/Hardmath123/nearley"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>-->
<a href="https://github.com/Hardmath123/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>
<div id="main" class="center">
<h1>nearley<span style="color: #559;">.js</span><span id="version">2.0.0</span></h1>
<p><strong>Parsers</strong> turn strings of characters into
meaningful data structures (like a JSON object!).
<strong>nearley</strong> is a <strong>fast</strong>,
<strong>feature-rich</strong>, and <strong>modern</strong> parser
toolkit for JavaScript. nearley is an <a
href="https://github.com/npm/npm-collection-staff-picks">npm Staff
Pick</a>.
<h2>nearley 101</h2>
<ol class="tutorial">
<li>Install: <code>$ npm install -g nearley</code> (or try
nearley live in your browser <a
href="https://omrelli.ug/nearley-playground/">here</a>!)</li>
<li>Write your grammar:
<pre><code># Match a CSS color
# http://www.w3.org/TR/css3-color/#colorunits
@builtin "whitespace.ne" # `_` means arbitrary amount of whitespace
@builtin "number.ne" # `int`, `decimal`, and `percentage` number primitives
csscolor -> "#" hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit
| "#" hexdigit hexdigit hexdigit
| "rgb" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")"
| "hsl" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")"
| "rgba" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")"
| "hsla" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")"
hexdigit -> [a-fA-F0-9]
colnum -> int | percentage
</code></pre>
</li>
<li>Compile your grammar:
<pre><code>$ nearleyc csscolor.ne -o csscolor.js</code></pre>
</li>
<li>Test your grammar:
<pre><code>$ nearley-test -i "#00ff00" csscolor.js
Parse results:
[ [ '#', [ '0' ], [ '0' ], [ 'f' ], [ 'f' ], [ '0' ], [ '0' ] ] ]
</code></pre></li>
<li>Turn your grammar into a generator:
<pre><code>$ nearley-unparse -n 3 csscolor.js
#Ab21F2
rgb ( -29.889%,7,8172)
#a40
</code></pre></li>
<li>You try it! Type a CSS color here:
<input type="text" id="parse-input" placeholder="rgba(0, 255, 0, 0.5)"></input>
<pre><code id="parse-output">…and the parsed output will appear here!
</code></pre>
<script type="text/javascript">
window.addEventListener('load', function() {
document.getElementById('parse-input').addEventListener('keyup', function() {
// $ ./bin/nearleyc.js examples/csscolor.ne -o examples/js/csscolor.js
var str = this.value;
var parser = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);
try {
parser.feed(str);
document.getElementById('parse-output').innerHTML = JSON.stringify(parser.results, null, 2);
this.style.color = "#559";
} catch(e) {
document.getElementById('parse-output').innerHTML = "";
this.style.color = "red";
}
}, false);
var X = new XMLHttpRequest();
X.open('GET', 'package.json');
X.onreadystatechange = function() {
if (X.readyState === 4 && X.status === 200 && X.responseText) {
var J = JSON.parse(X.responseText);
document.getElementById("version").innerHTML = J.version || "2.0.0";
}
};
X.send();
}, false);
</script>
</li>
<li>Create beautiful railroad diagrams to document your grammar
formally.
<pre><code>$ nearley-railroad csscolor.ne -o csscolor.html</pre></code>
Output looks like:
<br/>
<style type="text/css">
svg.railroad-diagram {
background-color: inherit;
}
svg.railroad-diagram path {
stroke-width: 3;
stroke: black;
fill: rgba(0,0,0,0);
}
svg.railroad-diagram text {
font: bold 14px monospace;
text-anchor: middle;
}
svg.railroad-diagram text.diagram-text {
font-size: 12px;
}
svg.railroad-diagram text.diagram-arrow {
font-size: 16px;
}
svg.railroad-diagram text.label {
text-anchor: start;
}
svg.railroad-diagram text.comment {
font: italic 12px monospace;
}
svg.railroad-diagram g.non-terminal text {
/*font-style: italic;*/
}
svg.railroad-diagram rect {
stroke-width: 3;
stroke: black;
fill: hsl(120,100%,90%);
}
svg.railroad-diagram path.diagram-text {
stroke-width: 3;
stroke: black;
fill: white;
cursor: help;
}
svg.railroad-diagram g.diagram-text:hover path.diagram-text {
fill: #eee;
}
</style>
<svg class="railroad-diagram" width="244" height="62" viewBox="0 0 244 62">
<g transform="translate(.5 .5)">
<path d="M 20 21 v 20 m 10 -20 v 20 m -10 -10 h 20.5"></path>
<g>
<path d="M40 31h0"></path>
<path d="M204 31h0"></path>
<path d="M40 31h20"></path>
<g>
<path d="M60 31h0"></path>
<path d="M184 31h0"></path>
<g class="terminal">
<path d="M60 31h0"></path>
<path d="M184 31h0"></path>
<rect x="60" y="20" width="124" height="22" rx="10" ry="10"></rect>
<text x="122" y="35">/[a-fA-F0-9]/</text>
</g>
</g>
<path d="M184 31h20"></path>
</g>
<path d="M 204 31 h 20 m -10 -10 v 20 m 10 -20 v 20"></path>
</g>
</svg>
<p>See a bigger example <a href="www/railroad-demo.html">here</a>.</p>
</li>
</ol>
<h2>Features</h2>
<ul>
<li>nearley is the first JS parser to use the
<strong>Earley</strong> algorithm (insert your own ‘early
bird’ pun here). It also implements Joop Leo's
optimizations for right-recursion, making it effectively
<strong>linear-time</strong> for LL(k) grammars.
<li>nearley lives happily in <strong>node</strong>, but doesn't
mind the <strong>browser</strong>.
<li>nearley outputs <strong>small</strong> files. And its
<strong>expressive</strong> DSL comes with plenty of
<strong>syntactic sugar</strong> to keep your source files
short. And sweet.
<li>nearley's grammar language is powerful and expressive: you
can use <strong>macros</strong>, import from a large
<strong>builtin library</strong> of pre-defined parser-pieces,
use a <strong>tokenizer</strong> for extra performance, and
more!
<li>nearley is built on an idiomatic <strong>streaming
API</strong>. You even have access to partial parses to build
<strong>predictive</strong> user interfaces.
<li>nearley processes <strong>left recursion</strong> without
choking. In fact, nearley will parse anything you throw at it
without complaining or going into a <s>sulk</s> infinite loop.
<li>nearley handles <strong>ambiguous grammars</strong>
gracefully. Ambiguous grammars can be parsed in multiple ways:
instead of getting confused, nearley gives you all the parsings
(in a deterministic order!).
<li>nearley allows for debugging with generous <strong>error
detection</strong>. When it catches a parse-time error, nearley
tells you exactly what went wrong and where.
<li>nearley is powerful enough to be
<strong>bootstrapped</strong>. That means nearley uses nearley
to compile parts of nearley. <em>nearleyception!</em>
<li>nearley parsers can be inverted to form
<strong>generators</strong> which output random strings that
match a grammar. Useful for writing <strong>test
cases</strong>, <strong>fuzzers</strong>, and
<strong>Mad-Libs</strong>.<br/>
<li>You can export nearley parsers as <strong>railroad
diagrams</strong>, which provide easy-to-understand
documentation of your grammar.
</ul>
<h2>Projects using nearley</h2>
<ul>
<li><strong>Artificial Intelligence and NLP</strong>: <a
href="https://github.com/ChalmersGU-AI-course/shrdlite-course-project">Shrdlite</a>
is a programming project in Artificial Intelligence, a course
given at the University of Gothenburg and Chalmers University
of Technology. It uses nearley for reading instructions in
natural language (i.e. English).</a>
<li><strong>Standard formats</strong>: <a
href="https://github.com/raymond-h/node-dmi">node-dmi</a> is a
module that reads iconstate metadata from BYOND DMI files, <a
href="https://github.com/inukshuk/edtf.js">edtf.js</a> is a
parser for Extended Date Time Format, <a
href="https://github.com/farskipper/node-krl-parser">node-krl-parser</a>
is a KRL parser for node, <a
href="https://github.com/digitalheir/bibliography-js">bibliography</a>
is a BibTeX-to-HTML converter, <a
href="https://github.com/fiduswriter/biblatex-csl-converter">biblatex-csl-converter</a>
converts between bibtex/CSL/JSON, <a
href="https://github.com/gajus/scalpel">scalpel</a> parses CSS
selectors, <a
href="https://github.com/waratuman/rfc5545-rrule">rfc5545-rrule</a>
helps parse iCalendar data.
<li><strong>Templating and files</strong>: <a
href="https://github.com/bobbybee/uPresent">uPresent</a> is a
markdown-based presentation authoring system, <a
href="https://github.com/rtsao/saison">saison</a> is a minimal
templating language, <a
href="https://github.com/imsky/packdown">Packdown</a> is a tool
to generate human-readable archives of multiple files.
<li><strong>Programming languages</strong>: <a
href="https://github.com/bobbybee/carbon">Carbon</a> is a C
subset that compiles to JavaScript, optimized for game
development, <a
href="https://github.com/tleb/ezlang">ezlang</a> is a simple
language, <a
href="https://github.com/liam4/tlnccuwagnf">tlnccuwagnf</a> is
a fun general-purpose language, <a
href="https://github.com/nanalan/g">nanalang</a> is a silly
esoteric language, <a
href="https://github.com/nanalan/english">english</a> is a less
esoteric programming language, <a
href="https://github.com/farskipper/ecmaless">ecmaless</a> is
an easily-extensible language, <a
href="https://github.com/xodio/hm-parser">hm-parser</a> parses
Haskell-like Hindley-Milner type signatures, <a
href="https://github.com/kozily/web">kozily</a> implements the
Oz language, <a
href="https://stefan1niculae.github.io/abstract-machine/">abstract-machine</a>
inspects execution models.
<li><strong>Mathematics</strong>: <a
href="https://github.com/andrejewski/solvent">Solvent</a> is a
powerful desktop calculator, <a
href="https://github.com/andrejewski/truth-table">Truth-table</a>
is a tool to visualize propositional logic in truth tables, <a
href="http://emunotes.com">Emunotes</a> is a personal Wiki with
inline graphing and computation.
<li><strong>Domain-specific languages</strong>: <a
href="https://github.com/jcorbin/hexant">Hexant</a> is a
cellular automata simulator with a DSL for custom automata, <a
href="https://github.com/justjake/dicetower">Dicetower</a> is
an advanced dice plugin for hubot, <a
href="https://github.com/seiyria/deck.zone">deck.zone</a> is a
language to create board games, <a
href="https://github.com/danigb/in-seconds">in-seconds</a> is a
time calculator for music applications, <a
href="https://github.com/bumbu/website-spec">website-spec</a>
is a tool for functional web testing, <a
href="https://idyll-lang.github.io/idyll/">idyll</a> is a
markup language for data-driven documents, <a
href="https://github.com/affinipay/virtsecgroup">virtsecgroup</a>
provides virtual AWS security groups.
<li><strong>Parsing libraries</strong>: <a
href="http://nearley.js.org">nearley</a> is a parser toolkit
for JavaScript. It has a nearley-based DSL to specify parsers.
</ul>
<p>Excited? Get started on
<a href="https://github.com/Hardmath123/nearley">Github</a>, visit
us on <a href="http://npmjs.org/package/nearley">npm</a>, explore
nearley in your browser on the <a
href="https://omrelli.ug/nearley-playground/">playground</a>, or
try out the <a href="examples/calculator/">calculator demo</a> for
more action.
<!--
<h2>Try nearley</h2>
<p>Enter a nearley grammar here and press the button. nearley will
parse it into an AST and compile the AST into a JavaScript file.
<br/><textarea id="grammar-input">a -> a "1" | "1"</textarea>
<button id="compile-grammar">Make me a grammar!</button>
<div id="cy-viz"></div>
-->
<div id="footer"><p>nearley is MIT-licensed. It's maintained by <a
href="http://hardmath123.github.io">Hardmath123</a> (<a
href="http://github.com/Hardmath123">Github</a>). You're welcome to
leave questions, comments, advice, or ideas as Github issues. And
feel free to fork nearley with your own experiments!
<p><a href="http://js.org" target="_blank" title="JS.ORG | JavaScript Community">
<img src="http://logo.js.org/dark_tiny.png" width="102" alt="JS.ORG Logo"/></a></div>
</div>
</body>
</html>