Skip to content

Commit

Permalink
refactor(lexer): handle newline lexing in consumeNewline function
Browse files Browse the repository at this point in the history
  • Loading branch information
langpavel committed Dec 28, 2018
1 parent a44dcf8 commit 06ae24b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/language/__tests__/lexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ describe('Lexer', () => {
});
});

it('advance line after lexing block string', () => {
it('advance line after lexing multiline block string', () => {
expect(
lexSecond(`"""
Expand Down
89 changes: 53 additions & 36 deletions src/language/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,34 @@ function unexpectedCharacterMessage(code) {
return `Cannot parse the unexpected character ${printCharCode(code)}.`;
}

function consumeNewline(
startPosition: number,
code: number,
codeahead: number,
lexer: Lexer<*>,
): [boolean, number] {
let position = startPosition;
let consumed = false;
if (code === 10) {
// new line
++position;
++lexer.line;
lexer.lineStart = position;
consumed = true;
} else if (code === 13) {
// carriage return
if (codeahead === 10) {
position += 2;
} else {
++position;
}
++lexer.line;
lexer.lineStart = position;
consumed = true;
}
return [consumed, position];
}

/**
* Reads from body starting at startPosition until it finds a non-whitespace
* character, then returns the position of that character for lexing.
Expand All @@ -377,22 +405,17 @@ function positionAfterWhitespace(
// tab | space | comma | BOM
if (code === 9 || code === 32 || code === 44 || code === 0xfeff) {
++position;
} else if (code === 10) {
// new line
++position;
++lexer.line;
lexer.lineStart = position;
} else if (code === 13) {
// carriage return
if (charCodeAt.call(body, position + 1) === 10) {
position += 2;
} else {
++position;
}
++lexer.line;
lexer.lineStart = position;
} else {
break;
const [consumed, positionAfterNewLine] = consumeNewline(
position,
code,
charCodeAt.call(body, position + 1),
lexer,
);
if (!consumed) {
break;
}
position = positionAfterNewLine;
}
}
return position;
Expand Down Expand Up @@ -636,12 +659,11 @@ function readBlockString(source, start, line, col, prev, lexer): Token {
position < body.length &&
(code = charCodeAt.call(body, position)) !== null
) {
const code1ahead = charCodeAt.call(body, position + 1);
const code2ahead = charCodeAt.call(body, position + 2);

// Closing Triple-Quote (""")
if (
code === 34 &&
charCodeAt.call(body, position + 1) === 34 &&
charCodeAt.call(body, position + 2) === 34
) {
if (code === 34 && code1ahead === 34 && code2ahead === 34) {
rawValue += slice.call(body, chunkStart, position);
return new Tok(
TokenKind.BLOCK_STRING,
Expand All @@ -668,25 +690,20 @@ function readBlockString(source, start, line, col, prev, lexer): Token {
);
}

if (code === 10) {
// new line
++position;
++lexer.line;
lexer.lineStart = position;
} else if (code === 13) {
// carriage return
if (charCodeAt.call(body, position + 1) === 10) {
position += 2;
} else {
++position;
}
++lexer.line;
lexer.lineStart = position;
const [isNewline, positionAfterNewLine] = consumeNewline(
position,
code,
code1ahead,
lexer,
);

if (isNewline) {
position = positionAfterNewLine;
} else if (
// Escape Triple-Quote (\""")
code === 92 &&
charCodeAt.call(body, position + 1) === 34 &&
charCodeAt.call(body, position + 2) === 34 &&
code1ahead === 34 &&
code2ahead === 34 &&
charCodeAt.call(body, position + 3) === 34
) {
rawValue += slice.call(body, chunkStart, position) + '"""';
Expand Down

0 comments on commit 06ae24b

Please sign in to comment.