-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Expose TranslationUnit to TypeScript
- Loading branch information
1 parent
90e1f8a
commit 7792714
Showing
11 changed files
with
262 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Parser, TokenKind, TranslationUnit, AST, ASTKind } from "../dist/index.js"; | ||
import { readFile } from "fs/promises"; | ||
import { fileURLToPath } from "url"; | ||
|
||
const source = ` | ||
int main() { | ||
return 0; | ||
} | ||
`; | ||
|
||
async function main() { | ||
const wasmBinaryFile = fileURLToPath(Parser.DEFAULT_WASM_BINARY_URL); | ||
|
||
const wasmBinary = await readFile(wasmBinaryFile); | ||
|
||
// initialize the parser | ||
await Parser.init({ wasmBinary }); | ||
|
||
const translationUnit = new TranslationUnit(); | ||
|
||
translationUnit.preprocess(source, "main.cc"); | ||
|
||
console.log("== Tokens:"); | ||
|
||
for (const token of translationUnit.tokens()) { | ||
console.log(TokenKind[token.getKind()], token.getText()); | ||
} | ||
|
||
const ast = translationUnit.parse(); | ||
|
||
console.log(); | ||
console.log("== AST:") | ||
|
||
ast?.walk().preVisit((node, depth) => { | ||
if (node instanceof AST) { | ||
const ind = " ".repeat(depth * 2); | ||
const kind = ASTKind[node.getKind()]; | ||
console.log(`${ind}${kind}`); | ||
} | ||
}); | ||
|
||
translationUnit.dispose(); | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) 2023 Roberto Raggi <roberto.raggi@gmail.com> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import { AST } from "./AST.js"; | ||
import { cxx } from "./cxx.js"; | ||
import { Token } from "./Token.js"; | ||
|
||
export class TranslationUnit { | ||
#control: typeof cxx.Control; | ||
#diagnosticsClient: typeof cxx.DiagnosticsClient; | ||
#handle: typeof cxx.TranslationUnit | ||
|
||
/** | ||
* Creates a new translation unit. | ||
*/ | ||
constructor() { | ||
this.#control = new cxx.Control(); | ||
this.#diagnosticsClient = new cxx.DiagnosticsClient(); | ||
this.#handle = new cxx.TranslationUnit(this.#control, this.#diagnosticsClient); | ||
} | ||
|
||
/** | ||
* Disposes the translation unit. | ||
*/ | ||
dispose() { | ||
this.#handle.delete(); | ||
this.#diagnosticsClient.delete(); | ||
this.#control.delete(); | ||
} | ||
|
||
/** | ||
* Preprocesses the given source code. | ||
* | ||
* @param source the source code | ||
* @param path the path of the source code | ||
*/ | ||
preprocess(source: string, path: string) { | ||
this.#handle.setSource(source, path); | ||
} | ||
|
||
/** | ||
* Parses the preprocessed code | ||
* | ||
* @returns the AST or undefined | ||
*/ | ||
parse(): AST | undefined { | ||
if (!this.#handle.parse(false)) { | ||
return undefined; | ||
} | ||
|
||
return this.getAST(); | ||
} | ||
|
||
/** | ||
* Returns the AST. | ||
* | ||
* @returns the AST or undefined | ||
*/ | ||
getAST(): AST | undefined { | ||
return AST.from(this.#handle.getAST(), this.#handle); | ||
} | ||
|
||
/** | ||
* Returns the preprocessed tokens. | ||
*/ | ||
tokens(): Iterable<Token> { | ||
return { | ||
[Symbol.iterator]: () => { | ||
const count = this.tokenCount(); | ||
let index = 1; | ||
return { | ||
next: () => { | ||
if (index < count) { | ||
const token = this.tokenAt(index++); | ||
|
||
if (token !== undefined) { | ||
return { value: token, done: false }; | ||
} | ||
} | ||
return { value: undefined, done: true }; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the number of tokens. | ||
* | ||
* @returns the number of tokens | ||
*/ | ||
tokenCount(): number { | ||
return this.#handle.tokenCount(); | ||
} | ||
|
||
/** | ||
* Returns the token at the given index. | ||
* | ||
* @param index the index | ||
* @returns the token or undefined | ||
*/ | ||
tokenAt(index: number): Token | undefined { | ||
return Token.from(index, this.#handle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.