• 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     UNEXPECTED_MODIFIER("unexpected modifier: %s"),
61     PROC("%s");
62 
63     private final String message;
64 
ErrorKind(String message)65     ErrorKind(String message) {
66       this.message = message;
67     }
68 
format(Object... args)69     String format(Object... args) {
70       return String.format(message, args);
71     }
72   }
73 
74   /**
75    * Formats a diagnostic.
76    *
77    * @param source the current source file
78    * @param kind the error kind
79    * @param args format args
80    */
format(SourceFile source, ErrorKind kind, Object... args)81   public static TurbineError format(SourceFile source, ErrorKind kind, Object... args) {
82     return new TurbineError(ImmutableList.of(TurbineDiagnostic.format(source, kind, args)));
83   }
84 
85   /**
86    * Formats a diagnostic.
87    *
88    * @param position the diagnostic position
89    * @param kind the error kind
90    * @param args format args
91    */
format( SourceFile source, int position, ErrorKind kind, Object... args)92   public static TurbineError format(
93       SourceFile source, int position, ErrorKind kind, Object... args) {
94     return new TurbineError(
95         ImmutableList.of(
96             TurbineDiagnostic.format(Diagnostic.Kind.ERROR, source, position, kind, args)));
97   }
98 
99   private final ImmutableList<TurbineDiagnostic> diagnostics;
100 
TurbineError(ImmutableList<TurbineDiagnostic> diagnostics)101   public TurbineError(ImmutableList<TurbineDiagnostic> diagnostics) {
102     this.diagnostics = diagnostics;
103   }
104 
105   @Override
getMessage()106   public String getMessage() {
107     return diagnostics.stream().map(d -> d.diagnostic()).collect(joining(System.lineSeparator()));
108   }
109 
diagnostics()110   public ImmutableList<TurbineDiagnostic> diagnostics() {
111     return diagnostics;
112   }
113 }
114