Debian Codegraph @

src/com/google/javascript/jscomp/RhinoErrorReporter.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
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.javascript.jscomp;
18
19 import com.google.common.collect.ImmutableMap;
20 import com.google.javascript.jscomp.CheckLevel;
21 import com.google.javascript.rhino.ErrorReporter;
22 import com.google.javascript.rhino.ScriptRuntime;
23
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.regex.Pattern;
27
28 /**
29 * An error reporter for serializing Rhino errors into our error format.
30 * @author [email protected] (Nick Santos)
31 */
32 class RhinoErrorReporter {
33
34 static final DiagnosticType PARSE_ERROR =
35 DiagnosticType.error("JSC_PARSE_ERROR", "Parse error. {0}");
36
37 static final DiagnosticType TYPE_PARSE_ERROR =
38 DiagnosticType.warning("JSC_TYPE_PARSE_ERROR", "{0}");
39
40 // Special-cased errors, so that they can be configured via the
41 // warnings API.
42 static final DiagnosticType TRAILING_COMMA =
43 DiagnosticType.error("JSC_TRAILING_COMMA",
44 "Parse error. IE8 (and below) will parse trailing commas in " +
45 "array and object literals incorrectly. " +
46 "If you are targeting newer versions of JS, " +
47 "set the appropriate language_in option.");
48
49 static final DiagnosticType DUPLICATE_PARAM =
50 DiagnosticType.error("JSC_DUPLICATE_PARAM", "Parse error. {0}");
51
52 static final DiagnosticType BAD_JSDOC_ANNOTATION =
53 DiagnosticType.warning("JSC_BAD_JSDOC_ANNOTATION", "Parse error. {0}");
54
55 static final DiagnosticType MISPLACED_TYPE_ANNOTATION =
56 DiagnosticType.warning("JSC_MISPLACED_TYPE_ANNOTATION",
57 "Type annotations are not allowed here. " +
58 "Are you missing parentheses?");
59
60 static final DiagnosticType PARSE_TREE_TOO_DEEP =
61 DiagnosticType.error("PARSE_TREE_TOO_DEEP",
62 "Parse tree too deep.");
63
64 // A map of Rhino messages to their DiagnosticType.
65 private final Map<Pattern, DiagnosticType> typeMap;
66
67 final AbstractCompiler compiler;
68
69 /**
70 * For each message such as "Not a good use of {0}", replace the place
71 * holder {0} with a wild card that matches all possible strings.
72 * Also put the any non-place-holder in quotes for regex matching later.
73 */
74 private Pattern replacePlaceHolders(String s) {
75 s = Pattern.quote(s);
76 return Pattern.compile(s.replaceAll("\\{\\d+\\}", "\\\\E.*\\\\Q"));
77 }
78
79 private RhinoErrorReporter(AbstractCompiler compiler) {
80 this.compiler = compiler;
81 typeMap = ImmutableMap.<Pattern,DiagnosticType>builder()
82 // Trailing comma
83 .put(replacePlaceHolders(
84 com.google.javascript.rhino.head.ScriptRuntime.getMessage0(
85 "msg.extra.trailing.comma")),
86 TRAILING_COMMA)
87
88 // Duplicate parameter
89 .put(replacePlaceHolders(
90 com.google.javascript.rhino.head.ScriptRuntime.getMessage0(
91 "msg.dup.parms")),
92 DUPLICATE_PARAM)
93
94 // Unknown @annotations.
95 .put(replacePlaceHolders(ScriptRuntime.getMessage0("msg.bad.jsdoc.tag")),
96 BAD_JSDOC_ANNOTATION)
97
98 .put(Pattern.compile("^Type annotations are not allowed here.*"),
99 MISPLACED_TYPE_ANNOTATION)
100
101 // Type annotation errors.
102 .put(Pattern.compile("^Bad type annotation.*"),
103 TYPE_PARSE_ERROR)
104
105 // Parse tree too deep.
106 .put(replacePlaceHolders(
107 com.google.javascript.rhino.head.ScriptRuntime.getMessage0(
108 "msg.too.deep.parser.recursion")),
109 PARSE_TREE_TOO_DEEP)
110 .build();
111 }
112
113 public static com.google.javascript.rhino.head.ErrorReporter
114 forNewRhino(AbstractCompiler compiler) {
115 return new NewRhinoErrorReporter(compiler);
116 }
117
118 public static ErrorReporter forOldRhino(AbstractCompiler compiler) {
119 return new OldRhinoErrorReporter(compiler);
120 }
121
122 void warningAtLine(String message, String sourceName, int line,
123 int lineOffset) {
124 compiler.report(
125 makeError(message, sourceName, line, lineOffset, CheckLevel.WARNING));
126 }
127
128 void errorAtLine(String message, String sourceName, int line,
129 int lineOffset) {
130 compiler.report(
131 makeError(message, sourceName, line, lineOffset, CheckLevel.ERROR));
132 }
133
134 protected DiagnosticType mapError(String message) {
135 for (Entry<Pattern, DiagnosticType> entry : typeMap.entrySet()) {
136 if (entry.getKey().matcher(message).matches()) {
137 return entry.getValue();
138 }
139 }
140 return null;
141 }
142
143 private JSError makeError(String message, String sourceName, int line,
144 int lineOffset, CheckLevel defaultLevel) {
145
146 // Try to see if the message is one of the rhino errors we want to
147 // expose as DiagnosticType by matching it with the regex key.
148 DiagnosticType type = mapError(message);
149 if (type != null) {
150 return JSError.make(
151 sourceName, line, lineOffset, type, message);
152 }
153
154 return JSError.make(sourceName, line, lineOffset, defaultLevel,
155 PARSE_ERROR, message);
156 }
157
158 private static class OldRhinoErrorReporter extends RhinoErrorReporter
159 implements ErrorReporter {
160
161 private OldRhinoErrorReporter(AbstractCompiler compiler) {
162 super(compiler);
163 }
164
165 @Override
166 public void error(String message, String sourceName, int line,
167 int lineOffset) {
168 super.errorAtLine(message, sourceName, line, lineOffset);
169 }
170
171 @Override
172 public void warning(String message, String sourceName, int line,
173 int lineOffset) {
174 super.warningAtLine(message, sourceName, line, lineOffset);
175 }
176 }
177
178 private static class NewRhinoErrorReporter extends RhinoErrorReporter
179 implements com.google.javascript.rhino.head.ast.IdeErrorReporter {
180
181 private NewRhinoErrorReporter(AbstractCompiler compiler) {
182 super(compiler);
183 }
184
185 @Override
186 public com.google.javascript.rhino.head.EvaluatorException
187 runtimeError(String message, String sourceName, int line,
188 String lineSource, int lineOffset) {
189 DiagnosticType type = mapError(message);
190 if (type != null) {
191 super.errorAtLine(message, sourceName, line, lineOffset);
192 }
193 return new com.google.javascript.rhino.head.EvaluatorException(
194 message, sourceName, line, lineSource, lineOffset);
195 }
196
197 @Override
198 public void error(String message, String sourceName, int line,
199 String sourceLine, int lineOffset) {
200 super.errorAtLine(message, sourceName, line, lineOffset);
201 }
202
203 @Override
204 public void error(String message, String sourceName,
205 int offset, int length) {
206 int line = 1;
207 int column = 0;
208 SourceFile file = this.compiler.getSourceFileByName(sourceName);
209 if (file != null) {
210 line = file.getLineOfOffset(offset);
211 column = file.getColumnOfOffset(offset);
212 }
213 super.errorAtLine(message, sourceName, line, column);
214 }
215
216 @Override
217 public void warning(String message, String sourceName, int line,
218 String sourceLine, int lineOffset) {
219 super.warningAtLine(message, sourceName, line, lineOffset);
220 }
221
222 @Override
223 public void warning(String message, String sourceName,
224 int offset, int length) {
225 int line = 1;
226 int column = 0;
227 SourceFile file = this.compiler.getSourceFileByName(sourceName);
228 if (file != null) {
229 line = file.getLineOfOffset(offset);
230 column = file.getColumnOfOffset(offset);
231 }
232 super.errorAtLine(message, sourceName, line, column);
233 }
234 }
235 }