Search in sources :

Example 1 with DebugBatfishException

use of org.batfish.common.DebugBatfishException in project batfish by batfish.

the class BatfishLexerErrorListener method syntaxError.

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
    if (!_settings.getDisableUnrecognized()) {
        return;
    }
    StringBuilder sb = new StringBuilder();
    BatfishParser parser = _combinedParser.getParser();
    BatfishLexer lexer = _combinedParser.getLexer();
    List<String> ruleNames = Arrays.asList(parser.getRuleNames());
    ParserRuleContext ctx = parser.getContext();
    String ruleStack = ctx.toString(ruleNames);
    sb.append("lexer: " + _grammarName + ": line " + line + ":" + charPositionInLine + ": " + msg + "\n");
    sb.append("Current rule stack: '" + ruleStack + "'.\n");
    if (ctx.getStart() != null) {
        sb.append("Current rule starts at: line: " + ctx.getStart().getLine() + ", col " + ctx.getStart().getCharPositionInLine() + "\n");
    }
    sb.append("Parse tree for current rule:\n");
    sb.append(ParseTreePrettyPrinter.print(ctx, _combinedParser) + "\n");
    sb.append("Lexer mode: " + lexer.getMode() + "\n");
    sb.append("Lexer state variables:\n");
    sb.append(lexer.printStateVariables());
    // collect context from text
    String text = _combinedParser.getInput();
    String[] lines = text.split("\n", -1);
    int errorLineIndex = line - 1;
    int errorContextStartLine = Math.max(errorLineIndex - _settings.getMaxParserContextLines(), 0);
    int errorContextEndLine = Math.min(errorLineIndex + _settings.getMaxParserContextLines(), lines.length);
    sb.append("Error context lines:\n");
    for (int i = errorContextStartLine; i < errorLineIndex; i++) {
        sb.append(String.format("%-11s%s\n", "   " + (i + 1) + ":", lines[i]));
    }
    sb.append(String.format("%-11s%s\n", ">>>" + (errorLineIndex + 1) + ":", lines[errorLineIndex]));
    for (int i = errorLineIndex + 1; i <= errorContextEndLine && i < lines.length; i++) {
        sb.append(String.format("%-11s%s\n", "   " + (i + 1) + ":", lines[i]));
    }
    String error = sb.toString();
    if (_settings.getThrowOnLexerError()) {
        throw new DebugBatfishException("\n" + error);
    } else {
        _combinedParser.getErrors().add(error);
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DebugBatfishException(org.batfish.common.DebugBatfishException)

Example 2 with DebugBatfishException

use of org.batfish.common.DebugBatfishException in project batfish by batfish.

the class BatfishParserErrorListener method syntaxError.

public void syntaxError(ParserRuleContext ctx, Object offendingSymbol, int line, int charPositionInLine, String msg) {
    if (!_settings.getDisableUnrecognized()) {
        return;
    }
    BatfishParser parser = _combinedParser.getParser();
    List<String> ruleNames = Arrays.asList(parser.getRuleNames());
    String ruleStack = ctx.toString(ruleNames);
    List<Token> tokens = _combinedParser.getTokens().getTokens();
    int startTokenIndex = parser.getInputStream().index();
    StringBuilder sb = new StringBuilder();
    sb.append("parser: " + _grammarName + ": line " + line + ":" + charPositionInLine + ": " + msg + "\n");
    Token offendingToken = (Token) offendingSymbol;
    String offendingTokenText = printToken(offendingToken);
    sb.append("Offending Token: " + offendingTokenText + "\n");
    sb.append("Error parsing top (leftmost) parser rule in stack: '" + ruleStack + "'.\n");
    String ctxParseTree = ParseTreePrettyPrinter.print(ctx, _combinedParser);
    sb.append("Parse tree of current rule:\n");
    sb.append(ctxParseTree);
    sb.append("\n");
    sb.append("Unconsumed tokens:\n");
    int endTokenIndex = tokens.size();
    for (int i = startTokenIndex; i < endTokenIndex; i++) {
        Token token = tokens.get(i);
        String tokenText = printToken(token);
        sb.append(tokenText + "\n");
    }
    int lookbackIndex = Math.max(0, startTokenIndex - _settings.getMaxParserContextTokens());
    if (lookbackIndex < startTokenIndex) {
        int numLookbackTokens = startTokenIndex - lookbackIndex;
        sb.append("Previous " + numLookbackTokens + " tokens:\n");
        for (int i = lookbackIndex; i < startTokenIndex; i++) {
            Token lookbackToken = tokens.get(i);
            String tokenText = printToken(lookbackToken);
            sb.append(tokenText + "\n");
        }
    }
    if (offendingToken.getType() == Token.EOF) {
        sb.append("Lexer mode at EOF: " + _combinedParser.getLexer().getMode() + "\n");
    }
    String stateInfo = parser.getStateInfo();
    if (stateInfo != null) {
        sb.append("Parser state info:\n" + stateInfo + "\n");
    }
    // collect context from text
    String text = _combinedParser.getInput();
    String[] lines = text.split("\n", -1);
    int errorLineIndex = offendingToken.getLine() - 1;
    int errorContextStartLine = Math.max(errorLineIndex - _settings.getMaxParserContextLines(), 0);
    sb.append("Error context lines:\n");
    for (int i = errorContextStartLine; i < errorLineIndex; i++) {
        sb.append(String.format("%-11s%s\n", "   " + (i + 1) + ":", lines[i]));
    }
    sb.append(String.format("%-11s%s\n", ">>>" + (errorLineIndex + 1) + ":", lines[errorLineIndex]));
    int errorContextEndLine = Math.min(errorLineIndex + _settings.getMaxParserContextLines(), lines.length - 1);
    for (int i = errorLineIndex + 1; i <= errorContextEndLine; i++) {
        sb.append(String.format("%-11s%s\n", "   " + (i + 1) + ":", lines[i]));
    }
    String error = sb.toString();
    if (_settings.getThrowOnParserError()) {
        throw new DebugBatfishException("\n" + error);
    } else {
        _combinedParser.getErrors().add(error);
    }
}
Also used : DebugBatfishException(org.batfish.common.DebugBatfishException) Token(org.antlr.v4.runtime.Token)

Aggregations

DebugBatfishException (org.batfish.common.DebugBatfishException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 Token (org.antlr.v4.runtime.Token)1