Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle new lines inside block string correctly #1637

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/language/__tests__/lexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ function lexOne(str) {
return lexer.advance();
}

function lexSecond(str) {
const lexer = createLexer(new Source(str));
lexer.advance();
return lexer.advance();
}

function expectSyntaxError(text, message, location) {
expect(() => lexOne(text))
.to.throw('Syntax Error: ' + message)
Expand Down Expand Up @@ -359,6 +365,24 @@ describe('Lexer', () => {
});
});

it('advance line after lexing block string', () => {
expect(
lexSecond(`"""

spans
multiple
lines

""" second_token`),
langpavel marked this conversation as resolved.
Show resolved Hide resolved
).to.contain({
kind: TokenKind.NAME,
start: 69,
end: 81,
line: 7,
value: 'second_token',
});
});

it('lex reports useful block string errors', () => {
expectSyntaxError('"""', 'Unterminated string.', { line: 1, column: 4 });

Expand Down
22 changes: 18 additions & 4 deletions src/language/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ function readToken(lexer: Lexer<*>, prev: Token): Token {
charCodeAt.call(body, pos + 1) === 34 &&
charCodeAt.call(body, pos + 2) === 34
) {
return readBlockString(source, pos, line, col, prev);
return readBlockString(source, pos, line, col, prev, lexer);
}
return readString(source, pos, line, col, prev);
}
Expand Down Expand Up @@ -625,7 +625,7 @@ function readString(source, start, line, col, prev): Token {
*
* """("?"?(\\"""|\\(?!=""")|[^"\\]))*"""
*/
function readBlockString(source, start, line, col, prev): Token {
function readBlockString(source, start, line, col, prev, lexer): Token {
const body = source.body;
let position = start + 3;
let chunkStart = position;
Expand Down Expand Up @@ -668,8 +668,22 @@ function readBlockString(source, start, line, col, prev): Token {
);
}

// Escape Triple-Quote (\""")
if (
if (code === 10) {
// new line
++position;
++lexer.line;
lexer.lineStart = position;
} else if (code === 13) {
// carriage return
langpavel marked this conversation as resolved.
Show resolved Hide resolved
if (charCodeAt.call(body, position + 1) === 10) {
position += 2;
} else {
++position;
}
++lexer.line;
lexer.lineStart = position;
} else if (
// Escape Triple-Quote (\""")
code === 92 &&
charCodeAt.call(body, position + 1) === 34 &&
charCodeAt.call(body, position + 2) === 34 &&
Expand Down