#ifndef TREE_SITTER_C_SHARP_AWAIT_IDENTIFIER_H_
#define TREE_SITTER_C_SHARP_AWAIT_IDENTIFIER_H_
static QuestionScanResult scan_await_word(TSLexer *lexer);
static QuestionScanResult scan_await_follower(TSLexer *lexer);
static bool await_continues_identifier(TSLexer *lexer);
static bool continues_expression(TSLexer *lexer);
static bool starts_pattern(TSLexer *lexer);
static bool skip_await_trivia(TSLexer *lexer);
static QuestionScanResult scan_await_identifier(TSLexer *lexer, const bool *valid_symbols) {
if (!valid_symbols[AWAIT_IDENTIFIER] || valid_symbols[PRAGMA_TEXT] ||
valid_symbols[PRAGMA_BOUNDARY] || valid_symbols[DIRECTIVE_NEWLINE] || valid_symbols[FILE_DIRECTIVE_NEWLINE] ||
valid_symbols[END_OF_INPUT])
return QUESTION_NOT_STARTED;
return scan_await_word(lexer);
}
static QuestionScanResult scan_await_word(TSLexer *lexer) {
while (is_space_but_line_feed(lexer->lookahead) || is_line_terminator(lexer->lookahead)) skip(lexer);
if (lexer->lookahead != 'a') return QUESTION_NOT_STARTED;
char word[6] = {0};
size_t length = consume_identifier_into(lexer, word, sizeof(word));
if (!buf_equals(word, length, "await")) return QUESTION_MISMATCHED;
return scan_await_follower(lexer);
}
static QuestionScanResult scan_await_follower(TSLexer *lexer) {
if (lexer->lookahead == '\\') return QUESTION_MISMATCHED;
lexer->mark_end(lexer);
lexer->result_symbol = AWAIT_IDENTIFIER;
return await_continues_identifier(lexer) ? QUESTION_MATCHED : QUESTION_MISMATCHED;
}
static bool await_continues_identifier(TSLexer *lexer) {
if (!skip_await_trivia(lexer)) return true;
if (lexer->lookahead == '!') {
advance(lexer);
return continues_expression(lexer);
}
return is_ascii_in(lexer->lookahead, "+-[") || continues_expression(lexer);
}
static bool continues_expression(TSLexer *lexer) {
if (!skip_await_trivia(lexer) || at_eof(lexer)) return true;
int32_t c = lexer->lookahead;
if (c == '.') {
advance(lexer);
return lexer->lookahead != '.' && (lexer->lookahead < '0' || lexer->lookahead > '9');
}
if (is_ascii_in(c, ")]},;:?=<>|&^*%")) return true;
if (c == '!') {
advance(lexer);
return lexer->lookahead == '=';
}
char word[7] = {0};
size_t length = consume_identifier_into(lexer, word, sizeof(word));
if (lexer->lookahead == '\\') return false;
if (buf_equals(word, length, "with")) return skip_await_trivia(lexer) && lexer->lookahead == '{';
if (buf_equals(word, length, "is") || buf_equals(word, length, "as") || buf_equals(word, length, "switch"))
return true;
if (buf_equals(word, length, "and") || buf_equals(word, length, "or") || buf_equals(word, length, "when"))
return starts_pattern(lexer);
return false;
}
static bool starts_pattern(TSLexer *lexer) {
if (!skip_await_trivia(lexer) || at_eof(lexer)) return false;
int32_t c = lexer->lookahead;
if (c == '.') {
advance(lexer);
return lexer->lookahead >= '0' && lexer->lookahead <= '9';
}
if (!is_id_start(c)) return (c >= '0' && c <= '9') || is_ascii_in(c, "@\\\"'${");
static const char *const continuations[] = {
"and", "as", "ascending", "by", "descending", "equals", "from", "group", "in", "into", "is", "join",
"let", "on", "or", "orderby", "select", "switch", "when", "where", "with",
};
char word[10] = {0};
size_t length = consume_identifier_into(lexer, word, sizeof(word));
if (lexer->lookahead == '\\') return true;
for (size_t i = 0; i < sizeof(continuations) / sizeof(continuations[0]); i++) {
if (buf_equals(word, length, continuations[i])) return false;
}
return true;
}
static bool skip_await_trivia(TSLexer *lexer) {
bool line_start = false;
for (;;) {
while (is_space_but_line_feed(lexer->lookahead) || is_line_terminator(lexer->lookahead)) {
line_start = line_start || is_line_terminator(lexer->lookahead);
advance(lexer);
}
if (line_start && lexer->lookahead == '#') {
skip_line_comment(lexer);
continue;
}
if (lexer->lookahead != '/') return true;
advance(lexer);
if (lexer->lookahead == '/') skip_line_comment(lexer);
else if (lexer->lookahead != '*' || !skip_block_comment(lexer)) return false;
}
}
#endif