|
| 1 | +package de.vill.main; |
| 2 | + |
| 3 | +import de.vill.exception.ErrorCategory; |
| 4 | +import de.vill.exception.ErrorField; |
| 5 | +import de.vill.exception.ErrorReport; |
| 6 | +import de.vill.exception.ParseError; |
| 7 | +import org.antlr.v4.runtime.*; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.regex.*; |
| 11 | + |
| 12 | +public class UVLErrorListener extends BaseErrorListener { |
| 13 | + |
| 14 | + private final List<ParseError> errorList; |
| 15 | + |
| 16 | + public UVLErrorListener(List<ParseError> errorList) { |
| 17 | + this.errorList = errorList; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPosition, String message, RecognitionException e) { |
| 22 | + ErrorReport report = translateToReport(message, offendingSymbol, recognizer, line, charPosition); |
| 23 | + errorList.add(new ParseError(report)); |
| 24 | + } |
| 25 | + |
| 26 | + private ErrorReport translateToReport(String message, Object offendingSymbol, Recognizer<?, ?> recognizer, int line, int charPosition) { |
| 27 | + // Token recognition error -> LEXICAL |
| 28 | + Matcher m = Pattern.compile("token recognition error at: '(.*)'").matcher(message); |
| 29 | + if (m.find()) { |
| 30 | + String character = m.group(1); |
| 31 | + return new ErrorReport.Builder(ErrorCategory.LEXICAL, |
| 32 | + "Unexpected character '" + character + "'") |
| 33 | + .line(line).charPosition(charPosition) |
| 34 | + .reference(character) |
| 35 | + .cause("The character '" + character + "' is not supported in UVL.") |
| 36 | + .hint("Remove or replace the unsupported character.") |
| 37 | + .build(); |
| 38 | + } |
| 39 | + |
| 40 | + // Missing token -> SYNTAX |
| 41 | + m = Pattern.compile("missing '?([<>\\w]+)'? at '?(.*?)'?$").matcher(message); |
| 42 | + if (m.find()) { |
| 43 | + String missing = tokenToReadable(m.group(1)); |
| 44 | + String found = tokenToReadable(m.group(2)); |
| 45 | + return new ErrorReport.Builder(ErrorCategory.SYNTAX, |
| 46 | + "Missing " + missing + " before " + found) |
| 47 | + .line(line).charPosition(charPosition) |
| 48 | + .cause("Expected " + missing + " but found " + found + " instead.") |
| 49 | + .hint("Add " + missing + " before " + found + ".") |
| 50 | + .build(); |
| 51 | + } |
| 52 | + |
| 53 | + // Extraneous input -> SYNTAX |
| 54 | + m = Pattern.compile("extraneous input '?(.*?)'? expecting (.*)").matcher(message); |
| 55 | + if (m.find()) { |
| 56 | + String extra = tokenToReadable(m.group(1)); |
| 57 | + return new ErrorReport.Builder(ErrorCategory.SYNTAX, |
| 58 | + "Unexpected input " + extra) |
| 59 | + .line(line).charPosition(charPosition) |
| 60 | + .reference(m.group(1)) |
| 61 | + .cause("Found " + extra + " where a valid UVL element was expected.") |
| 62 | + .hint("Remove the unexpected input or check the indentation.") |
| 63 | + .build(); |
| 64 | + } |
| 65 | + |
| 66 | + // Mismatched input -> SYNTAX |
| 67 | + m = Pattern.compile("mismatched input '?(.*?)'? expecting (.*)").matcher(message); |
| 68 | + if (m.find()) { |
| 69 | + String found = tokenToReadable(m.group(1)); |
| 70 | + String expected = simplifySet(m.group(2)); |
| 71 | + |
| 72 | + // Sonderfall: nach Gruppierung ohne Features |
| 73 | + // ANTLR meldet entweder "a new line" oder ein Gruppen-Keyword als found, |
| 74 | + // wenn eine Gruppe leer ist und expected "an indentation" ist |
| 75 | + if (expected.equals("an indentation") && (found.equals("a new line") |
| 76 | + || found.contains("'mandatory'") || found.contains("'optional'") |
| 77 | + || found.contains("'alternative'") || found.contains("'or'"))) { |
| 78 | + return new ErrorReport.Builder(ErrorCategory.SYNTAX, |
| 79 | + "Missing features after group type") |
| 80 | + .line(line).charPosition(charPosition) |
| 81 | + .field(ErrorField.GROUP) |
| 82 | + .cause("A group type ('optional', 'or', 'mandatory', 'alternative') must be followed by at least one feature.") |
| 83 | + .hint("Add at least one child feature below the group type.") |
| 84 | + .build(); |
| 85 | + } |
| 86 | + |
| 87 | + // Sonderfall: mehr als ein root Feature |
| 88 | + if (expected.equals("an indentation or a dedentation")) { |
| 89 | + return new ErrorReport.Builder(ErrorCategory.SYNTAX, |
| 90 | + "More than one root feature detected") |
| 91 | + .line(line).charPosition(charPosition) |
| 92 | + .cause("A UVL model can only have one root feature.") |
| 93 | + .hint("Check if a group type ('mandatory', 'optional', 'or', 'alternative') is missing.") |
| 94 | + .build(); |
| 95 | + } |
| 96 | + |
| 97 | + return new ErrorReport.Builder(ErrorCategory.SYNTAX, |
| 98 | + "Found " + found + " but expected " + expected) |
| 99 | + .line(line).charPosition(charPosition) |
| 100 | + .cause("The parser expected " + expected + " at this position.") |
| 101 | + .hint("Replace " + found + " with " + expected + ".") |
| 102 | + .build(); |
| 103 | + } |
| 104 | + |
| 105 | + // No viable alternative |
| 106 | + m = Pattern.compile("no viable alternative at input '(.*)'").matcher(message); |
| 107 | + if (m.find()) { |
| 108 | + String input = m.group(1).replace("\n", "").trim(); |
| 109 | + return new ErrorReport.Builder(ErrorCategory.SYNTAX, |
| 110 | + "No valid interpretation for '" + input + "'") |
| 111 | + .line(line).charPosition(charPosition) |
| 112 | + .reference(input) |
| 113 | + .cause("The input '" + input + "' does not match any valid UVL structure at this position.") |
| 114 | + .hint("Check if a keyword like 'features', 'constraints', or 'imports' is missing, or if the indentation is correct.") |
| 115 | + .build(); |
| 116 | + } |
| 117 | + |
| 118 | + // Fallback |
| 119 | + return new ErrorReport.Builder(ErrorCategory.SYNTAX, message) |
| 120 | + .line(line).charPosition(charPosition) |
| 121 | + .cause("An unexpected syntax error occurred.") |
| 122 | + .hint("Check the UVL syntax around this position.") |
| 123 | + .build(); |
| 124 | + } |
| 125 | + |
| 126 | + // Translating ANTLR tokens |
| 127 | + private String tokenToReadable(String token) { |
| 128 | + switch (token) { |
| 129 | + case "<INDENT>": return "an indentation"; |
| 130 | + case "<DEDENT>": return "a dedentation"; |
| 131 | + case "<EOF>": case "EOF": return "end of file"; |
| 132 | + case "\\n": return "a new line"; |
| 133 | + case "features": return "'features' keyword"; |
| 134 | + case "constraints": return "'constraints' keyword"; |
| 135 | + case "imports": return "'imports' keyword"; |
| 136 | + case "mandatory": return "'mandatory' group"; |
| 137 | + case "optional": return "'optional' group"; |
| 138 | + case "alternative": return "'alternative' group"; |
| 139 | + case "or": return "'or' group"; |
| 140 | + default: return "'" + token + "'"; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private String simplifySet(String expectedSet) { |
| 145 | + String cleaned = expectedSet.replaceAll("[{}]", "").trim(); |
| 146 | + String[] tokens = cleaned.split(",\\s*"); |
| 147 | + StringBuilder sb = new StringBuilder(); |
| 148 | + for (int i = 0; i < tokens.length; i++) { |
| 149 | + if (i > 0) { |
| 150 | + if (i == tokens.length - 1) { |
| 151 | + sb.append(" or "); |
| 152 | + } else { |
| 153 | + sb.append(", "); |
| 154 | + } |
| 155 | + } |
| 156 | + sb.append(tokenToReadable(tokens[i].replace("'", "").trim())); |
| 157 | + } |
| 158 | + return sb.toString(); |
| 159 | + } |
| 160 | +} |
0 commit comments