1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

adding newline escaping, with tests

This commit is contained in:
Jeremy Ashkenas 2009-12-26 09:29:03 -08:00
parent 2a1fc4b1b7
commit 08dddb27a0
4 changed files with 18 additions and 4 deletions

View file

@ -234,7 +234,7 @@
</dict>
<dict>
<key>match</key>
<string>\b(debugger)\b</string>
<string>\b(debugger|\\)\b</string>
<key>name</key>
<string>keyword.other.coffee</string>
</dict>

View file

@ -130,11 +130,13 @@ module CoffeeScript
# We treat all other single characters as a token. Eg.: ( ) , . !
# Multi-character operators are also literal tokens, so that Racc can assign
# the proper order of operations. Multiple newlines get merged together.
# Use a trailing \ to escape newlines.
def literal_token
value = @chunk[NEWLINE, 1]
if value
@line += value.length
token("\n", "\n") unless last_value == "\n"
token("\n", "\n") unless ["\n", "\\"].include?(last_value)
@tokens.pop if last_value == "\\"
return @i += value.length
end
value = @chunk[OPERATOR, 1]

View file

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

View file

@ -38,8 +38,14 @@ class LexerTest < Test::Unit::TestCase
def test_lexing_comment
code = "a: 1\n # comment\n # on two lines\nb: 2"
assert @lex.tokenize(code) == [[:IDENTIFIER, "a"], [:ASSIGN, ":"], [:NUMBER, "1"],
["\n", "\n"], [:COMMENT, [" comment", " on two lines"]], ["\n", "\n"],
[:IDENTIFIER, "b"], [:ASSIGN, ":"], [:NUMBER, "2"]]
["\n", "\n"], [:COMMENT, [" comment", " on two lines"]], ["\n", "\n"],
[:IDENTIFIER, "b"], [:ASSIGN, ":"], [:NUMBER, "2"]]
end
def test_lexing_newline_escaper
code = "two: 1 + \\\n\n 1"
assert @lex.tokenize(code) == [[:IDENTIFIER, "two"], [:ASSIGN, ":"],
[:NUMBER, "1"], ["+", "+"], [:NUMBER, "1"]]
end
def test_lexing