willbooster-tree-sitter-c-sharp 38.2.1

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

static bool is_structured_pragma_warning(TSLexer *lexer);
static bool is_structured_pragma_warning_list(TSLexer *lexer);
static bool is_structured_pragma_checksum(TSLexer *lexer);
static void skip_pragma_space(TSLexer *lexer);
static bool is_pragma_end(TSLexer *lexer);

static bool scan_pragma_boundary(TSLexer *lexer) {
    if (is_directive_identifier_continue(lexer->lookahead)) return false;
    lexer->mark_end(lexer);
    if (lexer->lookahead == '\\') {
        advance(lexer);
        if (lexer->lookahead == 'u' || lexer->lookahead == 'U') return false;
    }
    lexer->result_symbol = PRAGMA_BOUNDARY;
    return true;
}

static bool scan_pragma_text(TSLexer *lexer) {
    char name[8];
    size_t length = 0;
    while (length < sizeof(name) && lexer->lookahead >= 'a' && lexer->lookahead <= 'z') {
        name[length++] = (char)lexer->lookahead;
        advance(lexer);
    }
    if (!is_directive_identifier_continue(lexer->lookahead) && !is_id_continue(lexer->lookahead) &&
        lexer->lookahead != '\\') {
        if (buf_equals(name, length, "warning") && is_structured_pragma_warning(lexer)) return false;
        if (buf_equals(name, length, "checksum") && is_structured_pragma_checksum(lexer)) return false;
    }
    return scan_preproc_arg(lexer, true, PRAGMA_TEXT);
}

// Roslyn reports malformed `warning` and `checksum` pragmas only with warnings, so they must not produce syntax errors.
// Only well-formed ones keep their structured children; the rest become opaque text like unknown pragmas. Roslyn also
// reports a delimited comment before the last operand as malformed, so only `is_pragma_end` skips one.
static bool is_structured_pragma_warning(TSLexer *lexer) {
    if (!is_directive_horizontal(lexer->lookahead)) return false;
    skip_pragma_space(lexer);
    char action[8];
    size_t length = 0;
    while (length < sizeof(action) && lexer->lookahead >= 'a' && lexer->lookahead <= 'z') {
        action[length++] = (char)lexer->lookahead;
        advance(lexer);
    }
    if (!buf_equals(action, length, "disable") && !buf_equals(action, length, "restore")) return false;
    if (lexer->lookahead == '\\' || is_directive_identifier_continue(lexer->lookahead)) return false;
    skip_pragma_space(lexer);
    if (lexer->eof(lexer) || is_line_terminator(lexer->lookahead)) return true;
    if (lexer->lookahead == '/') {
        advance(lexer);
        return lexer->lookahead == '/';
    }
    return is_structured_pragma_warning_list(lexer);
}

// An identifier with a Unicode escape keeps the structured reading, whose grammar validates it.
static bool is_structured_pragma_warning_list(TSLexer *lexer) {
    for (;;) {
        if (!is_directive_identifier_continue(lexer->lookahead)) return lexer->lookahead == '\\';
        while (is_directive_identifier_continue(lexer->lookahead)) advance(lexer);
        if (lexer->lookahead == '\\') return true;
        skip_pragma_space(lexer);
        if (lexer->lookahead != ',') return is_pragma_end(lexer);
        advance(lexer);
        skip_pragma_space(lexer);
    }
}

static bool is_structured_pragma_checksum(TSLexer *lexer) {
    for (int i = 0; i < 3; i++) {
        skip_pragma_space(lexer);
        if (lexer->lookahead != '"') return false;
        advance(lexer);
        while (lexer->lookahead != '"') {
            if (lexer->eof(lexer) || is_line_terminator(lexer->lookahead)) return false;
            advance(lexer);
        }
        advance(lexer);
    }
    return is_pragma_end(lexer);
}

static void skip_pragma_space(TSLexer *lexer) {
    while (is_directive_horizontal(lexer->lookahead)) advance(lexer);
}

// Skips space and delimited comments closed on the line, then accepts the line end or a line comment. A delimited comment
// left open at the line end is opaque text, because the grammar's comment would run past the directive.
static bool is_pragma_end(TSLexer *lexer) {
    for (;;) {
        skip_pragma_space(lexer);
        if (lexer->eof(lexer) || is_line_terminator(lexer->lookahead)) return true;
        if (lexer->lookahead != '/') return false;
        advance(lexer);
        if (lexer->lookahead == '/') return true;
        if (lexer->lookahead != '*') return false;
        advance(lexer);
        for (;;) {
            if (lexer->eof(lexer) || is_line_terminator(lexer->lookahead)) return false;
            bool star = lexer->lookahead == '*';
            advance(lexer);
            if (star && lexer->lookahead == '/') break;
        }
        advance(lexer);
    }
}

#endif // TREE_SITTER_C_SHARP_PRAGMA_DIRECTIVES_H_