|
| 1 | +/* |
| 2 | + * jsweet-candy-generator-util |
| 3 | + * Copyright 2023 Christian Kohlschütter |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.jsweet.candyutil.parser; |
| 18 | + |
| 19 | +import java.io.BufferedReader; |
| 20 | +import java.io.Closeable; |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.FileOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStreamReader; |
| 26 | +import java.io.OutputStreamWriter; |
| 27 | +import java.io.PrintWriter; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.function.BiFunction; |
| 32 | +import java.util.function.Function; |
| 33 | + |
| 34 | +/** |
| 35 | + * Generate parsers and remove the date from the generated files so we can add them to source |
| 36 | + * control. |
| 37 | + * |
| 38 | + * @author Christian Kohlschütter |
| 39 | + */ |
| 40 | +public class GenerateParser { |
| 41 | + public static void main(String[] args) throws Exception { |
| 42 | + if (args.length != 1) { |
| 43 | + throw new IllegalArgumentException("Syntax: <path/to/src/main/java>"); |
| 44 | + } |
| 45 | + File sourceDir = new File(args[0]); |
| 46 | + |
| 47 | + generateJavaCupParser(sourceDir); |
| 48 | + generateTSDefAPILexer(sourceDir); |
| 49 | + } |
| 50 | + |
| 51 | + private static void generateJavaCupParser(File sourceDir) throws Exception { |
| 52 | + Path parserOutFile = Path.of("TypescriptDefParser.java"); |
| 53 | + Path symOutFile = Path.of("sym.java"); |
| 54 | + if (Files.exists(parserOutFile)) { |
| 55 | + System.err.println("WARNING: Overwriting " + parserOutFile); |
| 56 | + Files.delete(parserOutFile); |
| 57 | + } |
| 58 | + if (Files.exists(symOutFile)) { |
| 59 | + System.err.println("WARNING: Overwriting " + symOutFile); |
| 60 | + Files.delete(symOutFile); |
| 61 | + } |
| 62 | + File parserPackageDir = new File(sourceDir, "org/jsweet/input/typescriptdef/parser/"); |
| 63 | + |
| 64 | + java_cup.Main.main(new String[] { |
| 65 | + "-expect", "0", // |
| 66 | + "-package", "org.jsweet.input.typescriptdef.parser", // |
| 67 | + "-parser", "TypescriptDefParser", // |
| 68 | + new File(parserPackageDir, "typescriptdef.cup").toString(), // |
| 69 | + // |
| 70 | + }); |
| 71 | + |
| 72 | + if (!Files.exists(parserOutFile)) { |
| 73 | + throw new IllegalStateException("Expected output file not created: " + parserOutFile); |
| 74 | + } |
| 75 | + if (!Files.exists(symOutFile)) { |
| 76 | + throw new IllegalStateException("Expected output file not created: " + symOutFile); |
| 77 | + } |
| 78 | + |
| 79 | + // Remove date from the line after "The following code was generated ..." |
| 80 | + Function<String, String> transformer = new PreviousLineAwareTransformer((p, c) -> { |
| 81 | + if (c.startsWith("// ") && p.startsWith("// The following code was generated by ")) { |
| 82 | + return null; |
| 83 | + } else if (c.startsWith(" * @version ") && p.startsWith("/** CUP ")) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + return c; |
| 87 | + }); |
| 88 | + moveFileAndModify(parserOutFile.toFile(), new File(parserPackageDir, parserOutFile.getName( |
| 89 | + parserOutFile.getNameCount() - 1).toString()), transformer); |
| 90 | + moveFileAndModify(symOutFile.toFile(), new File(parserPackageDir, symOutFile.getName( |
| 91 | + parserOutFile.getNameCount() - 1).toString()), transformer); |
| 92 | + } |
| 93 | + |
| 94 | + private static void generateTSDefAPILexer(File sourceDir) throws IOException { |
| 95 | + File typescriptDefLex = new File(sourceDir, |
| 96 | + "org/jsweet/input/typescriptdef/parser/typescriptdef.lex"); |
| 97 | + |
| 98 | + File typescriptDefScanner = new File(sourceDir, |
| 99 | + "org/jsweet/input/typescriptdef/parser/TypescriptDefScanner.java"); |
| 100 | + |
| 101 | + File typescriptDefScannerBackup = new File(typescriptDefScanner.toString() + "~"); |
| 102 | + |
| 103 | + jflex.Main.main(new String[] {typescriptDefLex.toString()}); |
| 104 | + Files.deleteIfExists(typescriptDefScannerBackup.toPath()); |
| 105 | + |
| 106 | + // Remove date from "The following code was generated ..." |
| 107 | + // We cannot modify in place, so let's temporarily move it around. |
| 108 | + Files.move(typescriptDefScanner.toPath(), typescriptDefScannerBackup.toPath()); |
| 109 | + moveFileAndModify(typescriptDefScannerBackup, typescriptDefScanner, (l) -> { |
| 110 | + if (l.startsWith("/* The following code was generated ")) { |
| 111 | + int i = l.indexOf(" on "); |
| 112 | + if (i != -1) { |
| 113 | + l = l.substring(0, i) + " */"; |
| 114 | + } |
| 115 | + return l; |
| 116 | + } else if (l.startsWith(" * on ") && l.endsWith(" from the specification file")) { |
| 117 | + return null; |
| 118 | + } else { |
| 119 | + return l; |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + private static void moveFileAndModify(File from, File to, |
| 125 | + Function<String, String> lineTransformer) throws IOException { |
| 126 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(from), |
| 127 | + StandardCharsets.UTF_8)); |
| 128 | + PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(to), |
| 129 | + StandardCharsets.UTF_8))) { |
| 130 | + String l; |
| 131 | + while ((l = in.readLine()) != null) { |
| 132 | + if ((l = lineTransformer.apply(l)) != null) { |
| 133 | + out.println(l); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + Files.delete(from.toPath()); |
| 138 | + if (lineTransformer instanceof Closeable) { |
| 139 | + ((Closeable) lineTransformer).close(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static final class PreviousLineAwareTransformer implements Function<String, String>, |
| 144 | + Closeable { |
| 145 | + private final BiFunction<String, String, String> transformer; |
| 146 | + private String previous = null; |
| 147 | + |
| 148 | + public PreviousLineAwareTransformer(BiFunction<String, String, String> transformer) { |
| 149 | + this.transformer = transformer; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public final String apply(String t) { |
| 154 | + String current = transformer.apply(previous, t); |
| 155 | + this.previous = current; |
| 156 | + return current; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void close() throws IOException { |
| 161 | + this.previous = null; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments