001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.js;
017:
018: /**
019: * Indicates inability to parse JavaScript source.
020: */
021: public class JsParserException extends Exception {
022:
023: /**
024: * Represents the location of a parser exception.
025: */
026: public static class SourceDetail {
027: private final int line;
028:
029: private final int lineOffset;
030:
031: private final String lineSource;
032:
033: public SourceDetail(int line, String lineSource, int lineOffset) {
034: this .line = line;
035: this .lineSource = lineSource;
036: this .lineOffset = lineOffset;
037: }
038:
039: public int getLine() {
040: return line;
041: }
042:
043: public int getLineOffset() {
044: return lineOffset;
045: }
046:
047: public String getLineSource() {
048: return lineSource;
049: }
050: }
051:
052: private final SourceDetail sourceDetail;
053:
054: public JsParserException(String msg) {
055: super (msg);
056: sourceDetail = null;
057: }
058:
059: public JsParserException(String msg, int line, String lineSource,
060: int lineOffset) {
061: this (msg, line, lineSource, lineOffset, null);
062: }
063:
064: public JsParserException(String msg, int line, String lineSource,
065: int lineOffset, Throwable cause) {
066: super (msg, cause);
067: sourceDetail = new SourceDetail(line, lineSource, lineOffset);
068: }
069:
070: public JsParserException(String msg, Throwable cause) {
071: super (msg, cause);
072: sourceDetail = null;
073: }
074:
075: public String getDescription() {
076: StringBuffer sb = new StringBuffer();
077:
078: if (sourceDetail != null) {
079: sb.append("Line ");
080: sb.append(sourceDetail.getLine());
081: sb.append(": ");
082: sb.append(getMessage());
083: sb.append("\n");
084: sb.append("> ");
085: sb.append(sourceDetail.getLineSource());
086: sb.append("\n> ");
087: for (int i = 0, n = sourceDetail.getLineOffset(); i < n; ++i) {
088: sb.append('-');
089: }
090: sb.append('^');
091: } else {
092: sb.append(getMessage());
093: }
094:
095: return sb.toString();
096: }
097:
098: /**
099: * Provides additional source detail in some cases.
100: *
101: * @return additional detail regarding the error, or <code>null</code> if no
102: * additional detail is available
103: */
104: public SourceDetail getSourceDetail() {
105: return sourceDetail;
106: }
107: }
|