1 /* 2 * Copyright 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.turbine.diag; 18 19 import static java.util.stream.Collectors.joining; 20 21 import com.google.common.collect.ImmutableList; 22 23 /** A compilation error. */ 24 public class TurbineError extends Error { 25 26 /** A diagnostic kind. */ 27 public enum ErrorKind { 28 UNEXPECTED_INPUT("unexpected input: %c"), 29 UNEXPECTED_IDENTIFIER("unexpected identifier '%s'"), 30 UNEXPECTED_EOF("unexpected end of input"), 31 UNTERMINATED_STRING("unterminated string literal"), 32 UNTERMINATED_CHARACTER_LITERAL("unterminated char literal"), 33 UNTERMINATED_EXPRESSION("unterminated expression, expected ';' not found"), 34 EMPTY_CHARACTER_LITERAL("empty char literal"), 35 EXPECTED_TOKEN("expected token %s"), 36 INVALID_LITERAL("invalid literal: %s"), 37 UNEXPECTED_TYPE_PARAMETER("unexpected type parameter %s"), 38 SYMBOL_NOT_FOUND("symbol not found %s"), 39 CLASS_FILE_NOT_FOUND("could not locate class file for %s"), 40 TYPE_PARAMETER_QUALIFIER("type parameter used as type qualifier"), 41 UNEXPECTED_TOKEN("unexpected token: %s"), 42 INVALID_ANNOTATION_ARGUMENT("invalid annotation argument"), 43 CANNOT_RESOLVE("could not resolve %s"), 44 EXPRESSION_ERROR("could not evaluate constant expression"), 45 CYCLIC_HIERARCHY("cycle in class hierarchy: %s"), 46 NOT_AN_ANNOTATION("%s is not an annotation"), 47 NONREPEATABLE_ANNOTATION("%s is not @Repeatable"), 48 DUPLICATE_DECLARATION("duplicate declaration of %s"), 49 BAD_MODULE_INFO("unexpected declaration found in module-info"); 50 51 private final String message; 52 ErrorKind(String message)53 ErrorKind(String message) { 54 this.message = message; 55 } 56 format(Object... args)57 String format(Object... args) { 58 return String.format(message, args); 59 } 60 } 61 62 /** 63 * Formats a diagnostic. 64 * 65 * @param source the current source file 66 * @param kind the error kind 67 * @param args format args 68 */ format(SourceFile source, ErrorKind kind, Object... args)69 public static TurbineError format(SourceFile source, ErrorKind kind, Object... args) { 70 return new TurbineError(ImmutableList.of(TurbineDiagnostic.format(source, kind, args))); 71 } 72 73 /** 74 * Formats a diagnostic. 75 * 76 * @param position the diagnostic position 77 * @param kind the error kind 78 * @param args format args 79 */ format( SourceFile source, int position, ErrorKind kind, Object... args)80 public static TurbineError format( 81 SourceFile source, int position, ErrorKind kind, Object... args) { 82 return new TurbineError( 83 ImmutableList.of(TurbineDiagnostic.format(source, position, kind, args))); 84 } 85 86 private final ImmutableList<TurbineDiagnostic> diagnostics; 87 TurbineError(ImmutableList<TurbineDiagnostic> diagnostics)88 public TurbineError(ImmutableList<TurbineDiagnostic> diagnostics) { 89 super(diagnostics.stream().map(d -> d.diagnostic()).collect(joining("\n"))); 90 this.diagnostics = diagnostics; 91 } 92 diagnostics()93 public ImmutableList<TurbineDiagnostic> diagnostics() { 94 return diagnostics; 95 } 96 } 97