Generator -> Synthesize
authorThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Thu, 5 Sep 2024 23:16:10 +0000 (23:16 +0000)
committerThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Thu, 5 Sep 2024 23:16:10 +0000 (23:16 +0000)
developer/javac/GeneratePrintVisitor.java [deleted file]
developer/javac/GeneratePrintVisitorMethod.java [deleted file]
developer/javac/Synthesize_PrintVisitor.java [new file with mode: 0644]
developer/javac/Synthhesize_PrintVisitorMethod.java [new file with mode: 0644]

diff --git a/developer/javac/GeneratePrintVisitor.java b/developer/javac/GeneratePrintVisitor.java
deleted file mode 100644 (file)
index 53d60af..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-import org.antlr.v4.runtime.*;
-import org.antlr.v4.runtime.tree.*;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.util.List;
-import java.io.PrintWriter;
-
-public class GeneratePrintVisitor {
-
-    public static void main(String[] args) throws IOException {
-        if (args.length < 2 || args.length > 3) {
-            System.err.println("Usage: java GeneratePrintVisitor <grammarFile> <outputFile> [indentLevel]");
-            System.exit(1);
-        }
-
-        String grammarFile = args[0];
-        String outputFile = args[1];
-        int indentLevel = args.length == 3 ? Integer.parseInt(args[2]) : 0;
-
-        // Extract the grammar name from the file name
-        String grammarName = Paths.get(grammarFile).getFileName().toString().replace(".g4", "");
-        String parserName = grammarName + "Parser";
-
-        // Parse the .g4 file
-        CharStream input = CharStreams.fromFileName(grammarFile);
-        ANTLRv4Lexer lexer = new ANTLRv4Lexer(input);
-        CommonTokenStream tokens = new CommonTokenStream(lexer);
-        ANTLRv4Parser parser = new ANTLRv4Parser(tokens);
-
-        // Extract rules
-        ParseTree tree = parser.grammarSpec();
-        List<String> ruleNames = extractRuleNames(parser);
-
-        // Template for the PrintVisitor class
-        String classTemplate = """
-          import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
-
-          public class PrintVisitor extends AbstractParseTreeVisitor<String> {
-            private final String[] ruleNames;
-
-            public PrintVisitor(String[] ruleNames) {
-              this.ruleNames = ruleNames;
-            }
-
-            // Generated print methods
-          """;
-
-        // Indent the class template
-        String indentedClassTemplate = StringUtils.indentString(classTemplate, indentLevel);
-
-        // Generate and output the PrintVisitor class
-        try (PrintWriter writer = new PrintWriter(outputFile)) {
-            // Write the class template
-            writer.print(indentedClassTemplate);
-
-            for (String ruleName : ruleNames) {
-                GeneratePrintVisitorMethod.generatePrintMethod(parserName, ruleName, writer, indentLevel + 1);
-            }
-
-            // Close the class
-            writer.println(StringUtils.indentString("}", indentLevel));
-        }
-    }
-
-    private static List<String> extractRuleNames(Parser parser) {
-        // Extract rule names from the parser
-        return List.of(parser.getRuleNames());
-    }
-}
diff --git a/developer/javac/GeneratePrintVisitorMethod.java b/developer/javac/GeneratePrintVisitorMethod.java
deleted file mode 100644 (file)
index f44f478..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-import java.io.PrintWriter;
-
-public class GeneratePrintVisitorMethod {
-
-    public static void main(String[] args) {
-        if (args.length != 3) {
-            System.err.println("Usage: java GeneratePrintVisitorMethod <parserName> <ruleName> <outputFile>");
-            System.exit(1);
-        }
-
-        String parserName = args[0];
-        String ruleName = args[1];
-        String outputFile = args[2];
-
-        try (PrintWriter writer = new PrintWriter(outputFile)) {
-            generatePrintMethod(parserName, ruleName, writer, 0); // Default indent level 0
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public static void generatePrintMethod(String parserName, String ruleName, PrintWriter writer, int indentLevel) {
-        // Template for the print method using text blocks
-        String template = """
-          public String visit_______0_(_______1_._______0_Context ctx) {
-            StringBuilder result = new StringBuilder();
-            result.append("_______0_(");
-            for (int i = 0; i < ctx.getChildCount(); i++) {
-              if (i > 0) result.append(", ");
-              result.append(visit(ctx.getChild(i)));
-            }
-            result.append(")");
-            return result.toString();
-          }
-          """;
-
-        // Fill in the blanks in the template
-        template = template.replace("_______0_", ruleName);
-        template = template.replace("_______1_", parserName);
-
-        // Indent the template
-        String indentedTemplate = StringUtils.indentString(template, indentLevel);
-
-        // Write the template to the output file
-        writer.print(indentedTemplate);
-        writer.println();
-    }
-}
diff --git a/developer/javac/Synthesize_PrintVisitor.java b/developer/javac/Synthesize_PrintVisitor.java
new file mode 100644 (file)
index 0000000..53d60af
--- /dev/null
@@ -0,0 +1,70 @@
+import org.antlr.v4.runtime.*;
+import org.antlr.v4.runtime.tree.*;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.io.PrintWriter;
+
+public class GeneratePrintVisitor {
+
+    public static void main(String[] args) throws IOException {
+        if (args.length < 2 || args.length > 3) {
+            System.err.println("Usage: java GeneratePrintVisitor <grammarFile> <outputFile> [indentLevel]");
+            System.exit(1);
+        }
+
+        String grammarFile = args[0];
+        String outputFile = args[1];
+        int indentLevel = args.length == 3 ? Integer.parseInt(args[2]) : 0;
+
+        // Extract the grammar name from the file name
+        String grammarName = Paths.get(grammarFile).getFileName().toString().replace(".g4", "");
+        String parserName = grammarName + "Parser";
+
+        // Parse the .g4 file
+        CharStream input = CharStreams.fromFileName(grammarFile);
+        ANTLRv4Lexer lexer = new ANTLRv4Lexer(input);
+        CommonTokenStream tokens = new CommonTokenStream(lexer);
+        ANTLRv4Parser parser = new ANTLRv4Parser(tokens);
+
+        // Extract rules
+        ParseTree tree = parser.grammarSpec();
+        List<String> ruleNames = extractRuleNames(parser);
+
+        // Template for the PrintVisitor class
+        String classTemplate = """
+          import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
+
+          public class PrintVisitor extends AbstractParseTreeVisitor<String> {
+            private final String[] ruleNames;
+
+            public PrintVisitor(String[] ruleNames) {
+              this.ruleNames = ruleNames;
+            }
+
+            // Generated print methods
+          """;
+
+        // Indent the class template
+        String indentedClassTemplate = StringUtils.indentString(classTemplate, indentLevel);
+
+        // Generate and output the PrintVisitor class
+        try (PrintWriter writer = new PrintWriter(outputFile)) {
+            // Write the class template
+            writer.print(indentedClassTemplate);
+
+            for (String ruleName : ruleNames) {
+                GeneratePrintVisitorMethod.generatePrintMethod(parserName, ruleName, writer, indentLevel + 1);
+            }
+
+            // Close the class
+            writer.println(StringUtils.indentString("}", indentLevel));
+        }
+    }
+
+    private static List<String> extractRuleNames(Parser parser) {
+        // Extract rule names from the parser
+        return List.of(parser.getRuleNames());
+    }
+}
diff --git a/developer/javac/Synthhesize_PrintVisitorMethod.java b/developer/javac/Synthhesize_PrintVisitorMethod.java
new file mode 100644 (file)
index 0000000..f44f478
--- /dev/null
@@ -0,0 +1,48 @@
+import java.io.PrintWriter;
+
+public class GeneratePrintVisitorMethod {
+
+    public static void main(String[] args) {
+        if (args.length != 3) {
+            System.err.println("Usage: java GeneratePrintVisitorMethod <parserName> <ruleName> <outputFile>");
+            System.exit(1);
+        }
+
+        String parserName = args[0];
+        String ruleName = args[1];
+        String outputFile = args[2];
+
+        try (PrintWriter writer = new PrintWriter(outputFile)) {
+            generatePrintMethod(parserName, ruleName, writer, 0); // Default indent level 0
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void generatePrintMethod(String parserName, String ruleName, PrintWriter writer, int indentLevel) {
+        // Template for the print method using text blocks
+        String template = """
+          public String visit_______0_(_______1_._______0_Context ctx) {
+            StringBuilder result = new StringBuilder();
+            result.append("_______0_(");
+            for (int i = 0; i < ctx.getChildCount(); i++) {
+              if (i > 0) result.append(", ");
+              result.append(visit(ctx.getChild(i)));
+            }
+            result.append(")");
+            return result.toString();
+          }
+          """;
+
+        // Fill in the blanks in the template
+        template = template.replace("_______0_", ruleName);
+        template = template.replace("_______1_", parserName);
+
+        // Indent the template
+        String indentedTemplate = StringUtils.indentString(template, indentLevel);
+
+        // Write the template to the output file
+        writer.print(indentedTemplate);
+        writer.println();
+    }
+}