|
| 1 | +// This class is for mapping specific error messages, making UVLListener and UVLModelFactory easier to read. |
| 2 | + |
| 3 | +package de.vill.exception; |
| 4 | + |
| 5 | +import de.vill.model.LanguageLevel; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +public final class ErrorMessages { |
| 9 | + |
| 10 | + public static ParseError build( |
| 11 | + ErrorCategory category, |
| 12 | + String message, |
| 13 | + int line, |
| 14 | + int charPosition, |
| 15 | + ErrorField field, |
| 16 | + String reference, |
| 17 | + String cause, |
| 18 | + String hint |
| 19 | + ){ |
| 20 | + return new ParseError( |
| 21 | + new ErrorReport.Builder(category, message) |
| 22 | + .line(line) |
| 23 | + .charPosition(charPosition) |
| 24 | + .field(field) |
| 25 | + .reference(reference) |
| 26 | + .cause(cause) |
| 27 | + .hint(hint) |
| 28 | + .build() |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + // Invalid language level import |
| 33 | + public static ParseError invalidLanguageLevel( |
| 34 | + String languageLevel, |
| 35 | + int line, |
| 36 | + int charPos |
| 37 | + ){ |
| 38 | + return build( |
| 39 | + ErrorCategory.CONTEXT, |
| 40 | + "Invalid language level import: '" + languageLevel + "'", |
| 41 | + line, |
| 42 | + charPos, |
| 43 | + ErrorField.LANGUAGE_LEVEL, |
| 44 | + languageLevel, |
| 45 | + "Invalid language level import.", |
| 46 | + "Use a valid language level format." |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + // No Import |
| 51 | + public static ParseError noImport( |
| 52 | + String featureReference, |
| 53 | + int line, |
| 54 | + int charPos |
| 55 | + ){ |
| 56 | + return build( |
| 57 | + ErrorCategory.CONTEXT, |
| 58 | + "Feature '" + featureReference + "' is referenced as imported, but no matching import exists", |
| 59 | + line, |
| 60 | + charPos, |
| 61 | + ErrorField.IMPORT, |
| 62 | + featureReference, |
| 63 | + "The feature name suggests it comes from an imported submodel, but the import was not declared.", |
| 64 | + "Add the corresponding import in the 'imports' section or correct the feature name." |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // Duplicate Feature |
| 69 | + public static ParseError duplicateFeature( |
| 70 | + String featureReference, |
| 71 | + int originalLine, |
| 72 | + int line, |
| 73 | + int charPos |
| 74 | + ){ |
| 75 | + return build( |
| 76 | + ErrorCategory.CONTEXT, |
| 77 | + "Duplicate feature name: '" + featureReference + "'", |
| 78 | + line, |
| 79 | + charPos, |
| 80 | + ErrorField.FEATURE, |
| 81 | + featureReference, |
| 82 | + "A feature with the name '" + featureReference + "' already exists in the feature tree (first defined at line " + originalLine + ").", |
| 83 | + "Rename one of the duplicate features to make names unique." |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + // Unsupported Attribute Value Type |
| 88 | + public static ParseError unsupportedAttributeValueType( |
| 89 | + String attributeName, |
| 90 | + String value, |
| 91 | + int line, |
| 92 | + int charPos |
| 93 | + ){ |
| 94 | + return build( |
| 95 | + ErrorCategory.CONTEXT, |
| 96 | + "Unsupported attribute value type: '" + value + "'", |
| 97 | + line, |
| 98 | + charPos, |
| 99 | + ErrorField.ATTRIBUTE, |
| 100 | + attributeName, |
| 101 | + "The value '" + value + "' does not match any supported attribute type (Boolean, Integer, Float, String, Vector, Attributes).", |
| 102 | + "Use a supported value type for the attribute." |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + // Feature not in Tree but in Constraint |
| 107 | + public static ParseError featureNotInTreeButConstraint( |
| 108 | + String referenceName, |
| 109 | + int line, |
| 110 | + int charPos |
| 111 | + ){ |
| 112 | + return build( |
| 113 | + ErrorCategory.CONTEXT, |
| 114 | + "Reference '" + referenceName + "' in constraint could not be resolved", |
| 115 | + line, |
| 116 | + charPos, |
| 117 | + ErrorField.CONSTRAINT, |
| 118 | + referenceName, |
| 119 | + "The feature or attribute '" + referenceName + "' is used in a constraint but does not exist in the feature tree.", |
| 120 | + "Check if the feature name is spelled correctly or add it to the feature tree." |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + // Feature not in Tree but in Expression |
| 125 | + public static ParseError featureNotInTreeButExpression( |
| 126 | + String referenceName, |
| 127 | + int line, |
| 128 | + int charPos |
| 129 | + ){ |
| 130 | + return build( |
| 131 | + ErrorCategory.CONTEXT, |
| 132 | + "Variable '" + referenceName + "' in expression could not be resolved", |
| 133 | + line, |
| 134 | + charPos, |
| 135 | + ErrorField.EXPRESSION, |
| 136 | + referenceName, |
| 137 | + "The feature or attribute '" + referenceName + "' is used in an expression but does not exist in the feature tree.", |
| 138 | + "Check if the variable name is spelled correctly or add it to the feature tree." |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + // Attribute used with sum() does not exist |
| 143 | + public static ParseError sumAttributeNotDefined( |
| 144 | + String attributeIdentifier, |
| 145 | + int line, |
| 146 | + int charPos |
| 147 | + ){ |
| 148 | + return build( |
| 149 | + ErrorCategory.CONTEXT, |
| 150 | + "Attribute '" + attributeIdentifier + "' does not exist in the feature model", |
| 151 | + line, |
| 152 | + charPos, |
| 153 | + ErrorField.ATTRIBUTE, |
| 154 | + attributeIdentifier, |
| 155 | + "The attribute '" + attributeIdentifier + "' is used in a sum() aggregate function but is not defined on any feature.", |
| 156 | + "Define the attribute on the relevant features or correct the attribute name." |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + // Feature used with sum() invalid or does not exist |
| 161 | + public static ParseError sumFeatureNotDefined( |
| 162 | + String referenceName, |
| 163 | + int line, |
| 164 | + int charPos |
| 165 | + ){ |
| 166 | + return build( |
| 167 | + ErrorCategory.CONTEXT, |
| 168 | + "'" + referenceName + "' is not a valid feature for sum() aggregate function", |
| 169 | + line, |
| 170 | + charPos, |
| 171 | + ErrorField.FEATURE, |
| 172 | + referenceName, |
| 173 | + "The parameter '" + referenceName + "' must be a feature but could not be found in the feature tree.", |
| 174 | + "Check if the feature name is spelled correctly or add it to the feature tree." |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + // Attribute used with avg() does not exist |
| 179 | + public static ParseError avgAttributeNotDefined( |
| 180 | + String attributeIdentifier, |
| 181 | + int line, |
| 182 | + int charPos |
| 183 | + ){ |
| 184 | + return build( |
| 185 | + ErrorCategory.CONTEXT, |
| 186 | + "Attribute '" + attributeIdentifier + "' does not exist in the feature model", |
| 187 | + line, |
| 188 | + charPos, |
| 189 | + ErrorField.ATTRIBUTE, |
| 190 | + attributeIdentifier, |
| 191 | + "The attribute '" + attributeIdentifier + "' is used in a avg() aggregate function but is not defined on any feature.", |
| 192 | + "Define the attribute on the relevant features or correct the attribute name." |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + // Feature used with avg() invalid or does not exist |
| 197 | + public static ParseError avgFeatureNotDefined( |
| 198 | + String referenceName, |
| 199 | + int line, |
| 200 | + int charPos |
| 201 | + ){ |
| 202 | + return build( |
| 203 | + ErrorCategory.CONTEXT, |
| 204 | + "'" + referenceName + "' is not a valid feature for sum() aggregate function", |
| 205 | + line, |
| 206 | + charPos, |
| 207 | + ErrorField.FEATURE, |
| 208 | + referenceName, |
| 209 | + "The parameter '" + referenceName + "' must be a feature but could not be found in the feature tree.", |
| 210 | + "Check if the feature name is spelled correctly or add it to the feature tree." |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + // Feature used with length() does not exist |
| 215 | + public static ParseError lenFeatureNotDefined( |
| 216 | + String referenceName, |
| 217 | + int line, |
| 218 | + int charPos |
| 219 | + ){ |
| 220 | + return build( |
| 221 | + ErrorCategory.CONTEXT, |
| 222 | + "Reference '" + referenceName + "' in length() could not be resolved", |
| 223 | + line, |
| 224 | + charPos, |
| 225 | + ErrorField.EXPRESSION, |
| 226 | + referenceName, |
| 227 | + "The feature '" + referenceName + "' does not exist in the feature tree.", |
| 228 | + "Check if the feature name is spelled correctly or add it to the feature tree." |
| 229 | + ); |
| 230 | + } |
| 231 | + |
| 232 | + |
| 233 | + // length() can only be used with String features |
| 234 | + public static ParseError lenFeatureNotString( |
| 235 | + String referenceName, |
| 236 | + int line, |
| 237 | + int charPos |
| 238 | + ){ |
| 239 | + return build( |
| 240 | + ErrorCategory.CONTEXT, |
| 241 | + "length() can only be used with String features, but '" + referenceName + "' is not a String feature", |
| 242 | + line, |
| 243 | + charPos, |
| 244 | + ErrorField.EXPRESSION, |
| 245 | + referenceName, |
| 246 | + "The feature '" + referenceName + "' is not of type String.", |
| 247 | + "Change the feature type to 'String' or use a different aggregate function." |
| 248 | + ); |
| 249 | + } |
| 250 | + |
| 251 | + // Imported language levels do not match |
| 252 | + public static ParseError languageLevelsDoNotMatch( |
| 253 | + Set<LanguageLevel> importedLanguageLevels, |
| 254 | + Set<LanguageLevel> actualLanguageLevels, |
| 255 | + int line, |
| 256 | + int charPos |
| 257 | + ){ |
| 258 | + return build( |
| 259 | + ErrorCategory.CONTEXT, |
| 260 | + "Imported and actually used language levels do not match", |
| 261 | + line, |
| 262 | + charPos, |
| 263 | + ErrorField.LANGUAGE_LEVEL, |
| 264 | + importedLanguageLevels.toString(), |
| 265 | + "Imported levels: " + importedLanguageLevels + ". Actually used levels: " + actualLanguageLevels + ".", |
| 266 | + "Update the 'include' section to match the language features used in the model, or remove unsupported constructs." |
| 267 | + ); |
| 268 | + } |
| 269 | + |
| 270 | + // Imported feature not found |
| 271 | + public static ParseError importedFeatureNotFound( |
| 272 | + String fullRef |
| 273 | + ){ |
| 274 | + return build( |
| 275 | + ErrorCategory.CONTEXT, |
| 276 | + "Imported feature '" + fullRef + "' could not be resolved", |
| 277 | + 0, |
| 278 | + 0, |
| 279 | + ErrorField.IMPORT, |
| 280 | + fullRef, |
| 281 | + "The feature '" + fullRef + "' does not exist in the imported submodel.", |
| 282 | + "Check that the feature exists in the imported model or correct the reference." |
| 283 | + ); |
| 284 | + } |
| 285 | + |
| 286 | + // Imported feature attribute reference does not exist |
| 287 | + public static ParseError importFeatureAttributeReferenceMissing( |
| 288 | + String fullRef |
| 289 | + ){ |
| 290 | + return build( |
| 291 | + ErrorCategory.CONTEXT, |
| 292 | + "Imported feature for attribute reference '" + fullRef + "' could not be resolved", |
| 293 | + 0, |
| 294 | + 0, |
| 295 | + ErrorField.IMPORT, |
| 296 | + fullRef, |
| 297 | + "The feature '" + fullRef + "' does not exist in the imported submodel.", |
| 298 | + "Check that the feature exists in the imported model or correct the reference." |
| 299 | + ); |
| 300 | + } |
| 301 | + |
| 302 | + // Attribute not found on imported feature |
| 303 | + public static ParseError missingAttributewithImportedFeature( |
| 304 | + String fullRef, |
| 305 | + String attrName |
| 306 | + ){ |
| 307 | + return build( |
| 308 | + ErrorCategory.CONTEXT, |
| 309 | + "Attribute '" + attrName + "' not found on imported feature '" + fullRef + "'", |
| 310 | + 0, |
| 311 | + 0, |
| 312 | + ErrorField.ATTRIBUTE, |
| 313 | + fullRef, |
| 314 | + "The attribute '" + attrName + "' does not exist on the feature referenced by '" + fullRef + "'.", |
| 315 | + "Check the attribute name or define it on the feature in the imported model." |
| 316 | + ); |
| 317 | + } |
| 318 | + |
| 319 | + // Reference couldn't be resolved to a feature or attribute |
| 320 | + public static ParseError unresolvedImportedReference( |
| 321 | + String fullRef |
| 322 | + ){ |
| 323 | + return build( |
| 324 | + ErrorCategory.CONTEXT, |
| 325 | + "Could not resolve imported reference '" + fullRef + "'", |
| 326 | + 0, |
| 327 | + 0, |
| 328 | + ErrorField.IMPORT, |
| 329 | + fullRef, |
| 330 | + "The reference could not be resolved to a feature or attribute in the imported submodel.", |
| 331 | + "Check the import path and ensure the referenced element exists." |
| 332 | + ); |
| 333 | + } |
| 334 | +} |
0 commit comments