use of org.antlr.v4.runtime.Recognizer in project sts4 by spring-projects.
the class AntlrParser method parse.
@Override
public ParseResults parse(String text) {
ArrayList<Problem> syntaxErrors = new ArrayList<>();
ArrayList<Problem> problems = new ArrayList<>();
ArrayList<PropertiesAst.Node> astNodes = new ArrayList<>();
JavaPropertiesLexer lexer = new JavaPropertiesLexer(new ANTLRInputStream(text.toCharArray(), text.length()));
CommonTokenStream tokens = new CommonTokenStream(lexer);
JavaPropertiesParser parser = new JavaPropertiesParser(tokens);
// To avoid printing parse errors in the console
parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
// Add listener to collect various parser errors
parser.addErrorListener(new ANTLRErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
syntaxErrors.add(createProblem(msg, ProblemCodes.PROPERTIES_SYNTAX_ERROR, (Token) offendingSymbol));
}
@Override
public void reportAmbiguity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
problems.add(createProblem("Ambiguity detected!", ProblemCodes.PROPERTIES_AMBIGUITY_ERROR, recognizer.getCurrentToken()));
}
@Override
public void reportAttemptingFullContext(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
problems.add(createProblem("Full-Context attempt detected!", ProblemCodes.PROPERTIES_FULL_CONTEXT_ERROR, recognizer.getCurrentToken()));
}
@Override
public void reportContextSensitivity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
problems.add(createProblem("Context sensitivity detected!", ProblemCodes.PROPERTIES_CONTEXT_SENSITIVITY_ERROR, recognizer.getCurrentToken()));
}
});
// Add listener to the parse tree to collect AST nodes
parser.addParseListener(new JavaPropertiesBaseListener() {
private Key key = null;
private Value value = null;
@Override
public void exitPropertyLine(PropertyLineContext ctx) {
KeyValuePair pair = new KeyValuePair(ctx, key, value);
key.parent = value.parent = pair;
astNodes.add(pair);
key = null;
value = null;
}
@Override
public void exitCommentLine(CommentLineContext ctx) {
astNodes.add(new Comment(ctx));
}
@Override
public void exitKey(KeyContext ctx) {
key = new Key(ctx);
}
@Override
public void exitSeparatorAndValue(SeparatorAndValueContext ctx) {
value = new Value(ctx);
}
@Override
public void exitEmptyLine(EmptyLineContext ctx) {
astNodes.add(new EmptyLine(ctx));
}
});
parser.parse();
// Collect and return parse results
return new ParseResults(new PropertiesAst(ImmutableList.copyOf(astNodes)), ImmutableList.copyOf(syntaxErrors), ImmutableList.copyOf(problems));
}
use of org.antlr.v4.runtime.Recognizer in project elasticsearch by elastic.
the class ParserErrorStrategy method recover.
@Override
public void recover(final Parser recognizer, final RecognitionException re) {
final Token token = re.getOffendingToken();
String message;
if (token == null) {
message = "no parse token found.";
} else if (re instanceof InputMismatchException) {
message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
} else if (re instanceof NoViableAltException) {
if (token.getType() == PainlessParser.EOF) {
message = "unexpected end of script.";
} else {
message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
}
} else {
message = "unexpected token near [" + getTokenErrorDisplay(token) + "].";
}
Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex());
throw location.createError(new IllegalArgumentException(message, re));
}
use of org.antlr.v4.runtime.Recognizer in project elasticsearch by elastic.
the class ParserErrorStrategy method recoverInline.
@Override
public Token recoverInline(final Parser recognizer) throws RecognitionException {
final Token token = recognizer.getCurrentToken();
final String message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
Location location = new Location(sourceName, token.getStartIndex());
throw location.createError(new IllegalArgumentException(message));
}
use of org.antlr.v4.runtime.Recognizer in project elasticsearch by elastic.
the class Walker method setupPicky.
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);
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by antlr.
the class BaseSwiftTest method execTest.
private String execTest() {
try {
String exec = tmpdir + "/" + EXEC_NAME;
String[] args = //new File(tmpdir, "input").getAbsolutePath()
new String[] { exec, "input" };
ProcessBuilder pb = new ProcessBuilder(args);
pb.directory(new File(tmpdir));
Process p = pb.start();
StreamVacuum stdoutVacuum = new StreamVacuum(p.getInputStream());
StreamVacuum stderrVacuum = new StreamVacuum(p.getErrorStream());
stdoutVacuum.start();
stderrVacuum.start();
p.waitFor();
stdoutVacuum.join();
stderrVacuum.join();
String output = stdoutVacuum.toString();
if (output.length() == 0) {
output = null;
}
if (stderrVacuum.toString().length() > 0) {
this.stderrDuringParse = stderrVacuum.toString();
}
return output;
} catch (Exception e) {
System.err.println("can't exec recognizer");
e.printStackTrace(System.err);
}
return null;
}
Aggregations