kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.6k stars 232 forks source link

Using forward slash as a keyword in Moo #546

Closed jnbbender closed 3 years ago

jnbbender commented 3 years ago

I am currently writing the following:

@{%
const moo.require("moo");

const lex = moo.compile({
   ws:          { match: /[ \t]+/, value: w => null }, 
   spd:         ['spd','speed'],
   spd_unit:  ['mps','m/s','mph'],
});
%}

@lexer lex

input -> %spd %ws %spd_unit

This is a very stripped down version.

When I do the following: nearley-test ./grammar.js --input "speed m/s" I get at syntax error at speed m How do I handle a forward slash? I tried escaping it and double-quoting but that did not work.

Floffah commented 3 years ago

i think that is talking about the whole m/s bit not just the /

Floffah commented 3 years ago

try changing spd_unit: ['mps','m/s','mph'], to spd_unit: { match: /m(\/s|ph|ps)/ }? if not im not sure

jnbbender commented 3 years ago

That one didn't work

jnbbender commented 3 years ago

Turns out I had another Moo rule that got in the way: alt_unit: ['meter','mtr','m','feet','ft'] It was resolving to the m in the %alt_unit rule. Apologies for not posting a more complete example.