@@ -186,19 +186,15 @@ exports.Lexer = class Lexer
186186 # Matches strings, including multi-line strings. Ensures that quotation marks
187187 # are balanced within the string's contents, and within nested interpolations.
188188 stringToken : ->
189- switch @chunk .charAt 0
190- when " '"
191- return 0 unless match = SIMPLESTR .exec @chunk
192- string = match[0 ]
193- @ token ' STRING' , @ escapeLines (string), 0 , string .length
194- when ' "'
195- return 0 unless string = @ balancedString @chunk , ' "'
196- if 0 < string .indexOf ' #{' , 1
197- @ interpolateString string[1 ... - 1 ], strOffset : 1 , lexedLength : string .length
198- else
199- @ token ' STRING' , @ escapeLines (string), 0 , string .length
200- else
201- return 0
189+ switch quote = @chunk .charAt 0
190+ when " '" then [string ] = SIMPLESTR .exec @chunk
191+ when ' "' then string = @ balancedString @chunk , ' "'
192+ return 0 unless string
193+ trimmed = @ removeNewlines string[1 ... - 1 ]
194+ if quote is ' "' and 0 < string .indexOf ' #{' , 1
195+ @ interpolateString trimmed, strOffset : 1 , lexedLength : string .length
196+ else
197+ @ token ' STRING' , quote + @ escapeLines (trimmed) + quote, 0 , string .length
202198 if octalEsc = / ^ (?:\\ . | [^ \\ ] )* \\ (?:0[0-7 ] | [1-7 ] )/ .test string
203199 @ error " octal escape sequences #{ string} are not allowed"
204200 string .length
@@ -686,6 +682,11 @@ exports.Lexer = class Lexer
686682 @ tag () in [' \\ ' , ' .' , ' ?.' , ' ?::' , ' UNARY' , ' MATH' , ' +' , ' -' , ' SHIFT' , ' RELATION'
687683 ' COMPARE' , ' LOGIC' , ' THROW' , ' EXTENDS' ]
688684
685+ # Remove newlines from beginning and (non escaped) from end of string literals.
686+ removeNewlines : (str ) ->
687+ str .replace (/ ^ \s * \n\s * / , ' ' )
688+ .replace (/ ([^ \\ ] | \\\\ )\s * \n\s * $ / , ' $1' )
689+
689690 # Converts newlines for string literals.
690691 escapeLines : (str , heredoc ) ->
691692 # Ignore escaped backslashes and remove escaped newlines
@@ -695,9 +696,7 @@ exports.Lexer = class Lexer
695696 str .replace MULTILINER, ' \\ n'
696697 else
697698 # Trim leading and trailing whitespace, string includes quotes
698- str .replace (/ ^ (. )\s * \n\s * / , ' $1' )
699- .replace (/ \s * \n\s * (. )$ / , ' $1' )
700- .replace (/ \s * \n\s * / g , ' ' )
699+ str .replace / \s * \n\s * / g , ' '
701700
702701 # Constructs a string token by escaping quotes and newlines.
703702 makeString : (body , quote , heredoc ) ->
0 commit comments