Skip to content

Regular expressions can't start with whitespace #3756

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

Closed
cscott opened this issue Dec 11, 2014 · 5 comments
Closed

Regular expressions can't start with whitespace #3756

cscott opened this issue Dec 11, 2014 · 5 comments

Comments

@cscott
Copy link

cscott commented Dec 11, 2014

This might be bug or an "undocumented feature" -- but it's the latter, it should be better documented.
See also #3182 (regexps can't start with the equals sign either).

$ coffee -e '/a/'
$ coffee -e '/ a/'
[stdin]:1:1: error: unexpected /
/ a/
^
$ coffee -e 'console.log / a/'
[stdin]:1:17: error: unexpected end of input
console.log / a/
                ^
$ coffee -e 'console.log(/ a/)'
[stdin]:1:13: error: unexpected /
console.log(/ a/)
            ^
$ coffee -e 'r=/ a/'
[stdin]:1:3: error: unexpected /
r=/ a/
  ^

Escaping the initial whitespace as /\ a/ is a workaround.

@xixixao
Copy link
Contributor

xixixao commented Dec 11, 2014

@cscott @phadej Yes, this is an undocumented part of the syntax rules (well it has been documented many times in various issues, but I appreciate that not every CS user will go search through them when they encouter this). I suggest we add it to the Block Regular Expressions section, possibly renaming it to Regular Expressions.

@cscott
Copy link
Author

cscott commented Dec 11, 2014

A renamed 'Regular Expressions' section would be good. In that section you might link to http://www-archive.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions (by way of explaining the possible ambiguity with division) and mention the following:

  • division operator ambiguity
  • can't start with equals (any other bad characters?)
  • can't start with whitespace
  • just backslash-escape the first character to ensure it will be treated as a regexp

wrt Block Regular Expressions, I noticed that it was missing an example of interpolation. It is probably worth adding and example and explicitly mentioning that the interpolation is a string and not escaped. That is:

r1 = /\b/; # matches word boundary
r2 = /// #{r1} ///; # Never matches anything, because it wants a word boundary between two slash characters (!)
r3 = /// #{r1.source} ///; # yes

ext = ".jpg"
r4 = /// filename#{ext} ///; # matches filenameXext, the dot is not escaped

(As a feature request, it might be nice to have a "regular expression interpolation" mode which would use RegExp#source if the value was a regexp and would otherwise convert the value to a string and then do the proper regexp escaping to match the literal string. But that's another issue.)

@xixixao
Copy link
Contributor

xixixao commented Dec 11, 2014

I think adding something like

Leading space or = sign must be escaped with \.

will suffice.

@lydell
Copy link
Collaborator

lydell commented Jan 8, 2015

It would be great if the error messages were better: A / is not unexpected when it could have been a regex. Explicitly telling that the / (or /=) was parsed as division would be better:

$ coffee -e '/ a/'
[stdin]:1:1: error: unexpected / (division)
/ a/
^
$ coffee -e 'console.log(/ a/)'
[stdin]:1:13: error: unexpected / (division)
console.log(/ a/)
            ^
$ coffee -e 'r=/ a/'
[stdin]:1:3: error: unexpected / (division)
r=/ a/
  ^

or something like that.

Does anyone know how to customize Jison to do this? Edit: Never mind, I figured it out.

@lydell
Copy link
Collaborator

lydell commented Jan 10, 2015

#3782 + the section I just added in the Common Gotchas wiki page should be enough in my opinion. No reason to add edge cases to the docs.

Regarding my comment about those error messages above: They’re not needed anymore because of #3782.

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

No branches or pull requests

5 participants