Skip to content

Commit

Permalink
Lexer: fix line & column for multiline BLOCK_STRING tokens (#3354)
Browse files Browse the repository at this point in the history
Fixes #3353
  • Loading branch information
IvanGoncharov committed Nov 1, 2021
1 parent 958ac6c commit b262418
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/language/__tests__/lexer-test.ts
Expand Up @@ -537,69 +537,89 @@ describe('Lexer', () => {
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 6,
line: 1,
column: 1,
value: '',
});

expect(lexOne('"""simple"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 12,
line: 1,
column: 1,
value: 'simple',
});

expect(lexOne('""" white space """')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 19,
line: 1,
column: 1,
value: ' white space ',
});

expect(lexOne('"""contains " quote"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 22,
line: 1,
column: 1,
value: 'contains " quote',
});

expect(lexOne('"""contains \\""" triple quote"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 32,
line: 1,
column: 1,
value: 'contains """ triple quote',
});

expect(lexOne('"""multi\nline"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 16,
line: 1,
column: 1,
value: 'multi\nline',
});

expect(lexOne('"""multi\rline\r\nnormalized"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 28,
line: 1,
column: 1,
value: 'multi\nline\nnormalized',
});

expect(lexOne('"""unescaped \\n\\r\\b\\t\\f\\u1234"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 32,
line: 1,
column: 1,
value: 'unescaped \\n\\r\\b\\t\\f\\u1234',
});

expect(lexOne('"""unescaped unicode outside BMP \u{1f600}"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 38,
line: 1,
column: 1,
value: 'unescaped unicode outside BMP \u{1f600}',
});

expect(lexOne('"""slashes \\\\ \\/"""')).to.contain({
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 19,
line: 1,
column: 1,
value: 'slashes \\\\ \\/',
});

Expand All @@ -615,6 +635,8 @@ describe('Lexer', () => {
kind: TokenKind.BLOCK_STRING,
start: 0,
end: 68,
line: 1,
column: 1,
value: 'spans\n multiple\n lines',
});
});
Expand Down
8 changes: 6 additions & 2 deletions src/language/lexer.ts
Expand Up @@ -739,6 +739,9 @@ function readEscapedCharacter(lexer: Lexer, position: number): EscapeSequence {
function readBlockString(lexer: Lexer, start: number): Token {
const body = lexer.source.body;
const bodyLength = body.length;
const startLine = lexer.line;
const startColumn = 1 + start - lexer.lineStart;

let position = start + 3;
let chunkStart = position;
let rawValue = '';
Expand All @@ -753,11 +756,12 @@ function readBlockString(lexer: Lexer, start: number): Token {
body.charCodeAt(position + 2) === 0x0022
) {
rawValue += body.slice(chunkStart, position);
return createToken(
lexer,
return new Token(
TokenKind.BLOCK_STRING,
start,
position + 3,
startLine,
startColumn,
dedentBlockStringValue(rawValue),
);
}
Expand Down

0 comments on commit b262418

Please sign in to comment.