adds rule and token options to RuleNameList
authorThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Thu, 5 Sep 2024 08:00:31 +0000 (08:00 +0000)
committerThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Thu, 5 Sep 2024 08:00:31 +0000 (08:00 +0000)
developer/executor/ANTLR_OUT_FL
developer/javac/RuleNameList.java

index bc88289..c01acc3 100755 (executable)
@@ -1,2 +1,2 @@
 #!/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/developer/jvm:/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/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/ANTLR_OUT_FL.jar ANTLR_OUT_FL $@
+/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/developer/jvm:/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/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/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/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/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/ANTLR_OUT_FL.jar ANTLR_OUT_FL $@
index 438d060..bfbf81b 100644 (file)
@@ -1,21 +1,73 @@
 /*
- * Accepts an grammar source file and outputs the rule names found in the grammar.
- * Accepts the same input structure as ANTLRv4_Syntax.java.
- * Usage: java ANTLRv4_RuleNames <input-file>
- */
+  Accepts an grammar name (not a file path or file name) and outputs the rule
+  names found in the grammar.  
+
+  RuleNameList does not run antlr to build the grammar files, that must have
+  been done separately before this program is run.
+
+  RuleNameList produces a more reliable list than does RuleNameListRegx.
+
+  RuleNameList does not output terminal symbols (the lexer tokens).
+
+  Usage: ANTLRv4_RuleNames <input-file>
+*/
 
 import org.antlr.v4.runtime.*;
+import java.util.ArrayList;
 import java.util.List;
 
 public class RuleNameList {
 
+  // Constant for the usage message
+  private static final String USAGE_MESSAGE = "Usage: java RuleNameList <grammar_name> " +
+          "[-rule (default)] [-no-rule] " +
+          "[-token] [-no-token (default)]";
+
   public static void main(String[] args) {
-    if (args.length != 1) {
-      System.err.println("Usage: RuleNameList <grammar_name>");
+    if (args.length == 0) {
+      System.err.println(USAGE_MESSAGE);
       System.exit(1);
     }
 
-    String grammarName = args[0];
+    // Defaults
+    boolean printRules = true;
+    boolean printTokens = false;
+    List<String> argList = new ArrayList<>();
+
+    // Parse the arguments
+    for (int i = 0; i < args.length; i++) {
+      String arg = args[i];
+      if (arg.startsWith("-")) {
+        switch (arg) {
+          case "-rule":
+            printRules = true;
+            break;
+          case "-no-rule":
+            printRules = false;
+            break;
+          case "-token":
+            printTokens = true;
+            break;
+          case "-no-token":
+            printTokens = false;
+            break;
+          default:
+            System.err.println("Unrecognized option: " + arg);
+            System.err.println(USAGE_MESSAGE);
+            System.exit(1);
+        }
+      } else {
+        argList.add(arg);
+      }
+    }
+
+    // Ensure there is exactly one grammar name argument
+    if (argList.size() != 1) {
+      System.err.println(USAGE_MESSAGE);
+      System.exit(1);
+    }
+
+    String grammarName = argList.get(0);
 
     try {
       // Dynamically load the appropriate lexer and parser
@@ -30,10 +82,29 @@ public class RuleNameList {
       // Get the rule names from the parser
       List<String> ruleNames = List.of(parser.getRuleNames());
 
-      // Print the rule names
-      System.out.println("Rule names found in the grammar:");
-      for (String ruleName : ruleNames) {
-        System.out.println(ruleName);
+      // Get the token names from the lexer
+      List<String> tokenNames = List.of(lexer.getTokenNames());
+
+      // Print the rule names if requested
+      if (printRules) {
+        if (printTokens) {
+          System.out.println("#----------------------------------------");
+          System.out.println("# Rule names found in the grammar:");
+        }
+        for (String ruleName : ruleNames) {
+          System.out.println(ruleName);
+        }
+      }
+
+      // Print the token names if requested
+      if (printTokens) {
+        if (printRules) {
+          System.out.println("#----------------------------------------");
+          System.out.println("# Token names found in the grammar:");
+        }
+        for (String tokenName : tokenNames) {
+          System.out.println(tokenName);
+        }
       }
     } catch (Exception e) {
       e.printStackTrace();