JS: simplified parser exceptions

This commit is contained in:
Alexey Tsvetkov
2015-02-20 12:21:56 +03:00
parent 802a512899
commit b01aa5e0a5
5 changed files with 16 additions and 114 deletions
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.resolve.diagnostics
import com.google.gwt.dev.js.AbortParsingException
import com.google.gwt.dev.js.parserExceptions.AbortParsingException
import com.google.gwt.dev.js.rhino.*
import com.google.gwt.dev.js.rhino.Utils.*
import org.jetbrains.annotations.TestOnly
@@ -18,6 +18,7 @@ package com.google.gwt.dev.js;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.backend.js.ast.JsLiteral.JsBooleanLiteral;
import com.google.dart.compiler.common.SourceInfo;
import com.google.gwt.dev.js.parserExceptions.JsParserException;
import com.google.gwt.dev.js.rhino.*;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
@@ -26,7 +27,6 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
@@ -86,7 +86,8 @@ public class JsParser {
}
private static JsParserException createParserException(String msg, Node offender) {
return new JsParserException(msg, offender.getLineno(), null, 0, SOURCE_NAME_STUB);
CodePosition position = new CodePosition(offender.getLineno(), 0);
return new JsParserException("Parser encountered internal error: " + msg, position);
}
private JsScope getScope() {
@@ -1,106 +0,0 @@
/*
* Copyright 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.dev.js;
/**
* Indicates inability to parse JavaScript source.
*/
public class JsParserException extends Exception {
/**
* Represents the location of a parser exception.
*/
public static class SourceDetail {
private final String fileName;
private final int line;
private final int lineOffset;
private final String lineSource;
public SourceDetail(int line, String lineSource, int lineOffset,
String fileName) {
this.line = line;
this.lineSource = lineSource;
this.lineOffset = lineOffset;
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
public int getLine() {
return line;
}
public int getLineOffset() {
return lineOffset;
}
public String getLineSource() {
return lineSource;
}
}
private static String createMessageWithDetail(String msg,
SourceDetail sourceDetail) {
if (sourceDetail == null) {
return msg;
}
StringBuffer sb = new StringBuffer();
sb.append(sourceDetail.getFileName());
sb.append('(');
sb.append(sourceDetail.getLine());
sb.append(')');
sb.append(": ");
sb.append(msg);
if (sourceDetail.getLineSource() != null) {
sb.append("\n> ");
sb.append(sourceDetail.getLineSource());
sb.append("\n> ");
for (int i = 0, n = sourceDetail.getLineOffset(); i < n; ++i) {
sb.append('-');
}
sb.append('^');
}
return sb.toString();
}
private final SourceDetail sourceDetail;
public JsParserException(String msg) {
this(msg, null);
}
public JsParserException(String msg, int line, String lineSource,
int lineOffset, String fileName) {
this(msg, new SourceDetail(line, lineSource, lineOffset, fileName));
}
public JsParserException(String msg, SourceDetail sourceDetail) {
super(createMessageWithDetail(msg, sourceDetail));
this.sourceDetail = sourceDetail;
}
/**
* Provides additional source detail in some cases.
*
* @return additional detail regarding the error, or <code>null</code> if no
* additional detail is available
*/
public SourceDetail getSourceDetail() {
return sourceDetail;
}
}
@@ -13,11 +13,16 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.dev.js;
package com.google.gwt.dev.js.parserExceptions
import com.google.gwt.dev.js.rhino.CodePosition
/**
* Used only to exit parser on error
* Can be used in Error reporter to exit parser
*/
public class AbortParsingException extends RuntimeException {
public AbortParsingException() {}
}
public class AbortParsingException : RuntimeException()
public class JsParserException(
message: String,
val position: CodePosition
) : RuntimeException("$message at $position")
@@ -23,4 +23,6 @@ class CodePosition(val line: Int, val offset: Int) : Comparable<CodePosition> {
line > other.line -> 1
else -> offset.compareTo(other.offset)
}
override fun toString(): String = "($line, $offset)"
}