From: Thomas Walker Lynch Date: Thu, 5 Sep 2024 08:00:31 +0000 (+0000) Subject: adds rule and token options to RuleNameList X-Git-Url: https://git.reasoningtechnology.com/usr/lib/python2.7/gettext.py?a=commitdiff_plain;h=c4f5180bc40c03e7494907ad8eaab98d887f41a8;p=GQL-to-Cypher adds rule and token options to RuleNameList --- diff --git a/developer/executor/ANTLR_OUT_FL b/developer/executor/ANTLR_OUT_FL index bc88289..c01acc3 100755 --- a/developer/executor/ANTLR_OUT_FL +++ b/developer/executor/ANTLR_OUT_FL @@ -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 $@ diff --git a/developer/javac/RuleNameList.java b/developer/javac/RuleNameList.java index 438d060..bfbf81b 100644 --- a/developer/javac/RuleNameList.java +++ b/developer/javac/RuleNameList.java @@ -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 - */ + 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 +*/ 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 " + + "[-rule (default)] [-no-rule] " + + "[-token] [-no-token (default)]"; + public static void main(String[] args) { - if (args.length != 1) { - System.err.println("Usage: RuleNameList "); + if (args.length == 0) { + System.err.println(USAGE_MESSAGE); System.exit(1); } - String grammarName = args[0]; + // Defaults + boolean printRules = true; + boolean printTokens = false; + List 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 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 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();