From afa26c37f1e77839c20f6ac7f913ddd5f9979943 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 27 Feb 2010 14:30:14 -0500 Subject: [PATCH] fixing regexp literals versus division, with tests --- lib/lexer.js | 5 +++-- src/lexer.coffee | 7 +++---- test/test_regexps.coffee | 12 ++++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 test/test_regexps.coffee diff --git a/lib/lexer.js b/lib/lexer.js index d0506c86..bb685704 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -32,7 +32,7 @@ WHITESPACE = /^([ \t]+)/; COMMENT = /^(((\n?[ \t]*)?#[^\n]*)+)/; CODE = /^((-|=)>)/; - REGEX = /^(\/(.*?)([^\\]|\\\\)\/[imgy]{0,4})/; + REGEX = /^(\/(\S.*?)?([^\\]|\\\\)\/[imgy]{0,4})/; MULTI_DENT = /^((\n([ \t]*))+)(\.)?/; LAST_DENTS = /\n([ \t]*)/g; LAST_DENT = /\n([ \t]*)/; @@ -47,7 +47,8 @@ // Tokens which a regular expression will never immediately follow, but which // a division operator might. // See: http://www.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions - NOT_REGEX = ['IDENTIFIER', 'NUMBER', 'REGEX', 'STRING', ')', '++', '--', ']', '}', 'FALSE', 'NULL', 'TRUE']; + // Our list is shorter, due to sans-parentheses method calls. + NOT_REGEX = ['NUMBER', 'REGEX', '++', '--', 'FALSE', 'NULL', 'TRUE']; // Tokens which could legitimately be invoked or indexed. CALLABLE = ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING', '@']; // Tokens that indicate an access -- keywords immediately following will be diff --git a/src/lexer.coffee b/src/lexer.coffee index d0061be5..9ee55aab 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -54,7 +54,7 @@ OPERATOR : /^([+\*&|\/\-%=<>:!?]+)/ WHITESPACE : /^([ \t]+)/ COMMENT : /^(((\n?[ \t]*)?#[^\n]*)+)/ CODE : /^((-|=)>)/ -REGEX : /^(\/(.*?)([^\\]|\\\\)\/[imgy]{0,4})/ +REGEX : /^(\/(\S.*?)?([^\\]|\\\\)\/[imgy]{0,4})/ MULTI_DENT : /^((\n([ \t]*))+)(\.)?/ LAST_DENTS : /\n([ \t]*)/g LAST_DENT : /\n([ \t]*)/ @@ -71,10 +71,9 @@ HEREDOC_INDENT : /^[ \t]+/mg # Tokens which a regular expression will never immediately follow, but which # a division operator might. # See: http://www.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions +# Our list is shorter, due to sans-parentheses method calls. NOT_REGEX: [ - 'IDENTIFIER', 'NUMBER', 'REGEX', 'STRING', - ')', '++', '--', ']', '}', - 'FALSE', 'NULL', 'TRUE' + 'NUMBER', 'REGEX', '++', '--', 'FALSE', 'NULL', 'TRUE' ] # Tokens which could legitimately be invoked or indexed. diff --git a/test/test_regexps.coffee b/test/test_regexps.coffee new file mode 100644 index 00000000..bce336fd --- /dev/null +++ b/test/test_regexps.coffee @@ -0,0 +1,12 @@ +ok 'x'.match(/x/g) +ok 'x'.match /x/g +ok 'x'.match(/x/) +ok 'x'.match /x/ + +ok 4 / 2 / 1 is 2 + +y: 4 +x: 2 +g: 1 + +ok y / x/g is 2 \ No newline at end of file