Skip to content

Commit

Permalink
feat: [cxx-parser] Add SimpleType.lenOfArrayType extension
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Nov 20, 2023
1 parent 866d883 commit 4247ef7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
17 changes: 17 additions & 0 deletions cxx-parser/__tests__/unit_test/cxx_terra_node_ext.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from 'fs';
import os from 'os';
import path from 'path';

import '../../src/cxx_terra_node_ext';
import { SimpleType, SimpleTypeKind } from '../../src/cxx_terra_node';

describe('cxx_terra_node_ext', () => {
it('lenOfArrayType can get len of array type', () => {
let simpleType = new SimpleType();
simpleType.kind = SimpleTypeKind.array_t;
simpleType.source = 'int[10]';

let len = simpleType.lenOfArrayType();
expect(len).toBe('10');
});
});
29 changes: 29 additions & 0 deletions cxx-parser/src/cxx_terra_node_ext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { strict as assert } from 'assert';

import { SimpleType, SimpleTypeKind } from './cxx_terra_node';

export {};

declare global {
Expand All @@ -17,6 +21,15 @@ declare global {
}
}

declare module '@agoraio-extensions/cxx-parser' {
export interface SimpleType {
/**
* The length of the array type. For example, "int[10]" returns "10".
*/
lenOfArrayType(): string;
}
}

/**
* This function removes the namespace from a fully qualified name.
* For example, "foo::bar" becomes "bar".
Expand Down Expand Up @@ -51,3 +64,19 @@ String.prototype.getNamespace = function (): string {
const splitted = this.split('::');
return Array.from(splitted.slice(0, splitted.length - 1)).join('::');
};

/**
* The length of the array type. For example, "int[10]" returns "10".
*/
SimpleType.prototype.lenOfArrayType = function (): string {
assert(this.kind === SimpleTypeKind.array_t);
assert(this.source);

let regex = new RegExp(/\d+/);
let len = this.source.match(regex);
if (len) {
return len[0];
}

assert(false); // Should not reach here
};
6 changes: 4 additions & 2 deletions cxx-parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './cxx_terra_node';
// This should be above the `cxx_terra_node`, or it will throw TypeError: Cannot read properties of undefined (reading 'xxx')
export * from './cxx_terra_node_ext';
export * from './cxx_terra_node';
export * from './cxx_parser_configs';
export * from './cxx_parser';
// This should be above the `cxx_parser`, or it will throw TypeError: Cannot read properties of undefined (reading 'xxx')
export * from './cxx_parser_ext';
export * from './cxx_parser';

0 comments on commit 4247ef7

Please sign in to comment.