#include "tree_sitter/alloc.h"
#include "tree_sitter/array.h"
#include "tree_sitter/parser.h"
#include "identifierCharacters.h"
enum TokenType {
OPT_SEMI,
INTERPOLATION_REGULAR_START,
INTERPOLATION_VERBATIM_START,
INTERPOLATION_RAW_START,
INTERPOLATION_START_QUOTE,
INTERPOLATION_END_QUOTE,
INTERPOLATION_OPEN_BRACE,
INTERPOLATION_CLOSE_BRACE,
INTERPOLATION_STRING_CONTENT,
RAW_STRING_START,
RAW_STRING_END,
RAW_STRING_CONTENT,
LAMBDA_PAREN_OPEN,
END_OF_INPUT,
DIRECTIVE_NEWLINE,
FILE_DIRECTIVE_PREPROC_ARG,
FILE_DIRECTIVE_SDK,
FILE_DIRECTIVE_PACKAGE,
FILE_DIRECTIVE_PROPERTY,
FILE_DIRECTIVE_PROJECT,
FILE_DIRECTIVE_REF,
FILE_DIRECTIVE_INCLUDE,
FILE_DIRECTIVE_EXCLUDE,
FILE_DIRECTIVE_KIND,
FILE_DIRECTIVE_LITERAL_CONTEXT,
FILE_DIRECTIVE_NEWLINE,
PRAGMA_BOUNDARY,
PRAGMA_TEXT,
PREPROC_INACTIVE,
CONDITIONAL_ACCESS_QUESTION,
NULLABLE_QUESTION,
TERNARY_QUESTION,
TYPE_LIST_OPEN,
LESS_THAN,
TYPE_LIST_OR_LESS_THAN,
NULLABLE_OR_TERNARY_QUESTION,
AWAIT_IDENTIFIER,
RECORD_KEYWORD,
RECORD_CONSTRUCTOR,
MEMBER_RECORD_MODIFIER,
CREATION_TYPE_START,
CREATION_NULLABLE_QUESTION,
};
typedef enum {
REGULAR = 1 << 0,
VERBATIM = 1 << 1,
RAW = 1 << 2,
} StringType;
typedef struct {
uint8_t dollar_count;
uint8_t open_brace_count;
uint8_t quote_count;
StringType string_type;
} Interpolation;
static inline bool is_regular(Interpolation *interpolation) { return interpolation->string_type & REGULAR; }
static inline bool is_verbatim(Interpolation *interpolation) { return interpolation->string_type & VERBATIM; }
static inline bool is_raw(Interpolation *interpolation) { return interpolation->string_type & RAW; }
typedef struct {
uint8_t quote_count;
bool next_file_directive;
Array(Interpolation) interpolation_stack;
} Scanner;
static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static inline void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
static inline bool is_line_terminator(int32_t c) {
return c == '\r' || c == '\n' || c == 0x85 || c == 0x2028 || c == 0x2029;
}
static inline bool is_space_but_line_feed(int32_t c) {
return c == '\t' || c == '\v' || c == '\f' || c == '\r' || c == ' ' || c == 0xA0 || c == 0x3000 || c == 0xFEFF;
}
static bool skip_block_comment(TSLexer *lexer);
static void skip_line_comment(TSLexer *lexer);
static bool skip_ws_and_comments(TSLexer *lexer) {
for (;;) {
int32_t c = lexer->lookahead;
if (is_space_but_line_feed(c) || is_line_terminator(c)) {
advance(lexer);
continue;
}
if (c != '/') return false;
advance(lexer);
if (lexer->lookahead == '/') skip_line_comment(lexer);
else if (lexer->lookahead == '*') skip_block_comment(lexer);
else return true;
}
}
static bool skip_block_comment(TSLexer *lexer) {
advance(lexer);
int32_t previous = 0;
while (!at_eof(lexer) && !(previous == '*' && lexer->lookahead == '/')) {
previous = lexer->lookahead;
advance(lexer);
}
if (at_eof(lexer)) return false;
advance(lexer);
return true;
}
static void skip_line_comment(TSLexer *lexer) {
while (!at_eof(lexer) && !is_line_terminator(lexer->lookahead)) advance(lexer);
}
static size_t consume_identifier_into(TSLexer *lexer, char *buf, size_t max) {
size_t n = 0;
while (is_id_continue(lexer->lookahead)) {
if (n < max) buf[n] = lexer->lookahead <= 0x7F ? (char)lexer->lookahead : '?';
n++;
advance(lexer);
}
return n;
}
void *tree_sitter_c_sharp_external_scanner_create() {
Scanner *scanner = ts_calloc(1, sizeof(Scanner));
array_init(&scanner->interpolation_stack);
return scanner;
}
void tree_sitter_c_sharp_external_scanner_destroy(void *payload) {
Scanner *scanner = (Scanner *)payload;
array_delete(&scanner->interpolation_stack);
ts_free(scanner);
}
unsigned tree_sitter_c_sharp_external_scanner_serialize(void *payload, char *buffer) {
Scanner *scanner = (Scanner *)payload;
if (!scanner->quote_count && !scanner->next_file_directive && !scanner->interpolation_stack.size) return 0;
if (scanner->interpolation_stack.size * 4 + 3 > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) return 0;
unsigned size = 0;
buffer[size++] = (char)scanner->quote_count;
buffer[size++] = (char)scanner->next_file_directive;
buffer[size++] = (char)scanner->interpolation_stack.size;
for (unsigned i = 0; i < scanner->interpolation_stack.size; i++) {
Interpolation item = scanner->interpolation_stack.contents[i];
buffer[size++] = (char)item.dollar_count;
buffer[size++] = (char)item.open_brace_count;
buffer[size++] = (char)item.quote_count;
buffer[size++] = (char)item.string_type;
}
return size;
}
void tree_sitter_c_sharp_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
Scanner *scanner = (Scanner *)payload;
scanner->quote_count = 0;
scanner->next_file_directive = false;
array_clear(&scanner->interpolation_stack);
if (length < 3) return;
unsigned count = (unsigned char)buffer[2];
if (3 + count * 4 != length) return;
scanner->quote_count = (unsigned char)buffer[0];
scanner->next_file_directive = buffer[1] != 0;
array_reserve(&scanner->interpolation_stack, count);
scanner->interpolation_stack.size = count;
for (unsigned i = 0; i < count; i++) {
const char *item = buffer + 3 + i * 4;
scanner->interpolation_stack.contents[i] = (Interpolation){
.dollar_count = (unsigned char)item[0],
.open_brace_count = (unsigned char)item[1],
.quote_count = (unsigned char)item[2],
.string_type = (unsigned char)item[3],
};
}
}
typedef enum {
LAMBDA_SCAN_NO_PAREN,
LAMBDA_SCAN_FAILED_AFTER_PAREN,
LAMBDA_SCAN_SUCCESS,
} LambdaScanResult;
static bool consume_lambda_element(TSLexer *lexer, bool *saw_hard_modifier);
static LambdaScanResult scan_lambda_paren_open(TSLexer *lexer) {
while (is_unicode_whitespace(lexer->lookahead)) skip(lexer);
if (lexer->lookahead != '(') return LAMBDA_SCAN_NO_PAREN;
advance(lexer);
lexer->mark_end(lexer);
#define BAIL return LAMBDA_SCAN_FAILED_AFTER_PAREN
bool saw_hard_modifier = false;
bool expecting_element = true;
for (;;) {
if (skip_ws_and_comments(lexer)) BAIL;
int32_t c = lexer->lookahead;
if (at_eof(lexer)) BAIL;
if (c == ')') {
advance(lexer);
if (skip_ws_and_comments(lexer) || !saw_hard_modifier || lexer->lookahead != '=') BAIL;
advance(lexer);
if (lexer->lookahead != '>') BAIL;
lexer->result_symbol = LAMBDA_PAREN_OPEN;
return LAMBDA_SCAN_SUCCESS;
}
if (!expecting_element) {
if (c != ',') BAIL;
advance(lexer);
expecting_element = true;
continue;
}
if (!consume_lambda_element(lexer, &saw_hard_modifier)) BAIL;
expecting_element = false;
}
}
#undef BAIL
static bool consume_lambda_element(TSLexer *lexer, bool *saw_hard_modifier) {
for (;;) {
if (skip_ws_and_comments(lexer) || !is_id_start(lexer->lookahead)) return false;
char buf[9];
size_t n = consume_identifier_into(lexer, buf, sizeof(buf));
if (buf_equals(buf, n, "ref") || buf_equals(buf, n, "out") ||
buf_equals(buf, n, "in") || buf_equals(buf, n, "readonly")) {
*saw_hard_modifier = true;
} else if (!buf_equals(buf, n, "scoped")) {
return true;
}
}
}
static bool scan_file_directive_prefix(TSLexer *lexer) {
if (lexer->lookahead != '#') return false;
advance(lexer);
if (lexer->lookahead != ':') return false;
advance(lexer);
return true;
}
static bool scan_preproc_arg(TSLexer *lexer, bool opaque, enum TokenType symbol) {
lexer->mark_end(lexer);
while (!at_eof(lexer) && !is_line_terminator(lexer->lookahead)) {
int32_t c = lexer->lookahead;
advance(lexer);
if (!opaque && c == '/') {
if (lexer->lookahead == '*') break;
if (at_eof(lexer) || is_line_terminator(lexer->lookahead)) {
lexer->mark_end(lexer);
break;
}
advance(lexer);
}
lexer->mark_end(lexer);
}
lexer->result_symbol = symbol;
return true;
}
static bool is_directive_horizontal(int32_t c);
static bool scan_file_directive_kind(TSLexer *lexer);
static bool scan_pragma_boundary(TSLexer *lexer);
static bool scan_pragma_text(TSLexer *lexer);
static bool scan_preproc_inactive(TSLexer *lexer);
static bool scan_question(TSLexer *lexer, const bool *valid_symbols);
typedef enum {
QUESTION_NOT_STARTED,
QUESTION_MATCHED,
QUESTION_MISMATCHED,
} QuestionScanResult;
static void advance_identifier(TSLexer *lexer);
static int32_t advance_skipping_literal(TSLexer *lexer);
#include "awaitIdentifier.h"
#include "recordKeyword.h"
static bool scan_less_than(TSLexer *lexer, const bool *valid_symbols);
static bool scan_recovery(Scanner *scanner, TSLexer *lexer, bool next_file_directive);
bool tree_sitter_c_sharp_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
Scanner *scanner = (Scanner *)payload;
uint8_t brace_advanced = 0;
uint8_t quote_count = 0;
bool did_advance = false;
bool next_file_directive = scanner->next_file_directive;
scanner->next_file_directive = false;
if (valid_symbols[OPT_SEMI] && valid_symbols[INTERPOLATION_REGULAR_START]) {
return scan_recovery(scanner, lexer, next_file_directive);
}
if (valid_symbols[PRAGMA_BOUNDARY]) return scan_pragma_boundary(lexer);
if (valid_symbols[PRAGMA_TEXT]) {
while (is_directive_horizontal(lexer->lookahead)) skip(lexer);
if (!at_eof(lexer) && !is_line_terminator(lexer->lookahead)) return scan_pragma_text(lexer);
}
if (valid_symbols[PREPROC_INACTIVE] && !valid_symbols[OPT_SEMI] && !valid_symbols[FILE_DIRECTIVE_LITERAL_CONTEXT] &&
!valid_symbols[FILE_DIRECTIVE_PREPROC_ARG] && !valid_symbols[END_OF_INPUT] && !valid_symbols[DIRECTIVE_NEWLINE] &&
!valid_symbols[FILE_DIRECTIVE_NEWLINE] && !valid_symbols[RAW_STRING_CONTENT] && !valid_symbols[INTERPOLATION_STRING_CONTENT]) {
while (is_directive_horizontal(lexer->lookahead) || is_line_terminator(lexer->lookahead)) skip(lexer);
if (lexer->lookahead == '#') {
advance(lexer);
if (lexer->lookahead != ':') return scan_preproc_inactive(lexer);
advance(lexer);
return valid_symbols[FILE_DIRECTIVE_KIND] && scan_file_directive_kind(lexer);
}
}
QuestionScanResult word = scan_contextual_word(lexer, valid_symbols);
if (word != QUESTION_NOT_STARTED) return word == QUESTION_MATCHED;
if ((valid_symbols[CONDITIONAL_ACCESS_QUESTION] || valid_symbols[NULLABLE_QUESTION] || valid_symbols[TERNARY_QUESTION] ||
valid_symbols[CREATION_NULLABLE_QUESTION]) &&
!valid_symbols[OPT_SEMI]) {
while (is_unicode_whitespace(lexer->lookahead)) skip(lexer);
if (lexer->lookahead == '?') return scan_question(lexer, valid_symbols);
}
if ((valid_symbols[TYPE_LIST_OPEN] || valid_symbols[LESS_THAN]) && !valid_symbols[OPT_SEMI]) {
while (is_unicode_whitespace(lexer->lookahead)) skip(lexer);
if (lexer->lookahead == '<') return scan_less_than(lexer, valid_symbols);
}
if (valid_symbols[LAMBDA_PAREN_OPEN]) {
LambdaScanResult lambda = scan_lambda_paren_open(lexer);
if (lambda != LAMBDA_SCAN_NO_PAREN) return lambda == LAMBDA_SCAN_SUCCESS;
}
if (valid_symbols[FILE_DIRECTIVE_PREPROC_ARG]) {
while (lexer->lookahead == ' ' || lexer->lookahead == '\t' || lexer->lookahead == '\v' || lexer->lookahead == '\f') skip(lexer);
while (is_space_but_line_feed(lexer->lookahead) && !is_line_terminator(lexer->lookahead)) advance(lexer);
if (lexer->lookahead == '#') return scan_file_directive_prefix(lexer) && scan_preproc_arg(lexer, false, FILE_DIRECTIVE_PREPROC_ARG);
}
if (valid_symbols[DIRECTIVE_NEWLINE] || valid_symbols[FILE_DIRECTIVE_NEWLINE]) {
while (is_directive_horizontal(lexer->lookahead)) skip(lexer);
if (is_line_terminator(lexer->lookahead)) {
bool cr = lexer->lookahead == '\r';
advance(lexer);
bool crlf = cr && lexer->lookahead == '\n';
if (crlf) advance(lexer);
lexer->mark_end(lexer);
if (!valid_symbols[FILE_DIRECTIVE_NEWLINE] && !crlf) {
while (is_line_terminator(lexer->lookahead)) advance(lexer);
lexer->mark_end(lexer);
}
while (is_directive_horizontal(lexer->lookahead) || is_line_terminator(lexer->lookahead)) advance(lexer);
scanner->next_file_directive = scan_file_directive_prefix(lexer);
lexer->result_symbol = valid_symbols[FILE_DIRECTIVE_NEWLINE] ? FILE_DIRECTIVE_NEWLINE : DIRECTIVE_NEWLINE;
return true;
}
}
if (valid_symbols[END_OF_INPUT]) {
while (is_space_but_line_feed(lexer->lookahead)) skip(lexer);
lexer->result_symbol = END_OF_INPUT;
return at_eof(lexer);
}
if (valid_symbols[OPT_SEMI]) {
lexer->result_symbol = OPT_SEMI;
if (lexer->lookahead == ';') advance(lexer);
return true;
}
if (valid_symbols[RAW_STRING_START]) {
while (is_unicode_whitespace(lexer->lookahead)) skip(lexer);
if (lexer->lookahead == '"') {
while (lexer->lookahead == '"') {
advance(lexer);
quote_count++;
}
if (quote_count >= 3) {
lexer->result_symbol = RAW_STRING_START;
scanner->quote_count = quote_count;
return true;
}
}
}
if (valid_symbols[RAW_STRING_END] && lexer->lookahead == '"') {
while (lexer->lookahead == '"') {
advance(lexer);
quote_count++;
}
if (quote_count == scanner->quote_count) {
lexer->result_symbol = RAW_STRING_END;
scanner->quote_count = 0;
return true;
}
did_advance = quote_count > 0;
}
if (valid_symbols[RAW_STRING_CONTENT]) {
while (!at_eof(lexer)) {
if (lexer->lookahead == '"') {
lexer->mark_end(lexer);
quote_count = 0;
while (lexer->lookahead == '"') {
advance(lexer);
quote_count++;
}
if (quote_count == scanner->quote_count) {
lexer->result_symbol = RAW_STRING_CONTENT;
return true;
}
}
advance(lexer);
did_advance = true;
}
lexer->mark_end(lexer);
lexer->result_symbol = RAW_STRING_CONTENT;
return true;
}
if (valid_symbols[INTERPOLATION_REGULAR_START] || valid_symbols[INTERPOLATION_VERBATIM_START] ||
valid_symbols[INTERPOLATION_RAW_START]) {
while (is_unicode_whitespace(lexer->lookahead)) skip(lexer);
uint8_t dollar_advanced = 0;
bool is_verbatim = false;
if (lexer->lookahead == '@') {
is_verbatim = true;
advance(lexer);
}
while (lexer->lookahead == '$' && quote_count == 0) {
advance(lexer);
dollar_advanced++;
}
if (dollar_advanced > 0 && (lexer->lookahead == '"' || lexer->lookahead == '@')) {
lexer->result_symbol = INTERPOLATION_REGULAR_START;
Interpolation interpolation = {
.dollar_count = dollar_advanced,
.open_brace_count = 0,
.quote_count = 0,
.string_type = 0,
};
if (is_verbatim || lexer->lookahead == '@') {
if (lexer->lookahead == '@') {
advance(lexer);
is_verbatim = true;
}
lexer->result_symbol = INTERPOLATION_VERBATIM_START;
interpolation.string_type = VERBATIM;
}
lexer->mark_end(lexer);
advance(lexer);
interpolation.string_type |= REGULAR;
if (lexer->lookahead == '"' && !is_verbatim) {
advance(lexer);
if (lexer->lookahead == '"') {
lexer->result_symbol = INTERPOLATION_RAW_START;
interpolation.string_type = RAW;
}
}
array_push(&scanner->interpolation_stack, interpolation);
return true;
}
}
if (valid_symbols[INTERPOLATION_START_QUOTE] && scanner->interpolation_stack.size > 0) {
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
while (lexer->lookahead == '"') {
advance(lexer);
current_interpolation->quote_count++;
if (is_verbatim(current_interpolation) || is_regular(current_interpolation)) break;
}
lexer->result_symbol = INTERPOLATION_START_QUOTE;
return current_interpolation->quote_count > 0;
}
if (valid_symbols[INTERPOLATION_END_QUOTE] && scanner->interpolation_stack.size > 0) {
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
while (lexer->lookahead == '"') {
advance(lexer);
quote_count++;
if (is_verbatim(current_interpolation) && quote_count == 2) {
lexer->mark_end(lexer);
did_advance = true;
quote_count = 0;
}
}
if (is_verbatim(current_interpolation) && did_advance && quote_count == 1) {
lexer->result_symbol = INTERPOLATION_STRING_CONTENT;
return valid_symbols[INTERPOLATION_STRING_CONTENT];
}
if (quote_count == current_interpolation->quote_count) {
lexer->result_symbol = INTERPOLATION_END_QUOTE;
array_pop(&scanner->interpolation_stack);
return true;
}
did_advance = did_advance || quote_count > 0;
}
if (valid_symbols[INTERPOLATION_OPEN_BRACE] && scanner->interpolation_stack.size > 0) {
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
if (did_advance) lexer->mark_end(lexer);
while (lexer->lookahead == '{' && brace_advanced < current_interpolation->dollar_count) {
advance(lexer);
brace_advanced++;
if (is_raw(current_interpolation) && !did_advance && brace_advanced == 1) lexer->mark_end(lexer);
}
if (!is_raw(current_interpolation) && brace_advanced == 1 && lexer->lookahead == '{') {
advance(lexer);
brace_advanced = 0;
did_advance = true;
}
if (is_raw(current_interpolation) && brace_advanced == current_interpolation->dollar_count && lexer->lookahead == '{') {
lexer->result_symbol = INTERPOLATION_STRING_CONTENT;
return valid_symbols[INTERPOLATION_STRING_CONTENT];
}
if (brace_advanced > 0 && brace_advanced == current_interpolation->dollar_count &&
(brace_advanced == 0 || lexer->lookahead != '{')) {
if (did_advance) {
lexer->result_symbol = INTERPOLATION_STRING_CONTENT;
return valid_symbols[INTERPOLATION_STRING_CONTENT];
}
lexer->mark_end(lexer);
current_interpolation->open_brace_count = brace_advanced;
lexer->result_symbol = INTERPOLATION_OPEN_BRACE;
return true;
}
}
if (valid_symbols[INTERPOLATION_CLOSE_BRACE] && scanner->interpolation_stack.size > 0) {
uint8_t brace_advanced = 0;
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
while (is_unicode_whitespace(lexer->lookahead)) advance(lexer);
while (lexer->lookahead == '}') {
advance(lexer);
brace_advanced++;
if (brace_advanced == current_interpolation->open_brace_count) {
current_interpolation->open_brace_count = 0;
lexer->mark_end(lexer);
lexer->result_symbol = INTERPOLATION_CLOSE_BRACE;
return true;
}
}
return false;
}
if (valid_symbols[INTERPOLATION_STRING_CONTENT] && scanner->interpolation_stack.size > 0) {
lexer->result_symbol = INTERPOLATION_STRING_CONTENT;
Interpolation *current_interpolation = array_back(&scanner->interpolation_stack);
bool raw = is_raw(current_interpolation);
bool verbatim = !raw && is_verbatim(current_interpolation);
bool regular = !raw && !verbatim && is_regular(current_interpolation);
while (!at_eof(lexer)) {
if (raw) {
if (lexer->lookahead == '"') {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead == '"') {
advance(lexer);
uint8_t quote_advanced = 2;
while (lexer->lookahead == '"') {
quote_advanced++;
advance(lexer);
}
if (quote_advanced == current_interpolation->quote_count) return did_advance;
}
}
} else if (verbatim) {
if (lexer->lookahead == '"') {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead != '"') return did_advance;
advance(lexer);
did_advance = true;
continue;
}
} else if (regular) {
if (lexer->lookahead == '\\' || is_line_terminator(lexer->lookahead) || lexer->lookahead == '"') {
lexer->mark_end(lexer);
return did_advance;
}
}
if ((raw || verbatim || regular) && lexer->lookahead == '{') {
lexer->mark_end(lexer);
while (lexer->lookahead == '{' && brace_advanced < current_interpolation->open_brace_count) {
advance(lexer);
brace_advanced++;
}
if (brace_advanced == current_interpolation->open_brace_count &&
(brace_advanced == 0 || lexer->lookahead != '{')) {
return did_advance;
}
}
if (lexer->lookahead != '{') brace_advanced = 0;
advance(lexer);
did_advance = true;
}
lexer->mark_end(lexer);
return did_advance;
}
return false;
}
static bool is_directive_horizontal(int32_t c) {
return c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == 0xA0 || c == 0x1680 ||
(c >= 0x2000 && c <= 0x200A) || c == 0x202F || c == 0x205F || c == 0x3000 || c == 0xFEFF;
}
#include "pragmaDirectives.h"
static bool scan_file_directive_kind(TSLexer *lexer) {
char kind[9] = {0};
unsigned length = 0;
while (!at_eof(lexer) && !is_directive_horizontal(lexer->lookahead) && !is_line_terminator(lexer->lookahead)) {
int32_t c = lexer->lookahead;
if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
if (length < sizeof(kind) - 1) kind[length] = c > 0 && c < 128 ? c : '?';
if (length < sizeof(kind)) length++;
advance(lexer);
}
const char *kinds[] = {"sdk", "package", "property", "project", "ref", "include", "exclude"};
lexer->result_symbol = FILE_DIRECTIVE_KIND;
if (length < sizeof(kind)) {
for (unsigned i = 0; i < sizeof(kinds) / sizeof(kinds[0]); i++) {
if (!strcmp(kind, kinds[i])) {
lexer->result_symbol = FILE_DIRECTIVE_SDK + i;
break;
}
}
}
lexer->mark_end(lexer);
return true;
}
static bool scan_interpolation_recovery(Scanner *scanner, TSLexer *lexer) {
Interpolation *interpolation = array_back(&scanner->interpolation_stack);
bool closes_hole = lexer->lookahead == '}';
bool line_end = false;
if (closes_hole) {
if (!interpolation->open_brace_count) return false;
unsigned braces = 0;
while (lexer->lookahead == '}' && braces < interpolation->open_brace_count) {
advance(lexer);
braces++;
}
if (braces != interpolation->open_brace_count) return false;
lexer->mark_end(lexer);
if (is_regular(interpolation) && !is_verbatim(interpolation)) {
while (is_directive_horizontal(lexer->lookahead)) advance(lexer);
line_end = is_line_terminator(lexer->lookahead);
}
}
unsigned quotes = 0;
while (lexer->lookahead == '"' && quotes < interpolation->quote_count) {
advance(lexer);
quotes++;
}
if (!line_end && (!quotes || quotes != interpolation->quote_count || lexer->lookahead == '"')) return false;
if (closes_hole) {
interpolation->open_brace_count = 0;
lexer->result_symbol = INTERPOLATION_CLOSE_BRACE;
} else {
array_pop(&scanner->interpolation_stack);
lexer->result_symbol = INTERPOLATION_END_QUOTE;
}
return true;
}
static bool scan_recovery(Scanner *scanner, TSLexer *lexer, bool next_file_directive) {
bool line_start = next_file_directive;
while (is_directive_horizontal(lexer->lookahead) || is_line_terminator(lexer->lookahead)) {
if (is_line_terminator(lexer->lookahead)) line_start = true;
skip(lexer);
}
if (scanner->interpolation_stack.size && (lexer->lookahead == '"' || lexer->lookahead == '}')) {
return scan_interpolation_recovery(scanner, lexer);
}
if (lexer->lookahead != '#') return false;
advance(lexer);
if (lexer->lookahead != ':') return scan_preproc_inactive(lexer);
advance(lexer);
return line_start ? scan_file_directive_kind(lexer) : scan_preproc_arg(lexer, true, FILE_DIRECTIVE_PREPROC_ARG);
}
#include "preprocInactive.h"
typedef char QuestionKeyword[sizeof("stackalloc")];
static bool is_word(const char *word, size_t length, const QuestionKeyword *words, size_t count) {
if (length >= sizeof *words) return false;
for (size_t i = 0; i < count; i++) {
if (buf_equals(word, length, words[i])) return true;
}
return false;
}
static bool skip_type_parameters(TSLexer *lexer, unsigned *budget) {
for (unsigned angles = 0, brackets = 0; *budget > 0; (*budget)--) {
if (skip_ws_and_comments(lexer) && brackets == 0) return false;
int32_t c = lexer->lookahead;
if (at_eof(lexer) || (brackets == 0 && !is_id_continue(c) && !is_ascii_in(c, "<>,[]@"))) return false;
c = advance_skipping_literal(lexer);
brackets += (c == '[') - (c == ']');
if (brackets == 0) angles += (c == '<') - (c == '>');
if (brackets == 0 && c == '>' && angles == 0) return true;
}
return false;
}
static bool local_function_body_follows(TSLexer *lexer) {
if (skip_ws_and_comments(lexer)) return false;
if (lexer->lookahead == '{') return true;
if (lexer->lookahead == '=') {
advance(lexer);
return lexer->lookahead == '>';
}
char word[6];
size_t length = consume_identifier_into(lexer, word, sizeof word);
return buf_equals(word, length, "where");
}
static void skip_verbatim_string(TSLexer *lexer) {
while (!at_eof(lexer)) {
int32_t c = lexer->lookahead;
advance(lexer);
if (c == '"' && lexer->lookahead != '"') return;
if (c == '"') advance(lexer);
}
}
static void skip_raw_string(TSLexer *lexer, unsigned quotes) {
while (lexer->lookahead == '"') {
quotes++;
advance(lexer);
}
for (unsigned run = 0; !at_eof(lexer) && run < quotes;) {
run = lexer->lookahead == '"' ? run + 1 : 0;
advance(lexer);
}
}
static void skip_quoted_literal(TSLexer *lexer, int32_t quote) {
while (!at_eof(lexer) && !is_line_terminator(lexer->lookahead) && lexer->lookahead != quote) {
if (lexer->lookahead == '\\') advance(lexer);
advance(lexer);
}
advance(lexer);
}
static int32_t advance_skipping_literal(TSLexer *lexer) {
int32_t c = lexer->lookahead;
advance(lexer);
if (c == '/' && (lexer->lookahead == '/' || lexer->lookahead == '*')) {
if (lexer->lookahead == '/') skip_line_comment(lexer);
else skip_block_comment(lexer);
return 0;
}
if (c == '@' && lexer->lookahead == '"') {
advance(lexer);
skip_verbatim_string(lexer);
return '"';
}
if (c == '"' && lexer->lookahead == '"') {
advance(lexer);
if (lexer->lookahead == '"') skip_raw_string(lexer, 2);
return c;
}
if (c == '"' || c == '\'') skip_quoted_literal(lexer, c);
return c;
}
static bool parameter_list_follows(TSLexer *lexer) {
unsigned budget = 1024;
if (lexer->lookahead == '<') {
if (!skip_type_parameters(lexer, &budget) || skip_ws_and_comments(lexer)) return false;
if (lexer->lookahead != '(') return false;
}
for (unsigned depth = 0; budget > 0 && !at_eof(lexer); budget--) {
int32_t c = advance_skipping_literal(lexer);
if (c == '(') depth++;
if (c == ')' && --depth == 0) return local_function_body_follows(lexer);
}
return false;
}
typedef enum {
TERNARY,
NULLABLE,
NULLABLE_OR_TERNARY,
} QuestionReading;
static QuestionReading declaration_end_follows(TSLexer *lexer) {
if (is_ascii_in(lexer->lookahead, ";,)")) return NULLABLE;
if (lexer->lookahead != '=') return TERNARY;
advance(lexer);
return lexer->lookahead == '=' || lexer->lookahead == '>' ? TERNARY : NULLABLE_OR_TERNARY;
}
static QuestionReading name_ends_type(TSLexer *lexer) {
static const QuestionKeyword operators[] = {"is", "as", "switch", "with"};
if (lexer->lookahead == '{' || lexer->lookahead == '@') return NULLABLE;
if (lexer->lookahead == '(' || lexer->lookahead == '<') return parameter_list_follows(lexer) ? NULLABLE : TERNARY;
if (!is_id_start(lexer->lookahead)) return declaration_end_follows(lexer);
char word[7];
size_t length = consume_identifier_into(lexer, word, sizeof word);
return length <= sizeof word && is_word(word, length, operators, sizeof operators / sizeof *operators) ? TERNARY
: NULLABLE;
}
static QuestionReading identifier_follows_type(TSLexer *lexer, bool verbatim) {
static const QuestionKeyword expression_starts[] = {
"new", "typeof", "default", "null", "true", "false", "this", "base", "sizeof", "checked", "unchecked",
"throw", "stackalloc", "delegate", "ref", "static",
"bool", "byte", "char", "decimal", "double", "float", "int", "long", "object", "sbyte", "short", "string",
"uint", "ulong", "ushort", "void",
};
static const QuestionKeyword type_followers[] = {"in", "is", "as", "switch"};
static const QuestionKeyword contextual[] = {"await", "async", "from", "not", "nameof", "and", "or", "when", "where", "with"};
QuestionKeyword word;
size_t length = consume_identifier_into(lexer, word, sizeof word);
bool keyword = !verbatim && length < sizeof word;
if (keyword && is_word(word, length, contextual, sizeof contextual / sizeof *contextual)) return NULLABLE_OR_TERNARY;
if (keyword && is_word(word, length, expression_starts, sizeof expression_starts / sizeof *expression_starts)) {
return TERNARY;
}
if (keyword && is_word(word, length, type_followers, sizeof type_followers / sizeof *type_followers)) return NULLABLE;
return skip_ws_and_comments(lexer) ? TERNARY : name_ends_type(lexer);
}
static void advance_identifier(TSLexer *lexer) {
while (is_id_continue(lexer->lookahead) || lexer->lookahead == '\\') advance(lexer);
}
static bool arrow_follows(TSLexer *lexer) {
if (skip_ws_and_comments(lexer) || lexer->lookahead != '=') return false;
advance(lexer);
return lexer->lookahead == '>';
}
typedef enum {
HEADER_NAME_LAMBDA,
HEADER_NAME_NOT_LAMBDA,
HEADER_NAME_TYPE,
} HeaderName;
static HeaderName read_header_name(TSLexer *lexer, bool typed) {
if (skip_ws_and_comments(lexer)) return HEADER_NAME_NOT_LAMBDA;
if (typed || lexer->lookahead != '=') return HEADER_NAME_TYPE;
return arrow_follows(lexer) ? HEADER_NAME_LAMBDA : HEADER_NAME_NOT_LAMBDA;
}
static bool lambda_follows(TSLexer *lexer) {
unsigned depth = 0;
bool typed = false;
for (unsigned budget = 128; budget > 0 && !at_eof(lexer); budget--) {
if (skip_ws_and_comments(lexer) && depth == 0) return false;
bool identifier = is_id_start(lexer->lookahead) || lexer->lookahead == '@';
int32_t c = advance_skipping_literal(lexer);
if (identifier) {
advance_identifier(lexer);
if (depth > 0) continue;
HeaderName name = read_header_name(lexer, typed);
if (name != HEADER_NAME_TYPE) return name == HEADER_NAME_LAMBDA;
typed = true;
} else if (c == '(' || c == '[' || c == '<') {
depth++;
} else if (c == ')' || c == ']' || c == '>') {
if (depth-- == 0) return false;
if (depth == 0 && c == ')') return arrow_follows(lexer);
} else if (depth == 0 && !is_ascii_in(c, ".?*,")) {
return false;
}
}
return false;
}
typedef enum {
BRACKET_INDEX,
BRACKET_ARM,
BRACKET_UNDECIDED,
} BracketFollower;
static BracketFollower read_bracket_follower(TSLexer *lexer) {
if (skip_ws_and_comments(lexer)) return BRACKET_UNDECIDED;
if (lexer->lookahead == '.') return BRACKET_INDEX;
if (lexer->lookahead == '(' || lexer->lookahead == '[' || is_id_start(lexer->lookahead)) {
return lambda_follows(lexer) ? BRACKET_ARM : BRACKET_INDEX;
}
return BRACKET_UNDECIDED;
}
static bool spaced_index_follows(TSLexer *lexer, bool in_interpolation, bool *empty_rank) {
advance(lexer);
if (skip_ws_and_comments(lexer)) return false;
*empty_rank = lexer->lookahead == ']' || lexer->lookahead == ',';
if (*empty_rank) return false;
unsigned depth = 1;
for (unsigned budget = 128; budget > 0; budget--) {
skip_ws_and_comments(lexer);
if (at_eof(lexer)) return true;
bool identifier = is_id_continue(lexer->lookahead);
int32_t c = advance_skipping_literal(lexer);
if (identifier) {
advance_identifier(lexer);
} else if (c == '(' || c == '[' || c == '{') {
depth++;
} else if (c == ')' || c == ']' || c == '}') {
if (depth-- == 0) return true;
if (depth > 0 || c != ']') continue;
BracketFollower follower = read_bracket_follower(lexer);
if (follower != BRACKET_UNDECIDED) return follower == BRACKET_INDEX;
} else if (depth == 0 && (c == ';' || c == ',')) {
return true;
} else if (depth == 0 && c == ':') {
if (lexer->lookahead != ':') return in_interpolation;
advance(lexer);
}
}
return false;
}
static bool select_question(TSLexer *lexer, const bool *valid_symbols, QuestionReading reading);
static QuestionReading read_spaced_bracket(TSLexer *lexer, const bool *valid_symbols, bool *access) {
if (valid_symbols[CREATION_NULLABLE_QUESTION]) return NULLABLE;
bool empty_rank = false;
*access = spaced_index_follows(lexer, valid_symbols[INTERPOLATION_CLOSE_BRACE], &empty_rank);
return empty_rank ? NULLABLE_OR_TERNARY : TERNARY;
}
static bool empty_rank_follows(TSLexer *lexer) {
advance(lexer);
return !skip_ws_and_comments(lexer) && (lexer->lookahead == ']' || lexer->lookahead == ',');
}
static bool skip_trivia_and_directives(TSLexer *lexer) {
while (!skip_ws_and_comments(lexer)) {
if (lexer->lookahead != '#') return false;
advance_to_line_end(lexer);
}
return true;
}
static QuestionReading read_question_operand(TSLexer *lexer);
static bool scan_question(TSLexer *lexer, const bool *valid_symbols) {
advance(lexer);
lexer->mark_end(lexer);
if (lexer->lookahead == '?') return false;
bool access = false;
QuestionReading reading = TERNARY;
if (lexer->lookahead == '[') {
if (valid_symbols[CREATION_NULLABLE_QUESTION] || empty_rank_follows(lexer)) reading = NULLABLE;
else access = true;
} else if (skip_trivia_and_directives(lexer)) {
reading = NULLABLE;
} else if (lexer->lookahead == '.') {
advance(lexer);
access = !(lexer->lookahead >= '0' && lexer->lookahead <= '9') && lexer->lookahead != '.';
} else if (lexer->lookahead == '[') {
reading = read_spaced_bracket(lexer, valid_symbols, &access);
} else if (valid_symbols[CREATION_NULLABLE_QUESTION] || (valid_symbols[NULLABLE_QUESTION] && valid_symbols[TERNARY_QUESTION])) {
reading = read_question_operand(lexer);
}
if (access && valid_symbols[CONDITIONAL_ACCESS_QUESTION]) {
lexer->result_symbol = CONDITIONAL_ACCESS_QUESTION;
return true;
}
return select_question(lexer, valid_symbols, reading);
}
static QuestionReading read_question_operand(TSLexer *lexer) {
int32_t c = lexer->lookahead;
if (c == '(') return parameter_list_follows(lexer) ? NULLABLE_OR_TERNARY : TERNARY;
if (c == '&' || c == '!') {
advance(lexer);
return lexer->lookahead == (c == '&' ? '&' : '=') ? NULLABLE : TERNARY;
}
if (c == '@') advance(lexer);
if (c == '@' || is_id_start(c)) return identifier_follows_type(lexer, c == '@');
return at_eof(lexer) || is_ascii_in(c, ")]},;:=><|?{%") ? NULLABLE : TERNARY;
}
static bool select_question(TSLexer *lexer, const bool *valid_symbols, QuestionReading reading) {
bool both = valid_symbols[NULLABLE_QUESTION] && valid_symbols[TERNARY_QUESTION];
if (reading == NULLABLE && valid_symbols[CREATION_NULLABLE_QUESTION]) {
lexer->result_symbol = CREATION_NULLABLE_QUESTION;
} else if (reading == NULLABLE_OR_TERNARY && both) {
lexer->result_symbol = NULLABLE_OR_TERNARY_QUESTION;
} else if (valid_symbols[NULLABLE_QUESTION] && (reading == NULLABLE || !valid_symbols[TERNARY_QUESTION])) {
lexer->result_symbol = NULLABLE_QUESTION;
} else if (valid_symbols[TERNARY_QUESTION]) {
lexer->result_symbol = TERNARY_QUESTION;
} else {
return false;
}
return true;
}
typedef enum {
COMPARISON,
TYPE_ARGUMENTS,
TYPE_ARGUMENTS_OR_COMPARISON,
} LessThanReading;
static LessThanReading type_arguments_end(TSLexer *lexer) {
if (skip_trivia_and_directives(lexer)) return COMPARISON;
int32_t c = lexer->lookahead;
if (is_id_start(c) || c == '@' || c == '\\') return TYPE_ARGUMENTS_OR_COMPARISON;
if (c == '!' || c == '=') {
advance(lexer);
bool closes = lexer->lookahead == '=' || (c == '=' && lexer->lookahead == '>');
return closes ? TYPE_ARGUMENTS : COMPARISON;
}
return at_eof(lexer) || is_ascii_in(c, "()]}:;,.?|^&[{*") ? TYPE_ARGUMENTS : COMPARISON;
}
static bool follows_complete_type(int32_t c, bool identifier, unsigned parentheses) {
return (identifier && parentheses == 0) || c == '(';
}
static LessThanReading type_arguments_follow(TSLexer *lexer) {
unsigned angles = 1;
unsigned parentheses = 0;
bool after_identifier = false;
for (unsigned budget = 128; budget > 0; budget--) {
if (skip_trivia_and_directives(lexer) || at_eof(lexer)) return COMPARISON;
int32_t c = lexer->lookahead;
bool identifier = is_id_start(c) || c == '@' || c == '\\';
if (after_identifier && follows_complete_type(c, identifier, parentheses)) return COMPARISON;
after_identifier = identifier || c == '?' || c == ']' || c == '>';
advance(lexer);
if (identifier) {
advance_identifier(lexer);
} else if (c == '>') {
if (--angles == 0) return parentheses == 0 ? type_arguments_end(lexer) : COMPARISON;
} else if (c == '<') {
angles++;
} else if (c == '(') {
parentheses++;
} else if (c == ')') {
if (parentheses-- == 0) return COMPARISON;
} else if (c == ':') {
if (lexer->lookahead != ':') return COMPARISON;
advance(lexer);
} else if (!is_ascii_in(c, ".,?*[]")) {
return COMPARISON;
}
}
return COMPARISON;
}
static bool scan_less_than(TSLexer *lexer, const bool *valid_symbols) {
advance(lexer);
lexer->mark_end(lexer);
if (lexer->lookahead == '<' || lexer->lookahead == '=') return false;
LessThanReading reading = !valid_symbols[LESS_THAN] ? TYPE_ARGUMENTS
: !valid_symbols[TYPE_LIST_OPEN] ? COMPARISON
: type_arguments_follow(lexer);
lexer->result_symbol = reading == TYPE_ARGUMENTS ? TYPE_LIST_OPEN
: reading == COMPARISON ? LESS_THAN
: TYPE_LIST_OR_LESS_THAN;
return true;
}