|
21 | 21 |
|
22 | 22 | package com.vaticle.typeql.lang.parser; |
23 | 23 |
|
24 | | -import org.antlr.v4.runtime.BaseErrorListener; |
25 | | -import org.antlr.v4.runtime.RecognitionException; |
26 | | -import org.antlr.v4.runtime.Recognizer; |
27 | | - |
28 | 24 | import java.util.ArrayList; |
29 | 25 | import java.util.List; |
| 26 | +import java.util.Objects; |
30 | 27 | import java.util.stream.Collectors; |
31 | | - |
| 28 | +import org.antlr.v4.runtime.BaseErrorListener; |
| 29 | +import org.antlr.v4.runtime.RecognitionException; |
| 30 | +import org.antlr.v4.runtime.Recognizer; |
32 | 31 | import static com.vaticle.typedb.common.collection.Collections.list; |
| 32 | +import static com.vaticle.typeql.lang.common.exception.ErrorMessage.SYNTAX_ERROR_DETAILED; |
| 33 | +import static com.vaticle.typeql.lang.common.exception.ErrorMessage.SYNTAX_ERROR_NO_DETAILS; |
33 | 34 |
|
34 | 35 | /** |
35 | | - * ANTLR error listener that listens for syntax errors. |
36 | | - * When a syntax error occurs, it is recorded. Call {@link ErrorListener#hasErrors()} to see if there were errors. |
| 36 | + * ANTLR error listener that listens for syntax errors, and record them. |
37 | 37 | * View the errors with {@link ErrorListener#toString()}. |
38 | 38 | */ |
39 | 39 | public class ErrorListener extends BaseErrorListener { |
40 | 40 |
|
41 | | - private final List<String> query; |
| 41 | + private final List<String> queryLines; |
42 | 42 | private final List<SyntaxError> errors = new ArrayList<>(); |
43 | 43 |
|
44 | | - private ErrorListener(List<String> query) { |
45 | | - this.query = query; |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * Create a {@link ErrorListener} without a reference to a query string. |
50 | | - * This will have limited error-reporting abilities, but is necessary when dealing with very large queries |
51 | | - * that should not be held in memory all at once. |
52 | | - */ |
53 | | - public static ErrorListener withoutQueryString() { |
54 | | - return new ErrorListener(null); |
| 44 | + private ErrorListener(List<String> queryLines) { |
| 45 | + this.queryLines = queryLines; |
55 | 46 | } |
56 | 47 |
|
57 | 48 | public static ErrorListener of(String query) { |
58 | | - List<String> queryList = list(query.split("\n")); |
59 | | - return new ErrorListener(queryList); |
| 49 | + List<String> queryLines = list(query.split("\n")); |
| 50 | + return new ErrorListener(queryLines); |
60 | 51 | } |
61 | 52 |
|
62 | 53 | @Override |
63 | 54 | public void syntaxError( |
64 | | - Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, |
65 | | - RecognitionException e) { |
66 | | - |
67 | | - if (query == null) { |
68 | | - errors.add(new SyntaxError(null, line, 0, msg)); |
69 | | - } else { |
70 | | - errors.add(new SyntaxError(query.get(line - 1), line, charPositionInLine, msg)); |
71 | | - } |
| 55 | + Recognizer<?, ?> recognizer, Object offendingSymbol, |
| 56 | + int line, int charPositionInLine, String msg, RecognitionException e |
| 57 | + ) { |
| 58 | + errors.add(new SyntaxError(queryLines.get(line - 1), line, charPositionInLine, msg)); |
72 | 59 | } |
73 | 60 |
|
74 | | - public boolean hasErrors() { |
75 | | - return !errors.isEmpty(); |
| 61 | + @Override |
| 62 | + public String toString() { |
| 63 | + return errors.stream().map(SyntaxError::toString).collect(Collectors.joining("\n\n")); |
76 | 64 | } |
77 | 65 |
|
78 | | - public void clearErrors() { |
79 | | - errors.clear(); |
80 | | - } |
| 66 | + private static class SyntaxError { |
81 | 67 |
|
82 | | - @Override |
83 | | - public String toString() { |
84 | | - return errors.stream().map(SyntaxError::toString).collect(Collectors.joining("\n")); |
| 68 | + private final String queryLine; |
| 69 | + private final int line; |
| 70 | + private final int charPositionInLine; |
| 71 | + private final String msg; |
| 72 | + private final int hash; |
| 73 | + |
| 74 | + public SyntaxError(String queryLine, int line, int charPositionInLine, String msg) { |
| 75 | + if (msg == null) throw new NullPointerException("Null msg"); |
| 76 | + this.queryLine = queryLine; |
| 77 | + this.line = line; |
| 78 | + this.charPositionInLine = charPositionInLine; |
| 79 | + this.msg = msg; |
| 80 | + this.hash = Objects.hash(this.queryLine, this.line, this.charPositionInLine, this.msg); |
| 81 | + } |
| 82 | + |
| 83 | + private String spaces(int len) { |
| 84 | + final char ch = ' '; |
| 85 | + char[] output = new char[len]; |
| 86 | + for (int i = len - 1; i >= 0; i--) { |
| 87 | + output[i] = ch; |
| 88 | + } |
| 89 | + return new String(output); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + if (queryLine == null) { |
| 95 | + return SYNTAX_ERROR_NO_DETAILS.message(line, msg); |
| 96 | + } else { |
| 97 | + // Error message appearance: |
| 98 | + // |
| 99 | + // syntax error at line 1: |
| 100 | + // match $ |
| 101 | + // ^ |
| 102 | + // blah blah antlr blah |
| 103 | + String pointer = spaces(charPositionInLine) + "^"; |
| 104 | + return SYNTAX_ERROR_DETAILED.message(line, queryLine, pointer, msg); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean equals(Object o) { |
| 110 | + if (o == this) return true; |
| 111 | + if (!(o instanceof SyntaxError)) return false; |
| 112 | + SyntaxError that = (SyntaxError) o; |
| 113 | + return (Objects.equals(this.queryLine, that.queryLine) && |
| 114 | + this.line == that.line && |
| 115 | + this.charPositionInLine == that.charPositionInLine && |
| 116 | + this.msg.equals(that.msg)); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public int hashCode() { |
| 121 | + return hash; |
| 122 | + } |
85 | 123 | } |
86 | 124 | } |
87 | 125 |
|
0 commit comments