willbooster-tree-sitter-c-sharp 38.2.1

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

// Reads a directive name after `#` into `name` (truncated to `max` characters).
static size_t read_directive_name(TSLexer *lexer, char *name, size_t max) {
    while (is_directive_horizontal(lexer->lookahead)) advance(lexer);
    size_t length = 0;
    while (lexer->lookahead >= 'a' && lexer->lookahead <= 'z') {
        if (length < max) name[length] = (char)lexer->lookahead;
        length++;
        advance(lexer);
    }
    return length;
}

static void advance_to_line_end(TSLexer *lexer) {
    while (!at_eof(lexer) && !is_line_terminator(lexer->lookahead)) advance(lexer);
}

// Whether the condition of an `#if`, which the lexer is in, is the literal `false`. Other conditions name build
// symbols that the source cannot tell, so their branch is kept as written.
static bool condition_is_false(TSLexer *lexer) {
    while (is_directive_horizontal(lexer->lookahead)) advance(lexer);
    char word[6];
    size_t length = consume_identifier_into(lexer, word, sizeof word);
    while (is_directive_horizontal(lexer->lookahead)) advance(lexer);
    bool ends = at_eof(lexer) || is_line_terminator(lexer->lookahead) || lexer->lookahead == '/';
    return ends && buf_equals(word, length, "false");
}

// Consumes source lines up to and including the line of the `#endif` that closes the current conditional section or,
// with `stop_at_alternative`, of its next `#else` or `#elif` whose condition is not the literal `false`, which starts a
// kept branch.
// code-gauge-allow functionCognitiveComplexity -- Nesting depth and the directive kinds that end a section share one line scan.
static void skip_inactive_lines(TSLexer *lexer, bool stop_at_alternative) {
    unsigned depth = 0;
    for (;;) {
        advance_to_line_end(lexer);
        lexer->mark_end(lexer);
        if (at_eof(lexer)) return;
        advance(lexer);
        while (is_directive_horizontal(lexer->lookahead)) advance(lexer);
        if (lexer->lookahead != '#') continue;
        advance(lexer);
        char name[5];
        size_t length = read_directive_name(lexer, name, sizeof name);
        if (buf_equals(name, length, "if")) {
            depth++;
        } else if (buf_equals(name, length, "endif")) {
            if (depth == 0) break;
            depth--;
        } else if (depth == 0 && stop_at_alternative &&
                   (buf_equals(name, length, "else") || (buf_equals(name, length, "elif") && !condition_is_false(lexer)))) {
            break;
        }
    }
    advance_to_line_end(lexer);
    lexer->mark_end(lexer);
}

// The lexer is after `#`. Keeps the first branch of each conditional section, unless its condition is the literal
// `false`, and matches the source that is not kept as one token: from an `#if false` through the directive line of the
// next `#elif` that is not `#elif false` or `#else`, or the closing `#endif`, and from an `#elif` or `#else` that
// follows a kept branch through the closing `#endif`.
static bool scan_preproc_inactive(TSLexer *lexer) {
    char name[5];
    size_t length = read_directive_name(lexer, name, sizeof name);
    bool stop_at_alternative = buf_equals(name, length, "if");
    if (stop_at_alternative) {
        if (!condition_is_false(lexer)) return false;
    } else if (!buf_equals(name, length, "elif") && !buf_equals(name, length, "else")) {
        return false;
    }
    skip_inactive_lines(lexer, stop_at_alternative);
    lexer->result_symbol = PREPROC_INACTIVE;
    return true;
}

#endif