本文整理汇总了Java中org.antlr.v4.runtime.atn.PredictionMode类的典型用法代码示例。如果您正苦于以下问题:Java PredictionMode类的具体用法?Java PredictionMode怎么用?Java PredictionMode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PredictionMode类属于org.antlr.v4.runtime.atn包,在下文中一共展示了PredictionMode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setupPicky
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
private void setupPicky(PainlessParser parser) {
// Diagnostic listener invokes syntaxError on other listeners for ambiguity issues,
parser.addErrorListener(new DiagnosticErrorListener(true));
// a second listener to fail the test when the above happens.
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(final Recognizer<?,?> recognizer, final Object offendingSymbol, final int line,
final int charPositionInLine, final String msg, final RecognitionException e) {
throw new AssertionError("line: " + line + ", offset: " + charPositionInLine +
", symbol:" + offendingSymbol + " " + msg);
}
});
// Enable exact ambiguity detection (costly). we enable exact since its the default for
// DiagnosticErrorListener, life is too short to think about what 'inexact ambiguity' might mean.
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
示例2: compileFiles
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
private void compileFiles(List<RawFile> files, OOPSourceCodeModel srcModel, List<String> projectFileTypes) {
for (RawFile file : files) {
try {
CharStream charStream = new ANTLRInputStream(file.content());
GolangLexer lexer = new GolangLexer(charStream);
TokenStream tokens = new CommonTokenStream(lexer);
GolangParser parser = new GolangParser(tokens);
SourceFileContext sourceFileContext = parser.sourceFile();
parser.setErrorHandler(new BailErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
ParseTreeWalker walker = new ParseTreeWalker();
GolangBaseListener listener = new GoLangTreeListener(srcModel, projectFileTypes, file);
walker.walk(listener, sourceFileContext);
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例3: parsePHP
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
public static Pair<Parser, Lexer> parsePHP(String filePath) {
AntlrCaseInsensitiveFileStream input;
try {
input = new AntlrCaseInsensitiveFileStream(filePath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
PHPLexer lexer = new PHPLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PHPParser parser = new InterruptablePHPParser(tokens, filePath);
/* turn on prediction mode to speed up parsing */
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
Pair<Parser, Lexer> retval = new Pair<Parser, Lexer>(parser, lexer);
return retval;
}
示例4: recover
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
@Override
public void recover(Parser recognizer, RecognitionException e) {
for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
context.exception = e;
}
if (PredictionMode.LL.equals(recognizer.getInterpreter().getPredictionMode())) {
if (e instanceof NoViableAltException) {
this.reportNoViableAlternative(recognizer, (NoViableAltException) e);
} else if (e instanceof InputMismatchException) {
this.reportInputMismatch(recognizer, (InputMismatchException) e);
} else if (e instanceof FailedPredicateException) {
this.reportFailedPredicate(recognizer, (FailedPredicateException) e);
}
}
throw new ParseCancellationException(e);
}
示例5: parse
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
private static Start parse(final CommonTokenStream tokens, final BoaParser parser, final BoaErrorListener parserErrorListener) {
parser.setBuildParseTree(false);
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
try {
return parser.start().ast;
} catch (final ParseCancellationException e) {
// fall-back to LL mode parsing if SLL fails
tokens.reset();
parser.reset();
parser.removeErrorListeners();
parser.addErrorListener(parserErrorListener);
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
return parser.start().ast;
}
}
示例6: parseLL
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
/**
* "Second phase" parsing attempt. Will accept any valid HyperTalk script entry, but is less performant for inputs
* utilizing certain parts of the grammar.
*
* @param compilationUnit The unit of work to compile/parse. Represents the grammar's start symbol that should be
* used.
* @param scriptText A plaintext representation of the HyperTalk script to parse
* @return The root of the abstract syntax tree associated with the given compilation unit (i.e., {@link Script}).
* @throws HtSyntaxException Thrown if an error occurs while parsing the script.
*/
static Object parseLL(CompilationUnit compilationUnit, String scriptText) throws HtSyntaxException {
HyperTalkErrorListener errors = new HyperTalkErrorListener();
HyperTalkLexer lexer = new HyperTalkLexer(new CaseInsensitiveInputStream(scriptText));
CommonTokenStream tokens = new CommonTokenStream(lexer);
HyperTalkParser parser = new HyperTalkParser(tokens);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
parser.removeErrorListeners(); // don't log to console
parser.addErrorListener(errors);
try {
ParseTree tree = compilationUnit.getParseTree(parser);
if (!errors.errors.isEmpty()) {
throw errors.errors.get(0);
}
return new HyperTalkTreeVisitor().visit(tree);
} catch (RecognitionException e) {
throw new HtSyntaxException(e);
}
}
示例7: parseSLL
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
/**
* "First phase" parsing attempt. Provides better performance than {@link #parseLL(CompilationUnit, String)}, but
* will erroneously report syntax errors when parsing script text utilizing certain parts of the grammar.
*
* @param compilationUnit The unit of work to compile/parse. Represents the grammar's start symbol that should be
* used.
* @param scriptText A plaintext representation of the HyperTalk script to parse
* @return The root of the abstract syntax tree associated with the given compilation unit (i.e., {@link Script}),
* or null if parsing fails.
*/
static Object parseSLL(CompilationUnit compilationUnit, String scriptText) {
HyperTalkLexer lexer = new HyperTalkLexer(new CaseInsensitiveInputStream(scriptText));
CommonTokenStream tokens = new CommonTokenStream(lexer);
HyperTalkParser parser = new HyperTalkParser(tokens);
parser.setErrorHandler(new BailErrorStrategy());
parser.removeErrorListeners();
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
try {
ParseTree tree = compilationUnit.getParseTree(parser);
return new HyperTalkTreeVisitor().visit(tree);
} catch (ParseCancellationException e) {
return null;
}
}
示例8: getParser
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
@NonNull
public CodeCompletionGoParser getParser(@NonNull TokenStream input) {
CodeCompletionGoParser parser = createParser(input);
parser.removeErrorListeners();
parser.setBuildParseTree(false);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
parser.getInterpreter().force_global_context = false;
parser.getInterpreter().always_try_local_context = true;
parser.setCheckPackageNames(false);
parser.setPackageNames(Collections.<String>emptyList());
return parser;
}
示例9: getParser
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
public static <P extends Parser> P getParser(Class<? extends Lexer> lexerClass, Class<P> parserClass, String source) {
Lexer lexer = getLexer(lexerClass, source);
TokenStream tokens = new CommonTokenStream(lexer);
P parser;
try {
parser = parserClass.getConstructor(TokenStream.class).newInstance(tokens);
} catch (Exception e) {
throw new IllegalArgumentException("couldn't invoke parser constructor", e);
}
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
parser.removeErrorListeners(); // don't spit to stderr
parser.addErrorListener(new DiagnosticErrorListener());
parser.addErrorListener(new AntlrFailureListener());
return parser;
}
示例10: invokeParser
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction, ParsingOptions parsingOptions)
{
try {
SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokenStream);
parser.addParseListener(new PostProcessor(Arrays.asList(parser.getRuleNames())));
lexer.removeErrorListeners();
lexer.addErrorListener(ERROR_LISTENER);
parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
}
catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
tokenStream.reset(); // rewind input stream
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(parser);
}
return new AstBuilder(parsingOptions).visit(tree);
}
catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}
示例11: parseTypeCalculation
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
private static ParserRuleContext parseTypeCalculation(String calculation)
{
TypeCalculationLexer lexer = new TypeCalculationLexer(new CaseInsensitiveStream(new ANTLRInputStream(calculation)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
TypeCalculationParser parser = new TypeCalculationParser(tokenStream);
lexer.removeErrorListeners();
lexer.addErrorListener(ERROR_LISTENER);
parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parser.typeCalculation();
}
catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
tokenStream.reset(); // rewind input stream
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parser.typeCalculation();
}
return tree;
}
示例12: doParse
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
private static Expressie doParse(final String expressieString, final Context context) {
// TEAMBRP-2535 de expressie syntax kan niet goed omgaan met een string waarachter ongedefinieerde velden staan.
// Door haakjes toe te voegen zal dan een fout gesignaleerd worden, aangezien de content dan niet meer precies
// gematched kan worden.
final String expressieStringMetHaakjes = String.format("(%s)", expressieString);
// Parsing gebeurt met een door ANTLR gegenereerde visitor. Om die te kunnen gebruiken, moet een treintje
// opgetuigd worden (String->CharStream->Lexer->TokenStream).
final CharStream cs = CharStreams.fromString(expressieStringMetHaakjes);
final ParserErrorListener parserErrorListener = new ParserErrorListener();
final BRPExpressietaalLexer lexer = new BRPExpressietaalLexer(cs);
// verwijdert de interne listener van de lexer die naar system/out/err wegschrijft
// expressies als ***bla*** logt bijvoorbeeld unrecognized token errors naar system/out
// in plaats hiervan neem een eigen error listener op.
lexer.removeErrorListeners();
lexer.addErrorListener(parserErrorListener);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final BRPExpressietaalParser parser = new BRPExpressietaalParser(tokens);
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
// Verwijder bestaande (default) error listeners en voeg de eigen error listener toe.
parser.removeErrorListeners();
parser.addErrorListener(parserErrorListener);
// Maak de parse tree. Hier gebeurt het feitelijke parsing.
final BRPExpressietaalParser.Brp_expressieContext tree = parser.brp_expressie();
// Maak een visitor voor parsing.
final ExpressieVisitor visitor = new ExpressieVisitor(context);
// De visitor zet een parse tree om in een Expressie. Tenzij er een fout optreedt.
return visitor.visit(tree);
}
示例13: parseTerm
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
public static Term parseTerm(String s, boolean trySLL) {
CoqFTParser p = new CoqFTParser(s);
if(trySLL) {
p.getInterpreter().setPredictionMode(PredictionMode.SLL);
p.setErrorHandler(new BailErrorStrategy());
try {
return p.parseTerm();
} catch(ParseCancellationException | CoqSyntaxException e) {
p = new CoqFTParser(s);
}
}
return p.parseTerm();
}
示例14: parseTactic
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
public static Tactic parseTactic(String s, boolean trySLL) {
CoqFTParser p = new CoqFTParser(s);
if(trySLL) {
p.getInterpreter().setPredictionMode(PredictionMode.SLL);
p.setErrorHandler(new BailErrorStrategy());
try {
return p.parseTactic();
} catch(ParseCancellationException | CoqSyntaxException e) {
p = new CoqFTParser(s);
}
}
return p.parseTactic();
}
示例15: addVerboseErrorListener
import org.antlr.v4.runtime.atn.PredictionMode; //导入依赖的package包/类
public static VerboseErrorListener addVerboseErrorListener(FusionTablesSqlParser parser)
{
VerboseErrorListener verbose = new VerboseErrorListener();
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
parser.removeErrorListeners();
parser.addErrorListener(verbose);
return verbose;
}