mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
fixing heredocs to use the left-most indent as the indentation guide -- not just the first line of the heredoc
This commit is contained in:
@@ -64,7 +64,7 @@
|
|||||||
<key>comment</key>
|
<key>comment</key>
|
||||||
<string>match stuff like: a => … </string>
|
<string>match stuff like: a => … </string>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>([a-zA-Z0-9_?., $:*]*)\s*(=>)</string>
|
<string>([a-zA-Z0-9_?., $*]*)\s*(=>)</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>meta.inline.function.coffee</string>
|
<string>meta.inline.function.coffee</string>
|
||||||
</dict>
|
</dict>
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ module CoffeeScript
|
|||||||
IDENTIFIER = /\A([a-zA-Z$_](\w|\$)*)/
|
IDENTIFIER = /\A([a-zA-Z$_](\w|\$)*)/
|
||||||
NUMBER = /\A(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i
|
NUMBER = /\A(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i
|
||||||
STRING = /\A(""|''|"(.*?)([^\\]|\\\\)"|'(.*?)([^\\]|\\\\)')/m
|
STRING = /\A(""|''|"(.*?)([^\\]|\\\\)"|'(.*?)([^\\]|\\\\)')/m
|
||||||
HEREDOC = /\A("{6}|'{6}|"{3}\n?(\s*)(.*?)\n?(\s*)"{3}|'{3}\n?(\s*)(.*?)\n?(\s*)'{3})/m
|
HEREDOC = /\A("{6}|'{6}|"{3}\n?(.*?)\n?(\s*)"{3}|'{3}\n?(.*?)\n?(\s*)'{3})/m
|
||||||
JS = /\A(``|`(.*?)([^\\]|\\\\)`)/m
|
JS = /\A(``|`(.*?)([^\\]|\\\\)`)/m
|
||||||
OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/
|
OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/
|
||||||
WHITESPACE = /\A([ \t]+)/
|
WHITESPACE = /\A([ \t]+)/
|
||||||
@@ -38,6 +38,7 @@ module CoffeeScript
|
|||||||
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/
|
NO_NEWLINE = /\A([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)\Z/
|
||||||
|
HEREDOC_INDENT = /^\s+/
|
||||||
|
|
||||||
# Tokens which a regular expression will never immediately follow, but which
|
# Tokens which a regular expression will never immediately follow, but which
|
||||||
# a division operator might.
|
# a division operator might.
|
||||||
@@ -50,12 +51,12 @@ module CoffeeScript
|
|||||||
|
|
||||||
# Scan by attempting to match tokens one character at a time. Slow and steady.
|
# Scan by attempting to match tokens one character at a time. Slow and steady.
|
||||||
def tokenize(code)
|
def tokenize(code)
|
||||||
@code = code.chomp # Cleanup code by remove extra line breaks
|
@code = code.chomp # Cleanup code by remove extra line breaks
|
||||||
@i = 0 # Current character position we're parsing
|
@i = 0 # Current character position we're parsing
|
||||||
@line = 1 # The current line.
|
@line = 1 # The current line.
|
||||||
@indent = 0 # The current indent level.
|
@indent = 0 # The current indent level.
|
||||||
@indents = [] # The stack of all indent levels we are currently within.
|
@indents = [] # The stack of all indent levels we are currently within.
|
||||||
@tokens = [] # Collection of all parsed tokens in the form [:TOKEN_TYPE, value]
|
@tokens = [] # Collection of all parsed tokens in the form [:TOKEN_TYPE, value]
|
||||||
while @i < @code.length
|
while @i < @code.length
|
||||||
@chunk = @code[@i..-1]
|
@chunk = @code[@i..-1]
|
||||||
extract_next_token
|
extract_next_token
|
||||||
@@ -114,9 +115,10 @@ module CoffeeScript
|
|||||||
# Matches heredocs, adjusting indentation to the correct level.
|
# Matches heredocs, adjusting indentation to the correct level.
|
||||||
def heredoc_token
|
def heredoc_token
|
||||||
return false unless match = @chunk.match(HEREDOC)
|
return false unless match = @chunk.match(HEREDOC)
|
||||||
indent = match[2] || match[5]
|
doc = match[2] || match[4]
|
||||||
doc = match[3] || match[6]
|
indent = doc.scan(HEREDOC_INDENT).min
|
||||||
doc.gsub!(/\n#{indent}/, "\\n")
|
doc.gsub!(/^#{indent}/, "")
|
||||||
|
doc.gsub!("\n", "\\n")
|
||||||
doc.gsub!('"', '\\"')
|
doc.gsub!('"', '\\"')
|
||||||
token(:STRING, "\"#{doc}\"")
|
token(:STRING, "\"#{doc}\"")
|
||||||
@line += match[1].count("\n")
|
@line += match[1].count("\n")
|
||||||
|
|||||||
9
test/fixtures/execution/test_heredocs.coffee
vendored
9
test/fixtures/execution/test_heredocs.coffee
vendored
@@ -26,3 +26,12 @@ a: """
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
print(a is "out\nhere")
|
print(a is "out\nhere")
|
||||||
|
|
||||||
|
|
||||||
|
a: '''
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
'''
|
||||||
|
|
||||||
|
print(a is " a\n b\nc")
|
||||||
Reference in New Issue
Block a user