📄 tool.java
字号:
System.err.println(" -glib superGrammar specify location of supergrammar file.");
System.err.println(" -debug launch the ParseView debugger upon parser invocation.");
System.err.println(" -html generate an html file from your grammar (minus actions).");
System.err.println(" -diagnostic generate a textfile with diagnostics.");
System.err.println(" -trace have all rules call traceIn/traceOut.");
System.err.println(" -traceParser have parser rules call traceIn/traceOut.");
System.err.println(" -traceLexer have lexer rules call traceIn/traceOut.");
System.err.println(" -traceTreeParser have tree parser rules call traceIn/traceOut.");
}
public static void main(String[] args) {
System.err.println("ANTLR Parser Generator Version "+
Tool.version+" 1989-2000 jGuru.com");
try {
if ( args.length==0 ) {
help();
}
Tool theTool = new Tool();
theTool.doEverything(args);
theTool = null;
}
catch (Exception e) {
System.err.println(System.getProperty("line.separator")+
System.getProperty("line.separator"));
System.err.println("#$%%*&@# internal error: "+e.toString());
System.err.println("[complain to nearest government official");
System.err.println(" or send hate-mail to parrt@jguru.com;");
System.err.println(" please send stack trace with report.]"+
System.getProperty("line.separator"));
e.printStackTrace();
}
System.exit(0);
}
public static PrintWriter openOutputFile(String f) throws IOException {
return new PrintWriter(new FileWriter(outputDir+System.getProperty("file.separator")+f));
}
public Reader getGrammarReader() {
try {
if (grammarFile != null) {
f = new FileReader(grammarFile);
}
}
catch (IOException e) {
panic("Error: cannot open grammar file " + grammarFile);
help();
System.exit(1);
}
return f;
}
/** Issue an unknown fatal error */
public static void panic() {
System.err.println("panic");
System.exit(1);
}
/** Issue a fatal error message
* @param s The message
*/
public static void panic(String s) {
System.err.println("panic: "+s);
System.exit(1);
}
// File.getParent() can return null when the file is specified without
// a directory or is in the root directory.
// This method handles those cases.
public static File parent(File f) {
String dirname = f.getParent();
if (dirname == null) {
if (f.isAbsolute()) return new File(File.separator);
else return new File(System.getProperty("user.dir"));
}
return new File(dirname);
}
/** Parse a list such as "f1.g;f2.g;..." and return a Vector
* of the elements.
*/
public static Vector parseSeparatedList(String list, char separator) {
Vector v = new Vector(10);
StringBuffer buf = new StringBuffer(100);
int i=0;
while ( i<list.length() ) {
while ( i<list.length() && list.charAt(i)!=separator ) {
buf.append(list.charAt(i));
i++;
}
// add element to vector
v.appendElement(buf.toString());
buf.setLength(0);
// must be a separator or finished.
if ( i<list.length() ) { // not done?
i++; // skip separator
}
}
if ( v.size()==0 ) return null;
return v;
}
/** given a filename, strip off the directory prefix (if any)
* and return it. Return "./" if f has no dir prefix.
*/
public static String pathToFile(String f) {
String separator = System.getProperty("file.separator");
int endOfPath = f.lastIndexOf(separator);
if ( endOfPath == -1 ) {
// no path, use current directory
return "."+System.getProperty("file.separator");
}
return f.substring(0, endOfPath+1);
}
/** Process the command-line arguments. Can only be called by Tool.
* @param args The command-line arguments passed to main()
*/
private void processArguments(String[] args) {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-diagnostic")) {
genDiagnostics = true;
genHTML = false;
Tool.setArgOK(i);
} else {
if (args[i].equals("-o")) {
Tool.setArgOK(i);
if (i + 1 >= args.length) {
error("missing output directory with -o option; ignoring");
} else {
i++;
setOutputDirectory(args[i]);
Tool.setArgOK(i);
}
} else
if (args[i].equals("-html")) {
genHTML = true;
genDiagnostics = false;
Tool.setArgOK(i);
} else {
if (args[i].charAt(0) != '-') {
// Must be the grammar file
grammarFile = args[i];
Tool.setArgOK(i);
}
}
}
}
}
public static void setArgOK(int i) {
cmdLineArgValid.add(i);
}
public static void setOutputDirectory(String o) { outputDir = o; }
/** General-purpose utility function for removing
* characters from back of string
* @param s The string to process
* @param c The character to remove
* @return The resulting string
*/
static public String stripBack(String s, char c) {
while (s.length() > 0 && s.charAt(s.length()-1) == c)
{
s = s.substring(0, s.length()-1);
}
return s;
}
/** General-purpose utility function for removing
* characters from back of string
* @param s The string to process
* @param remove A string containing the set of characters to remove
* @return The resulting string
*/
static public String stripBack(String s, String remove) {
boolean changed;
do {
changed = false;
for (int i = 0; i < remove.length(); i++) {
char c = remove.charAt(i);
while (s.length() > 0 && s.charAt(s.length()-1) == c)
{
changed = true;
s = s.substring(0, s.length()-1);
}
}
} while (changed);
return s;
}
/** General-purpose utility function for removing
* characters from front of string
* @param s The string to process
* @param c The character to remove
* @return The resulting string
*/
static public String stripFront(String s, char c) {
while (s.length() > 0 && s.charAt(0) == c) {
s = s.substring(1);
}
return s;
}
/** General-purpose utility function for removing
* characters from front of string
* @param s The string to process
* @param remove A string containing the set of characters to remove
* @return The resulting string
*/
static public String stripFront(String s, String remove) {
boolean changed;
do {
changed = false;
for (int i = 0; i < remove.length(); i++) {
char c = remove.charAt(i);
while (s.length() > 0 && s.charAt(0) == c) {
changed = true;
s = s.substring(1);
}
}
} while (changed);
return s;
}
/** General-purpose utility function for removing
* characters from the front and back of string
* @param s The string to process
* @param head exact string to strip from head
* @param tail exact string to strip from tail
* @return The resulting string
*/
public static String stripFrontBack(String src, String head, String tail) {
int h = src.indexOf(head);
int t = src.lastIndexOf(tail);
if ( h==-1 || t==-1 ) return src;
return src.substring(h+1,t);
}
/** Issue an error; used for general tool errors not for grammar stuff
* @param s The message
*/
public static void toolError(String s) {
System.err.println("error: "+s);
}
/** Issue a warning
* @param s the message
*/
public static void warning(String s) {
System.err.println("warning: "+s);
}
/** Issue a warning with line number information
* @param s The message
* @param file The file that has the warning
* @param line The grammar file line number on which the warning occured
*/
public static void warning(String s, String file, int line) {
if ( file!=null ) {
System.err.println(FileLineFormatter.getFormatter().getFormatString(file,line)+"warning:"+s);
}
else {
System.err.println("warning; line "+line+": "+s);
}
}
/** Issue a warning with line number information
* @param s The lines of the message
* @param file The file that has the warning
* @param line The grammar file line number on which the warning occured
*/
public static void warning(String[] s, String file, int line) {
if ( s==null || s.length==0 ) {
panic("bad multi-line message to Tool.warning");
}
if ( file!=null ) {
System.err.println(FileLineFormatter.getFormatter().getFormatString(file,line)+"warning:"+s[0]);
for (int i=1; i<s.length; i++) {
System.err.println(FileLineFormatter.getFormatter().getFormatString(file,line)+s[i]);
}
}
else {
System.err.println("warning: line "+line+": "+s[0]);
for (int i=1; i<s.length; i++) {
System.err.println("warning: line "+line+": "+s[i]);
}
}
}
/**
* Support C++ namespaces (for now). Add a nested namespace name to the
* current namespace.
* DAW: David Wagner
*/
public void setNameSpace(String name) {
if ( null == nameSpace )
nameSpace = new NameSpace(stripFrontBack(name,"\"", "\""));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -