use of org.antlr.v4.runtime.Recognizer in project beetl2.0 by javamonkey.
the class BeetlAntlrErrorStrategy method reportMissingToken.
protected void reportMissingToken(@NotNull Parser recognizer) {
if (inErrorRecoveryMode(recognizer)) {
return;
}
beginErrorCondition(recognizer);
// Token t = recognizer.getCurrentToken();
Token t = recognizer.getTokenStream().LT(-1);
IntervalSet expecting = getExpectedTokens(recognizer);
String expect = expecting.toString(recognizer.getTokenNames());
if (expects.containsKey(expect)) {
expect = expects.get(expect);
}
if (expect.equals("'>>'")) {
expect = "'模板的占位结束符号'";
}
String tokenStr = getTokenErrorDisplay(t);
String msg = "缺少输入 " + expect + " 在 " + tokenStr + " 后面";
BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
exception.pushToken(this.getGrammarToken(t));
throw exception;
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by tunnelvisionlabs.
the class RuleDependencyProcessor method extractRuleRelations.
private RuleRelations extractRuleRelations(TypeMirror recognizer) {
String serializedATN = getSerializedATN(recognizer);
if (serializedATN == null) {
return null;
}
ATN atn = new ATNDeserializer().deserialize(serializedATN.toCharArray());
RuleRelations relations = new RuleRelations(atn.ruleToStartState.length);
for (ATNState state : atn.states) {
if (!state.epsilonOnlyTransitions) {
continue;
}
for (Transition transition : state.getTransitions()) {
if (transition.getSerializationType() != Transition.RULE) {
continue;
}
RuleTransition ruleTransition = (RuleTransition) transition;
relations.addRuleInvocation(state.ruleIndex, ruleTransition.target.ruleIndex);
}
}
return relations;
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by tunnelvisionlabs.
the class RuleDependencyProcessor method getRuleVersions.
private int[] getRuleVersions(TypeMirror recognizerClass, String[] ruleNames) {
int[] versions = new int[ruleNames.length];
List<? extends Element> elements = processingEnv.getElementUtils().getAllMembers((TypeElement) processingEnv.getTypeUtils().asElement(recognizerClass));
for (Element element : elements) {
if (element.getKind() != ElementKind.FIELD) {
continue;
}
VariableElement field = (VariableElement) element;
boolean isStatic = element.getModifiers().contains(Modifier.STATIC);
Object constantValue = field.getConstantValue();
boolean isInteger = constantValue instanceof Integer;
String name = field.getSimpleName().toString();
if (isStatic && isInteger && name.startsWith("RULE_")) {
try {
name = name.substring("RULE_".length());
if (name.isEmpty() || !Character.isLowerCase(name.charAt(0))) {
continue;
}
int index = (Integer) constantValue;
if (index < 0 || index >= versions.length) {
String message = String.format("Rule index %d for rule '%s' out of bounds for recognizer %s.", index, name, recognizerClass.toString());
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
continue;
}
if (name.indexOf(ATNSimulator.RULE_VARIANT_DELIMITER) >= 0) {
// ignore left-factored pseudo-rules
continue;
}
ExecutableElement ruleMethod = getRuleMethod(recognizerClass, name);
if (ruleMethod == null) {
String message = String.format("Could not find rule method for rule '%s' in recognizer %s.", name, recognizerClass.toString());
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
continue;
}
RuleVersion ruleVersion = ruleMethod.getAnnotation(RuleVersion.class);
int version = ruleVersion != null ? ruleVersion.value() : 0;
versions[index] = version;
} catch (IllegalArgumentException ex) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Exception occurred while validating rule dependencies.", element);
}
}
}
return versions;
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by tunnelvisionlabs.
the class BuildDependencyGenerator method getGeneratedFileList.
/**
* From T.g return a list of File objects that
* name files ANTLR will emit from T.g.
*/
public List<File> getGeneratedFileList() {
Target target = generator.getTarget();
if (target == null) {
// if the target could not be loaded, no code will be generated.
return new ArrayList<File>();
}
List<File> files = new ArrayList<File>();
// add generated recognizer; e.g., TParser.java
if (generator.getTarget().needsHeader()) {
files.add(getOutputFile(generator.getRecognizerFileName(true)));
}
files.add(getOutputFile(generator.getRecognizerFileName(false)));
// add output vocab file; e.g., T.tokens. This is always generated to
// the base output directory, which will be just . if there is no -o option
//
files.add(getOutputFile(generator.getVocabFileName()));
// are we generating a .h file?
ST headerExtST = null;
ST extST = target.getTemplates().getInstanceOf("codeFileExtension");
if (target.getTemplates().isDefined("headerFile")) {
headerExtST = target.getTemplates().getInstanceOf("headerFileExtension");
String suffix = Grammar.getGrammarTypeToFileNameSuffix(g.getType());
String fileName = g.name + suffix + headerExtST.render();
files.add(getOutputFile(fileName));
}
if (g.isCombined()) {
// add autogenerated lexer; e.g., TLexer.java TLexer.h TLexer.tokens
String suffix = Grammar.getGrammarTypeToFileNameSuffix(ANTLRParser.LEXER);
String lexer = g.name + suffix + extST.render();
files.add(getOutputFile(lexer));
String lexerTokens = g.name + suffix + CodeGenerator.VOCAB_FILE_EXTENSION;
files.add(getOutputFile(lexerTokens));
// TLexer.h
if (headerExtST != null) {
String header = g.name + suffix + headerExtST.render();
files.add(getOutputFile(header));
}
}
if (g.tool.gen_listener) {
// add generated listener; e.g., TListener.java
if (generator.getTarget().needsHeader()) {
files.add(getOutputFile(generator.getListenerFileName(true)));
}
files.add(getOutputFile(generator.getListenerFileName(false)));
// add generated base listener; e.g., TBaseListener.java
if (generator.getTarget().needsHeader()) {
files.add(getOutputFile(generator.getBaseListenerFileName(true)));
}
files.add(getOutputFile(generator.getBaseListenerFileName(false)));
}
if (g.tool.gen_visitor) {
// add generated visitor; e.g., TVisitor.java
if (generator.getTarget().needsHeader()) {
files.add(getOutputFile(generator.getVisitorFileName(true)));
}
files.add(getOutputFile(generator.getVisitorFileName(false)));
// add generated base visitor; e.g., TBaseVisitor.java
if (generator.getTarget().needsHeader()) {
files.add(getOutputFile(generator.getBaseVisitorFileName(true)));
}
files.add(getOutputFile(generator.getBaseVisitorFileName(false)));
}
// handle generated files for imported grammars
List<Grammar> imports = g.getAllImportedGrammars();
if (imports != null) {
for (Grammar g : imports) {
// File outputDir = tool.getOutputDirectory(g.fileName);
// String fname = groomQualifiedFileName(outputDir.toString(), g.getRecognizerName() + extST.render());
// files.add(new File(outputDir, fname));
files.add(getOutputFile(g.fileName));
}
}
if (files.isEmpty()) {
return null;
}
return files;
}
use of org.antlr.v4.runtime.Recognizer in project presto by prestodb.
the class ErrorHandler method syntaxError.
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String message, RecognitionException e) {
try {
Parser parser = (Parser) recognizer;
ATN atn = parser.getATN();
ATNState currentState;
Token currentToken;
RuleContext context;
if (e != null) {
currentState = atn.states.get(e.getOffendingState());
currentToken = e.getOffendingToken();
context = e.getCtx();
if (e instanceof NoViableAltException) {
currentToken = ((NoViableAltException) e).getStartToken();
}
} else {
currentState = atn.states.get(parser.getState());
currentToken = parser.getCurrentToken();
context = parser.getContext();
}
Analyzer analyzer = new Analyzer(atn, parser.getVocabulary(), specialRules, specialTokens, ignoredRules, parser.getTokenStream());
Multimap<Integer, String> candidates = analyzer.process(currentState, currentToken.getTokenIndex(), context);
// pick the candidate tokens associated largest token index processed (i.e., the path that consumed the most input)
String expected = candidates.asMap().entrySet().stream().max(Comparator.comparing(Map.Entry::getKey)).get().getValue().stream().sorted().collect(Collectors.joining(", "));
message = String.format("mismatched input '%s'. Expecting: %s", ((Token) offendingSymbol).getText(), expected);
} catch (Exception exception) {
LOG.error(exception, "Unexpected failure when handling parsing error. This is likely a bug in the implementation");
}
throw new ParsingException(message, e, line, charPositionInLine);
}
Aggregations