willbooster-tree-sitter-c-sharp 38.2.1

C# grammar for tree-sitter
Documentation
#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) {
    // Pragma text must see the words that a consumed word would hide, and directive ends must see the line break that
    // skipped whitespace would hide.
    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);
}

// `await` is the await operator inside async functions and an identifier elsewhere, but the parser does not track async
// context. Like Roslyn outside async functions, this takes `await` as an identifier when the next token continues an
// identifier expression or pattern rather than starting an operand. Of the tokens that can do both, `+`, `-`, `*`, `&`,
// `^`, and `[` make an identifier because awaiting such a unary or collection operand is rare, while `(` keeps the
// operator because `await (task)` is common in async functions; `..` and a `!` followed by an operand keep it as well,
// as the identifier reading is no more common there.
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);
}

// Decides the role of an `await` word that the lexer has just consumed.
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);
}

// Whether the next token can follow a complete expression or pattern but cannot start an operand.
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;
    // These pattern words are also variable names that an async function can await, as in `await or;`.
    if (buf_equals(word, length, "and") || buf_equals(word, length, "or") || buf_equals(word, length, "when"))
        return starts_pattern(lexer);
    return false;
}

// Excludes pattern starts that can also continue an awaited variable, such as `(` in `await or(x)` or the keyword in
// `await or is T`, so a pattern like `x is await or (1)` keeps the operator.
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;
}

// Skips whitespace, comments, and directive lines. Returns false after consuming a `/` that is a division operator or
// an unterminated comment.
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