adding proper auto-newline escaping

This commit is contained in:
Jeremy Ashkenas
2009-12-30 22:49:25 -05:00
parent a6539a030c
commit 8762162dff
3 changed files with 15 additions and 11 deletions

View File

@@ -403,9 +403,9 @@ _.isEqual: a, b =>
return true if _.isNaN(a) and _.isNaN(b) return true if _.isNaN(a) and _.isNaN(b)
# Compare regular expressions. # Compare regular expressions.
if _.isRegExp(a) and _.isRegExp(b) if _.isRegExp(a) and _.isRegExp(b)
return a.source is b.source and \ return a.source is b.source and
a.global is b.global and \ a.global is b.global and
a.ignoreCase is b.ignoreCase and \ a.ignoreCase is b.ignoreCase and
a.multiline is b.multiline a.multiline is b.multiline
# If a is not an object by this point, we can't handle it. # If a is not an object by this point, we can't handle it.
return false if atype isnt 'object' return false if atype isnt 'object'

View File

@@ -34,6 +34,7 @@ module CoffeeScript
JS_CLEANER = /(\A`|`\Z)/ JS_CLEANER = /(\A`|`\Z)/
MULTILINER = /\n/ MULTILINER = /\n/
COMMENT_CLEANER = /(^\s*#|\n\s*$)/ COMMENT_CLEANER = /(^\s*#|\n\s*$)/
NO_NEWLINE = /\A([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)\Z/
# Assignment tokens. # Assignment tokens.
ASSIGN = [':', '='] ASSIGN = [':', '=']
@@ -157,10 +158,7 @@ module CoffeeScript
return false unless indent = @chunk[MULTI_DENT, 1] return false unless indent = @chunk[MULTI_DENT, 1]
@line += indent.scan(MULTILINER).size @line += indent.scan(MULTILINER).size
@i += indent.size @i += indent.size
if last_value == "\\" return suppress_newlines(indent) if last_value.to_s.match(NO_NEWLINE) && last_value != "=>"
@tokens.pop
return true
end
size = indent.scan(LAST_DENT).last.last.length size = indent.scan(LAST_DENT).last.last.length
return newline_token(indent) if size == @indent return newline_token(indent) if size == @indent
if size > @indent if size > @indent
@@ -193,6 +191,12 @@ module CoffeeScript
token("\n", "\n") unless last_value == "\n" token("\n", "\n") unless last_value == "\n"
true true
end 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.: ( ) , . ! # We treat all other single characters as a token. Eg.: ( ) , . !
# Multi-character operators are also literal tokens, so that Racc can assign # Multi-character operators are also literal tokens, so that Racc can assign

View File

@@ -1,6 +1,6 @@
six: \ six:
1 + \ 1 +
2 + \ 2 +
3 3
print(six is 6) print(six is 6)