@@ -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 "> @{%
311311const moo = require("moo");
312312
313313const 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: "x" }) } ,
317+ times: /\*/
318318});
319319%}
320320
321321# Pass your lexer object using the @lexer option:
322322@lexer lexer
323323
324+ expr -> multiplication {% id %} | trig {% id %}
325+
324326# Use %token to match any token of that type instead of "token":
325327multiplication -> %number %ws %times %ws %number {% ([first, , , , second]) => first * second %}
326328
327329# Literal strings now match tokens with that text:
328- trig -> "sin" %number</ code > </ pre >
330+ trig -> "sin" %ws % number {% ([, , x]) => Math.sin(x) %} </ code > </ pre >
329331< p > Have a look at < a href ="https://github.com/tjvr/moo#usage "> the Moo documentation</ a > to
330332learn 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
370372main -> %tokenPrint %tokenNumber ";;"
371373
372- # parser.feed(["print", 12, ";;"]);</ code > </ pre >
374+ # parser.feed(["print", 12, ";", " ;"]);</ code > </ pre >
373375
374376
375377 < div id ="footer "> < p > nearley is MIT-licensed. It's maintained by < a
0 commit comments