Skip to content

Commit 3094198

Browse files
Martin Vogeljmcmacnz
andcommitted
fix(imports): extract ES imports from embedded <script> blocks (#224)
Generalizes #224's Svelte/Vue script-block import extraction into a spec-driven embedded-language pattern that also covers HTML and Astro, so any future host grammar that keeps sub-language source as raw text gets coverage with a one-line declaration. The existing CBMLangSpec already exposes the per-language tree-sitter grammar via cbm_ts_language(); add a parallel embedded_imports field that lists which AST node types hold which sub-language. The new generic walker in extract_imports.c reads that list, locates each embedded content node in the host AST, re-parses the slice with the embedded grammar resolved through cbm_ts_language(), and runs the standard ES import extractor (walk_es_imports) over the inner AST. No grammar symbols are referenced from the extraction file. - internal/cbm/lang_specs.h: add CBMEmbeddedLangSpec + embedded_imports field on CBMLangSpec. - internal/cbm/lang_specs.c: declare vue/svelte/html/astro embed lists ({"script_element", "raw_text", CBM_LANG_JAVASCRIPT}); set the new field on those four spec entries; explicit NULL initializer on every other entry to satisfy -Wmissing-field-initializers. - internal/cbm/extract_imports.c: add embedded_collect_content_nodes + parse_embedded_imports; route SVELTE/VUE/HTML/ASTRO through the generic helper. - tests/test_extraction.c: cover Svelte (with and without <script>), Vue, and HTML. Full suite: 2824 passed, 0 failed. Closes #223. Closes #224. Co-authored-by: James <[email protected]>
1 parent aa6df6b commit 3094198

4 files changed

Lines changed: 385 additions & 155 deletions

File tree

‎internal/cbm/extract_imports.c‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cbm.h"
22
#include "arena.h" // CBMArena, cbm_arena_strdup/strndup/sprintf
33
#include "helpers.h"
4+
#include "lang_specs.h" // CBMLangSpec, CBMEmbeddedLangSpec, cbm_lang_spec, cbm_ts_language
45
#include "tree_sitter/api.h" // TSNode, ts_node_*
56
#include "foundation/constants.h"
67
#include "ts_node_stack.h"
@@ -846,6 +847,99 @@ static void parse_wolfram_imports(CBMExtractCtx *ctx) {
846847
walk_wolfram_imports(ctx, ctx->root);
847848
}
848849

850+
// --- Embedded-language imports ---
851+
// Generic walker for host grammars (Svelte, Vue, HTML, Astro, ...) whose AST
852+
// keeps embedded sub-language source as raw_text (or similar) without parsing
853+
// it. The host's CBMLangSpec.embedded_imports declares which content nodes
854+
// hold which sub-language; we re-parse each match with the embedded grammar
855+
// and run the standard ES import walker over the inner AST.
856+
//
857+
// No grammar symbols are referenced here — the embedded TSLanguage is
858+
// resolved through cbm_ts_language(spec->embedded_language), the same hook
859+
// that the main parser uses. Adding another host language is a one-line
860+
// declaration in lang_specs.c.
861+
862+
static void embedded_collect_content_nodes(TSNode root, const CBMEmbeddedLangSpec *spec,
863+
TSNode *out, int *out_count, int max_out) {
864+
/* Iterative DFS so deeply-nested script blocks are still found. Cap the
865+
* stack to a sane bound (host grammars do not have million-deep markup
866+
* trees) — no need to introduce TSNodeStack here. */
867+
enum { EMBED_STACK_CAP = 1024 };
868+
TSNode stack[EMBED_STACK_CAP];
869+
int top = 0;
870+
stack[top++] = root;
871+
while (top > 0 && *out_count < max_out) {
872+
TSNode node = stack[--top];
873+
const char *kind = ts_node_type(node);
874+
if (strcmp(kind, spec->script_node_type) == 0) {
875+
uint32_t cc = ts_node_child_count(node);
876+
for (uint32_t k = 0; k < cc; k++) {
877+
TSNode c = ts_node_child(node, k);
878+
if (strcmp(ts_node_type(c), spec->content_node_type) == 0) {
879+
out[(*out_count)++] = c;
880+
if (*out_count >= max_out) {
881+
return;
882+
}
883+
break; /* one content node per script element */
884+
}
885+
}
886+
/* Do not descend into <script>'s children — content already taken. */
887+
continue;
888+
}
889+
uint32_t count = ts_node_child_count(node);
890+
for (int i = (int)count - 1; i >= 0 && top < EMBED_STACK_CAP; i--) {
891+
stack[top++] = ts_node_child(node, (uint32_t)i);
892+
}
893+
}
894+
}
895+
896+
static void parse_embedded_imports(CBMExtractCtx *ctx) {
897+
const CBMLangSpec *spec = cbm_lang_spec(ctx->language);
898+
if (!spec || !spec->embedded_imports) {
899+
return;
900+
}
901+
for (const CBMEmbeddedLangSpec *e = spec->embedded_imports; e->script_node_type != NULL; e++) {
902+
const TSLanguage *embedded_lang = cbm_ts_language(e->embedded_language);
903+
if (!embedded_lang) {
904+
continue; /* embedded grammar not linked in — silently skip */
905+
}
906+
enum { MAX_EMBEDDED_BLOCKS = 16 };
907+
TSNode hits[MAX_EMBEDDED_BLOCKS];
908+
int hit_count = 0;
909+
embedded_collect_content_nodes(ctx->root, e, hits, &hit_count, MAX_EMBEDDED_BLOCKS);
910+
if (hit_count == 0) {
911+
continue;
912+
}
913+
TSParser *parser = ts_parser_new();
914+
if (!parser) {
915+
continue;
916+
}
917+
if (!ts_parser_set_language(parser, embedded_lang)) {
918+
ts_parser_delete(parser);
919+
continue;
920+
}
921+
for (int i = 0; i < hit_count; i++) {
922+
uint32_t s = ts_node_start_byte(hits[i]);
923+
uint32_t end = ts_node_end_byte(hits[i]);
924+
if (end <= s) {
925+
continue;
926+
}
927+
const char *sub_src = ctx->source + s;
928+
uint32_t sub_len = end - s;
929+
TSTree *sub_tree = ts_parser_parse_string(parser, NULL, sub_src, sub_len);
930+
if (!sub_tree) {
931+
continue;
932+
}
933+
CBMExtractCtx sub_ctx = *ctx;
934+
sub_ctx.source = sub_src;
935+
sub_ctx.root = ts_tree_root_node(sub_tree);
936+
walk_es_imports(&sub_ctx, sub_ctx.root);
937+
ts_tree_delete(sub_tree);
938+
}
939+
ts_parser_delete(parser);
940+
}
941+
}
942+
849943
// --- Main dispatch ---
850944

851945
void cbm_extract_imports(CBMExtractCtx *ctx) {
@@ -937,6 +1031,14 @@ void cbm_extract_imports(CBMExtractCtx *ctx) {
9371031
case CBM_LANG_WOLFRAM:
9381032
parse_wolfram_imports(ctx);
9391033
break;
1034+
/* Host languages whose tree-sitter grammar leaves <script> bodies as raw
1035+
* text — re-parse the embedded slice via the embedded-language spec. */
1036+
case CBM_LANG_SVELTE:
1037+
case CBM_LANG_VUE:
1038+
case CBM_LANG_HTML:
1039+
case CBM_LANG_ASTRO:
1040+
parse_embedded_imports(ctx);
1041+
break;
9401042
default:
9411043
break;
9421044
}

0 commit comments

Comments
 (0)