feat: Add C/C++ typedef enum and struct extraction with inner type resolution
Addresses C/C++ typedef syntax where anonymous enum/struct definitions are wrapped in typedef declarations (e.g. `typedef enum { A, B } MyEnum;`). Adds resolveTypeAliasKind to identify inner enum_specifier and struct_specifier nodes within typedefs, enabling proper extraction of enum members and struct fields from the inner anonymous definitions rather than treating them as simple type aliases.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Node as SyntaxNode } from 'web-tree-sitter';
|
||||
import { getNodeText } from '../tree-sitter-helpers';
|
||||
import { getChildByField, getNodeText } from '../tree-sitter-helpers';
|
||||
import type { LanguageExtractor } from '../tree-sitter-types';
|
||||
|
||||
export const cExtractor: LanguageExtractor = {
|
||||
@@ -17,6 +17,18 @@ export const cExtractor: LanguageExtractor = {
|
||||
nameField: 'declarator',
|
||||
bodyField: 'body',
|
||||
paramsField: 'parameters',
|
||||
resolveTypeAliasKind: (node, _source) => {
|
||||
// C typedef: `typedef enum { ... } name;` or `typedef struct { ... } name;`
|
||||
// The inner enum_specifier/struct_specifier is anonymous, but we want the typedef name
|
||||
// to become the enum/struct node name.
|
||||
for (let i = 0; i < node.namedChildCount; i++) {
|
||||
const child = node.namedChild(i);
|
||||
if (!child) continue;
|
||||
if (child.type === 'enum_specifier' && getChildByField(child, 'body')) return 'enum';
|
||||
if (child.type === 'struct_specifier' && getChildByField(child, 'body')) return 'struct';
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
extractImport: (node, source) => {
|
||||
const importText = source.substring(node.startIndex, node.endIndex).trim();
|
||||
// C includes: #include <stdio.h>, #include "myheader.h"
|
||||
@@ -66,6 +78,16 @@ export const cppExtractor: LanguageExtractor = {
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
resolveTypeAliasKind: (node, _source) => {
|
||||
// C++ typedef: `typedef enum { ... } name;` or `typedef struct { ... } name;`
|
||||
for (let i = 0; i < node.namedChildCount; i++) {
|
||||
const child = node.namedChild(i);
|
||||
if (!child) continue;
|
||||
if (child.type === 'enum_specifier' && getChildByField(child, 'body')) return 'enum';
|
||||
if (child.type === 'struct_specifier' && getChildByField(child, 'body')) return 'struct';
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
extractImport: (node, source) => {
|
||||
const importText = source.substring(node.startIndex, node.endIndex).trim();
|
||||
// C++ includes: #include <iostream>, #include "myheader.h"
|
||||
|
||||
Reference in New Issue
Block a user