| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 30 |
|
| 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 |
|
| 41 |
|
| 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 |
|
| 65 |
private final Map<Pattern, DiagnosticType> typeMap; |
| 66 |
|
| 67 |
final AbstractCompiler compiler; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 83 |
.put(replacePlaceHolders( |
| 84 |
com.google.javascript.rhino.head.ScriptRuntime.getMessage0( |
| 85 |
"msg.extra.trailing.comma")), |
| 86 |
TRAILING_COMMA) |
| 87 |
|
| 88 |
|
| 89 |
.put(replacePlaceHolders( |
| 90 |
com.google.javascript.rhino.head.ScriptRuntime.getMessage0( |
| 91 |
"msg.dup.parms")), |
| 92 |
DUPLICATE_PARAM) |
| 93 |
|
| 94 |
|
| 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 |
|
| 102 |
.put(Pattern.compile("^Bad type annotation.*"), |
| 103 |
TYPE_PARSE_ERROR) |
| 104 |
|
| 105 |
|
| 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 |
|
| 147 |
|
| 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 |
} |