• 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: %c"),
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     UNTERMINATED_EXPRESSION("unterminated expression, expected ';' not found"),
35     INVALID_UNICODE("illegal unicode escape"),
36     EMPTY_CHARACTER_LITERAL("empty char literal"),
37     EXPECTED_TOKEN("expected token %s"),
38     INVALID_LITERAL("invalid literal: %s"),
39     UNEXPECTED_TYPE_PARAMETER("unexpected type parameter %s"),
40     SYMBOL_NOT_FOUND("symbol not found %s"),
41     CLASS_FILE_NOT_FOUND("could not locate class file for %s"),
42     TYPE_PARAMETER_QUALIFIER("type parameter used as type qualifier"),
43     UNEXPECTED_TOKEN("unexpected token: %s"),
44     INVALID_ANNOTATION_ARGUMENT("invalid annotation argument"),
45     CANNOT_RESOLVE("could not resolve %s"),
46     EXPRESSION_ERROR("could not evaluate constant expression"),
47     OPERAND_TYPE("bad operand type %s"),
48     CYCLIC_HIERARCHY("cycle in class hierarchy: %s"),
49     NOT_AN_ANNOTATION("%s is not an annotation"),
50     NONREPEATABLE_ANNOTATION("%s is not @Repeatable"),
51     DUPLICATE_DECLARATION("duplicate declaration of %s"),
52     BAD_MODULE_INFO("unexpected declaration found in module-info"),
53     UNCLOSED_COMMENT("unclosed comment"),
54     PROC("%s");
55 
56     private final String message;
57 
ErrorKind(String message)58     ErrorKind(String message) {
59       this.message = message;
60     }
61 
format(Object... args)62     String format(Object... args) {
63       return String.format(message, args);
64     }
65   }
66 
67   /**
68    * Formats a diagnostic.
69    *
70    * @param source the current source file
71    * @param kind the error kind
72    * @param args format args
73    */
format(SourceFile source, ErrorKind kind, Object... args)74   public static TurbineError format(SourceFile source, ErrorKind kind, Object... args) {
75     return new TurbineError(ImmutableList.of(TurbineDiagnostic.format(source, kind, args)));
76   }
77 
78   /**
79    * Formats a diagnostic.
80    *
81    * @param position the diagnostic position
82    * @param kind the error kind
83    * @param args format args
84    */
format( SourceFile source, int position, ErrorKind kind, Object... args)85   public static TurbineError format(
86       SourceFile source, int position, ErrorKind kind, Object... args) {
87     return new TurbineError(
88         ImmutableList.of(
89             TurbineDiagnostic.format(Diagnostic.Kind.ERROR, source, position, kind, args)));
90   }
91 
92   private final ImmutableList<TurbineDiagnostic> diagnostics;
93 
TurbineError(ImmutableList<TurbineDiagnostic> diagnostics)94   public TurbineError(ImmutableList<TurbineDiagnostic> diagnostics) {
95     this.diagnostics = diagnostics;
96   }
97 
98   @Override
getMessage()99   public String getMessage() {
100     return diagnostics.stream().map(d -> d.diagnostic()).collect(joining(System.lineSeparator()));
101   }
102 
diagnostics()103   public ImmutableList<TurbineDiagnostic> diagnostics() {
104     return diagnostics;
105   }
106 }
107