Debian Codegraph @

rhino/src/org/mozilla/javascript/DefaultErrorReporter.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 /**
10 * This is the default error reporter for JavaScript.
11 *
12 * @author Norris Boyd
13 */
14 class DefaultErrorReporter implements ErrorReporter
15 {
16 static final DefaultErrorReporter instance = new DefaultErrorReporter();
17
18 private boolean forEval;
19 private ErrorReporter chainedReporter;
20
21 private DefaultErrorReporter() { }
22
23 static ErrorReporter forEval(ErrorReporter reporter)
24 {
25 DefaultErrorReporter r = new DefaultErrorReporter();
26 r.forEval = true;
27 r.chainedReporter = reporter;
28 return r;
29 }
30
31 public void warning(String message, String sourceURI, int line,
32 String lineText, int lineOffset)
33 {
34 if (chainedReporter != null) {
35 chainedReporter.warning(
36 message, sourceURI, line, lineText, lineOffset);
37 } else {
38 // Do nothing
39 }
40 }
41
42 public void error(String message, String sourceURI, int line,
43 String lineText, int lineOffset)
44 {
45 if (forEval) {
46 // Assume error message strings that start with "TypeError: "
47 // should become TypeError exceptions. A bit of a hack, but we
48 // don't want to change the ErrorReporter interface.
49 String error = "SyntaxError";
50 final String TYPE_ERROR_NAME = "TypeError";
51 final String DELIMETER = ": ";
52 final String prefix = TYPE_ERROR_NAME + DELIMETER;
53 if (message.startsWith(prefix)) {
54 error = TYPE_ERROR_NAME;
55 message = message.substring(prefix.length());
56 }
57 throw ScriptRuntime.constructError(error, message, sourceURI,
58 line, lineText, lineOffset);
59 }
60 if (chainedReporter != null) {
61 chainedReporter.error(
62 message, sourceURI, line, lineText, lineOffset);
63 } else {
64 throw runtimeError(
65 message, sourceURI, line, lineText, lineOffset);
66 }
67 }
68
69 public EvaluatorException runtimeError(String message, String sourceURI,
70 int line, String lineText,
71 int lineOffset)
72 {
73 if (chainedReporter != null) {
74 return chainedReporter.runtimeError(
75 message, sourceURI, line, lineText, lineOffset);
76 } else {
77 return new EvaluatorException(
78 message, sourceURI, line, lineText, lineOffset);
79 }
80 }
81 }