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);
}
java类org.antlr.v4.runtime.atn.PredictionMode的实例源码
Walker.java 文件源码
项目:elasticsearch_my
阅读 22
收藏 0
点赞 0
评论 0
ClarpseGoCompiler.java 文件源码
项目:clarpse
阅读 20
收藏 0
点赞 0
评论 0
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();
}
}
}
ParseUtils.java 文件源码
项目:mdetect
阅读 20
收藏 0
点赞 0
评论 0
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;
}
DescriptiveErrorStrategy.java 文件源码
项目:groovy
阅读 23
收藏 0
点赞 0
评论 0
@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);
}
BoaCompiler.java 文件源码
项目:compiler
阅读 15
收藏 0
点赞 0
评论 0
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;
}
}
TwoPhaseParser.java 文件源码
项目:hypertalk-java
阅读 18
收藏 0
点赞 0
评论 0
/**
* "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);
}
}
TwoPhaseParser.java 文件源码
项目:hypertalk-java
阅读 17
收藏 0
点赞 0
评论 0
/**
* "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;
}
}
ParserFactory.java 文件源码
项目:goworks
阅读 27
收藏 0
点赞 0
评论 0
@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;
}
ParserUtils.java 文件源码
项目:ethereumj
阅读 25
收藏 0
点赞 0
评论 0
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;
}
ParserUtils.java 文件源码
项目:antlr-denter
阅读 26
收藏 0
点赞 0
评论 0
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;
}
SqlParser.java 文件源码
项目:rainbow
阅读 24
收藏 0
点赞 0
评论 0
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)");
}
}
TypeCalculation.java 文件源码
项目:rainbow
阅读 20
收藏 0
点赞 0
评论 0
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;
}
ExpressieParser.java 文件源码
项目:OperatieBRP
阅读 17
收藏 0
点赞 0
评论 0
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);
}
CoqFTParser.java 文件源码
项目:exterminator
阅读 17
收藏 0
点赞 0
评论 0
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();
}
CoqFTParser.java 文件源码
项目:exterminator
阅读 16
收藏 0
点赞 0
评论 0
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();
}
Util.java 文件源码
项目:ftc
阅读 21
收藏 0
点赞 0
评论 0
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;
}
ProgramParser.java 文件源码
项目:yql-plus
阅读 17
收藏 0
点赞 0
评论 0
private ProgramContext parseProgram(yqlplusParser parser) throws RecognitionException {
try {
return parser.program();
} catch (RecognitionException e) {
//Retry parsing using full LL mode
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
return parser.program();
}
}
AnswerSetsParser.java 文件源码
项目:Alpha
阅读 15
收藏 0
点赞 0
评论 0
public static Set<AnswerSet> parse(CharStream stream) throws IOException {
final ASPCore2Parser parser = new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(stream)));
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
return VISITOR.translate(parser.answer_sets());
}
ProgramParser.java 文件源码
项目:vespa
阅读 16
收藏 0
点赞 0
评论 0
private ProgramContext parseProgram(yqlplusParser parser) throws RecognitionException {
try {
return parser.program();
} catch (RecognitionException e) {
//Retry parsing using full LL mode
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
return parser.program();
}
}
KsqlParser.java 文件源码
项目:ksql
阅读 16
收藏 0
点赞 0
评论 0
private ParserRuleContext getParseTree(String sql) {
SqlBaseLexer
sqlBaseLexer =
new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(sqlBaseLexer);
SqlBaseParser sqlBaseParser = new SqlBaseParser(tokenStream);
sqlBaseLexer.removeErrorListeners();
sqlBaseLexer.addErrorListener(ERROR_LISTENER);
sqlBaseParser.removeErrorListeners();
sqlBaseParser.addErrorListener(ERROR_LISTENER);
Function<SqlBaseParser, ParserRuleContext> parseFunction = SqlBaseParser::statements;
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(sqlBaseParser);
} catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
tokenStream.reset(); // rewind input stream
sqlBaseParser.reset();
sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(sqlBaseParser);
}
return tree;
}
AstBuilder.java 文件源码
项目:groovy
阅读 20
收藏 0
点赞 0
评论 0
private GroovyParserRuleContext buildCST(PredictionMode predictionMode) {
parser.getInterpreter().setPredictionMode(predictionMode);
if (PredictionMode.SLL.equals(predictionMode)) {
this.removeErrorListeners();
} else {
parser.getInputStream().seek(0);
this.addErrorListeners();
}
return parser.compilationUnit();
}
SqlParser.java 文件源码
项目:presto
阅读 23
收藏 0
点赞 0
评论 0
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction)
{
try {
SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokenStream);
parser.addParseListener(new PostProcessor());
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().visit(tree);
}
catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}
ParserTest.java 文件源码
项目:Trinity
阅读 25
收藏 0
点赞 0
评论 0
private static TrinityParser createParser(ANTLRInputStream input) throws IOException {
TrinityLexer lexer = new TrinityLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
TrinityParser parser = new TrinityParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
return parser;
}
CreoleRenderer.java 文件源码
项目:reviki
阅读 17
收藏 0
点赞 0
评论 0
/**
* Try to run a parser, resetting the input on failure.
*
* @param tokens The token stream. Consumed by the parser, and reset on
* failure.
* @param parser The parser. Reset on failure.
* @param pmode The prediction mode.
* @return A parse tree.
*/
private static Optional<ParseTree> tryParse(final CommonTokenStream tokens, final Creole parser, final PredictionMode pmode) {
parser.getInterpreter().setPredictionMode(pmode);
try {
return Optional.of((ParseTree) parser.creole());
}
catch (Exception e) {
tokens.reset();
parser.reset();
return Optional.<ParseTree> absent();
}
}
CreoleRenderer.java 文件源码
项目:reviki
阅读 17
收藏 0
点赞 0
评论 0
/**
* Render a stream of text.
*
* @param in The input stream to render.
* @param visitor The visitor to do the rendering.
* @param macros List of macros to apply.
* @param reset Whether to reset the expansion limit or not.
* @return The AST of the page, after macro expansion.
*/
private static ASTNode renderInternal(final ANTLRInputStream in, final CreoleASTBuilder visitor, final Supplier<List<Macro>> macros, final CreoleTokens lexer, final boolean reset) {
lexer.setInputStream(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Creole parser = new Creole(tokens);
// First try parsing in SLL mode. This is really fast for pages with no
// parse errors.
Optional<ParseTree> tree = tryParse(tokens, parser, PredictionMode.SLL);
if (!tree.isPresent()) {
tree = tryParse(tokens, parser, PredictionMode.LL);
}
ASTNode rendered = visitor.visit(tree.get());
// Expand macros
if (reset) {
_expansionLimit = MACRO_DEPTH_LIMIT;
}
ASTNode expanded = rendered;
if (_expansionLimit > 0) {
_expansionLimit--;
expanded = rendered.expandMacros(macros);
_expansionLimit++;
}
return expanded;
}
GrammarParserFactory.java 文件源码
项目:goworks
阅读 17
收藏 0
点赞 0
评论 0
@NonNull
public GrammarParser getParser(@NonNull TokenStream input) {
GrammarParser result = createParser(input);
result.getInterpreter().setPredictionMode(PredictionMode.LL);
result.removeErrorListeners();
result.addErrorListener(DescriptiveErrorListener.INSTANCE);
result.setBuildParseTree(false);
result.setErrorHandler(new DefaultErrorStrategy());
return result;
}
TemplateParserFactory.java 文件源码
项目:goworks
阅读 17
收藏 0
点赞 0
评论 0
@NonNull
public TemplateParser getParser(@NonNull TokenStream input) {
TemplateParser result = createParser(input);
result.getInterpreter().setPredictionMode(PredictionMode.LL);
result.removeErrorListeners();
result.addErrorListener(DescriptiveErrorListener.INSTANCE);
result.setBuildParseTree(false);
result.setErrorHandler(new DefaultErrorStrategy());
return result;
}
GrammarParserFactory.java 文件源码
项目:antlrworks2
阅读 18
收藏 0
点赞 0
评论 0
@NonNull
public GrammarParser getParser(@NonNull TokenStream input) {
GrammarParser result = createParser(input);
result.getInterpreter().setPredictionMode(PredictionMode.LL);
result.removeErrorListeners();
result.addErrorListener(DescriptiveErrorListener.INSTANCE);
result.setBuildParseTree(false);
result.setErrorHandler(new DefaultErrorStrategy());
return result;
}
TemplateParserFactory.java 文件源码
项目:antlrworks2
阅读 19
收藏 0
点赞 0
评论 0
@NonNull
public TemplateParser getParser(@NonNull TokenStream input) {
TemplateParser result = createParser(input);
result.getInterpreter().setPredictionMode(PredictionMode.LL);
result.removeErrorListeners();
result.addErrorListener(DescriptiveErrorListener.INSTANCE);
result.setBuildParseTree(false);
result.setErrorHandler(new DefaultErrorStrategy());
return result;
}
BaseParser.java 文件源码
项目:dragoman
阅读 29
收藏 0
点赞 0
评论 0
private SQLParser withStrictMode(SQLParser parser) {
// if the faster (more lenient) mode fails then retry with the slower (stricter) mode
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
return parser;
}