Files
codebase-memory-mcp/internal/cbm/lang_specs.h
Martin VogelandJames 30941983cb 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 <jmcmacnz@gmail.com>
2026-05-08 23:04:33 +02:00

52 lines
2.3 KiB
C

#ifndef CBM_LANG_SPECS_H
#define CBM_LANG_SPECS_H
#include "cbm.h"
// CBMEmbeddedLangSpec describes a sub-language embedded inside a host AST.
// Used by host grammars whose tree-sitter parser does not recurse into the
// embedded content (e.g. Svelte, Vue, Astro: script_element -> raw_text holds
// JavaScript verbatim that the host grammar leaves unparsed). The generic
// embedded-imports walker locates each script_node_type in the host AST,
// finds its content_node_type child, and re-parses that slice with the
// embedded_language's grammar so existing extractors run on the inner AST.
typedef struct {
const char *script_node_type; // e.g. "script_element"
const char *content_node_type; // e.g. "raw_text"
CBMLanguage embedded_language; // grammar used to re-parse the content slice
} CBMEmbeddedLangSpec;
// CBMLangSpec mirrors Go's lang.LanguageSpec with NULL-terminated string arrays.
typedef struct {
CBMLanguage language;
const char **function_node_types;
const char **class_node_types;
const char **field_node_types;
const char **module_node_types;
const char **call_node_types;
const char **import_node_types;
const char **import_from_types;
const char **branching_node_types;
const char **variable_node_types;
const char **assignment_node_types;
const char **throw_node_types;
const char *throws_clause_field; // NULL if none
const char **decorator_node_types;
const char **env_access_functions; // NULL-terminated (NULL if none)
const char **env_access_member_patterns; // NULL-terminated (NULL if none)
const TSLanguage *(*ts_factory)(void); // Tree-sitter grammar factory (NULL if shared)
// NULL-terminated list of embedded sub-languages (NULL if host grammar has
// no embedded content to re-parse). The terminator is an entry whose
// script_node_type is NULL.
const CBMEmbeddedLangSpec *embedded_imports;
} CBMLangSpec;
// Get the language spec for a given language. Returns NULL for unsupported.
const CBMLangSpec *cbm_lang_spec(CBMLanguage lang);
// Get the TSLanguage* for a given language. Returns NULL for unsupported.
// These resolve at link time to grammar symbols from Go tree-sitter modules.
const TSLanguage *cbm_ts_language(CBMLanguage lang);
#endif // CBM_LANG_SPECS_H