Skip to content

Commit

Permalink
scanner: look for string_delimiter first to avoid error recovery hitt…
Browse files Browse the repository at this point in the history
…ing the content case and consuming the rest of the input
  • Loading branch information
jdrouhard committed Jan 9, 2023
1 parent 115ce37 commit b3bba38
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const unsigned RAW_STRING_DELIMITER_MAX = 16;
struct Scanner {
// Last raw_string_delimiter, used to detect when raw_string_content ends.
wstring delimiter;

// Scan a raw string delimiter in R"delimiter(content)delimiter".
bool scan_raw_string_delimiter(TSLexer *lexer) {
if (!delimiter.empty()) {
Expand All @@ -39,7 +39,7 @@ struct Scanner {
// d-char is any basic character except parens, backslashes, and spaces.
for (;;) {
if (delimiter.size() > RAW_STRING_DELIMITER_MAX ||
lexer->lookahead == 0 || lexer->lookahead == '\\' || iswspace(lexer->lookahead)) {
lexer->eof(lexer) || lexer->lookahead == '\\' || iswspace(lexer->lookahead)) {
return false;
}
if (lexer->lookahead == '(') {
Expand All @@ -60,7 +60,7 @@ struct Scanner {
for (;;) {
// If we hit EOF, consider the content to terminate there.
// This forms an incomplete raw_string_literal, and models the code well.
if (lexer->lookahead == 0) {
if (lexer->eof(lexer)) {
lexer->mark_end(lexer);
return true;
}
Expand Down Expand Up @@ -91,21 +91,20 @@ struct Scanner {
lexer->advance(lexer, false);
}
}


bool scan(TSLexer *lexer, const bool *valid_symbols) {
// No skipping leading whitespace: raw-string grammar is space-sensitive.

if (valid_symbols[RAW_STRING_CONTENT]) {
lexer->result_symbol = RAW_STRING_CONTENT;
return scan_raw_string_content(lexer);
}

if (valid_symbols[RAW_STRING_DELIMITER]) {
lexer->result_symbol = RAW_STRING_DELIMITER;
return scan_raw_string_delimiter(lexer);
}


if (valid_symbols[RAW_STRING_CONTENT]) {
lexer->result_symbol = RAW_STRING_CONTENT;
return scan_raw_string_content(lexer);
}

return false;
}
};
Expand Down

0 comments on commit b3bba38

Please sign in to comment.