From 8762162dff9b1e3c37eaaac4c54a505d77024079 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 30 Dec 2009 22:49:25 -0500 Subject: [PATCH] adding proper auto-newline escaping --- examples/underscore.coffee | 6 +++--- lib/coffee_script/lexer.rb | 12 ++++++++---- test/fixtures/execution/test_newline_escaping.coffee | 8 ++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/examples/underscore.coffee b/examples/underscore.coffee index f4dc9bc0..a6c5baf6 100644 --- a/examples/underscore.coffee +++ b/examples/underscore.coffee @@ -403,9 +403,9 @@ _.isEqual: a, b => return true if _.isNaN(a) and _.isNaN(b) # Compare regular expressions. if _.isRegExp(a) and _.isRegExp(b) - return a.source is b.source and \ - a.global is b.global and \ - a.ignoreCase is b.ignoreCase and \ + return a.source is b.source and + a.global is b.global and + a.ignoreCase is b.ignoreCase and a.multiline is b.multiline # If a is not an object by this point, we can't handle it. return false if atype isnt 'object' diff --git a/lib/coffee_script/lexer.rb b/lib/coffee_script/lexer.rb index 0531a917..24b4a96e 100644 --- a/lib/coffee_script/lexer.rb +++ b/lib/coffee_script/lexer.rb @@ -34,6 +34,7 @@ module CoffeeScript JS_CLEANER = /(\A`|`\Z)/ MULTILINER = /\n/ COMMENT_CLEANER = /(^\s*#|\n\s*$)/ + NO_NEWLINE = /\A([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)\Z/ # Assignment tokens. ASSIGN = [':', '='] @@ -157,10 +158,7 @@ module CoffeeScript return false unless indent = @chunk[MULTI_DENT, 1] @line += indent.scan(MULTILINER).size @i += indent.size - if last_value == "\\" - @tokens.pop - return true - end + return suppress_newlines(indent) if last_value.to_s.match(NO_NEWLINE) && last_value != "=>" size = indent.scan(LAST_DENT).last.last.length return newline_token(indent) if size == @indent if size > @indent @@ -193,6 +191,12 @@ module CoffeeScript token("\n", "\n") unless last_value == "\n" true end + + # Tokens to explicitly escape newlines are removed once their job is done. + def suppress_newlines(newlines) + @tokens.pop if last_value == "\\" + true + end # We treat all other single characters as a token. Eg.: ( ) , . ! # Multi-character operators are also literal tokens, so that Racc can assign diff --git a/test/fixtures/execution/test_newline_escaping.coffee b/test/fixtures/execution/test_newline_escaping.coffee index 972e2f79..117c2284 100644 --- a/test/fixtures/execution/test_newline_escaping.coffee +++ b/test/fixtures/execution/test_newline_escaping.coffee @@ -1,6 +1,6 @@ -six: \ -1 + \ -2 + \ -3 +six: + 1 + + 2 + + 3 print(six is 6) \ No newline at end of file