/* * doclink_jvm.c — Java and Kotlin doc-comment references and the JVM doc-link * scope. * * Tokens. * Javadoc: `{@link R}` / `{@link R label}`, `{@linkplain R label}`, * `{@value R#F}` (a bare `{@value}` names the documented field itself and is * no token), `@see R` at the start of a block-tag line (`@see ` is * a URL, `@see "text"` is prose), `@throws T` / `@exception T`. Nothing in * `{@code}`, `{@literal}`, `{@snippet}` or an HTML comment is a reference. * KDoc: `[Name]` and `[text][Name]` in the comment text (not `[text](url)`, * not an image, not a `[label]: url` definition, not in a code span or a * fenced or indented code block), `@see Name`, `@throws T` / `@exception T`, * `@sample qualified.name`. The subject of `@param` / `@property` is the * definition's own parameter, never a reference. * Only block doc comments (slash, two stars) are read: that is what javadoc * and Dokka read. * * Scope blob (one record per line, tab-separated, first line "jvm1"; records * in document order, a member after its type): * L lang j Java, k Kotlin * P package "" when the file declares none * I kind alias target import: s single (alias = the simple * name it binds), w on-demand / star, * m Java static single (alias = member, * target = type), n Java static * on-demand (target = type) * T start end kind path tparams supers slot own pnames * type: c class, i interface, e enum, * r record, a annotation, o object, * C companion object, t typealias; '!' * after the kind when a parse error * hides some of its members. path is * the dotted LOGICAL path (Outer.Inner), * tparams ','-joined, supers '|'-joined * as written without type arguments * ('?' for one that is no name) * M start kind flags owner name tparams sig recv slot own pnames * member: c callable, v value. flags: K * constructor, p constructor property / * record component, n enum constant, i * implicit (declared by no text of its * own). owner is the index of its T * record ('-' for a Kotlin top-level * declaration), sig the '|'-joined * normalized parameter types ('-' for a * value), recv the receiver type of a * Kotlin extension * Q name a type that is declared, but could not * be placed (parse error) * X from to lines whose declarations could not be * placed * slot / own say where the extractor put the declaration's node: slot is the * qualified name of its definition relative to the file's module ('=' when it * is the logical path, '-' when the declaration has no definition), own is 1 * when that definition is the one the graph keeps for the qualified name * inside this file. Both are read off the extractor's own definitions (same * start line, name and kind), so this file models neither the qualified-name * scheme nor its collisions: Kotlin flattens nested types and puts class * properties at module level, Java names a field after its whole declarator * (`MAX = 10`), overloads share one name, and whatever the scheme does next is * followed without a change here. * pnames (the parameter names of a callable, the primary-constructor * parameters of a Kotlin class) and all line numbers serve the file's own * references only: the persisted form drops them. * * Everything is a pure function of the file's bytes. Nesting is the tree's * while the tree has no parse error; otherwise it is checked against the * braces (see "Braces"). */ #include "doclink.h" #include "arena.h" #include "foundation/constants.h" #include "foundation/mem_core.h" #include "helpers.h" #include "tree_sitter/api.h" #include #include #include #include enum { JV_SB_INIT = 1024, JV_MAX_DEPTH = 128, /* nested type declarations */ JV_NAME_MAX = 255, /* one identifier */ JV_TYPE_MAX = 512, /* one written type */ JV_SLOT_MAX = 512, /* a definition's name relative to its module */ JV_UINT_DIGITS = 16, JV_ITEMS_INIT = 64, JV_BRACES_INIT = 1024, JV_CHAR_LITERAL_MAX = 12, /* a quoted unicode escape */ JV_QUOTES = 3, /* """ */ JV_UTF8_TAIL_MAX = 3, }; /* ── Small arena string builder ──────────────────────────────────── */ typedef struct { CBMArena *a; char *buf; size_t len; size_t cap; bool failed; } jv_sb_t; static void jv_reserve(jv_sb_t *sb, size_t extra) { if (sb->failed || sb->len + extra + SKIP_ONE <= sb->cap) { return; } size_t ncap = sb->cap ? sb->cap : JV_SB_INIT; while (ncap < sb->len + extra + SKIP_ONE) { ncap *= PAIR_LEN; } char *grown = (char *)cbm_arena_alloc(sb->a, ncap); if (!grown) { sb->failed = true; return; } if (sb->len > 0) { memcpy(grown, sb->buf, sb->len); } sb->buf = grown; sb->cap = ncap; } static void jv_putn(jv_sb_t *sb, const char *s, size_t n) { jv_reserve(sb, n); if (sb->failed) { return; } memcpy(sb->buf + sb->len, s, n); sb->len += n; sb->buf[sb->len] = '\0'; } static void jv_puts(jv_sb_t *sb, const char *s) { jv_putn(sb, s, strlen(s)); } static void jv_putc(jv_sb_t *sb, char c) { jv_putn(sb, &c, SKIP_ONE); } static void jv_putu(jv_sb_t *sb, uint32_t v) { char tmp[JV_UINT_DIGITS]; int n = snprintf(tmp, sizeof(tmp), "%u", v); if (n > 0) { jv_putn(sb, tmp, (size_t)n); } } /* ── Characters ──────────────────────────────────────────────────── */ static bool jv_blank(char c) { return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v'; } /* Identifier characters: Java and Kotlin identifiers are Unicode, so every * byte of a multi-byte character counts. */ static bool jv_ident_start(char c) { unsigned char u = (unsigned char)c; return isalpha(u) || c == '_' || c == '$' || u >= 0x80; } static bool jv_ident_char(char c) { unsigned char u = (unsigned char)c; return isalnum(u) || c == '_' || c == '$' || u >= 0x80; } /* ── Doc text ────────────────────────────────────────────────────── */ /* The text of the block doc comments in `doc` (slash, two stars) without its * decoration: every line break stays (a position's line is the doc's first * line plus the breaks before it), each continuation line loses its leading * blanks, its asterisks and one blank. Line comments and plain block comments * between doc comments leave only their line breaks. `nesting`: block * comments nest (Kotlin), so a comment written inside the doc is part of its * text and its end is not the doc's. */ static char *jv_clean_doc(CBMArena *a, const char *doc, bool nesting, size_t *out_len) { size_t n = strlen(doc); char *out = (char *)cbm_arena_alloc(a, n + SKIP_ONE); if (!out) { return NULL; } size_t w = 0; size_t i = 0; bool in_doc = false; size_t inner = 0; /* comments open inside the doc */ while (i < n) { char c = doc[i]; if (!in_doc) { if (c == '/' && doc[i + 1] == '*' && doc[i + 2] == '*' && doc[i + 3] != '/') { in_doc = true; inner = 0; i += 3; while (doc[i] == '*' && doc[i + 1] != '/') { i++; } continue; } if (c == '\n') { out[w++] = '\n'; } i++; continue; } if (nesting && c == '/' && doc[i + 1] == '*') { inner++; out[w++] = '/'; out[w++] = '*'; i += 2; continue; } if (c == '*' && doc[i + 1] == '/') { if (inner > 0) { inner--; out[w++] = '*'; out[w++] = '/'; } else { in_doc = false; } i += 2; continue; } if (c == '\n') { out[w++] = '\n'; i++; while (doc[i] == ' ' || doc[i] == '\t' || doc[i] == '\r') { i++; } if (doc[i] == '*' && doc[i + 1] != '/') { while (doc[i] == '*' && doc[i + 1] != '/') { i++; } if (doc[i] == ' ') { i++; } } continue; } if (c != '\r') { out[w++] = c; } i++; } out[w] = '\0'; *out_len = w; return out; } typedef struct { CBMExtractCtx *ctx; const CBMDefinition *def; const char *t; /* the cleaned doc text */ size_t n; } jv_doc_t; /* Push the reference s[0, len) written on source line `line`: whitespace runs * become one blank, control characters go, and a reference longer than any * name is cut (on a character boundary) just past CBM_DOCLINK_JVM_REF_MAX, * which is how the resolver knows it was oversized. */ static void jv_push(jv_doc_t *d, const char *s, size_t len, uint32_t line, int syntax) { size_t cap = (size_t)CBM_DOCLINK_JVM_REF_MAX + JV_UTF8_TAIL_MAX + SKIP_ONE; size_t take = len < cap ? len : cap; char *raw = (char *)cbm_arena_alloc(d->ctx->arena, take + SKIP_ONE); if (!raw) { return; } size_t w = 0; bool space = false; for (size_t i = 0; i < len && w < take; i++) { unsigned char c = (unsigned char)s[i]; if (c <= ' ') { space = w > 0; continue; } if (space && w + SKIP_ONE < take) { raw[w++] = ' '; } space = false; raw[w++] = (char)c; } if (w == take && len > take) { /* cut: do not leave half a multi-byte character behind */ while (w > 0 && ((unsigned char)raw[w - SKIP_ONE] & 0xC0) == 0x80) { w--; } if (w > 0 && (unsigned char)raw[w - SKIP_ONE] >= 0xC0) { w--; } } raw[w] = '\0'; if (w == 0) { return; } CBMDocLink link = { .source_qn = d->def->qualified_name, .raw = raw, .line = line, .def_line = d->def->start_line, .syntax = (uint16_t)syntax, }; cbm_doclinks_push(&d->ctx->result->doc_links, d->ctx->arena, link); } /* Index just past the group opened at s[i] by `open` (nesting groups of the * same kind), or n when it does not close. */ static size_t jv_group_end(const char *s, size_t n, size_t i, char open, char close) { int depth = 0; for (size_t k = i; k < n; k++) { if (s[k] == open) { depth++; } else if (s[k] == close) { depth--; if (depth == 0) { return k + SKIP_ONE; } } } return n; } static bool jv_word_is(const char *s, size_t len, const char *word) { return strlen(word) == len && memcmp(s, word, len) == 0; } /* ── Javadoc ─────────────────────────────────────────────────────── */ /* Length of the Javadoc reference at s: a (qualified) type, `#member`, * `module/`, type arguments, array brackets and a parameter list. A parameter * list that does not close takes the rest (the reference is malformed, and * the resolver says so). */ static size_t jv_java_ref_len(const char *s, size_t n) { size_t j = 0; while (j < n) { char c = s[j]; if (jv_ident_char(c) || c == '.' || c == '#' || c == '/') { j++; } else if (c == '<') { size_t e = jv_group_end(s, n, j, '<', '>'); if (e >= n && (n == 0 || s[n - SKIP_ONE] != '>')) { break; } j = e; } else if (c == '[' && j + SKIP_ONE < n && s[j + SKIP_ONE] == ']') { j += PAIR_LEN; } else { break; } } if (j < n && s[j] == '(') { j = jv_group_end(s, n, j, '(', ')'); } return j; } /* The reference at s[0, n): what the doc tool reads as one reference. Text * glued to it (`Foo,`, `http://x`) makes the whole word the (malformed) * reference. Returns its length, 0 when there is none. */ static size_t jv_java_ref(const char *s, size_t n) { size_t r = jv_java_ref_len(s, n); if (r < n && !jv_blank(s[r])) { while (r < n && !jv_blank(s[r])) { r++; } } return r; } /* The href of an `` at s[0, n): pushed as a URL. */ static void jv_java_href(jv_doc_t *d, const char *s, size_t n, uint32_t line) { static const char key[] = "href"; for (size_t i = 0; i + sizeof(key) <= n && s[i] != '>'; i++) { if (memcmp(s + i, key, sizeof(key) - SKIP_ONE) != 0) { continue; } size_t j = i + sizeof(key) - SKIP_ONE; while (j < n && (s[j] == ' ' || s[j] == '\t')) { j++; } if (j >= n || s[j] != '=') { continue; } j++; while (j < n && (s[j] == ' ' || s[j] == '\t')) { j++; } if (j >= n || (s[j] != '"' && s[j] != '\'')) { return; } char quote = s[j]; size_t v = j + SKIP_ONE; size_t e = v; while (e < n && s[e] != quote) { e++; } if (e < n && e > v) { jv_push(d, s + v, e - v, line, CBM_DOCLINK_HREF); } return; } } /* `@see ...` / `@throws ...`: the rest of the tag's line starts at `i`. */ static void jv_java_block_tag(jv_doc_t *d, size_t i, uint32_t line, bool see) { const char *t = d->t; while (i < d->n && (t[i] == ' ' || t[i] == '\t')) { i++; } size_t eol = i; while (eol < d->n && t[eol] != '\n') { eol++; } if (i >= eol) { return; } if (see && t[i] == '<') { jv_java_href(d, t + i, eol - i, line); return; } if (t[i] == '"' || t[i] == '{') { return; /* a quoted title; an inline tag is found by the text scan */ } size_t r = jv_java_ref(t + i, eol - i); if (r > 0) { jv_push(d, t + i, r, line, see ? CBM_DOCLINK_JAVA_SEE : CBM_DOCLINK_JAVA_THROWS); } } /* An inline tag `{@name body}` whose body is t[b, e) and starts on `line`: * its reference. */ static void jv_java_inline(jv_doc_t *d, size_t b, size_t e, uint32_t line, int syntax) { const char *t = d->t; for (; b < e && jv_blank(t[b]); b++) { line += t[b] == '\n'; } if (syntax == CBM_DOCLINK_JAVA_VALUE && b < e && t[b] == '"') { /* {@value "format" ref}: the format string is not the reference */ size_t q = b + SKIP_ONE; while (q < e && t[q] != '"') { line += t[q] == '\n'; q++; } for (b = q < e ? q + SKIP_ONE : e; b < e && jv_blank(t[b]); b++) { line += t[b] == '\n'; } } size_t r = jv_java_ref(t + b, e - b); if (r > 0) { jv_push(d, t + b, r, line, syntax); } } /* An open inline tag. javadoc ends it at the brace that balances its own * `{`; braces outside inline tags are plain text and balance nothing. */ typedef struct { int syntax; /* a link family, JV_TAG_OTHER, or JV_TAG_TEXT */ size_t body; /* first byte after the tag's name */ uint32_t line; /* the line of that byte */ int depth; /* plain braces open inside it */ } jv_tag_t; enum { JV_TAG_OTHER = -1, /* another inline tag: its body is scanned */ JV_TAG_TEXT = -2, /* code, literal, snippet: the body is text, not markup */ JV_TAG_NEST_MAX = 32, }; /* The kind of the inline tag `{@name`; `next` is the character after the * name (a tag named `linkage` is not `link`). */ static int jv_java_tag_kind(const char *name, size_t len, char next) { if (jv_word_is(name, len, "code") || jv_word_is(name, len, "literal") || jv_word_is(name, len, "snippet")) { return JV_TAG_TEXT; } if (!jv_blank(next) && next != '}') { return JV_TAG_OTHER; } if (jv_word_is(name, len, "link")) { return CBM_DOCLINK_JAVA_LINK; } if (jv_word_is(name, len, "linkplain")) { return CBM_DOCLINK_JAVA_LINKPLAIN; } if (jv_word_is(name, len, "value")) { return CBM_DOCLINK_JAVA_VALUE; } return JV_TAG_OTHER; } /* Past an HTML comment or a CDATA section that starts at t[i] (the doc tool * reads no markup in either); i itself when none starts there. Line breaks * passed are added to *line. One that never ends takes the rest. */ static size_t jv_skip_hidden(const jv_doc_t *d, size_t i, uint32_t *line) { static const struct { const char *open; const char *close; } kinds[] = {{""}, {""}}; for (size_t k = 0; k < sizeof(kinds) / sizeof(kinds[0]); k++) { size_t ol = strlen(kinds[k].open); if (d->n - i < ol || memcmp(d->t + i, kinds[k].open, ol) != 0) { continue; } const char *close = strstr(d->t + i + ol, kinds[k].close); size_t past = close ? (size_t)(close - d->t) + strlen(kinds[k].close) : d->n; for (size_t p = i; p < past; p++) { *line += d->t[p] == '\n'; } return past; } return i; } void cbm_doclink_java_parse_doc(CBMExtractCtx *ctx, const CBMDefinition *def, const char *doc, uint32_t doc_line) { /* A field is also emitted as a package-level Variable with the same doc: * the Field carries the references. A module declaration is no code * definition (its "class" is named like a package). */ if (!def->label || (strcmp(def->label, "Variable") == 0 && def->parent_class) || (strcmp(def->label, "Class") == 0 && strchr(def->name, '.'))) { return; } CBMArena *scratch = ctx->scratch ? ctx->scratch : ctx->arena; jv_doc_t d = {.ctx = ctx, .def = def}; d.t = jv_clean_doc(scratch, doc, false, &d.n); if (!d.t) { return; } const char *t = d.t; size_t n = d.n; jv_tag_t tags[JV_TAG_NEST_MAX]; int sp = 0; /* open inline tags */ uint32_t line = doc_line; size_t i = 0; bool bol = true; /* only blanks since the line began */ while (i < n) { char c = t[i]; if (c == '\n') { line++; bol = true; i++; continue; } if (bol && (c == ' ' || c == '\t')) { i++; continue; } bool in_text = sp > 0 && tags[sp - SKIP_ONE].syntax == JV_TAG_TEXT; if (bol && !in_text && c == '@' && i + SKIP_ONE < n && isalpha((unsigned char)t[i + SKIP_ONE])) { /* A block tag. javadoc ends an open {@link ..} (or any inline tag * whose content is markup) at it: that tag never closed and is no * link. Inside {@code}, {@literal} and {@snippet} an `@` at the * start of a line is text (annotations in sample code). */ size_t e = i + SKIP_ONE; while (e < n && isalpha((unsigned char)t[e])) { e++; } const char *name = t + i + SKIP_ONE; size_t len = e - i - SKIP_ONE; bool gap = e >= n || jv_blank(t[e]); if (gap && jv_word_is(name, len, "see")) { jv_java_block_tag(&d, e, line, true); } else if (gap && (jv_word_is(name, len, "throws") || jv_word_is(name, len, "exception"))) { jv_java_block_tag(&d, e, line, false); } sp = 0; bol = false; i = e; continue; } bol = false; bool text = in_text; if (c == '<' && sp == 0) { size_t past = jv_skip_hidden(&d, i, &line); if (past > i) { i = past; continue; } } if (c == '{' && !text && i + SKIP_ONE < n && t[i + SKIP_ONE] == '@' && sp < JV_TAG_NEST_MAX) { size_t ne = i + PAIR_LEN; while (ne < n && isalpha((unsigned char)t[ne])) { ne++; } tags[sp++] = (jv_tag_t){.syntax = jv_java_tag_kind(t + i + PAIR_LEN, ne - i - PAIR_LEN, ne < n ? t[ne] : '}'), .body = ne, .line = line}; i = ne; continue; } if (c == '{' && sp > 0) { tags[sp - SKIP_ONE].depth++; } else if (c == '}' && sp > 0) { if (tags[sp - SKIP_ONE].depth > 0) { tags[sp - SKIP_ONE].depth--; } else { jv_tag_t done = tags[--sp]; if (done.syntax > CBM_DOCLINK_NONE) { jv_java_inline(&d, done.body, i, done.line, done.syntax); } } } i++; } } /* ── KDoc ────────────────────────────────────────────────────────── */ /* Length of the KDoc link target at s[0, n): a qualified name whose segments * are identifiers or backticked names. 0 when s is not exactly one. */ static size_t jv_kdoc_target_len(const char *s, size_t n) { size_t i = 0; for (;;) { if (i < n && s[i] == '`') { size_t e = i + SKIP_ONE; while (e < n && s[e] != '`' && s[e] != '\n') { e++; } if (e >= n || s[e] != '`' || e == i + SKIP_ONE) { return 0; } i = e + SKIP_ONE; } else { if (i >= n || !jv_ident_start(s[i]) || s[i] == '$') { return 0; } while (i < n && jv_ident_char(s[i]) && s[i] != '$') { i++; } } if (i < n && s[i] == '.') { i++; continue; } return i == n ? i : 0; } } /* A `[...]` group opening at `i` on the line that ends at `eol`: the index of * its `]`, or eol when it holds a bracket or does not close on the line. */ static size_t jv_kdoc_bracket_close(const char *t, size_t i, size_t eol) { for (size_t k = i + SKIP_ONE; k < eol; k++) { if (t[k] == ']') { return k; } if (t[k] == '[') { return eol; } } return eol; } /* One line of KDoc text. */ typedef struct { size_t first; /* its first byte that is no blank */ size_t eol; /* its end */ uint32_t line; /* its source line */ } jv_kline_t; /* `[Name]` / `[text][Name]` at `i`. Returns the index to continue from. */ static size_t jv_kdoc_link(jv_doc_t *d, size_t i, const jv_kline_t *ln) { const char *t = d->t; size_t eol = ln->eol; size_t close = jv_kdoc_bracket_close(t, i, eol); if (close >= eol || close == i + SKIP_ONE) { return i + SKIP_ONE; } size_t b = i + SKIP_ONE; size_t e = close; size_t next = close + SKIP_ONE; if (next < eol && t[next] == '(') { return next; /* [text](url): a Markdown link, not a code reference */ } if (next < eol && t[next] == '[') { size_t close2 = jv_kdoc_bracket_close(t, next, eol); if (close2 >= eol) { return next; } if (close2 > next + SKIP_ONE) { b = next + SKIP_ONE; /* [text][Name] */ e = close2; } next = close2 + SKIP_ONE; } else if (next < eol && t[next] == ':' && i == ln->first) { return next; /* [label]: url -- a link definition */ } while (b < e && (t[b] == ' ' || t[b] == '\t')) { b++; } while (e > b && (t[e - SKIP_ONE] == ' ' || t[e - SKIP_ONE] == '\t')) { e--; } if (jv_kdoc_target_len(t + b, e - b) > 0) { jv_push(d, t + b, e - b, ln->line, CBM_DOCLINK_KT_KDOC_LINK); } return next; } /* The subject of `@see` / `@throws` / `@sample` starting at `i`: one word. * Returns the index to continue the line's text scan from. */ static size_t jv_kdoc_subject(jv_doc_t *d, size_t i, const jv_kline_t *ln, int syntax) { const char *t = d->t; size_t eol = ln->eol; while (i < eol && (t[i] == ' ' || t[i] == '\t')) { i++; } size_t e = i; while (e < eol && t[e] != ' ' && t[e] != '\t') { e++; } if (e == i) { return i; } size_t b = i; size_t w = e; if (t[b] == '[') { size_t close = jv_kdoc_bracket_close(t, b, eol); if (close >= eol) { return e; } b++; w = close; e = close + SKIP_ONE; } else { while (w > b && strchr(".,;:", t[w - SKIP_ONE])) { w--; } } bool url = t[b] == '<' || (w - b > 4 && memcmp(t + b, "http", 4) == 0 && memchr(t + b, ':', w - b)); if (w > b && !url) { jv_push(d, t + b, w - b, ln->line, syntax); } return e; } static int jv_kdoc_tag_syntax(const char *name, size_t len) { if (jv_word_is(name, len, "see")) { return CBM_DOCLINK_KT_SEE; } if (jv_word_is(name, len, "throws") || jv_word_is(name, len, "exception")) { return CBM_DOCLINK_KT_THROWS; } if (jv_word_is(name, len, "sample")) { return CBM_DOCLINK_KT_SAMPLE; } return CBM_DOCLINK_NONE; } /* Just past the code span that the run of `len` backticks ending before `run` * opens: the next run of exactly that many. 0 when the line has none (the * backticks are then text). */ static size_t jv_kdoc_span_end(const char *t, size_t run, size_t len, size_t eol) { size_t k = run; while (k < eol) { if (t[k] != '`') { k++; continue; } size_t r = k; while (r < eol && t[r] == '`') { r++; } if (r - k == len) { return r; } k = r; } return 0; } /* The text of one KDoc line from `i`: code spans and HTML comments are * skipped, bracket links pushed. *in_comment: an HTML comment is open (it may * have begun on an earlier line). */ static void jv_kdoc_text(jv_doc_t *d, size_t i, const jv_kline_t *ln, bool *in_comment) { const char *t = d->t; size_t eol = ln->eol; /* A run of n backticks was looked for once and the rest of the line has no * second one: a later run of n is text as well. Longer runs than a fence * are not looked for (they would be searched once per length). */ bool no_span[JV_QUOTES + SKIP_ONE] = {false}; while (i < eol) { char c = t[i]; if (*in_comment) { if (c == '-' && eol - i >= 3 && t[i + 1] == '-' && t[i + 2] == '>') { *in_comment = false; i += 3; } else { i++; } } else if (c == '\\') { i += PAIR_LEN; } else if (c == '`') { size_t run = i; while (run < eol && t[run] == '`') { run++; } size_t len = run - i; size_t past = (len > JV_QUOTES || no_span[len]) ? 0 : jv_kdoc_span_end(t, run, len, eol); if (past == 0 && len <= JV_QUOTES) { no_span[len] = true; } i = past ? past : run; } else if (c == '<' && eol - i >= 4 && memcmp(t + i, "