2010-12-29 00:48:54 -05:00
|
|
|
|
# Regular Expression Literals
|
|
|
|
|
# ---------------------------
|
|
|
|
|
|
2011-01-03 04:17:00 -05:00
|
|
|
|
# TODO: add method invocation tests: /regex/.toString()
|
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
|
# * Regexen
|
|
|
|
|
# * Heregexen
|
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
test "basic regular expression literals", ->
|
|
|
|
|
ok 'a'.match(/a/)
|
|
|
|
|
ok 'a'.match /a/
|
|
|
|
|
ok 'a'.match(/a/g)
|
|
|
|
|
ok 'a'.match /a/g
|
|
|
|
|
|
|
|
|
|
test "division is not confused for a regular expression", ->
|
2015-01-09 19:48:00 -05:00
|
|
|
|
# Any spacing around the slash is allowed when it cannot be a regex.
|
2010-12-29 00:48:54 -05:00
|
|
|
|
eq 2, 4 / 2 / 1
|
2015-01-09 19:48:00 -05:00
|
|
|
|
eq 2, 4/2/1
|
|
|
|
|
eq 2, 4/ 2 / 1
|
|
|
|
|
eq 2, 4 /2 / 1
|
|
|
|
|
eq 2, 4 / 2/ 1
|
|
|
|
|
eq 2, 4 / 2 /1
|
|
|
|
|
eq 2, 4 /2/ 1
|
|
|
|
|
|
|
|
|
|
a = (regex) -> regex.test 'a b c'
|
|
|
|
|
a.valueOf = -> 4
|
2010-12-29 00:48:54 -05:00
|
|
|
|
b = 2
|
|
|
|
|
g = 1
|
|
|
|
|
|
2015-01-09 19:48:00 -05:00
|
|
|
|
eq 2, a / b/g
|
|
|
|
|
eq 2, a/ b/g
|
|
|
|
|
eq 2, a / b/ g
|
|
|
|
|
eq 2, a / b/g # Tabs.
|
|
|
|
|
eq 2, a / b/g # Non-breaking spaces.
|
|
|
|
|
eq true, a /b/g
|
|
|
|
|
# Use parentheses to disambiguate.
|
|
|
|
|
eq true, a(/ b/g)
|
|
|
|
|
eq true, a(/ b/)
|
|
|
|
|
eq true, a (/ b/)
|
|
|
|
|
# Escape to disambiguate.
|
|
|
|
|
eq true, a /\ b/g
|
|
|
|
|
eq false, a /\ b/g
|
|
|
|
|
eq true, a /\ b/
|
2011-04-23 13:33:35 -04:00
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
obj = method: -> 2
|
|
|
|
|
two = 2
|
|
|
|
|
eq 2, (obj.method()/two + obj.method()/two)
|
|
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
|
eq 2, (4)/2/i
|
|
|
|
|
eq 1, i/i/i
|
|
|
|
|
|
2015-01-09 19:48:00 -05:00
|
|
|
|
a = ''
|
|
|
|
|
a += ' ' until / /.test a
|
|
|
|
|
eq a, ' '
|
|
|
|
|
|
|
|
|
|
a = if /=/.test '=' then yes else no
|
|
|
|
|
eq a, yes
|
|
|
|
|
|
|
|
|
|
a = if !/=/.test '=' then yes else no
|
|
|
|
|
eq a, no
|
|
|
|
|
|
|
|
|
|
#3182:
|
|
|
|
|
match = 'foo=bar'.match /=/
|
|
|
|
|
eq match[0], '='
|
|
|
|
|
|
|
|
|
|
#3410:
|
|
|
|
|
ok ' '.match(/ /)[0] is ' '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test "division vs regex after a callable token", ->
|
|
|
|
|
b = 2
|
|
|
|
|
g = 1
|
|
|
|
|
r = (r) -> r.test 'b'
|
|
|
|
|
|
|
|
|
|
a = 4
|
|
|
|
|
eq 2, a / b/g
|
|
|
|
|
eq 2, a/b/g
|
|
|
|
|
eq 2, a/ b/g
|
|
|
|
|
eq true, r /b/g
|
|
|
|
|
eq 2, (1 + 3) / b/g
|
|
|
|
|
eq 2, (1 + 3)/b/g
|
|
|
|
|
eq 2, (1 + 3)/ b/g
|
|
|
|
|
eq true, (r) /b/g
|
|
|
|
|
eq 2, [4][0] / b/g
|
|
|
|
|
eq 2, [4][0]/b/g
|
|
|
|
|
eq 2, [4][0]/ b/g
|
|
|
|
|
eq true, [r][0] /b/g
|
|
|
|
|
eq 0.5, 4? / b/g
|
|
|
|
|
eq 0.5, 4?/b/g
|
|
|
|
|
eq 0.5, 4?/ b/g
|
|
|
|
|
eq true, r? /b/g
|
|
|
|
|
(->
|
|
|
|
|
eq 2, @ / b/g
|
|
|
|
|
eq 2, @/b/g
|
|
|
|
|
eq 2, @/ b/g
|
|
|
|
|
).call 4
|
|
|
|
|
(->
|
|
|
|
|
eq true, @ /b/g
|
|
|
|
|
).call r
|
|
|
|
|
(->
|
|
|
|
|
eq 2, this / b/g
|
|
|
|
|
eq 2, this/b/g
|
|
|
|
|
eq 2, this/ b/g
|
|
|
|
|
).call 4
|
|
|
|
|
(->
|
|
|
|
|
eq true, this /b/g
|
|
|
|
|
).call r
|
|
|
|
|
class A
|
|
|
|
|
p: (regex) -> if regex then r regex else 4
|
|
|
|
|
class B extends A
|
|
|
|
|
p: ->
|
|
|
|
|
eq 2, super / b/g
|
|
|
|
|
eq 2, super/b/g
|
|
|
|
|
eq 2, super/ b/g
|
|
|
|
|
eq true, super /b/g
|
|
|
|
|
new B().p()
|
|
|
|
|
|
|
|
|
|
test "always division and never regex after some tokens", ->
|
|
|
|
|
b = 2
|
|
|
|
|
g = 1
|
|
|
|
|
|
|
|
|
|
eq 2, 4 / b/g
|
|
|
|
|
eq 2, 4/b/g
|
|
|
|
|
eq 2, 4/ b/g
|
|
|
|
|
eq 2, 4 /b/g
|
|
|
|
|
eq 2, "4" / b/g
|
|
|
|
|
eq 2, "4"/b/g
|
|
|
|
|
eq 2, "4"/ b/g
|
|
|
|
|
eq 2, "4" /b/g
|
2015-01-14 15:27:24 -05:00
|
|
|
|
eq 20, "4#{0}" / b/g
|
|
|
|
|
eq 20, "4#{0}"/b/g
|
|
|
|
|
eq 20, "4#{0}"/ b/g
|
|
|
|
|
eq 20, "4#{0}" /b/g
|
2015-01-09 19:48:00 -05:00
|
|
|
|
ok isNaN /a/ / b/g
|
|
|
|
|
ok isNaN /a/i / b/g
|
|
|
|
|
ok isNaN /a//b/g
|
|
|
|
|
ok isNaN /a/i/b/g
|
|
|
|
|
ok isNaN /a// b/g
|
|
|
|
|
ok isNaN /a/i/ b/g
|
|
|
|
|
ok isNaN /a/ /b/g
|
|
|
|
|
ok isNaN /a/i /b/g
|
|
|
|
|
eq 0.5, true / b/g
|
|
|
|
|
eq 0.5, true/b/g
|
|
|
|
|
eq 0.5, true/ b/g
|
|
|
|
|
eq 0.5, true /b/g
|
|
|
|
|
eq 0, false / b/g
|
|
|
|
|
eq 0, false/b/g
|
|
|
|
|
eq 0, false/ b/g
|
|
|
|
|
eq 0, false /b/g
|
|
|
|
|
eq 0, null / b/g
|
|
|
|
|
eq 0, null/b/g
|
|
|
|
|
eq 0, null/ b/g
|
|
|
|
|
eq 0, null /b/g
|
|
|
|
|
ok isNaN undefined / b/g
|
|
|
|
|
ok isNaN undefined/b/g
|
|
|
|
|
ok isNaN undefined/ b/g
|
|
|
|
|
ok isNaN undefined /b/g
|
|
|
|
|
ok isNaN {a: 4} / b/g
|
|
|
|
|
ok isNaN {a: 4}/b/g
|
|
|
|
|
ok isNaN {a: 4}/ b/g
|
|
|
|
|
ok isNaN {a: 4} /b/g
|
|
|
|
|
o = prototype: 4
|
|
|
|
|
eq 2, o:: / b/g
|
|
|
|
|
eq 2, o::/b/g
|
|
|
|
|
eq 2, o::/ b/g
|
|
|
|
|
eq 2, o:: /b/g
|
|
|
|
|
i = 4
|
|
|
|
|
eq 2.0, i++ / b/g
|
|
|
|
|
eq 2.5, i++/b/g
|
|
|
|
|
eq 3.0, i++/ b/g
|
|
|
|
|
eq 3.5, i++ /b/g
|
|
|
|
|
eq 4.0, i-- / b/g
|
|
|
|
|
eq 3.5, i--/b/g
|
|
|
|
|
eq 3.0, i--/ b/g
|
|
|
|
|
eq 2.5, i-- /b/g
|
|
|
|
|
|
|
|
|
|
test "compound division vs regex", ->
|
|
|
|
|
c = 4
|
|
|
|
|
i = 2
|
|
|
|
|
|
|
|
|
|
a = 10
|
|
|
|
|
b = a /= c / i
|
|
|
|
|
eq a, 5
|
|
|
|
|
|
|
|
|
|
a = 10
|
|
|
|
|
b = a /= c /i
|
|
|
|
|
eq a, 5
|
|
|
|
|
|
|
|
|
|
a = 10
|
|
|
|
|
b = a /= c /i # Tabs.
|
|
|
|
|
eq a, 5
|
|
|
|
|
|
|
|
|
|
a = 10
|
|
|
|
|
b = a /= c /i # Non-breaking spaces.
|
|
|
|
|
eq a, 5
|
|
|
|
|
|
|
|
|
|
a = 10
|
|
|
|
|
b = a/= c /i
|
|
|
|
|
eq a, 5
|
|
|
|
|
|
|
|
|
|
a = 10
|
|
|
|
|
b = a/=c/i
|
|
|
|
|
eq a, 5
|
|
|
|
|
|
|
|
|
|
a = (regex) -> regex.test '=C '
|
|
|
|
|
b = a /=c /i
|
|
|
|
|
eq b, true
|
|
|
|
|
|
|
|
|
|
a = (regex) -> regex.test '= C '
|
|
|
|
|
# Use parentheses to disambiguate.
|
|
|
|
|
b = a(/= c /i)
|
|
|
|
|
eq b, true
|
|
|
|
|
b = a(/= c /)
|
|
|
|
|
eq b, false
|
|
|
|
|
b = a (/= c /)
|
|
|
|
|
eq b, false
|
|
|
|
|
# Escape to disambiguate.
|
|
|
|
|
b = a /\= c /i
|
|
|
|
|
eq b, true
|
|
|
|
|
b = a /\= c /
|
|
|
|
|
eq b, false
|
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
test "#764: regular expressions should be indexable", ->
|
|
|
|
|
eq /0/['source'], ///#{0}///['source']
|
|
|
|
|
|
|
|
|
|
test "#584: slashes are allowed unescaped in character classes", ->
|
|
|
|
|
ok /^a\/[/]b$/.test 'a//b'
|
|
|
|
|
|
Refactor interpolation (and string and regex) handling in lexer
- Fix #3394: Unclosed single-quoted strings (both regular ones and heredocs)
used to pass through the lexer, causing a parsing error later, while
double-quoted strings caused an error already in the lexing phase. Now both
single and double-quoted unclosed strings error out in the lexer (which is the
more logical option) with consistent error messages. This also fixes the last
comment by @satyr in #3301.
- Similar to the above, unclosed heregexes also used to pass through the lexer
and not error until in the parsing phase, which resulted in confusing error
messages. This has been fixed, too.
- Fix #3348, by adding passing tests.
- Fix #3529: If a string starts with an interpolation, an empty string is no
longer emitted before the interpolation (unless it is needed to coerce the
interpolation into a string).
- Block comments cannot contain `*/`. Now the error message also shows exactly
where the offending `*/`. This improvement might seem unrelated, but I had to
touch that code anyway to refactor string and regex related code, and the
change was very trivial. Moreover, it's consistent with the next two points.
- Regexes cannot start with `*`. Now the error message also shows exactly where
the offending `*` is. (It might actually not be exatly at the start in
heregexes.) It is a very minor improvement, but it was trivial to add.
- Octal escapes in strings are forbidden in CoffeeScript (just like in
JavaScript strict mode). However, this used to be the case only for regular
strings. Now they are also forbidden in heredocs. Moreover, the errors now
point at the offending octal escape.
- Invalid regex flags are no longer allowed. This includes repeated modifiers
and unknown ones. Moreover, invalid modifiers do not stop a heregex from
being matched, which results in better error messages.
- Fix #3621: `///a#{1}///` compiles to `RegExp("a" + 1)`. So does
`RegExp("a#{1}")`. Still, those two code snippets used to generate different
tokens, which is a bit weird, but more importantly causes problems for
coffeelint (see clutchski/coffeelint#340). This required lots of tests in
test/location.coffee to be updated. Note that some updates to those tests are
unrelated to this point; some have been updated to be more consistent (I
discovered this because the refactored code happened to be seemingly more
correct).
- Regular regex literals used to erraneously allow newlines to be escaped,
causing invalid JavaScript output. This has been fixed.
- Heregexes may now be completely empty (`//////`), instead of erroring out with
a confusing message.
- Fix #2388: Heredocs and heregexes used to be lexed simply, which meant that
you couldn't nest a heredoc within a heredoc (double-quoted, that is) or a
heregex inside a heregex.
- Fix #2321: If you used division inside interpolation and then a slash later in
the string containing that interpolation, the division slash and the latter
slash was erraneously matched as a regex. This has been fixed.
- Indentation inside interpolations in heredocs no longer affect how much
indentation is removed from each line of the heredoc (which is more
intuitive).
- Whitespace is now correctly trimmed from the start and end of strings in a few
edge cases.
- Last but not least, the lexing of interpolated strings now seems to be more
efficient. For a regular double-quoted string, we used to use a custom
function to find the end of it (taking interpolations and interpolations
within interpolations etc. into account). Then we used to re-find the
interpolations and recursively lex their contents. In effect, the same string
was processed twice, or even more in the case of deeper nesting of
interpolations. Now the same string is processed just once.
- Code duplication between regular strings, heredocs, regular regexes and
heregexes has been reduced.
- The above two points should result in more easily read code, too.
2015-01-03 17:40:43 -05:00
|
|
|
|
test "does not allow to escape newlines", ->
|
|
|
|
|
throws -> CoffeeScript.compile '/a\\\nb/'
|
2011-09-22 04:09:58 -04:00
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
|
# Heregexe(n|s)
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
|
|
test "a heregex will ignore whitespace and comments", ->
|
|
|
|
|
eq /^I'm\x20+[a]\s+Heregex?\/\/\//gim + '', ///
|
|
|
|
|
^ I'm \x20+ [a] \s+
|
|
|
|
|
Heregex? / // # or not
|
|
|
|
|
///gim + ''
|
|
|
|
|
|
|
|
|
|
test "an empty heregex will compile to an empty, non-capturing group", ->
|
|
|
|
|
eq /(?:)/ + '', /// /// + ''
|
Refactor interpolation (and string and regex) handling in lexer
- Fix #3394: Unclosed single-quoted strings (both regular ones and heredocs)
used to pass through the lexer, causing a parsing error later, while
double-quoted strings caused an error already in the lexing phase. Now both
single and double-quoted unclosed strings error out in the lexer (which is the
more logical option) with consistent error messages. This also fixes the last
comment by @satyr in #3301.
- Similar to the above, unclosed heregexes also used to pass through the lexer
and not error until in the parsing phase, which resulted in confusing error
messages. This has been fixed, too.
- Fix #3348, by adding passing tests.
- Fix #3529: If a string starts with an interpolation, an empty string is no
longer emitted before the interpolation (unless it is needed to coerce the
interpolation into a string).
- Block comments cannot contain `*/`. Now the error message also shows exactly
where the offending `*/`. This improvement might seem unrelated, but I had to
touch that code anyway to refactor string and regex related code, and the
change was very trivial. Moreover, it's consistent with the next two points.
- Regexes cannot start with `*`. Now the error message also shows exactly where
the offending `*` is. (It might actually not be exatly at the start in
heregexes.) It is a very minor improvement, but it was trivial to add.
- Octal escapes in strings are forbidden in CoffeeScript (just like in
JavaScript strict mode). However, this used to be the case only for regular
strings. Now they are also forbidden in heredocs. Moreover, the errors now
point at the offending octal escape.
- Invalid regex flags are no longer allowed. This includes repeated modifiers
and unknown ones. Moreover, invalid modifiers do not stop a heregex from
being matched, which results in better error messages.
- Fix #3621: `///a#{1}///` compiles to `RegExp("a" + 1)`. So does
`RegExp("a#{1}")`. Still, those two code snippets used to generate different
tokens, which is a bit weird, but more importantly causes problems for
coffeelint (see clutchski/coffeelint#340). This required lots of tests in
test/location.coffee to be updated. Note that some updates to those tests are
unrelated to this point; some have been updated to be more consistent (I
discovered this because the refactored code happened to be seemingly more
correct).
- Regular regex literals used to erraneously allow newlines to be escaped,
causing invalid JavaScript output. This has been fixed.
- Heregexes may now be completely empty (`//////`), instead of erroring out with
a confusing message.
- Fix #2388: Heredocs and heregexes used to be lexed simply, which meant that
you couldn't nest a heredoc within a heredoc (double-quoted, that is) or a
heregex inside a heregex.
- Fix #2321: If you used division inside interpolation and then a slash later in
the string containing that interpolation, the division slash and the latter
slash was erraneously matched as a regex. This has been fixed.
- Indentation inside interpolations in heredocs no longer affect how much
indentation is removed from each line of the heredoc (which is more
intuitive).
- Whitespace is now correctly trimmed from the start and end of strings in a few
edge cases.
- Last but not least, the lexing of interpolated strings now seems to be more
efficient. For a regular double-quoted string, we used to use a custom
function to find the end of it (taking interpolations and interpolations
within interpolations etc. into account). Then we used to re-find the
interpolations and recursively lex their contents. In effect, the same string
was processed twice, or even more in the case of deeper nesting of
interpolations. Now the same string is processed just once.
- Code duplication between regular strings, heredocs, regular regexes and
heregexes has been reduced.
- The above two points should result in more easily read code, too.
2015-01-03 17:40:43 -05:00
|
|
|
|
eq /(?:)/ + '', ////// + ''
|
|
|
|
|
|
|
|
|
|
test "heregex starting with slashes", ->
|
|
|
|
|
ok /////a/\////.test ' //a// '
|
|
|
|
|
|
|
|
|
|
test '#2388: `///` in heregex interpolations', ->
|
|
|
|
|
ok ///a#{///b///}c///.test ' /a/b/c/ '
|
|
|
|
|
ws = ' \t'
|
|
|
|
|
scan = (regex) -> regex.exec('\t foo')[0]
|
|
|
|
|
eq '/\t /', /// #{scan /// [#{ws}]* ///} /// + ''
|
2011-09-22 04:39:13 -04:00
|
|
|
|
|
2015-01-15 13:44:14 -05:00
|
|
|
|
test "regexes are not callable", ->
|
|
|
|
|
throws -> CoffeeScript.compile '/a/()'
|
|
|
|
|
throws -> CoffeeScript.compile '///a#{b}///()'
|
|
|
|
|
throws -> CoffeeScript.compile '/a/ 1'
|
|
|
|
|
throws -> CoffeeScript.compile '///a#{b}/// 1'
|
|
|
|
|
throws -> CoffeeScript.compile '''
|
|
|
|
|
/a/
|
|
|
|
|
k: v
|
|
|
|
|
'''
|
|
|
|
|
throws -> CoffeeScript.compile '''
|
|
|
|
|
///a#{b}///
|
|
|
|
|
k: v
|
|
|
|
|
'''
|