Skip to content

Commit 9b6c7e8

Browse files
committed
chore: Expose async API for parsing C++ code
Signed-off-by: Roberto Raggi <[email protected]>
1 parent d0ae3e4 commit 9b6c7e8

File tree

11 files changed

+219
-141
lines changed

11 files changed

+219
-141
lines changed

packages/cxx-frontend/src/Parser.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import initCxx, { cxx } from "./cxx";
2222
import { Unit } from "./Unit";
2323
import { AST } from "./AST";
2424

25-
interface ParseParams {
25+
interface ParserParams {
2626
/**
2727
* Path to the file to parse.
2828
*/
@@ -32,6 +32,12 @@ interface ParseParams {
3232
* Source code to parse.
3333
*/
3434
source: string;
35+
36+
resolve?: (
37+
name: string,
38+
kind: "quoted" | "angled",
39+
next: boolean
40+
) => Promise<string | undefined>;
3541
}
3642

3743
export class Parser {
@@ -48,7 +54,7 @@ export class Parser {
4854

4955
static async initFromURL(
5056
url: URL,
51-
{ signal }: { signal?: AbortSignal } = {},
57+
{ signal }: { signal?: AbortSignal } = {}
5258
) {
5359
const response = await fetch(url, { signal });
5460

@@ -69,8 +75,8 @@ export class Parser {
6975
return cxx !== undefined && cxx !== null;
7076
}
7177

72-
constructor(options: ParseParams) {
73-
const { path, source } = options;
78+
constructor(options: ParserParams) {
79+
const { path, source, resolve } = options;
7480

7581
if (typeof path !== "string") {
7682
throw new TypeError("expected parameter 'path' of type 'string'");
@@ -80,14 +86,14 @@ export class Parser {
8086
throw new TypeError("expected parameter 'source' of type 'string'");
8187
}
8288

83-
this.#unit = cxx.createUnit(source, path);
89+
this.#unit = cxx.createUnit(source, path, { resolve });
8490
}
8591

86-
parse() {
92+
async parse() {
8793
if (!this.#unit) {
8894
return;
8995
}
90-
this.#unit.parse();
96+
await this.#unit.parse();
9197
this.#ast = AST.from(this.#unit.getHandle(), this);
9298
}
9399

packages/cxx-frontend/src/Preprocessor.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ import { cxx } from "./cxx";
2222

2323
interface PreprocessorOptions {
2424
systemIncludePaths?: string[];
25-
26-
fs?: {
27-
existsSync(path: string): boolean;
28-
readFileSync(path: string): string;
29-
};
3025
}
3126

3227
export class Preprocessor {
@@ -39,7 +34,7 @@ export class Preprocessor {
3934
*
4035
* @param source
4136
*/
42-
constructor({ systemIncludePaths, fs }: PreprocessorOptions = {}) {
37+
constructor({ systemIncludePaths }: PreprocessorOptions = {}) {
4338
this.#control = new cxx.Control();
4439
this.#diagnosticClient = new cxx.DiagnosticsClient();
4540
this.#handle = new cxx.Preprocessor(this.#control, this.#diagnosticClient);
@@ -48,13 +43,6 @@ export class Preprocessor {
4843
systemIncludePaths?.forEach((path) => {
4944
this.#handle.addIncludePath(path);
5045
});
51-
52-
if (fs) {
53-
const { existsSync, readFileSync } = fs;
54-
55-
this.#handle.setCanResolveFiles(true);
56-
this.#handle.setup(existsSync, readFileSync);
57-
}
5846
}
5947

6048
/**

packages/cxx-frontend/src/Unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { Diagnostic } from "./Diagnostic";
2222

2323
export interface Unit {
2424
delete(): void;
25-
parse(): boolean;
25+
parse(): Promise<boolean>;
2626
getHandle(): number;
2727
getUnitHandle(): number;
2828
getDiagnostics(): Diagnostic[];

packages/cxx-frontend/src/cxx-js.d.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,22 @@ interface Lexer {
8383
tokenText(): string;
8484
}
8585

86+
interface Api {
87+
resolve?: (
88+
name: string,
89+
kind: "quoted" | "angled",
90+
isIncludeNext: boolean
91+
) => Promise<string | undefined>;
92+
}
93+
8694
export type CXX = {
8795
Control: Control;
8896
DiagnosticsClient: DiagnosticsClient;
8997
Preprocessor: Preprocessor;
9098
Lexer: Lexer;
9199
TranslationUnit: TranslationUnit;
92100

93-
createUnit(source: string, path: string): Unit;
101+
createUnit(source: string, path: string, api: Api): Unit;
94102
getASTKind(handle: number): number;
95103
getASTSlot(handle: number, slot: number): number;
96104
getASTSlotKind(handle: number, slot: number): ASTSlotKind;
@@ -105,11 +113,11 @@ export type CXX = {
105113
getTokenLocation(handle: number, unitHandle: number): SourceLocation;
106114
getStartLocation(
107115
handle: number,
108-
unitHandle: number,
116+
unitHandle: number
109117
): SourceLocation | undefined;
110118
getEndLocation(
111119
handle: number,
112-
unitHandle: number,
120+
unitHandle: number
113121
): SourceLocation | undefined;
114122
};
115123

0 commit comments

Comments
 (0)