|
| 1 | +package graphql.parser; |
| 2 | + |
| 3 | +import graphql.language.SourceLocation; |
| 4 | +import org.antlr.v4.runtime.BailErrorStrategy; |
| 5 | +import org.antlr.v4.runtime.Parser; |
| 6 | +import org.antlr.v4.runtime.RecognitionException; |
| 7 | +import org.antlr.v4.runtime.Token; |
| 8 | +import org.antlr.v4.runtime.misc.ParseCancellationException; |
| 9 | + |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.StringReader; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class ExtendedBailStrategy extends BailErrorStrategy { |
| 17 | + private final String input; |
| 18 | + private final String sourceName; |
| 19 | + |
| 20 | + public ExtendedBailStrategy(String input, String sourceName) { |
| 21 | + this.input = input; |
| 22 | + this.sourceName = sourceName; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void recover(Parser recognizer, RecognitionException e) { |
| 27 | + try { |
| 28 | + super.recover(recognizer, e); |
| 29 | + } catch (ParseCancellationException parseException) { |
| 30 | + throw mkException(recognizer, e); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Token recoverInline(Parser recognizer) throws RecognitionException { |
| 36 | + try { |
| 37 | + return super.recoverInline(recognizer); |
| 38 | + } catch (ParseCancellationException parseException) { |
| 39 | + throw mkException(recognizer, null); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + InvalidSyntaxException mkMoreTokensException(Token token) { |
| 44 | + SourceLocation sourceLocation = new SourceLocation(token.getLine(), token.getCharPositionInLine()); |
| 45 | + String sourcePreview = mkPreview(token.getLine()); |
| 46 | + return new InvalidSyntaxException(sourceLocation, |
| 47 | + "There are more tokens in the query that have not been consumed", |
| 48 | + sourcePreview, token.getText(), null); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) { |
| 53 | + String sourcePreview = null; |
| 54 | + String offendingToken = null; |
| 55 | + SourceLocation sourceLocation = null; |
| 56 | + Token currentToken = recognizer.getCurrentToken(); |
| 57 | + if (currentToken != null) { |
| 58 | + int line = currentToken.getLine(); |
| 59 | + int column = currentToken.getCharPositionInLine(); |
| 60 | + offendingToken = currentToken.getText(); |
| 61 | + sourcePreview = mkPreview(line); |
| 62 | + sourceLocation = new SourceLocation(line, column, sourceName); |
| 63 | + } |
| 64 | + return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause); |
| 65 | + } |
| 66 | + |
| 67 | + /* grabs 3 lines before and after the syntax error */ |
| 68 | + private String mkPreview(int line) { |
| 69 | + StringBuilder sb = new StringBuilder(); |
| 70 | + BufferedReader reader = new BufferedReader(new StringReader(input)); |
| 71 | + int startLine = line - 3; |
| 72 | + int endLine = line + 3; |
| 73 | + try { |
| 74 | + List<String> lines = readAllLines(reader); |
| 75 | + for (int i = 0; i < lines.size(); i++) { |
| 76 | + if (i >= startLine && i <= endLine) { |
| 77 | + sb.append(lines.get(i)).append('\n'); |
| 78 | + } |
| 79 | + } |
| 80 | + } catch (IOException ignored) { |
| 81 | + // this cant happen - its in memory |
| 82 | + } |
| 83 | + return sb.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + private List<String> readAllLines(BufferedReader reader) throws IOException { |
| 87 | + List<String> lines = new ArrayList<>(); |
| 88 | + String ln; |
| 89 | + while ((ln = reader.readLine()) != null) { |
| 90 | + lines.add(ln); |
| 91 | + } |
| 92 | + reader.close(); |
| 93 | + return lines; |
| 94 | + } |
| 95 | +} |
0 commit comments