From: Thomas Walker Lynch Date: Thu, 5 Sep 2024 23:16:10 +0000 (+0000) Subject: Generator -> Synthesize X-Git-Url: https://git.reasoningtechnology.com/style/rt_dark_doc.css?a=commitdiff_plain;h=cd337c2111e8b62d628acebc7eb8dfe4befda0f8;p=GQL-to-Cypher Generator -> Synthesize --- diff --git a/developer/javac/GeneratePrintVisitor.java b/developer/javac/GeneratePrintVisitor.java deleted file mode 100644 index 53d60af..0000000 --- a/developer/javac/GeneratePrintVisitor.java +++ /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 [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 ruleNames = extractRuleNames(parser); - - // Template for the PrintVisitor class - String classTemplate = """ - import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; - - public class PrintVisitor extends AbstractParseTreeVisitor { - 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 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 index f44f478..0000000 --- a/developer/javac/GeneratePrintVisitorMethod.java +++ /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 "); - 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 index 0000000..53d60af --- /dev/null +++ b/developer/javac/Synthesize_PrintVisitor.java @@ -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 [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 ruleNames = extractRuleNames(parser); + + // Template for the PrintVisitor class + String classTemplate = """ + import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; + + public class PrintVisitor extends AbstractParseTreeVisitor { + 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 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 index 0000000..f44f478 --- /dev/null +++ b/developer/javac/Synthhesize_PrintVisitorMethod.java @@ -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 "); + 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(); + } +}