Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does moo support all regex syntax? #153

Open
ryanking1809 opened this issue Apr 23, 2021 · 1 comment
Open

Does moo support all regex syntax? #153

ryanking1809 opened this issue Apr 23, 2021 · 1 comment

Comments

@ryanking1809
Copy link

ryanking1809 commented Apr 23, 2021

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?

@gnbl
Copy link

gnbl commented Jan 23, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants