|
| 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(); |
0 commit comments