Skip to content

Commit b0f2057

Browse files
committed
Fixed a test error
Also added a configuration to convert the ANTLR3 runtime. Unfortunately, ANTLR4 uses a lot of that old stuff. Signed-off-by: Mike Lischke <[email protected]>
1 parent b487704 commit b0f2057

File tree

4 files changed

+171
-4
lines changed

4 files changed

+171
-4
lines changed

.vscode/launch.json

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
"smartStep": true,
2222
"trace": false
2323
},
24+
{
25+
"type": "node",
26+
"request": "launch",
27+
"name": "Convert ANTLR3 runtime",
28+
"sourceMaps": true,
29+
"stopOnEntry": false,
30+
"smartStep": true,
31+
"args": [],
32+
"runtimeArgs": [
33+
"--experimental-specifier-resolution=node",
34+
"--no-warnings",
35+
"--loader",
36+
"ts-node/esm",
37+
"tools/convertANTLR3Runtime.ts",
38+
],
39+
"console": "integratedTerminal",
40+
"trace": false
41+
},
2442
{
2543
"type": "node",
2644
"request": "launch",

tests/conversion/Option.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ describe("Options Tests", () => {
2626
exclude: [],
2727
include: [],
2828
options: {
29-
lib: path.join(testDir, "..", "lib"),
3029
prefix: (sourcePath) => {
3130
return `/**\n * ${rand}\n * Auto generated from ${sourcePath}\n * ${new Date().toString()} */`;
3231
},

tools/convertANTLR3Runtime.ts

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) Mike Lischke. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
/* eslint-disable max-classes-per-file */
7+
8+
// cspell:ignore Compiletime, Interp
9+
10+
import * as path from "path";
11+
12+
import { PackageSourceManager } from "../src/PackageSourceManager.js";
13+
import { IConverterConfiguration, JavaToTypescriptConverter } from "../src/conversion/JavaToTypeScript.js";
14+
import { PackageSource } from "../src/PackageSource.js";
15+
16+
/** Member sorting identifiers as used in the project's eslint configuration. */
17+
const memberOrderOptions = {
18+
default: [
19+
"public-static-field",
20+
"protected-static-field",
21+
"private-static-field",
22+
"public-instance-field",
23+
"protected-instance-field",
24+
"private-instance-field",
25+
"public-abstract-field",
26+
"protected-abstract-field",
27+
"public-field",
28+
"protected-field",
29+
"private-field",
30+
"static-field",
31+
"instance-field",
32+
"abstract-field",
33+
"decorated-field",
34+
"field",
35+
"public-constructor",
36+
"protected-constructor",
37+
"private-constructor",
38+
"constructor",
39+
"public-static-method",
40+
"protected-static-method",
41+
"private-static-method",
42+
"public-method",
43+
"protected-method",
44+
"private-method",
45+
"public-abstract-method",
46+
"protected-abstract-method",
47+
],
48+
};
49+
50+
const importResolver = (packageId: string): PackageSource | undefined => {
51+
if (packageId.startsWith("org.antlr.runtime")) {
52+
// ANTLRv3 runtime. Use ANTLRv4 instead.
53+
return new PackageSource("org.antlr.runtime", "", "antlr4ng");
54+
}
55+
56+
return PackageSourceManager.emptySource(packageId);
57+
};
58+
59+
const include: string[] = [
60+
//"ErrorBufferAllErrors.java",
61+
];
62+
63+
const classResolver = new Map([
64+
["String", { alias: "string", importPath: "" }],
65+
["Object", { alias: "unknown", importPath: "" }],
66+
["ArrayList", { alias: "Array", importPath: "" }],
67+
["Locale", { alias: "Intl.Locale", importPath: "" }],
68+
["Map", { alias: "Map", importPath: "" }],
69+
["HashMap", { alias: "Map", importPath: "" }],
70+
["Integer", { alias: "number", importPath: "" }],
71+
["RuntimeException", { alias: "Error", importPath: "" }],
72+
["NoSuchMethodError", { alias: "Error", importPath: "" }],
73+
]);
74+
75+
const convertANTLR4JavaRuntime = async () => {
76+
const antlrToolOptions: IConverterConfiguration = {
77+
javaLib: "",
78+
packageRoot: path.resolve(process.cwd(), "../antlr3/runtime/Java/src/main/java/org"),
79+
include,
80+
exclude: [],
81+
outputPath: "../antlr3ts-temp/",
82+
options: {
83+
prefix:
84+
`/*
85+
* Copyright (c) The ANTLR Project. All rights reserved.
86+
* Use of this file is governed by the BSD 3-clause license that
87+
* can be found in the LICENSE.txt file in the project root.
88+
*/
89+
90+
// cspell: disable
91+
92+
/* eslint-disable jsdoc/require-returns, jsdoc/require-param */`,
93+
importResolver,
94+
convertAnnotations: false,
95+
preferArrowFunctions: false,
96+
autoAddBraces: true,
97+
addIndexFiles: false,
98+
addNullUnionType: false,
99+
suppressTypeWithInitializer: true,
100+
wrapStringLiterals: false,
101+
memberOrderOptions,
102+
sourceMappings: [
103+
],
104+
useUnqualifiedTypes: true,
105+
classResolver,
106+
importExtension: ".js",
107+
convertNumberPrimitiveTypes: true,
108+
},
109+
sourceReplace: new Map([
110+
]),
111+
debug: {
112+
pathForPosition: {
113+
filePattern: "XXX",
114+
position: {
115+
row: 49,
116+
column: 5,
117+
},
118+
},
119+
},
120+
121+
};
122+
123+
const converter = new JavaToTypescriptConverter(antlrToolOptions);
124+
await converter.startConversion();
125+
};
126+
127+
await convertANTLR4JavaRuntime();

tools/convertANTLR4Tool.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,34 @@ const importResolver = (packageId: string): PackageSource | undefined => {
6464
const include: string[] = [
6565
];
6666

67+
const classResolver = new Map([
68+
["String", { alias: "string", importPath: "" }],
69+
["Object", { alias: "Object", importPath: "" }],
70+
["ArrayList", { alias: "Array", importPath: "" }],
71+
["List", { alias: "Array", importPath: "" }],
72+
["Locale", { alias: "Intl.Locale", importPath: "" }],
73+
["Map", { alias: "Map", importPath: "" }],
74+
["HashMap", { alias: "Map", importPath: "" }],
75+
["Integer", { alias: "number", importPath: "" }],
76+
["HashSet", { alias: "", importPath: "antlr4ng" }],
77+
["OrderedHashSet", { alias: "", importPath: "antlr4ng" }],
78+
["HashMap", { alias: "", importPath: "antlr4ng" }],
79+
["OrderedHashMap", { alias: "", importPath: "antlr4ng" }],
80+
["LinkedHashMap", { alias: "HashMap", importPath: "antlr4ng" }],
81+
["VocabularyImpl", { alias: "Vocabulary", importPath: "antlr4ng" }],
82+
["Pair", { alias: "", importPath: "" }],
83+
]);
84+
6785
const convertANTLR4Tool = async () => {
6886
const antlrToolOptions: IConverterConfiguration = {
69-
packageRoot: path.resolve(process.cwd(), "../antlr4/tool/src"),
87+
packageRoot: path.resolve(process.cwd(), "../ANTLRng/src/tree-walkers"),
7088
include,
7189
exclude: [],
72-
outputPath: "../ANTLRng/tool/src",
90+
outputPath: "../ANTLRng/src/tree-walkers",
91+
javaLib: "",
7392
options: {
93+
prefix: `
94+
/* eslint-disable jsdoc/require-returns, jsdoc/require-param */`,
7495
importResolver,
7596
convertAnnotations: true,
7697
preferArrowFunctions: false,
@@ -81,13 +102,15 @@ const convertANTLR4Tool = async () => {
81102
wrapStringLiterals: false,
82103
memberOrderOptions,
83104
sourceMappings: [
84-
//{ sourcePath: path.resolve(process.cwd(), "../antlr4/runtime/Java/src"), importPath: "antlr4ng" },
105+
{ sourcePath: path.resolve(process.cwd(), "../antlr4/runtime/Java/src"), importPath: "antlr4ng" },
85106
],
86107
useUnqualifiedTypes: true,
87108
libraryImports: new Map([
88109
//[path.resolve(process.cwd(), "../ANTLRng/runtime-testsuite/decorators.js"), ["Test", "Override"]],
89110
]),
90111
importExtension: ".js",
112+
convertNumberPrimitiveTypes: true,
113+
classResolver,
91114
},
92115
sourceReplace: new Map([
93116
]),

0 commit comments

Comments
 (0)