Skip to content

Commit

Permalink
Fixed a test error
Browse files Browse the repository at this point in the history
Also added a configuration to convert the ANTLR3 runtime. Unfortunately, ANTLR4 uses a lot of that old stuff.

Signed-off-by: Mike Lischke <mike@lischke-online.de>
  • Loading branch information
mike-lischke committed May 8, 2024
1 parent b487704 commit b0f2057
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 4 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
"smartStep": true,
"trace": false
},
{
"type": "node",
"request": "launch",
"name": "Convert ANTLR3 runtime",
"sourceMaps": true,
"stopOnEntry": false,
"smartStep": true,
"args": [],
"runtimeArgs": [
"--experimental-specifier-resolution=node",
"--no-warnings",
"--loader",
"ts-node/esm",
"tools/convertANTLR3Runtime.ts",
],
"console": "integratedTerminal",
"trace": false
},
{
"type": "node",
"request": "launch",
Expand Down
1 change: 0 additions & 1 deletion tests/conversion/Option.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe("Options Tests", () => {
exclude: [],
include: [],
options: {
lib: path.join(testDir, "..", "lib"),
prefix: (sourcePath) => {
return `/**\n * ${rand}\n * Auto generated from ${sourcePath}\n * ${new Date().toString()} */`;
},
Expand Down
127 changes: 127 additions & 0 deletions tools/convertANTLR3Runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) Mike Lischke. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

/* eslint-disable max-classes-per-file */

// cspell:ignore Compiletime, Interp

import * as path from "path";

import { PackageSourceManager } from "../src/PackageSourceManager.js";
import { IConverterConfiguration, JavaToTypescriptConverter } from "../src/conversion/JavaToTypeScript.js";
import { PackageSource } from "../src/PackageSource.js";

/** Member sorting identifiers as used in the project's eslint configuration. */
const memberOrderOptions = {
default: [
"public-static-field",
"protected-static-field",
"private-static-field",
"public-instance-field",
"protected-instance-field",
"private-instance-field",
"public-abstract-field",
"protected-abstract-field",
"public-field",
"protected-field",
"private-field",
"static-field",
"instance-field",
"abstract-field",
"decorated-field",
"field",
"public-constructor",
"protected-constructor",
"private-constructor",
"constructor",
"public-static-method",
"protected-static-method",
"private-static-method",
"public-method",
"protected-method",
"private-method",
"public-abstract-method",
"protected-abstract-method",
],
};

const importResolver = (packageId: string): PackageSource | undefined => {
if (packageId.startsWith("org.antlr.runtime")) {
// ANTLRv3 runtime. Use ANTLRv4 instead.
return new PackageSource("org.antlr.runtime", "", "antlr4ng");
}

return PackageSourceManager.emptySource(packageId);
};

const include: string[] = [
//"ErrorBufferAllErrors.java",
];

const classResolver = new Map([
["String", { alias: "string", importPath: "" }],
["Object", { alias: "unknown", importPath: "" }],
["ArrayList", { alias: "Array", importPath: "" }],
["Locale", { alias: "Intl.Locale", importPath: "" }],
["Map", { alias: "Map", importPath: "" }],
["HashMap", { alias: "Map", importPath: "" }],
["Integer", { alias: "number", importPath: "" }],
["RuntimeException", { alias: "Error", importPath: "" }],
["NoSuchMethodError", { alias: "Error", importPath: "" }],
]);

const convertANTLR4JavaRuntime = async () => {
const antlrToolOptions: IConverterConfiguration = {
javaLib: "",
packageRoot: path.resolve(process.cwd(), "../antlr3/runtime/Java/src/main/java/org"),
include,
exclude: [],
outputPath: "../antlr3ts-temp/",
options: {
prefix:
`/*
* Copyright (c) The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
// cspell: disable
/* eslint-disable jsdoc/require-returns, jsdoc/require-param */`,
importResolver,
convertAnnotations: false,
preferArrowFunctions: false,
autoAddBraces: true,
addIndexFiles: false,
addNullUnionType: false,
suppressTypeWithInitializer: true,
wrapStringLiterals: false,
memberOrderOptions,
sourceMappings: [
],
useUnqualifiedTypes: true,
classResolver,
importExtension: ".js",
convertNumberPrimitiveTypes: true,
},
sourceReplace: new Map([
]),
debug: {
pathForPosition: {
filePattern: "XXX",
position: {
row: 49,
column: 5,
},
},
},

};

const converter = new JavaToTypescriptConverter(antlrToolOptions);
await converter.startConversion();
};

await convertANTLR4JavaRuntime();
29 changes: 26 additions & 3 deletions tools/convertANTLR4Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,34 @@ const importResolver = (packageId: string): PackageSource | undefined => {
const include: string[] = [
];

const classResolver = new Map([
["String", { alias: "string", importPath: "" }],
["Object", { alias: "Object", importPath: "" }],
["ArrayList", { alias: "Array", importPath: "" }],
["List", { alias: "Array", importPath: "" }],
["Locale", { alias: "Intl.Locale", importPath: "" }],
["Map", { alias: "Map", importPath: "" }],
["HashMap", { alias: "Map", importPath: "" }],
["Integer", { alias: "number", importPath: "" }],
["HashSet", { alias: "", importPath: "antlr4ng" }],
["OrderedHashSet", { alias: "", importPath: "antlr4ng" }],
["HashMap", { alias: "", importPath: "antlr4ng" }],
["OrderedHashMap", { alias: "", importPath: "antlr4ng" }],
["LinkedHashMap", { alias: "HashMap", importPath: "antlr4ng" }],
["VocabularyImpl", { alias: "Vocabulary", importPath: "antlr4ng" }],
["Pair", { alias: "", importPath: "" }],
]);

const convertANTLR4Tool = async () => {
const antlrToolOptions: IConverterConfiguration = {
packageRoot: path.resolve(process.cwd(), "../antlr4/tool/src"),
packageRoot: path.resolve(process.cwd(), "../ANTLRng/src/tree-walkers"),
include,
exclude: [],
outputPath: "../ANTLRng/tool/src",
outputPath: "../ANTLRng/src/tree-walkers",
javaLib: "",
options: {
prefix: `
/* eslint-disable jsdoc/require-returns, jsdoc/require-param */`,
importResolver,
convertAnnotations: true,
preferArrowFunctions: false,
Expand All @@ -81,13 +102,15 @@ const convertANTLR4Tool = async () => {
wrapStringLiterals: false,
memberOrderOptions,
sourceMappings: [
//{ sourcePath: path.resolve(process.cwd(), "../antlr4/runtime/Java/src"), importPath: "antlr4ng" },
{ sourcePath: path.resolve(process.cwd(), "../antlr4/runtime/Java/src"), importPath: "antlr4ng" },
],
useUnqualifiedTypes: true,
libraryImports: new Map([
//[path.resolve(process.cwd(), "../ANTLRng/runtime-testsuite/decorators.js"), ["Test", "Override"]],
]),
importExtension: ".js",
convertNumberPrimitiveTypes: true,
classResolver,
},
sourceReplace: new Map([
]),
Expand Down

0 comments on commit b0f2057

Please sign in to comment.