/**
* Load the parser as requested by the command line arguments, and then setup
* the parser for the 'diagnostics', 'printTree' or 'SLL' options.
* <p>
* The {@link #parser} and {@link #treePrinter} variables my be null if no
* parser was requested (that is, if the start rule name is the
* LEXER_START_RULE_NAME), or if the requested parser could not be loaded or
* instantiated.
* <p>
* The {@link #treePrinter} variable may also be null if the printTree option
* has not been requested.
* <p>
* @throws various exceptions while loading the parser class and instantiating
* a parser instance.
*/
protected void loadParser() throws ClassNotFoundException, NoSuchMethodException,
InstantiationException, IllegalAccessException, InvocationTargetException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if ( !startRuleName.equals(LEXER_START_RULE_NAME) ) {
String parserName = grammarName+"Parser";
parserClass = null;
try {
parserClass = cl.loadClass(parserName).asSubclass(Parser.class);
} catch (ClassNotFoundException cnfe) {
System.err.println("ERROR: Can't load "+parserName+" as a parser");
throw cnfe;
}
try {
Constructor<? extends Parser> parserCtor = parserClass.getConstructor(TokenStream.class);
parser = parserCtor.newInstance((TokenStream)null);
} catch (Exception anException) {
System.err.println("ERROR: Could not create a parser for "+parserName);
throw anException;
}
if ( diagnostics ) {
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
if ( printTree ) {
parser.setBuildParseTree(true);
treePrinter = new TreePrinter(primaryIndentStr,
secondaryIndentStr,
indentCyclePeriod,
parser);
}
if ( trace ) {
traceListener = new PrintStreamTraceListener(parser);
parser.addParseListener(traceListener);
}
if ( SLL ) { // overrides diagnostics
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
}
}
}
RegressionTestRig.java 文件源码
java
阅读 16
收藏 0
点赞 0
评论 0
项目:antlr4-regressionTestRig
作者:
评论列表
文章目录