adds pretty point to the syntax tree output
authorThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Tue, 13 Aug 2024 07:41:38 +0000 (07:41 +0000)
committerThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Tue, 13 Aug 2024 07:41:38 +0000 (07:41 +0000)
developer/executor/Arithmetic_Syntax__Test [deleted file]
developer/javac/Arithmetic_Syntax.java
developer/javac/Arithmetic_Syntax_PrintVisitor.java

diff --git a/developer/executor/Arithmetic_Syntax__Test b/developer/executor/Arithmetic_Syntax__Test
deleted file mode 100755 (executable)
index a38f4df..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/usr/bin/env bash
-/var/user_data/Thomas-developer/GQL_to_Cypher/tool/jdk-22.0.1+8/bin/java -cp /var/user_data/Thomas-developer/GQL_to_Cypher/tool/executor/antlr-4.11.1-complete.jar:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm:/var/user_data/Thomas-developer/GQL_to_Cypher/developer/jvm/Arithmetic_Syntax__Test.jar Arithmetic_Syntax__Test $@
index 088d7ec..600d281 100644 (file)
@@ -3,30 +3,54 @@ import org.antlr.v4.runtime.tree.*;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
 
 public class Arithmetic_Syntax {
 
-  public static void main(String[] args) throws IOException {
-    if (args.length != 1) {
-      System.err.println("Usage: java Syntax <input-file>");
+  public static void main(String[] arg_array) throws IOException {
+    boolean pretty_print = false;
+    List<String> file_arg_list = new ArrayList<>();
+    boolean has_error = false;
+
+    // Parse the options and arguments
+    for (String arg : arg_array) {
+      if (arg.startsWith("-")) {
+        if (arg.equals("-pp")) {
+          pretty_print = true;
+        } else {
+          System.err.println("Unrecognized option: " + arg);
+          has_error = true;
+        }
+      } else {
+        file_arg_list.add(arg);
+      }
+    }
+
+    // If there were any errors, print usage and exit
+    if (has_error) {
+      System.err.println("Usage: java Arithmetic_Syntax [-pp] <input-file>");
       System.exit(1);
     }
 
-    String inputFile = args[0];
-    String input = Files.readString(Paths.get(inputFile));
+    // Ensure there is exactly one input file
+    if (file_arg_list.size() != 1) {
+      System.err.println("Usage: java Arithmetic_Syntax [-pp] <input-file>");
+      System.exit(1);
+    }
+
+    String input_file = file_arg_list.get(0);
+    String input = Files.readString(Paths.get(input_file));
 
     try {
-      // Direct instantiation without reflection
       ArithmeticLexer lexer = new ArithmeticLexer(CharStreams.fromString(input));
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       ArithmeticParser parser = new ArithmeticParser(tokens);
+      ParseTree tree = parser.program();
 
-      // Directly calling the start rule method
-      ParseTree tree = parser.program(); // Assuming 'program' is the start rule
-
-      Arithmetic_Syntax_PrintVisitor visitor = new Arithmetic_Syntax_PrintVisitor(parser.getRuleNames());
-      String syntaxSyntax = visitor.visit(tree);
-      System.out.println(syntaxSyntax);
+      Arithmetic_Syntax_PrintVisitor visitor = new Arithmetic_Syntax_PrintVisitor(parser.getRuleNames(), pretty_print);
+      String output = visitor.visit(tree);
+      System.out.println(output);
     } catch (Exception e) {
       e.printStackTrace();
     }
index d2a74a0..96f0b2b 100644 (file)
@@ -1,29 +1,62 @@
 import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
 
-public class Arithmetic_Syntax_PrintVisitor extends ArithmeticBaseVisitor<String> {
-  private final String[] ruleNames;
+public class Arithmetic_Syntax_PrintVisitor extends ArithmeticBaseVisitor<String>{
+  private final String[] rule_names;
+  private final boolean pretty_print;
 
-  public Arithmetic_Syntax_PrintVisitor(String[] ruleNames) {
-    this.ruleNames = ruleNames;
+  public Arithmetic_Syntax_PrintVisitor(String[] rule_names, boolean pretty_print){
+    this.rule_names = rule_names;
+    this.pretty_print = pretty_print;
+  }
+
+  private String indent(int level){
+    return "  ".repeat(level);
   }
 
   @Override
-  public String visitProgram(ArithmeticParser.ProgramContext ctx) {
-    return "program(" + visit(ctx.expression()) + ")";
+  public String visitProgram(ArithmeticParser.ProgramContext ctx){
+    if(pretty_print){
+      StringBuilder result = new StringBuilder();
+      result.append("program\n").append(visitExpression(ctx.expression(), 1));
+      return result.toString();
+    }else{
+      return "program(" + visit(ctx.expression()) + ")";
+    }
   }
 
   @Override
-  public String visitExpression(ArithmeticParser.ExpressionContext ctx) {
-    if (ctx.INT() != null) {
-      return "INT(" + ctx.INT().getText() + ")";
-    } else if (ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(")) {
-      // Handle parentheses in the context of the grammar structure
-      return "expression(" + visit(ctx.expression(0)) + ")";
-    } else {
-      String left = visit(ctx.expression(0));
-      String right = visit(ctx.expression(1));
-      String operator = "operator(" + ctx.getChild(1).getText() + ")";
-      return "expression(" + left + " " + operator + " " + right + ")";
+  public String visitExpression(ArithmeticParser.ExpressionContext ctx){
+    return visitExpression(ctx, 0);
+  }
+
+  private String visitExpression(ArithmeticParser.ExpressionContext ctx, int indent_level){
+    StringBuilder result = new StringBuilder();
+    if(pretty_print){
+      result.append(indent(indent_level)).append("expression(\n");
+      if( ctx.INT() != null ){
+        result.append(indent(indent_level + 1)).append("INT(").append(ctx.INT().getText()).append(")\n");
+      }else if( ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(") ){
+        result.append(indent(indent_level + 1)).append("(\n");
+        result.append(visitExpression(ctx.expression(0), indent_level + 2));
+        result.append(indent(indent_level + 1)).append(")\n");
+      }else{
+        result.append(visitExpression(ctx.expression(0), indent_level + 1));
+        result.append(indent(indent_level + 1)).append("operator(").append(ctx.getChild(1).getText()).append(")\n");
+        result.append(visitExpression(ctx.expression(1), indent_level + 1));
+      }
+      result.append(indent(indent_level)).append(")\n");
+    }else{
+      if( ctx.INT() != null ){
+        result.append("INT(").append(ctx.INT().getText()).append(")");
+      }else if( ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(") ){
+        result.append("(").append(visit(ctx.expression(0))).append(")");
+      }else{
+        String left = visit(ctx.expression(0));
+        String right = visit(ctx.expression(1));
+        String operator = "operator(" + ctx.getChild(1).getText() + ")";
+        result.append("expression(").append(left).append(" ").append(operator).append(" ").append(right).append(")");
+      }
     }
+    return result.toString();
   }
 }