Skip to content

Commit

Permalink
fix: [cxx-parser] Add missing fill parent node logic (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl authored Sep 18, 2023
1 parent 0ebb0da commit 368e985
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cxx-parser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cxx-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agoraio-extensions/cxx-parser",
"version": "0.1.4",
"version": "0.1.5",
"description": "",
"main": "src/index.js",
"scripts": {
Expand Down
56 changes: 49 additions & 7 deletions cxx-parser/src/cxx_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Parser,
TerraContext,
} from "@agoraio-extensions/terra-core";
import { CXXFile, cast } from "./cxx_terra_node";
import { CXXFile, CXXTYPE, cast } from "./cxx_terra_node";
import { CXXParserConfigs } from "./cxx_parser_configs";

export function dumpCXXAstJson(
Expand Down Expand Up @@ -57,21 +57,18 @@ export function dumpCXXAstJson(
}

export function genParseResultFromJson(astJsonContent: string): ParseResult {
let ast_json = JSON.parse(astJsonContent, (key, value) => {
const cxxFiles: CXXFile[] = JSON.parse(astJsonContent, (key, value) => {
if (typeof value === "object") {
if (Array.isArray(value)) {
return value;
}
return cast(value);
}

return value;
});
var parseResult: ParseResult = new ParseResult();
let cxxFiles: CXXFile[] = ast_json;

fillParentNode(cxxFiles);
const parseResult = new ParseResult();
parseResult.nodes = cxxFiles;

return parseResult;
}

Expand All @@ -95,3 +92,48 @@ export function CXXParser(

return genParseResultFromJson(jsonContent);
}

function fillParentNode(cxxFiles: CXXFile[]) {
cxxFiles.forEach((file) => {
file.nodes.forEach((node) => {
node.parent = file;
if (node.__TYPE === CXXTYPE.Clazz) {
node.asClazz().constructors.forEach((constructor) => {
constructor.parent = node;
constructor.parameters.forEach((param) => {
param.parent = constructor;
param.type.parent = param;
});
});
node.asClazz().methods.forEach((method) => {
method.parent = node;
method.parameters.forEach((param) => {
param.parent = method;
param.type.parent = param;
});
method.return_type.parent = method;
});
node.asClazz().member_variables.forEach((variable) => {
variable.parent = node;
variable.type.parent = variable;
});
} else if (node.__TYPE === CXXTYPE.Struct) {
node.asStruct().constructors.forEach((constructor) => {
constructor.parent = node;
constructor.parameters.forEach((param) => {
param.parent = constructor;
param.type.parent = param;
});
});
node.asStruct().member_variables.forEach((variable) => {
variable.parent = node;
variable.type.parent = variable;
});
} else if (node.__TYPE === CXXTYPE.Enumz) {
node.asEnumz().enum_constants.forEach((enum_constant) => {
enum_constant.parent = node;
});
}
});
});
}

0 comments on commit 368e985

Please sign in to comment.