1313
1414/* Local constants for magic number elimination. */
1515enum {
16- USE_PREFIX_LEN = 4 , /* strlen("use ") */
1716 MIN_WOLFRAM_CHILDREN = 2 ,
1817 /* Nested `#if` branches followed while collecting C includes. */
1918 C_INCLUDE_NEST_LIMIT = 64 ,
@@ -1897,9 +1896,134 @@ static void parse_java_imports(CBMExtractCtx *ctx) {
18971896// --- Rust imports ---
18981897// use_declaration -> use_list or scoped_use_list
18991898
1900- static void parse_rust_imports (CBMExtractCtx * ctx ) {
1899+ static TSNode rust_import_field (TSNode node , const char * field ) {
1900+ return ts_node_child_by_field_name (node , field , (uint32_t )strlen (field ));
1901+ }
1902+
1903+ static char * rust_import_node_text_compact (CBMArena * a , TSNode node , const char * source ) {
1904+ if (ts_node_is_null (node )) {
1905+ return NULL ;
1906+ }
1907+ uint32_t start = ts_node_start_byte (node );
1908+ uint32_t end = ts_node_end_byte (node );
1909+ if (end <= start ) {
1910+ return cbm_arena_strdup (a , "" );
1911+ }
1912+ char * out = (char * )cbm_arena_alloc (a , (size_t )(end - start ) + SKIP_ONE );
1913+ if (!out ) {
1914+ return NULL ;
1915+ }
1916+ size_t w = 0 ;
1917+ for (uint32_t i = start ; i < end ; i ++ ) {
1918+ if (!isspace ((unsigned char )source [i ])) {
1919+ out [w ++ ] = source [i ];
1920+ }
1921+ }
1922+ out [w ] = '\0' ;
1923+ return out ;
1924+ }
1925+
1926+ static char * rust_import_join (CBMArena * a , const char * prefix , const char * suffix ) {
1927+ if (!prefix || !prefix [0 ]) {
1928+ return cbm_arena_strdup (a , suffix ? suffix : "" );
1929+ }
1930+ if (!suffix || !suffix [0 ]) {
1931+ return cbm_arena_strdup (a , prefix );
1932+ }
1933+ return cbm_arena_sprintf (a , "%s::%s" , prefix , suffix );
1934+ }
1935+
1936+ static bool rust_import_exists (const CBMImportArray * imports , const char * local ,
1937+ const char * module_path ) {
1938+ for (int i = 0 ; i < imports -> count ; i ++ ) {
1939+ const CBMImport * imp = & imports -> items [i ];
1940+ if (imp -> local_name && imp -> module_path && strcmp (imp -> local_name , local ) == 0 &&
1941+ strcmp (imp -> module_path , module_path ) == 0 ) {
1942+ return true;
1943+ }
1944+ }
1945+ return false;
1946+ }
1947+
1948+ static void rust_import_push (CBMExtractCtx * ctx , const char * full , const char * alias ) {
1949+ static const char self_suffix [] = "::self" ;
19011950 CBMArena * a = ctx -> arena ;
1951+ if (!full || !full [0 ]) {
1952+ return ;
1953+ }
1954+
1955+ const char * module_path = full ;
1956+ size_t len = strlen (full );
1957+ size_t self_len = sizeof (self_suffix ) - SKIP_ONE ;
1958+ if (len > self_len && strcmp (full + len - self_len , self_suffix ) == 0 ) {
1959+ module_path = cbm_arena_strndup (a , full , len - self_len );
1960+ }
19021961
1962+ const char * local = alias && alias [0 ] ? alias : path_last (a , module_path );
1963+ if (!local || !local [0 ]) {
1964+ return ;
1965+ }
1966+ bool keyword =
1967+ strcmp (local , "crate" ) == 0 || strcmp (local , "super" ) == 0 || strcmp (local , "self" ) == 0 ;
1968+ if (keyword && (!alias || !alias [0 ])) {
1969+ return ;
1970+ }
1971+ if (rust_import_exists (& ctx -> result -> imports , local , module_path )) {
1972+ return ;
1973+ }
1974+
1975+ CBMImport imp = {.local_name = local , .module_path = module_path };
1976+ cbm_imports_push (& ctx -> result -> imports , a , imp );
1977+ }
1978+
1979+ static void rust_import_walk (CBMExtractCtx * ctx , TSNode node , const char * prefix ) {
1980+ if (ts_node_is_null (node )) {
1981+ return ;
1982+ }
1983+
1984+ CBMArena * a = ctx -> arena ;
1985+ const char * kind = ts_node_type (node );
1986+ if (strcmp (kind , "use_list" ) == 0 ) {
1987+ uint32_t count = ts_node_named_child_count (node );
1988+ for (uint32_t i = 0 ; i < count ; i ++ ) {
1989+ rust_import_walk (ctx , ts_node_named_child (node , i ), prefix );
1990+ }
1991+ return ;
1992+ }
1993+ if (strcmp (kind , "scoped_use_list" ) == 0 ) {
1994+ char * path = rust_import_node_text_compact (a , rust_import_field (node , "path" ), ctx -> source );
1995+ char * next_prefix = rust_import_join (a , prefix , path );
1996+ rust_import_walk (ctx , rust_import_field (node , "list" ), next_prefix );
1997+ return ;
1998+ }
1999+ if (strcmp (kind , "use_as_clause" ) == 0 ) {
2000+ char * path = rust_import_node_text_compact (a , rust_import_field (node , "path" ), ctx -> source );
2001+ char * alias =
2002+ rust_import_node_text_compact (a , rust_import_field (node , "alias" ), ctx -> source );
2003+ char * full = rust_import_join (a , prefix , path );
2004+ rust_import_push (ctx , full , alias );
2005+ return ;
2006+ }
2007+ if (strcmp (kind , "use_wildcard" ) == 0 ) {
2008+ TSNode inner =
2009+ ts_node_named_child_count (node ) > 0 ? ts_node_named_child (node , 0 ) : (TSNode ){0 };
2010+ char * path = rust_import_node_text_compact (a , inner , ctx -> source );
2011+ char * base = rust_import_join (a , prefix , path );
2012+ char * full =
2013+ base && base [0 ] ? cbm_arena_sprintf (a , "%s::*" , base ) : cbm_arena_strdup (a , "*" );
2014+ rust_import_push (ctx , full , NULL );
2015+ return ;
2016+ }
2017+ if (strcmp (kind , "line_comment" ) == 0 || strcmp (kind , "block_comment" ) == 0 ) {
2018+ return ;
2019+ }
2020+
2021+ char * path = rust_import_node_text_compact (a , node , ctx -> source );
2022+ char * full = rust_import_join (a , prefix , path );
2023+ rust_import_push (ctx , full , NULL );
2024+ }
2025+
2026+ static void parse_rust_imports (CBMExtractCtx * ctx ) {
19032027 TSTreeCursor cursor = ts_tree_cursor_new (ctx -> root );
19042028 if (!ts_tree_cursor_goto_first_child (& cursor )) {
19052029 ts_tree_cursor_delete (& cursor );
@@ -1911,21 +2035,7 @@ static void parse_rust_imports(CBMExtractCtx *ctx) {
19112035 continue ;
19122036 }
19132037
1914- char * full = cbm_node_text (a , node , ctx -> source );
1915- if (!full ) {
1916- continue ;
1917- }
1918- // Strip "use " prefix and trailing ";"
1919- if (strncmp (full , "use " , USE_PREFIX_LEN ) == 0 ) {
1920- full += USE_PREFIX_LEN ;
1921- }
1922- size_t len = strlen (full );
1923- if (len > 0 && full [len - SKIP_ONE ] == ';' ) {
1924- full [len - SKIP_ONE ] = '\0' ;
1925- }
1926-
1927- CBMImport imp = {.local_name = path_last (a , full ), .module_path = full };
1928- cbm_imports_push (& ctx -> result -> imports , a , imp );
2038+ rust_import_walk (ctx , rust_import_field (node , "argument" ), "" );
19292039 } while (ts_tree_cursor_goto_next_sibling (& cursor ));
19302040 ts_tree_cursor_delete (& cursor );
19312041}
0 commit comments