willbooster-tree-sitter-c-sharp 38.2.1

C# grammar for tree-sitter
Documentation
#ifndef TREE_SITTER_C_SHARP_RECORD_KEYWORD_H_
#define TREE_SITTER_C_SHARP_RECORD_KEYWORD_H_

static QuestionScanResult scan_record_keyword(TSLexer *lexer, const bool *valid_symbols);
static QuestionScanResult scan_record_follower(TSLexer *lexer, const bool *valid_symbols);
static QuestionScanResult scan_member_record_modifier(TSLexer *lexer);
static bool parenthesized_arrow_follows(TSLexer *lexer);

static QuestionScanResult scan_contextual_word(TSLexer *lexer, const bool *valid_symbols) {
    QuestionScanResult record = scan_record_keyword(lexer, valid_symbols);
    return record == QUESTION_NOT_STARTED ? scan_await_identifier(lexer, valid_symbols) : record;
}

static QuestionScanResult scan_record_keyword(TSLexer *lexer, const bool *valid_symbols) {
    bool member_modifier = valid_symbols[MEMBER_RECORD_MODIFIER];
    // Directive ends must see the line break that skipped whitespace would hide, and pragma text must see its words.
    if ((!valid_symbols[RECORD_KEYWORD] && !member_modifier) || valid_symbols[DIRECTIVE_NEWLINE] ||
        valid_symbols[FILE_DIRECTIVE_NEWLINE] || valid_symbols[PRAGMA_TEXT] || valid_symbols[PRAGMA_BOUNDARY])
        return QUESTION_NOT_STARTED;
    while (is_space_but_line_feed(lexer->lookahead) || is_line_terminator(lexer->lookahead)) skip(lexer);
    int32_t first = lexer->lookahead;
    if (first != 'r' && !(member_modifier && (first == 'p' || first == 'f'))) return QUESTION_NOT_STARTED;
    char word[8];
    size_t length = consume_identifier_into(lexer, word, sizeof(word));
    if (lexer->lookahead == '\\') return QUESTION_MISMATCHED;
    if (member_modifier &&
        (buf_equals(word, length, "partial") || buf_equals(word, length, "file") || buf_equals(word, length, "required")))
        return scan_member_record_modifier(lexer);
    if (!valid_symbols[RECORD_KEYWORD] || !buf_equals(word, length, "record")) return QUESTION_MISMATCHED;
    lexer->mark_end(lexer);
    return scan_record_follower(lexer, valid_symbols);
}

// code-gauge-allow functionCognitiveComplexity -- The tokens after `record` decide among its readings in one forward scan.
// code-gauge-allow functionNcss -- The tokens after `record` decide among its readings in one forward scan.
static QuestionScanResult scan_record_follower(TSLexer *lexer, const bool *valid_symbols) {
    lexer->result_symbol = RECORD_KEYWORD;
    bool constructor = valid_symbols[RECORD_CONSTRUCTOR];
    if (!skip_await_trivia(lexer)) return QUESTION_MATCHED;
    // After `record` and a name, an initializer declares a field or local, and an expression body a method, of a type
    // named `record`.
    if (is_id_start(lexer->lookahead)) {
        advance_identifier(lexer);
        if (!skip_await_trivia(lexer)) return QUESTION_MATCHED;
        if (lexer->lookahead == '(') return parenthesized_arrow_follows(lexer) ? QUESTION_MISMATCHED : QUESTION_MATCHED;
        if (lexer->lookahead != '=') return QUESTION_MATCHED;
        advance(lexer);
        return lexer->lookahead == '>' ? QUESTION_MATCHED : QUESTION_MISMATCHED;
    }
    if (!valid_symbols[CREATION_TYPE_START] && !constructor) return QUESTION_MATCHED;
    int32_t next = lexer->lookahead;
    if (valid_symbols[CREATION_TYPE_START] && is_ascii_in(next, "([<.{:?*")) return QUESTION_MISMATCHED;
    if (!constructor) return QUESTION_MATCHED;
    if (is_ascii_in(next, "=;,{")) return QUESTION_MISMATCHED;
    if (next == ':') {
        advance(lexer);
        if (lexer->lookahead != ':') return QUESTION_MATCHED;
    }
    if (is_ascii_in(next, "(<.:")) lexer->result_symbol = RECORD_CONSTRUCTOR;
    return QUESTION_MATCHED;
}

static QuestionScanResult scan_member_record_modifier(TSLexer *lexer) {
    lexer->mark_end(lexer);
    if (!skip_await_trivia(lexer) || lexer->lookahead != 'r') return QUESTION_MISMATCHED;
    char word[6];
    size_t length = consume_identifier_into(lexer, word, sizeof(word));
    if (!buf_equals(word, length, "record") || lexer->lookahead == '\\' ||
        !skip_await_trivia(lexer) || lexer->lookahead == '(') return QUESTION_MISMATCHED;
    lexer->result_symbol = MEMBER_RECORD_MODIFIER;
    return QUESTION_MATCHED;
}

// Whether `=>` follows the bounded, balanced parenthesized group that the lexer is at.
static bool parenthesized_arrow_follows(TSLexer *lexer) {
    unsigned depth = 0;
    for (unsigned budget = 1024; budget > 0 && !at_eof(lexer); budget--) {
        int32_t c = advance_skipping_literal(lexer);
        if (c == '(') depth++;
        if (c == ')' && --depth == 0) {
            if (!skip_await_trivia(lexer) || lexer->lookahead != '=') return false;
            advance(lexer);
            return lexer->lookahead == '>';
        }
    }
    return false;
}

#endif