Step 307: Go + C++ + Elisp parser deepening — struct/interface, class, template, lambda (12/12 tests)

Go: struct → ClassDeclaration, interface → InterfaceDeclaration, method receiver → MethodDeclaration, func literal → LambdaExpression, expression_list unwrapping
C++: class/struct → ClassDeclaration, template → TypeParameter, methods → MethodDeclaration, lambda_expression → LambdaExpression
Elisp: (lambda ...) special_form → LambdaExpression

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill
2026-02-15 05:22:56 +00:00
parent 30e718a996
commit dc462e8941
5 changed files with 868 additions and 8 deletions

View File

@@ -55,6 +55,199 @@ private:
if (type == "function_definition") {
auto* fn = convertCppFunction(child, source);
if (fn) module->addChild("functions", fn);
} else if (type == "class_specifier" || type == "struct_specifier") {
convertCppClassOrStruct(child, source, module, type == "struct_specifier");
} else if (type == "template_declaration") {
convertCppTemplateDecl(child, source, module);
}
}
}
static void convertCppClassOrStruct(TSNode node, const std::string& source,
Module* module, bool isStruct) {
TSNode nameNode = childByFieldName(node, "name");
std::string className;
if (!ts_node_is_null(nameNode)) {
className = nodeText(nameNode, source);
}
if (className.empty()) return;
auto* cls = new ClassDeclaration(IdGenerator::next("cls"), className);
applySpan(cls, node);
// Check for base class
TSNode baseClause = childByFieldName(node, "base_clause");
if (ts_node_is_null(baseClause)) {
// tree-sitter-cpp might use different field names — search children
uint32_t nc = ts_node_named_child_count(node);
for (uint32_t i = 0; i < nc; ++i) {
TSNode ch = ts_node_named_child(node, i);
if (nodeType(ch) == "base_class_clause") {
baseClause = ch;
break;
}
}
}
if (!ts_node_is_null(baseClause)) {
uint32_t bc = ts_node_named_child_count(baseClause);
for (uint32_t i = 0; i < bc; ++i) {
TSNode base = ts_node_named_child(baseClause, i);
std::string baseText = nodeText(base, source);
// Strip access specifier prefix
if (baseText.find("public ") == 0) baseText = baseText.substr(7);
else if (baseText.find("private ") == 0) baseText = baseText.substr(8);
else if (baseText.find("protected ") == 0) baseText = baseText.substr(10);
if (!baseText.empty() && cls->superClass.empty()) {
cls->superClass = baseText;
}
}
}
// Process class body
TSNode bodyNode = childByFieldName(node, "body");
if (!ts_node_is_null(bodyNode)) {
std::string currentVisibility = isStruct ? "public" : "private";
uint32_t bc = ts_node_named_child_count(bodyNode);
for (uint32_t i = 0; i < bc; ++i) {
TSNode member = ts_node_named_child(bodyNode, i);
std::string memberType = nodeType(member);
if (memberType == "access_specifier") {
std::string specText = nodeText(member, source);
if (specText.find("public") != std::string::npos) currentVisibility = "public";
else if (specText.find("private") != std::string::npos) currentVisibility = "private";
else if (specText.find("protected") != std::string::npos) currentVisibility = "protected";
} else if (memberType == "function_definition") {
auto* meth = convertCppMethodDecl(member, source, className, currentVisibility);
if (meth) cls->addChild("methods", meth);
} else if (memberType == "declaration") {
// Could be a field declaration
TSNode declNode = childByFieldName(member, "declarator");
if (!ts_node_is_null(declNode)) {
std::string fieldName = nodeText(declNode, source);
auto* var = new Variable(IdGenerator::next("var"), fieldName);
applySpan(var, member);
cls->addChild("fields", var);
} else {
// Try to find field names from named children
uint32_t dc = ts_node_named_child_count(member);
for (uint32_t d = 0; d < dc; ++d) {
TSNode dchild = ts_node_named_child(member, d);
std::string dtype = nodeType(dchild);
if (dtype == "field_identifier" || dtype == "identifier") {
// Skip type specifiers — just get the last identifier-like thing
}
}
}
} else if (memberType == "field_declaration") {
TSNode declNode = childByFieldName(member, "declarator");
if (!ts_node_is_null(declNode)) {
std::string fieldName = nodeText(declNode, source);
auto* var = new Variable(IdGenerator::next("var"), fieldName);
applySpan(var, member);
cls->addChild("fields", var);
}
}
}
}
module->addChild("classes", cls);
}
static MethodDeclaration* convertCppMethodDecl(TSNode node, const std::string& source,
const std::string& className,
const std::string& visibility) {
auto* meth = new MethodDeclaration();
meth->id = IdGenerator::next("meth");
meth->className = className;
meth->visibility = visibility;
applySpan(meth, node);
// Check for virtual keyword
TSNode typeNode = childByFieldName(node, "type");
if (!ts_node_is_null(typeNode)) {
std::string typeText = nodeText(typeNode, source);
auto* retType = new PrimitiveType(IdGenerator::next("type"), typeText);
meth->setChild("returnType", retType);
}
// Check full text for virtual/static keywords
std::string fullText = nodeText(node, source);
if (fullText.find("virtual ") != std::string::npos) meth->isVirtual = true;
if (fullText.find("static ") != std::string::npos) meth->isStatic = true;
if (fullText.find("override") != std::string::npos) meth->isOverride = true;
// Declarator
TSNode declaratorNode = childByFieldName(node, "declarator");
if (!ts_node_is_null(declaratorNode)) {
extractCppFunctionName(declaratorNode, source, meth);
extractCppParameters(declaratorNode, source, meth);
}
// Body
TSNode bodyNode = childByFieldName(node, "body");
if (!ts_node_is_null(bodyNode)) {
convertCppBody(bodyNode, source, meth);
}
return meth;
}
static void convertCppTemplateDecl(TSNode node, const std::string& source, Module* module) {
// template_declaration has template_parameter_list and a child declaration
TSNode paramsNode = childByFieldName(node, "parameters");
std::vector<std::pair<std::string, std::string>> typeParams; // name, constraint
if (!ts_node_is_null(paramsNode)) {
uint32_t pc = ts_node_named_child_count(paramsNode);
for (uint32_t p = 0; p < pc; ++p) {
TSNode param = ts_node_named_child(paramsNode, p);
std::string paramType = nodeType(param);
if (paramType == "type_parameter_declaration" ||
paramType == "template_type_parameter") {
TSNode pname = childByFieldName(param, "name");
if (!ts_node_is_null(pname)) {
typeParams.push_back({nodeText(pname, source), ""});
} else {
// Fallback: get all text
std::string txt = nodeText(param, source);
// Extract name from "typename T" or "class T"
size_t sp = txt.rfind(' ');
if (sp != std::string::npos) {
typeParams.push_back({txt.substr(sp + 1), ""});
} else {
typeParams.push_back({txt, ""});
}
}
}
}
}
// Find the inner declaration (class_specifier, struct_specifier, function_definition)
uint32_t nc = ts_node_named_child_count(node);
for (uint32_t i = 0; i < nc; ++i) {
TSNode child = ts_node_named_child(node, i);
std::string childType = nodeType(child);
if (childType == "class_specifier" || childType == "struct_specifier") {
convertCppClassOrStruct(child, source, module, childType == "struct_specifier");
// Attach TypeParameters to the last class
auto& classes = module->getChildren("classes");
if (!classes.empty()) {
auto* cls = dynamic_cast<ClassDeclaration*>(classes.back());
if (cls) {
for (auto& [tpName, tpConstraint] : typeParams) {
auto* tp = new TypeParameter(IdGenerator::next("tp"), tpName);
tp->constraint = tpConstraint;
cls->addChild("typeParameters", tp);
}
}
}
return;
} else if (childType == "function_definition") {
auto* fn = convertCppFunction(child, source);
if (fn) module->addChild("functions", fn);
return;
} else if (childType == "declaration") {
// Template function declaration without body — skip for now
return;
}
}
}
@@ -211,6 +404,9 @@ private:
auto* exprStmt = new ExpressionStatement();
exprStmt->id = IdGenerator::next("exprstmt");
applySpan(exprStmt, node);
// Scan for lambda expressions inside declarations
ASTNode* lambdaNode = findCppLambdaInSubtree(node, source);
if (lambdaNode) exprStmt->setChild("expression", lambdaNode);
return exprStmt;
} else if (type == "if_statement") {
auto* ifStmt = new IfStatement();
@@ -221,6 +417,68 @@ private:
return nullptr;
}
// Recursively scan for lambda_expression nodes in a subtree
static ASTNode* findCppLambdaInSubtree(TSNode node, const std::string& source) {
std::string type = nodeType(node);
if (type == "lambda_expression") {
return convertCppLambdaExpression(node, source);
}
uint32_t count = ts_node_named_child_count(node);
for (uint32_t i = 0; i < count; ++i) {
ASTNode* found = findCppLambdaInSubtree(ts_node_named_child(node, i), source);
if (found) return found;
}
return nullptr;
}
static LambdaExpression* convertCppLambdaExpression(TSNode node, const std::string& source) {
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
applySpan(lambda, node);
// Capture list
TSNode captureNode = childByFieldName(node, "captures");
if (!ts_node_is_null(captureNode)) {
uint32_t cc = ts_node_named_child_count(captureNode);
for (uint32_t c = 0; c < cc; ++c) {
TSNode cap = ts_node_named_child(captureNode, c);
std::string capText = nodeText(cap, source);
if (!capText.empty()) lambda->captureList.push_back(capText);
}
}
// Parameters
TSNode declNode = childByFieldName(node, "declarator");
if (!ts_node_is_null(declNode)) {
TSNode paramsNode = childByFieldName(declNode, "parameters");
if (!ts_node_is_null(paramsNode)) {
uint32_t pc = ts_node_named_child_count(paramsNode);
for (uint32_t p = 0; p < pc; ++p) {
TSNode paramChild = ts_node_named_child(paramsNode, p);
if (nodeType(paramChild) == "parameter_declaration") {
TSNode pnameNode = childByFieldName(paramChild, "declarator");
if (!ts_node_is_null(pnameNode)) {
auto* param = new Parameter(IdGenerator::next("param"),
nodeText(pnameNode, source));
applySpan(param, paramChild);
lambda->addChild("parameters", param);
}
}
}
}
}
// Body
TSNode bodyNode = childByFieldName(node, "body");
if (!ts_node_is_null(bodyNode)) {
uint32_t bc = ts_node_named_child_count(bodyNode);
for (uint32_t b = 0; b < bc; ++b) {
ASTNode* stmt = convertCppStatement(ts_node_named_child(bodyNode, b), source);
if (stmt) lambda->addChild("body", stmt);
}
}
return lambda;
}
static ASTNode* convertCppExpression(TSNode node, const std::string& source) {
std::string type = nodeType(node);
if (type == "binary_expression") {
@@ -260,6 +518,8 @@ private:
} else if (type == "parenthesized_expression") {
uint32_t count = ts_node_named_child_count(node);
if (count > 0) return convertCppExpression(ts_node_named_child(node, 0), source);
} else if (type == "lambda_expression") {
return convertCppLambdaExpression(node, source);
}
// Fallback
std::string text = nodeText(node, source);

View File

@@ -314,8 +314,20 @@ private:
if (right) binOp->setChild("right", right);
return binOp;
}
// Check for (lambda (args) body)
if (firstText == "lambda") {
return convertElispLambda(node, source);
}
}
// Generic list — could be a function call
// Check for (lambda (args) body) with 2 children
if (count >= 2) {
TSNode firstChild = ts_node_named_child(node, 0);
std::string firstText = nodeText(firstChild, source);
if (firstText == "lambda") {
return convertElispLambda(node, source);
}
}
// Generic list — could be a function call
if (count >= 1) {
auto* call = new FunctionCall();
call->id = IdGenerator::next("call");
@@ -362,8 +374,27 @@ private:
uint32_t count = ts_node_named_child_count(node);
if (count < 1) return nullptr;
TSNode firstChild = ts_node_named_child(node, 0);
std::string formName = nodeText(firstChild, source);
// Check for lambda keyword — may be unnamed child
// Scan all children (including unnamed) for the keyword
std::string formName;
uint32_t totalChildren = ts_node_child_count(node);
for (uint32_t c = 0; c < totalChildren; ++c) {
TSNode ch = ts_node_child(node, c);
std::string chType = ts_node_type(ch);
if (chType == "(" || chType == ")") continue;
// First non-paren child is the keyword
formName = chType;
break;
}
// Fallback: use first named child text
if (formName.empty()) {
TSNode firstChild = ts_node_named_child(node, 0);
formName = nodeText(firstChild, source);
}
if (formName == "lambda") {
return convertElispLambdaFromSpecialForm(node, source);
}
if (formName == "if" && count >= 3) {
auto* ifStmt = new IfStatement();
@@ -386,4 +417,79 @@ private:
return call;
}
// Convert (lambda (args) body...) to LambdaExpression
static LambdaExpression* convertElispLambda(TSNode node, const std::string& source) {
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
applySpan(lambda, node);
uint32_t count = ts_node_named_child_count(node);
// child 0 = "lambda" symbol, child 1 = arglist, rest = body
if (count >= 2) {
TSNode arglist = ts_node_named_child(node, 1);
if (nodeType(arglist) == "list") {
uint32_t ac = ts_node_named_child_count(arglist);
for (uint32_t a = 0; a < ac; ++a) {
TSNode arg = ts_node_named_child(arglist, a);
std::string argType = nodeType(arg);
if (argType == "symbol" || argType == "identifier") {
auto* param = new Parameter(IdGenerator::next("param"),
nodeText(arg, source));
applySpan(param, arg);
lambda->addChild("parameters", param);
}
}
}
}
// Body forms
for (uint32_t i = 2; i < count; ++i) {
ASTNode* expr = convertElispExpression(ts_node_named_child(node, i), source);
if (expr) {
auto* exprStmt = new ExpressionStatement();
exprStmt->id = IdGenerator::next("exprstmt");
applySpan(exprStmt, ts_node_named_child(node, i));
exprStmt->setChild("expression", expr);
lambda->addChild("body", exprStmt);
}
}
return lambda;
}
// Convert special_form lambda where "lambda" is an unnamed keyword child
// Named children: child 0 = arglist, child 1+ = body forms
static LambdaExpression* convertElispLambdaFromSpecialForm(TSNode node, const std::string& source) {
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
applySpan(lambda, node);
uint32_t count = ts_node_named_child_count(node);
// Named child 0 = arglist (list), rest = body
if (count >= 1) {
TSNode arglist = ts_node_named_child(node, 0);
if (nodeType(arglist) == "list") {
uint32_t ac = ts_node_named_child_count(arglist);
for (uint32_t a = 0; a < ac; ++a) {
TSNode arg = ts_node_named_child(arglist, a);
std::string argType = nodeType(arg);
if (argType == "symbol" || argType == "identifier") {
auto* param = new Parameter(IdGenerator::next("param"),
nodeText(arg, source));
applySpan(param, arg);
lambda->addChild("parameters", param);
}
}
}
}
// Body forms (named children after arglist)
for (uint32_t i = 1; i < count; ++i) {
ASTNode* expr = convertElispExpression(ts_node_named_child(node, i), source);
if (expr) {
auto* exprStmt = new ExpressionStatement();
exprStmt->id = IdGenerator::next("exprstmt");
applySpan(exprStmt, ts_node_named_child(node, i));
exprStmt->setChild("expression", expr);
lambda->addChild("body", exprStmt);
}
}
return lambda;
}
// ---------------------------------------------------------------

View File

@@ -49,7 +49,16 @@ private:
static void convertGoSourceFile(TSNode root,
const std::string& source,
Module* module) {
// First pass: collect type declarations (structs/interfaces)
uint32_t count = ts_node_named_child_count(root);
for (uint32_t i = 0; i < count; ++i) {
TSNode child = ts_node_named_child(root, i);
std::string type = nodeType(child);
if (type == "type_declaration") {
convertGoTypeDeclaration(child, source, module);
}
}
// Second pass: functions, methods, vars, imports
for (uint32_t i = 0; i < count; ++i) {
TSNode child = ts_node_named_child(root, i);
std::string type = nodeType(child);
@@ -59,12 +68,9 @@ private:
auto* fn = convertGoFunction(child, source, "");
if (fn) module->addChild("functions", fn);
} else if (type == "method_declaration") {
auto* fn = convertGoMethod(child, source);
if (fn) module->addChild("functions", fn);
convertGoMethodDecl(child, source, module);
} else if (type == "var_declaration") {
convertGoVarDeclaration(child, source, module);
} else if (type == "type_declaration") {
convertGoTypeDeclaration(child, source, module);
}
}
}
@@ -118,7 +124,57 @@ private:
if (nodeType(spec) != "type_spec") continue;
TSNode nameNode = childByFieldName(spec, "name");
if (ts_node_is_null(nameNode)) continue;
auto* var = new Variable(IdGenerator::next("var"), nodeText(nameNode, source));
std::string typeName = nodeText(nameNode, source);
TSNode typeNode = childByFieldName(spec, "type");
if (!ts_node_is_null(typeNode)) {
std::string typeKind = nodeType(typeNode);
if (typeKind == "struct_type") {
auto* cls = new ClassDeclaration(IdGenerator::next("cls"), typeName);
applySpan(cls, spec);
// Extract fields from struct body
uint32_t fc = ts_node_named_child_count(typeNode);
for (uint32_t f = 0; f < fc; ++f) {
TSNode field = ts_node_named_child(typeNode, f);
if (nodeType(field) == "field_declaration" ||
nodeType(field) == "field_declaration_list") {
TSNode fnameNode = childByFieldName(field, "name");
if (!ts_node_is_null(fnameNode)) {
auto* var = new Variable(IdGenerator::next("var"),
nodeText(fnameNode, source));
applySpan(var, field);
cls->addChild("fields", var);
}
}
}
module->addChild("classes", cls);
continue;
} else if (typeKind == "interface_type") {
auto* iface = new InterfaceDeclaration(IdGenerator::next("iface"), typeName);
applySpan(iface, spec);
// Extract method signatures
uint32_t mc = ts_node_named_child_count(typeNode);
for (uint32_t m = 0; m < mc; ++m) {
TSNode methSpec = ts_node_named_child(typeNode, m);
if (nodeType(methSpec) == "method_spec" ||
nodeType(methSpec) == "method_elem") {
TSNode methName = childByFieldName(methSpec, "name");
if (!ts_node_is_null(methName)) {
auto* meth = new MethodDeclaration(
IdGenerator::next("meth"), nodeText(methName, source));
meth->className = typeName;
meth->isVirtual = true;
applySpan(meth, methSpec);
iface->addChild("methods", meth);
}
}
}
module->addChild("classes", iface);
continue;
}
}
// Fallback: non-struct/interface type alias → Variable
auto* var = new Variable(IdGenerator::next("var"), typeName);
module->addChild("variables", var);
}
}
@@ -172,6 +228,82 @@ private:
return convertGoFunction(node, source, receiverType);
}
// Create a MethodDeclaration and attach to existing ClassDeclaration
static void convertGoMethodDecl(TSNode node,
const std::string& source,
Module* module) {
TSNode recvNode = childByFieldName(node, "receiver");
std::string receiverType;
if (!ts_node_is_null(recvNode)) {
TSNode typeNode = findDescendantByField(recvNode, "type");
if (!ts_node_is_null(typeNode)) {
receiverType = nodeText(typeNode, source);
}
if (receiverType.empty()) {
TSNode nameNode = findDescendantByField(recvNode, "name");
if (!ts_node_is_null(nameNode)) {
receiverType = nodeText(nameNode, source);
}
}
receiverType = trimPointerPrefix(receiverType);
}
// Extract method name
TSNode nameNode = childByFieldName(node, "name");
if (ts_node_is_null(nameNode)) return;
std::string methodName = nodeText(nameNode, source);
// Create MethodDeclaration
auto* meth = new MethodDeclaration(IdGenerator::next("meth"), methodName);
meth->className = receiverType;
applySpan(meth, node);
// Parameters
TSNode paramsNode = childByFieldName(node, "parameters");
if (!ts_node_is_null(paramsNode)) {
convertGoParameters(paramsNode, source, meth);
}
// Return type
TSNode resultNode = childByFieldName(node, "result");
if (!ts_node_is_null(resultNode)) {
if (auto* t = convertGoType(resultNode, source)) meth->setChild("returnType", t);
}
// Body
TSNode bodyNode = childByFieldName(node, "body");
if (!ts_node_is_null(bodyNode)) {
convertGoBlock(bodyNode, source, meth);
}
// Try to attach to existing ClassDeclaration
bool attached = false;
if (!receiverType.empty()) {
auto& classes = module->getChildren("classes");
for (auto* entry : classes) {
auto* cls = dynamic_cast<ClassDeclaration*>(entry);
if (cls && cls->name == receiverType) {
cls->addChild("methods", meth);
attached = true;
break;
}
}
}
// Also add as backward-compat Function in functions list
auto* fn = convertGoFunction(node, source, receiverType);
if (fn) module->addChild("functions", fn);
// If not attached to any class, the MethodDeclaration is still owned by the class search
if (!attached) {
// Create a ClassDeclaration stub for the receiver type
if (!receiverType.empty()) {
auto* cls = new ClassDeclaration(IdGenerator::next("cls"), receiverType);
cls->addChild("methods", meth);
module->addChild("classes", cls);
} else {
delete meth;
}
}
}
static void convertGoParameters(TSNode paramsNode,
const std::string& source,
Function* fn) {
@@ -421,6 +553,44 @@ private:
if (ts_node_named_child_count(node) > 0) {
return convertGoExpression(ts_node_named_child(node, 0), source);
}
} else if (type == "expression_list") {
// Unwrap single-element expression lists
uint32_t elc = ts_node_named_child_count(node);
if (elc == 1) {
return convertGoExpression(ts_node_named_child(node, 0), source);
}
if (elc > 0) {
return convertGoExpression(ts_node_named_child(node, 0), source);
}
} else if (type == "func_literal") {
auto* lambda = new LambdaExpression(IdGenerator::next("lambda"));
applySpan(lambda, node);
TSNode paramsNode = childByFieldName(node, "parameters");
if (!ts_node_is_null(paramsNode)) {
uint32_t pc = ts_node_named_child_count(paramsNode);
for (uint32_t p = 0; p < pc; ++p) {
TSNode paramChild = ts_node_named_child(paramsNode, p);
if (nodeType(paramChild) == "parameter_declaration" ||
nodeType(paramChild) == "variadic_parameter_declaration") {
TSNode pnameNode = childByFieldName(paramChild, "name");
if (!ts_node_is_null(pnameNode)) {
auto* param = new Parameter(IdGenerator::next("param"),
nodeText(pnameNode, source));
applySpan(param, paramChild);
lambda->addChild("parameters", param);
}
}
}
}
TSNode bodyNode = childByFieldName(node, "body");
if (!ts_node_is_null(bodyNode)) {
uint32_t bc = ts_node_named_child_count(bodyNode);
for (uint32_t b = 0; b < bc; ++b) {
ASTNode* stmt = convertGoStatement(ts_node_named_child(bodyNode, b), source);
if (stmt) lambda->addChild("body", stmt);
}
}
return lambda;
}
std::string text = nodeText(node, source);