Skip to content

Does moo support all regex syntax? #153

Description

@ryanking1809

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?

Activity

gnbl commented on Jan 23, 2024

@gnbl

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions