use of org.antlr.v4.runtime.Recognizer in project kripton by xcesco.
the class ContentUriChecker method preparePath.
private Pair<ParserRuleContext, CommonTokenStream> preparePath(final String input) {
UriLexer lexer = new UriLexer(CharStreams.fromString(input));
CommonTokenStream tokens = new CommonTokenStream(lexer);
UriParser parser = new UriParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new ContentUriBaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
AssertKripton.assertTrue(false, "unespected char at pos %s of SQL '%s'", charPositionInLine, input);
}
});
ParserRuleContext context = parser.path();
return new Pair<>(context, tokens);
}
use of org.antlr.v4.runtime.Recognizer in project kripton by xcesco.
the class ContentUriChecker method prepareUri.
private Pair<ParserRuleContext, CommonTokenStream> prepareUri(final String input) {
UriLexer lexer = new UriLexer(CharStreams.fromString(input));
CommonTokenStream tokens = new CommonTokenStream(lexer);
UriParser parser = new UriParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new ContentUriBaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
AssertKripton.assertTrue(false, "unespected char at pos %s of URI '%s'", charPositionInLine, input);
}
@Override
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
AssertKripton.assertTrue(false, "ambiguity syntax at pos %s of URI '%s'", startIndex, input);
}
@Override
public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
AssertKripton.assertTrue(false, "error at pos %s of URI '%s'", startIndex, input);
}
@Override
public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
AssertKripton.assertTrue(false, "context eror at pos %s of URI '%s'", startIndex, input);
}
});
ParserRuleContext context = parser.uri();
return new Pair<>(context, tokens);
}
use of org.antlr.v4.runtime.Recognizer in project vespa by vespa-engine.
the class ProgramParser method prepareParser.
private yqlplusParser prepareParser(String programName, CharStream input) {
yqlplusLexer lexer = new yqlplusLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
}
});
TokenStream tokens = new CommonTokenStream(lexer);
yqlplusParser parser = new yqlplusParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
}
});
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
return parser;
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by tunnelvisionlabs.
the class RuleDependencyChecker method getRuleVersions.
private static int[] getRuleVersions(Class<? extends Recognizer<?, ?>> recognizerClass, String[] ruleNames) {
int[] versions = new int[ruleNames.length];
Field[] fields = recognizerClass.getFields();
for (Field field : fields) {
boolean isStatic = (field.getModifiers() & Modifier.STATIC) != 0;
boolean isInteger = field.getType() == Integer.TYPE;
if (isStatic && isInteger && field.getName().startsWith("RULE_")) {
try {
String name = field.getName().substring("RULE_".length());
if (name.isEmpty() || !Character.isLowerCase(name.charAt(0))) {
continue;
}
int index = field.getInt(null);
if (index < 0 || index >= versions.length) {
Object[] params = { index, field.getName(), recognizerClass.getSimpleName() };
LOGGER.log(Level.WARNING, "Rule index {0} for rule ''{1}'' out of bounds for recognizer {2}.", params);
continue;
}
Method ruleMethod = getRuleMethod(recognizerClass, name);
if (ruleMethod == null) {
Object[] params = { name, recognizerClass.getSimpleName() };
LOGGER.log(Level.WARNING, "Could not find rule method for rule ''{0}'' in recognizer {1}.", params);
continue;
}
RuleVersion ruleVersion = ruleMethod.getAnnotation(RuleVersion.class);
int version = ruleVersion != null ? ruleVersion.value() : 0;
versions[index] = version;
} catch (IllegalArgumentException ex) {
LOGGER.log(Level.WARNING, null, ex);
} catch (IllegalAccessException ex) {
LOGGER.log(Level.WARNING, null, ex);
}
}
}
return versions;
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by tunnelvisionlabs.
the class DefaultErrorStrategy method reportUnwantedToken.
/**
* This method is called to report a syntax error which requires the removal
* of a token from the input stream. At the time this method is called, the
* erroneous symbol is current {@code LT(1)} symbol and has not yet been
* removed from the input stream. When this method returns,
* {@code recognizer} is in error recovery mode.
*
* <p>This method is called when {@link #singleTokenDeletion} identifies
* single-token deletion as a viable recovery strategy for a mismatched
* input error.</p>
*
* <p>The default implementation simply returns if the handler is already in
* error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
* enter error recovery mode, followed by calling
* {@link Parser#notifyErrorListeners}.</p>
*
* @param recognizer the parser instance
*/
protected void reportUnwantedToken(@NotNull Parser recognizer) {
if (inErrorRecoveryMode(recognizer)) {
return;
}
beginErrorCondition(recognizer);
Token t = recognizer.getCurrentToken();
String tokenName = getTokenErrorDisplay(t);
IntervalSet expecting = getExpectedTokens(recognizer);
String msg = "extraneous input " + tokenName + " expecting " + expecting.toString(recognizer.getVocabulary());
recognizer.notifyErrorListeners(t, msg, null);
}
Aggregations