#pragma once // TreeSitterParser Rust support. public: // Rust // --------------------------------------------------------------- static std::unique_ptr parseRust(const std::string& source) { TSParser* parser = ts_parser_new(); ts_parser_set_language(parser, tree_sitter_rust()); TSTree* tree = ts_parser_parse_string(parser, nullptr, source.c_str(), (uint32_t)source.size()); TSNode root = ts_tree_root_node(tree); auto module = std::make_unique(); module->id = IdGenerator::next("mod"); module->name = "parsed_rust_module"; module->targetLanguage = "rust"; applySpan(module.get(), root); convertRustCrate(root, source, module.get()); ts_tree_delete(tree); ts_parser_delete(parser); return module; } static ParseResult parseRustWithDiagnostics(const std::string& source) { ParseResult result; TSParser* parser = ts_parser_new(); ts_parser_set_language(parser, tree_sitter_rust()); TSTree* tree = ts_parser_parse_string(parser, nullptr, source.c_str(), (uint32_t)source.size()); TSNode root = ts_tree_root_node(tree); result.module = std::make_unique(); result.module->id = IdGenerator::next("mod"); result.module->name = "parsed_rust_module"; result.module->targetLanguage = "rust"; applySpan(result.module.get(), root); convertRustCrate(root, source, result.module.get()); collectDiagnostics(root, source, result.diagnostics); ts_tree_delete(tree); ts_parser_delete(parser); return result; } // --------------------------------------------------------------- private: // Rust CST -> AST // --------------------------------------------------------------- static void convertRustCrate(TSNode root, const std::string& source, Module* module) { 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 == "use_declaration") { TSNode arg = childByFieldName(child, "argument"); std::string importName = nodeText(arg, source); if (importName.empty()) importName = nodeText(child, source); if (!importName.empty()) { auto* imp = new Import(IdGenerator::next("imp"), importName, "module"); module->addChild("imports", imp); } } else if (type == "function_item") { auto* fn = convertRustFunction(child, source, ""); if (fn) module->addChild("functions", fn); } else if (type == "impl_item") { convertRustImpl(child, source, module); } else if (type == "struct_item" || type == "enum_item" || type == "trait_item") { // Record type name as a custom type variable for visibility. TSNode nameNode = childByFieldName(child, "name"); if (!ts_node_is_null(nameNode)) { auto* var = new Variable(IdGenerator::next("var"), nodeText(nameNode, source)); module->addChild("variables", var); } } } } static void convertRustImpl(TSNode node, const std::string& source, Module* module) { TSNode typeNode = childByFieldName(node, "type"); std::string typeName = nodeText(typeNode, source); TSNode bodyNode = childByFieldName(node, "body"); if (ts_node_is_null(bodyNode)) return; uint32_t count = ts_node_named_child_count(bodyNode); for (uint32_t i = 0; i < count; ++i) { TSNode child = ts_node_named_child(bodyNode, i); if (nodeType(child) == "function_item" || nodeType(child) == "function_signature_item") { auto* fn = convertRustFunction(child, source, typeName); if (fn) module->addChild("functions", fn); } } } static Function* convertRustFunction(TSNode node, const std::string& source, const std::string& receiverType) { TSNode nameNode = childByFieldName(node, "name"); if (ts_node_is_null(nameNode)) return nullptr; auto* fn = new Function(); fn->id = IdGenerator::next("fn"); applySpan(fn, node); std::string name = nodeText(nameNode, source); if (!receiverType.empty()) { fn->name = receiverType + "." + name; } else { fn->name = name; } TSNode paramsNode = childByFieldName(node, "parameters"); if (!ts_node_is_null(paramsNode)) { convertRustParameters(paramsNode, source, fn); } TSNode retNode = childByFieldName(node, "return_type"); if (!ts_node_is_null(retNode)) { if (auto* t = convertRustType(retNode, source)) fn->setChild("returnType", t); } TSNode bodyNode = childByFieldName(node, "body"); if (!ts_node_is_null(bodyNode)) { convertRustBlock(bodyNode, source, fn); } auto* lifetime = new LifetimeAnnotation(IdGenerator::next("anno"), "RAII"); fn->addChild("annotations", lifetime); return fn; } static void convertRustParameters(TSNode paramsNode, const std::string& source, Function* fn) { uint32_t count = ts_node_named_child_count(paramsNode); for (uint32_t i = 0; i < count; ++i) { TSNode child = ts_node_named_child(paramsNode, i); std::string type = nodeType(child); if (type == "self_parameter") { auto* param = new Parameter(IdGenerator::next("param"), "self"); applySpan(param, child); fn->addChild("parameters", param); } else if (type == "parameter") { TSNode patternNode = childByFieldName(child, "pattern"); TSNode typeNode = childByFieldName(child, "type"); if (ts_node_is_null(patternNode)) continue; auto* param = new Parameter(IdGenerator::next("param"), nodeText(patternNode, source)); applySpan(param, child); if (!ts_node_is_null(typeNode)) { if (auto* t = convertRustType(typeNode, source)) param->setChild("type", t); } fn->addChild("parameters", param); } } } static void convertRustBlock(TSNode blockNode, const std::string& source, Function* fn) { uint32_t count = ts_node_named_child_count(blockNode); for (uint32_t i = 0; i < count; ++i) { ASTNode* stmt = convertRustStatement(ts_node_named_child(blockNode, i), source); if (stmt) fn->addChild("body", stmt); } } static ASTNode* convertRustStatement(TSNode node, const std::string& source) { std::string type = nodeType(node); if (type == "return_expression") { auto* ret = new Return(); ret->id = IdGenerator::next("ret"); applySpan(ret, node); if (ts_node_named_child_count(node) > 0) { ASTNode* val = convertRustExpression(ts_node_named_child(node, 0), source); if (val) ret->setChild("value", val); } return ret; } else if (type == "let_declaration") { auto* assign = new Assignment(); assign->id = IdGenerator::next("assign"); applySpan(assign, node); TSNode patternNode = childByFieldName(node, "pattern"); TSNode valueNode = childByFieldName(node, "value"); if (!ts_node_is_null(patternNode)) { auto* target = new VariableReference(IdGenerator::next("var"), nodeText(patternNode, source)); assign->setChild("target", target); } if (!ts_node_is_null(valueNode)) { ASTNode* val = convertRustExpression(valueNode, source); if (val) assign->setChild("value", val); } return assign; } else if (type == "expression_statement") { auto* exprStmt = new ExpressionStatement(); exprStmt->id = IdGenerator::next("exprstmt"); applySpan(exprStmt, node); if (ts_node_named_child_count(node) > 0) { ASTNode* expr = convertRustExpression(ts_node_named_child(node, 0), source); if (expr) exprStmt->setChild("expression", expr); } return exprStmt; } else if (type == "if_expression") { auto* ifStmt = new IfStatement(); ifStmt->id = IdGenerator::next("if"); applySpan(ifStmt, node); TSNode condNode = childByFieldName(node, "condition"); if (!ts_node_is_null(condNode)) { ASTNode* cond = convertRustExpression(condNode, source); if (cond) ifStmt->setChild("condition", cond); } TSNode consNode = childByFieldName(node, "consequence"); if (!ts_node_is_null(consNode)) { uint32_t cc = ts_node_named_child_count(consNode); for (uint32_t i = 0; i < cc; ++i) { ASTNode* s = convertRustStatement(ts_node_named_child(consNode, i), source); if (s) ifStmt->addChild("thenBranch", s); } } TSNode altNode = childByFieldName(node, "alternative"); if (!ts_node_is_null(altNode)) { uint32_t ac = ts_node_named_child_count(altNode); for (uint32_t i = 0; i < ac; ++i) { ASTNode* s = convertRustStatement(ts_node_named_child(altNode, i), source); if (s) ifStmt->addChild("elseBranch", s); } } return ifStmt; } else if (type == "while_expression") { auto* loop = new WhileLoop(); loop->id = IdGenerator::next("while"); applySpan(loop, node); TSNode condNode = childByFieldName(node, "condition"); if (!ts_node_is_null(condNode)) { ASTNode* cond = convertRustExpression(condNode, source); if (cond) loop->setChild("condition", cond); } TSNode bodyNode = childByFieldName(node, "body"); if (!ts_node_is_null(bodyNode)) { uint32_t bc = ts_node_named_child_count(bodyNode); for (uint32_t i = 0; i < bc; ++i) { ASTNode* s = convertRustStatement(ts_node_named_child(bodyNode, i), source); if (s) loop->addChild("body", s); } } return loop; } else if (type == "for_expression") { auto* loop = new ForLoop(); loop->id = IdGenerator::next("for"); applySpan(loop, node); TSNode patternNode = childByFieldName(node, "pattern"); if (!ts_node_is_null(patternNode)) { loop->iteratorName = nodeText(patternNode, source); } TSNode valueNode = childByFieldName(node, "value"); if (!ts_node_is_null(valueNode)) { ASTNode* iter = convertRustExpression(valueNode, source); if (iter) loop->setChild("iterable", iter); } TSNode bodyNode = childByFieldName(node, "body"); if (!ts_node_is_null(bodyNode)) { uint32_t bc = ts_node_named_child_count(bodyNode); for (uint32_t i = 0; i < bc; ++i) { ASTNode* s = convertRustStatement(ts_node_named_child(bodyNode, i), source); if (s) loop->addChild("body", s); } } return loop; } else if (type == "block") { auto* block = new Block(); block->id = IdGenerator::next("block"); applySpan(block, node); uint32_t bc = ts_node_named_child_count(node); for (uint32_t i = 0; i < bc; ++i) { ASTNode* s = convertRustStatement(ts_node_named_child(node, i), source); if (s) block->addChild("statements", s); } return block; } ASTNode* expr = convertRustExpression(node, source); if (expr) { auto* exprStmt = new ExpressionStatement(); exprStmt->id = IdGenerator::next("exprstmt"); applySpan(exprStmt, node); exprStmt->setChild("expression", expr); return exprStmt; } return nullptr; } static ASTNode* convertRustExpression(TSNode node, const std::string& source) { std::string type = nodeType(node); if (type == "binary_expression") { auto* binOp = new BinaryOperation(); binOp->id = IdGenerator::next("binop"); applySpan(binOp, node); TSNode leftNode = childByFieldName(node, "left"); TSNode rightNode = childByFieldName(node, "right"); TSNode opNode = childByFieldName(node, "operator"); if (!ts_node_is_null(opNode)) binOp->op = nodeText(opNode, source); if (!ts_node_is_null(leftNode)) { ASTNode* left = convertRustExpression(leftNode, source); if (left) binOp->setChild("left", left); } if (!ts_node_is_null(rightNode)) { ASTNode* right = convertRustExpression(rightNode, source); if (right) binOp->setChild("right", right); } return binOp; } else if (type == "assignment_expression" || type == "compound_assignment_expr") { auto* assign = new Assignment(); assign->id = IdGenerator::next("assign"); applySpan(assign, node); TSNode leftNode = childByFieldName(node, "left"); TSNode rightNode = childByFieldName(node, "right"); if (!ts_node_is_null(leftNode)) { ASTNode* target = convertRustExpression(leftNode, source); if (target) assign->setChild("target", target); } if (!ts_node_is_null(rightNode)) { ASTNode* value = convertRustExpression(rightNode, source); if (value) assign->setChild("value", value); } return assign; } else if (type == "call_expression") { auto* call = new FunctionCall(); call->id = IdGenerator::next("call"); applySpan(call, node); TSNode funcNode = childByFieldName(node, "function"); if (!ts_node_is_null(funcNode)) { call->functionName = nodeText(funcNode, source); } TSNode argsNode = childByFieldName(node, "arguments"); if (!ts_node_is_null(argsNode)) { uint32_t count = ts_node_named_child_count(argsNode); for (uint32_t i = 0; i < count; ++i) { ASTNode* arg = convertRustExpression(ts_node_named_child(argsNode, i), source); if (arg) call->addChild("arguments", arg); } } return call; } else if (type == "field_expression") { auto* mem = new MemberAccess(); mem->id = IdGenerator::next("member"); applySpan(mem, node); TSNode valueNode = childByFieldName(node, "value"); TSNode fieldNode = childByFieldName(node, "field"); if (!ts_node_is_null(fieldNode)) { mem->memberName = nodeText(fieldNode, source); } if (!ts_node_is_null(valueNode)) { ASTNode* target = convertRustExpression(valueNode, source); if (target) mem->setChild("target", target); } return mem; } else if (type == "index_expression") { auto* access = new IndexAccess(); access->id = IdGenerator::next("index"); applySpan(access, node); if (ts_node_named_child_count(node) >= 2) { ASTNode* target = convertRustExpression(ts_node_named_child(node, 0), source); ASTNode* idx = convertRustExpression(ts_node_named_child(node, 1), source); if (target) access->setChild("target", target); if (idx) access->setChild("index", idx); } return access; } else if (type == "identifier") { auto* ref = new VariableReference(IdGenerator::next("var"), nodeText(node, source)); applySpan(ref, node); return ref; } else if (type == "integer_literal") { std::string text = nodeText(node, source); int val = 0; try { val = std::stoi(text); } catch (...) {} auto* lit = new IntegerLiteral(IdGenerator::next("int"), val); applySpan(lit, node); return lit; } else if (type == "float_literal") { auto* lit = new FloatLiteral(IdGenerator::next("float"), nodeText(node, source)); applySpan(lit, node); return lit; } else if (type == "string_literal" || type == "char_literal") { auto* lit = new StringLiteral(IdGenerator::next("str"), nodeText(node, source)); applySpan(lit, node); return lit; } else if (type == "true" || type == "false") { auto* lit = new BooleanLiteral(IdGenerator::next("bool"), type == "true"); applySpan(lit, node); return lit; } else if (type == "unit_expression") { auto* lit = new NullLiteral(); lit->id = IdGenerator::next("null"); applySpan(lit, node); return lit; } else if (type == "parenthesized_expression") { if (ts_node_named_child_count(node) > 0) { return convertRustExpression(ts_node_named_child(node, 0), source); } } else if (type == "array_expression") { auto* list = new ListLiteral(); list->id = IdGenerator::next("list"); applySpan(list, node); uint32_t count = ts_node_named_child_count(node); for (uint32_t i = 0; i < count; ++i) { ASTNode* elem = convertRustExpression(ts_node_named_child(node, i), source); if (elem) list->addChild("elements", elem); } return list; } std::string text = nodeText(node, source); if (!text.empty()) { auto* ref = new VariableReference(IdGenerator::next("var"), text); applySpan(ref, node); return ref; } return nullptr; } static Type* convertRustType(TSNode node, const std::string& source) { std::string type = nodeType(node); if (type == "primitive_type") { auto* prim = new PrimitiveType(); prim->id = IdGenerator::next("type"); prim->kind = nodeText(node, source); return prim; } else if (type == "reference_type") { TSNode valueNode = childByFieldName(node, "value"); if (!ts_node_is_null(valueNode)) { if (auto* inner = convertRustType(valueNode, source)) { auto* opt = new OptionalType(); opt->id = IdGenerator::next("type"); opt->setChild("innerType", inner); return opt; } } } else if (type == "array_type") { auto* arr = new ArrayType(); arr->id = IdGenerator::next("type"); TSNode elemNode = childByFieldName(node, "element"); if (!ts_node_is_null(elemNode)) { if (auto* et = convertRustType(elemNode, source)) arr->setChild("elementType", et); } return arr; } else if (type == "tuple_type") { auto* tuple = new TupleType(); tuple->id = IdGenerator::next("type"); uint32_t count = ts_node_named_child_count(node); for (uint32_t i = 0; i < count; ++i) { if (auto* t = convertRustType(ts_node_named_child(node, i), source)) { tuple->addChild("elementTypes", t); } } return tuple; } auto* custom = new CustomType(); custom->id = IdGenerator::next("type"); custom->typeName = nodeText(node, source); return custom; } // ---------------------------------------------------------------