moo icon indicating copy to clipboard operation
moo copied to clipboard

Does moo support all regex syntax?

Open ryanking1809 opened this issue 4 years ago • 1 comments

I'm just playing around with https://ablingeroscar.github.io/moo-playground/ and I don't understand why replacing WS: /[ \t]+/ with WS: /[\s]+/ doesn't work. Shouldn't that pick up any white space?

Similarly, I'm not sure why String: /[^]+/ (I'm just testing matching everything) doesn't work either.

Am I missing something?

ryanking1809 avatar Apr 23 '21 06:04 ryanking1809

Short version: the RegEx character class \s for whitespace is not supported, because it includes newlines, and Moo uses multiline RegExps.

I, too, had the issue with \s not working online at https://omrelli.ug/nearley-playground/ (which supports moo).

So I tried moo.compile({whitespace: /\s/}); locally with Nodejs, which gives: Uncaught Error: Rule should declare lineBreaks: /(?:(?:\s))/.

This comes from https://github.com/no-context/moo/blob/main/moo.js#L274.

Finally, https://github.com/no-context/moo#on-regular-expressions states:

Moo uses multiline RegExps. This has a few quirks: for example, the dot /./ doesn't include newlines. Use [^] instead if you want to match newlines too.

Since an excluding character ranges like /[^ ]/ (which matches anything but a space) will include newlines, you have to be careful not to include them by accident! In particular, the whitespace metacharacter \s includes newlines.

gnbl avatar Jan 23 '24 21:01 gnbl