• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import javax.tools.Diagnostic;
23 
24 /** A compilation error. */
25 public class TurbineError extends Error {
26 
27   /** A diagnostic kind. */
28   public enum ErrorKind {
29     UNEXPECTED_INPUT("unexpected input: %s"),
30     UNEXPECTED_IDENTIFIER("unexpected identifier '%s'"),
31     UNEXPECTED_EOF("unexpected end of input"),
32     UNTERMINATED_STRING("unterminated string literal"),
33     UNTERMINATED_CHARACTER_LITERAL("unterminated char literal"),
34     UNPAIRED_SURROGATE("unpaired surrogate 0x%x"),
35     UNTERMINATED_EXPRESSION("unterminated expression, expected ';' not found"),
36     INVALID_UNICODE("illegal unicode escape"),
37     EMPTY_CHARACTER_LITERAL("empty char literal"),
38     EXPECTED_TOKEN("expected token %s"),
39     EXTENDS_AFTER_IMPLEMENTS("'extends' must come before 'implements'"),
40     INVALID_LITERAL("invalid literal: %s"),
41     UNEXPECTED_TYPE_PARAMETER("unexpected type parameter %s"),
42     SYMBOL_NOT_FOUND("symbol not found %s"),
43     CLASS_FILE_NOT_FOUND("could not locate class file for %s"),
44     TYPE_PARAMETER_QUALIFIER("type parameter used as type qualifier"),
45     UNEXPECTED_TOKEN("unexpected token: %s"),
46     INVALID_ANNOTATION_ARGUMENT("invalid annotation argument"),
47     MISSING_ANNOTATION_ARGUMENT("missing required annotation argument: %s"),
48     CANNOT_RESOLVE("could not resolve %s"),
49     EXPRESSION_ERROR("could not evaluate constant expression"),
50     OPERAND_TYPE("bad operand type %s"),
51     TYPE_CONVERSION("value %s of type %s cannot be converted to %s"),
52     CYCLIC_HIERARCHY("cycle in class hierarchy: %s"),
53     NOT_AN_ANNOTATION("%s is not an annotation"),
54     ANNOTATION_VALUE_NAME("expected an annotation value of the form name=value"),
55     NONREPEATABLE_ANNOTATION("%s is not @Repeatable"),
56     DUPLICATE_DECLARATION("duplicate declaration of %s"),
57     BAD_MODULE_INFO("unexpected declaration found in module-info"),
58     UNCLOSED_COMMENT("unclosed comment"),
59     UNEXPECTED_TYPE("unexpected type %s"),
60     EXPECTED_INTERFACE("expected interface type"),
61     UNEXPECTED_INTERFACE("unexpected interface type"),
62     UNEXPECTED_MODIFIER("unexpected modifier: %s"),
63     PROC("%s");
64 
65     private final String message;
66 
ErrorKind(String message)67     ErrorKind(String message) {
68       this.message = message;
69     }
70 
format(Object... args)71     String format(Object... args) {
72       return String.format(message, args);
73     }
74   }
75 
76   /**
77    * Formats a diagnostic.
78    *
79    * @param source the current source file
80    * @param kind the error kind
81    * @param args format args
82    */
format(SourceFile source, ErrorKind kind, Object... args)83   public static TurbineError format(SourceFile source, ErrorKind kind, Object... args) {
84     return new TurbineError(ImmutableList.of(TurbineDiagnostic.format(source, kind, args)));
85   }
86 
87   /**
88    * Formats a diagnostic.
89    *
90    * @param position the diagnostic position
91    * @param kind the error kind
92    * @param args format args
93    */
format( SourceFile source, int position, ErrorKind kind, Object... args)94   public static TurbineError format(
95       SourceFile source, int position, ErrorKind kind, Object... args) {
96     return new TurbineError(
97         ImmutableList.of(
98             TurbineDiagnostic.format(Diagnostic.Kind.ERROR, source, position, kind, args)));
99   }
100 
101   private final ImmutableList<TurbineDiagnostic> diagnostics;
102 
TurbineError(ImmutableList<TurbineDiagnostic> diagnostics)103   public TurbineError(ImmutableList<TurbineDiagnostic> diagnostics) {
104     this.diagnostics = diagnostics;
105   }
106 
107   @Override
getMessage()108   public String getMessage() {
109     return diagnostics.stream().map(d -> d.diagnostic()).collect(joining(System.lineSeparator()));
110   }
111 
diagnostics()112   public ImmutableList<TurbineDiagnostic> diagnostics() {
113     return diagnostics;
114   }
115 }
116