Sixth R7b port — the T1½ batch finale. Checklist-first recipe
(docs/design/kotlin-kernel-port-checklist.md, 1,121 lines, dist-extractor
ground truth); parity passed FIRST RUN on all three repos.
THE NOVEL MECHANISM — vendored-grammar-C (the §4 tracker's prescription,
first use): the crates.io tree-sitter-kotlin 0.3.8 pins `tree-sitter >= 0.21,
< 0.23` (the kernel links 0.25) and tree-sitter-kotlin-ng is a DIFFERENT
grammar (8 fields vs 0, renamed kinds — extractor-breaking), so no crate dep
is possible. The fwcd 0.3.8 tag's sha-matched parser.c + scanner.c are
vendored into codegraph-kernel/grammars/kotlin and compiled by build.rs (cc),
exposed via tree-sitter-language::LanguageFn. The wasm re-vendor is
behavior-NEUTRAL (0 CST/error disagreements across 1,984 gate-repo files;
old-vs-new full-init dumps byte-identical ×3) — a reproducibility re-vendor,
ABI stays 14.
Walker firsts: extension-function receivers (getReceiverType →
`WidgetK::extend` QN OVERRIDE with no package prefix, the qualified-receiver
`com::qext` first-segment bug, and the owner-contains fallback that excludes
`interface` kinds and is source-order dependent) and extractModifiers
(expect/actual platform modifiers → the node DECORATORS wire field on every
created node — the KMP synthesizer's feed, incl. `actual typealias`).
Preserved bug-for-bug: the FIELD_COUNT-0 dead cluster (no signatures, ZERO
type-annotation refs), hook-consumed property initializers emitting nothing
(incl. `by lazy {}`), the bodiless-vs-bodied class header asymmetry, enum-
entry bodies being invisible, KDoc never a docstring AND chain-breaking,
comment-gluing into import/package extents, `@Anno(args)` emitting nothing
while `@Marker` decorates, zero instantiates refs, the paren-then-lambda
`trailing()` garbage callee, text-includes visibility/suspend false
positives, and the packaged-file value-ref target drop. The fun-interface
misparse-recovery hook is DEFER-SHIELDED (every such file has_error) and
deliberately not ported. The swift-sweep lesson pre-applied: the shared
`assignment` shadow-prune case is implemented alongside the
property_declaration case.
Gates: sweeps 0-diff okio 299/322, okhttp 531/580, kotlinx.coroutines
1031/1082 (deferrals exactly the predicted 23/49/51 — both-arm grammar
reality incl. PHANTOM hasError files with complete CSTs; the kernel trusts
the flag); full-init dumps byte-identical ×3 (46.5k/108.9k/92.3k lines); KMP
expect/actual synthesis IDENTICAL across arms (412 edges on
kotlinx.coroutines — the tracker's KMP validation); kernel-kotlin-parity
suite (torture reflowed off the phantom shapes + .kts script + CRLF variants
+ fun-interface and phantom defer pins) + kotlin grammar-parity row (the
C-build ↔ wasm table identity proof); full suite 2,633 green ×2 under
CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += kotlin (15 langs).
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
266 lines
6.8 KiB
C
266 lines
6.8 KiB
C
#ifndef TREE_SITTER_PARSER_H_
|
|
#define TREE_SITTER_PARSER_H_
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
#define ts_builtin_sym_error ((TSSymbol)-1)
|
|
#define ts_builtin_sym_end 0
|
|
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
|
|
|
|
#ifndef TREE_SITTER_API_H_
|
|
typedef uint16_t TSStateId;
|
|
typedef uint16_t TSSymbol;
|
|
typedef uint16_t TSFieldId;
|
|
typedef struct TSLanguage TSLanguage;
|
|
#endif
|
|
|
|
typedef struct {
|
|
TSFieldId field_id;
|
|
uint8_t child_index;
|
|
bool inherited;
|
|
} TSFieldMapEntry;
|
|
|
|
typedef struct {
|
|
uint16_t index;
|
|
uint16_t length;
|
|
} TSFieldMapSlice;
|
|
|
|
typedef struct {
|
|
bool visible;
|
|
bool named;
|
|
bool supertype;
|
|
} TSSymbolMetadata;
|
|
|
|
typedef struct TSLexer TSLexer;
|
|
|
|
struct TSLexer {
|
|
int32_t lookahead;
|
|
TSSymbol result_symbol;
|
|
void (*advance)(TSLexer *, bool);
|
|
void (*mark_end)(TSLexer *);
|
|
uint32_t (*get_column)(TSLexer *);
|
|
bool (*is_at_included_range_start)(const TSLexer *);
|
|
bool (*eof)(const TSLexer *);
|
|
};
|
|
|
|
typedef enum {
|
|
TSParseActionTypeShift,
|
|
TSParseActionTypeReduce,
|
|
TSParseActionTypeAccept,
|
|
TSParseActionTypeRecover,
|
|
} TSParseActionType;
|
|
|
|
typedef union {
|
|
struct {
|
|
uint8_t type;
|
|
TSStateId state;
|
|
bool extra;
|
|
bool repetition;
|
|
} shift;
|
|
struct {
|
|
uint8_t type;
|
|
uint8_t child_count;
|
|
TSSymbol symbol;
|
|
int16_t dynamic_precedence;
|
|
uint16_t production_id;
|
|
} reduce;
|
|
uint8_t type;
|
|
} TSParseAction;
|
|
|
|
typedef struct {
|
|
uint16_t lex_state;
|
|
uint16_t external_lex_state;
|
|
} TSLexMode;
|
|
|
|
typedef union {
|
|
TSParseAction action;
|
|
struct {
|
|
uint8_t count;
|
|
bool reusable;
|
|
} entry;
|
|
} TSParseActionEntry;
|
|
|
|
typedef struct {
|
|
int32_t start;
|
|
int32_t end;
|
|
} TSCharacterRange;
|
|
|
|
struct TSLanguage {
|
|
uint32_t version;
|
|
uint32_t symbol_count;
|
|
uint32_t alias_count;
|
|
uint32_t token_count;
|
|
uint32_t external_token_count;
|
|
uint32_t state_count;
|
|
uint32_t large_state_count;
|
|
uint32_t production_id_count;
|
|
uint32_t field_count;
|
|
uint16_t max_alias_sequence_length;
|
|
const uint16_t *parse_table;
|
|
const uint16_t *small_parse_table;
|
|
const uint32_t *small_parse_table_map;
|
|
const TSParseActionEntry *parse_actions;
|
|
const char * const *symbol_names;
|
|
const char * const *field_names;
|
|
const TSFieldMapSlice *field_map_slices;
|
|
const TSFieldMapEntry *field_map_entries;
|
|
const TSSymbolMetadata *symbol_metadata;
|
|
const TSSymbol *public_symbol_map;
|
|
const uint16_t *alias_map;
|
|
const TSSymbol *alias_sequences;
|
|
const TSLexMode *lex_modes;
|
|
bool (*lex_fn)(TSLexer *, TSStateId);
|
|
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
|
|
TSSymbol keyword_capture_token;
|
|
struct {
|
|
const bool *states;
|
|
const TSSymbol *symbol_map;
|
|
void *(*create)(void);
|
|
void (*destroy)(void *);
|
|
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
|
|
unsigned (*serialize)(void *, char *);
|
|
void (*deserialize)(void *, const char *, unsigned);
|
|
} external_scanner;
|
|
const TSStateId *primary_state_ids;
|
|
};
|
|
|
|
static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
|
|
uint32_t index = 0;
|
|
uint32_t size = len - index;
|
|
while (size > 1) {
|
|
uint32_t half_size = size / 2;
|
|
uint32_t mid_index = index + half_size;
|
|
TSCharacterRange *range = &ranges[mid_index];
|
|
if (lookahead >= range->start && lookahead <= range->end) {
|
|
return true;
|
|
} else if (lookahead > range->end) {
|
|
index = mid_index;
|
|
}
|
|
size -= half_size;
|
|
}
|
|
TSCharacterRange *range = &ranges[index];
|
|
return (lookahead >= range->start && lookahead <= range->end);
|
|
}
|
|
|
|
/*
|
|
* Lexer Macros
|
|
*/
|
|
|
|
#ifdef _MSC_VER
|
|
#define UNUSED __pragma(warning(suppress : 4101))
|
|
#else
|
|
#define UNUSED __attribute__((unused))
|
|
#endif
|
|
|
|
#define START_LEXER() \
|
|
bool result = false; \
|
|
bool skip = false; \
|
|
UNUSED \
|
|
bool eof = false; \
|
|
int32_t lookahead; \
|
|
goto start; \
|
|
next_state: \
|
|
lexer->advance(lexer, skip); \
|
|
start: \
|
|
skip = false; \
|
|
lookahead = lexer->lookahead;
|
|
|
|
#define ADVANCE(state_value) \
|
|
{ \
|
|
state = state_value; \
|
|
goto next_state; \
|
|
}
|
|
|
|
#define ADVANCE_MAP(...) \
|
|
{ \
|
|
static const uint16_t map[] = { __VA_ARGS__ }; \
|
|
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
|
|
if (map[i] == lookahead) { \
|
|
state = map[i + 1]; \
|
|
goto next_state; \
|
|
} \
|
|
} \
|
|
}
|
|
|
|
#define SKIP(state_value) \
|
|
{ \
|
|
skip = true; \
|
|
state = state_value; \
|
|
goto next_state; \
|
|
}
|
|
|
|
#define ACCEPT_TOKEN(symbol_value) \
|
|
result = true; \
|
|
lexer->result_symbol = symbol_value; \
|
|
lexer->mark_end(lexer);
|
|
|
|
#define END_STATE() return result;
|
|
|
|
/*
|
|
* Parse Table Macros
|
|
*/
|
|
|
|
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
|
|
|
|
#define STATE(id) id
|
|
|
|
#define ACTIONS(id) id
|
|
|
|
#define SHIFT(state_value) \
|
|
{{ \
|
|
.shift = { \
|
|
.type = TSParseActionTypeShift, \
|
|
.state = (state_value) \
|
|
} \
|
|
}}
|
|
|
|
#define SHIFT_REPEAT(state_value) \
|
|
{{ \
|
|
.shift = { \
|
|
.type = TSParseActionTypeShift, \
|
|
.state = (state_value), \
|
|
.repetition = true \
|
|
} \
|
|
}}
|
|
|
|
#define SHIFT_EXTRA() \
|
|
{{ \
|
|
.shift = { \
|
|
.type = TSParseActionTypeShift, \
|
|
.extra = true \
|
|
} \
|
|
}}
|
|
|
|
#define REDUCE(symbol_name, children, precedence, prod_id) \
|
|
{{ \
|
|
.reduce = { \
|
|
.type = TSParseActionTypeReduce, \
|
|
.symbol = symbol_name, \
|
|
.child_count = children, \
|
|
.dynamic_precedence = precedence, \
|
|
.production_id = prod_id \
|
|
}, \
|
|
}}
|
|
|
|
#define RECOVER() \
|
|
{{ \
|
|
.type = TSParseActionTypeRecover \
|
|
}}
|
|
|
|
#define ACCEPT_INPUT() \
|
|
{{ \
|
|
.type = TSParseActionTypeAccept \
|
|
}}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // TREE_SITTER_PARSER_H_
|