JS backend: report parser errors and warnings
This commit is contained in:
@@ -29,6 +29,9 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by Delegates.lazy {
|
||||
put(ErrorsJs.NATIVE_INDEXER_KEY_SHOULD_BE_STRING_OR_NUMBER, "Native {0}''s first parameter type should be ''kotlin.String'' or subtype of ''kotlin.Number''", Renderers.STRING)
|
||||
put(ErrorsJs.NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE, "Native getter''s return type should be nullable")
|
||||
put(ErrorsJs.NATIVE_INDEXER_WRONG_PARAMETER_COUNT, "Expected {0} parameters for native {1}", Renderers.TO_STRING, Renderers.STRING)
|
||||
put(ErrorsJs.JSCODE_ERROR, "JavaScript: {0}", Renderers.TO_STRING, Renderers.TO_STRING)
|
||||
put(ErrorsJs.JSCODE_WARNING, "JavaScript: {0}", Renderers.TO_STRING, Renderers.TO_STRING)
|
||||
put(ErrorsJs.JSCODE_ARGUMENT_SHOULD_BE_LITERAL, "Argument must be string literal")
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
@@ -16,24 +16,29 @@
|
||||
|
||||
package org.jetbrains.k2js.resolve.diagnostics;
|
||||
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory0;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory1;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory2;
|
||||
import org.jetbrains.jet.lang.diagnostics.Errors;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import org.jetbrains.jet.lang.diagnostics.*;
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.diagnostics.PositioningStrategies.CALL_EXPRESSION;
|
||||
import static org.jetbrains.jet.lang.diagnostics.PositioningStrategies.DECLARATION_RETURN_TYPE;
|
||||
import static org.jetbrains.jet.lang.diagnostics.PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT;
|
||||
import static org.jetbrains.jet.lang.diagnostics.Severity.ERROR;
|
||||
import static org.jetbrains.jet.lang.diagnostics.Severity.WARNING;
|
||||
|
||||
public interface ErrorsJs {
|
||||
|
||||
DiagnosticFactory1<JetElement, JetType> NATIVE_ANNOTATIONS_ALLOWED_ONLY_ON_MEMBER_OR_EXTENSION_FUN = DiagnosticFactory1.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
|
||||
DiagnosticFactory1<JetElement, String> NATIVE_INDEXER_KEY_SHOULD_BE_STRING_OR_NUMBER = DiagnosticFactory1.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
|
||||
DiagnosticFactory0<JetDeclaration> NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE = DiagnosticFactory0.create(ERROR, DECLARATION_RETURN_TYPE);
|
||||
DiagnosticFactory2<JetElement, Integer, String> NATIVE_INDEXER_WRONG_PARAMETER_COUNT = DiagnosticFactory2.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
|
||||
DiagnosticFactory2<JetExpression, String, List<TextRange>> JSCODE_ERROR = DiagnosticFactory2.create(ERROR, JsCodePositioningStrategy.INSTANCE$);
|
||||
DiagnosticFactory2<JetExpression, String, List<TextRange>> JSCODE_WARNING = DiagnosticFactory2.create(WARNING, JsCodePositioningStrategy.INSTANCE$);
|
||||
DiagnosticFactory0<JetExpression> JSCODE_ARGUMENT_SHOULD_BE_LITERAL = DiagnosticFactory0.create(ERROR, CALL_EXPRESSION);
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
Object _initializer = new Object() {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2014 JetBrains s.r.o.
|
||||
*
|
||||
* 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 org.jetbrains.k2js.resolve.diagnostics
|
||||
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters1
|
||||
import org.jetbrains.jet.lang.diagnostics.PositioningStrategy
|
||||
import org.jetbrains.jet.lang.psi.JetExpression
|
||||
import org.jetbrains.jet.renderer.Renderer
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2
|
||||
import org.jetbrains.jet.lang.diagnostics.ParametrizedDiagnostic
|
||||
|
||||
public object JsCodePositioningStrategy : PositioningStrategy<PsiElement>() {
|
||||
override fun markDiagnostic(diagnostic: ParametrizedDiagnostic<out PsiElement>): List<TextRange> {
|
||||
[suppress("UNCHECKED_CAST")]
|
||||
val diagnosticWithParameters = diagnostic as DiagnosticWithParameters2<JetExpression, String, List<TextRange>>
|
||||
return diagnosticWithParameters.getB()
|
||||
}
|
||||
}
|
||||
+5
-12
@@ -15,16 +15,9 @@
|
||||
*/
|
||||
package com.google.gwt.dev.js;
|
||||
|
||||
// An unchecked wrapper exception to interop with Rhino.
|
||||
class UncheckedJsParserException extends RuntimeException {
|
||||
|
||||
private final JsParserException parserException;
|
||||
|
||||
public UncheckedJsParserException(JsParserException parserException) {
|
||||
this.parserException = parserException;
|
||||
}
|
||||
|
||||
public JsParserException getParserException() {
|
||||
return parserException;
|
||||
}
|
||||
/**
|
||||
* Used only to exit parser on error
|
||||
*/
|
||||
public class AbortParsingException extends RuntimeException {
|
||||
public AbortParsingException() {}
|
||||
}
|
||||
@@ -79,9 +79,6 @@ public class JsParser {
|
||||
popScope();
|
||||
return stmts;
|
||||
}
|
||||
catch (UncheckedJsParserException e) {
|
||||
throw e.getParserException();
|
||||
}
|
||||
finally {
|
||||
Context.exit();
|
||||
}
|
||||
|
||||
+72
-9
@@ -21,30 +21,37 @@ import com.google.dart.compiler.backend.js.ast.metadata.MetadataPackage;
|
||||
import com.google.dart.compiler.common.SourceInfoImpl;
|
||||
import com.google.gwt.dev.js.JsParser;
|
||||
import com.google.gwt.dev.js.JsParserException;
|
||||
import com.google.gwt.dev.js.AbortParsingException;
|
||||
import com.google.gwt.dev.js.rhino.ErrorReporter;
|
||||
import com.google.gwt.dev.js.rhino.EvaluatorException;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.psi.JetCallExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory2;
|
||||
import org.jetbrains.jet.lang.diagnostics.ParametrizedDiagnostic;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.calls.callUtil.CallUtilPackage;
|
||||
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||
import org.jetbrains.jet.lang.resolve.calls.model.VariableAsFunctionResolvedCall;
|
||||
import org.jetbrains.jet.lang.types.lang.InlineStrategy;
|
||||
import org.jetbrains.jet.lang.types.lang.InlineUtil;
|
||||
import org.jetbrains.jet.lang.psi.ValueArgument;
|
||||
import org.jetbrains.k2js.translate.callTranslator.CallTranslator;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.DescriptorPredicate;
|
||||
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder;
|
||||
import org.jetbrains.k2js.resolve.diagnostics.ErrorsJs;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static com.google.gwt.dev.js.rhino.Utils.isEndOfLine;
|
||||
import static org.jetbrains.jet.lang.resolve.calls.callUtil.CallUtilPackage.getFunctionResolvedCallWithAssert;
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getCompileTimeValue;
|
||||
|
||||
public final class CallExpressionTranslator extends AbstractCallExpressionTranslator {
|
||||
|
||||
@@ -126,13 +133,17 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
|
||||
private JsNode translateJsCode() {
|
||||
List<? extends ValueArgument> arguments = expression.getValueArguments();
|
||||
JetExpression argumentExpression = arguments.get(0).getArgumentExpression();
|
||||
assert argumentExpression != null;
|
||||
|
||||
if (!(argumentExpression instanceof JetStringTemplateExpression)) {
|
||||
context().getTrace().report(ErrorsJs.JSCODE_ARGUMENT_SHOULD_BE_LITERAL.on(expression));
|
||||
return program().getEmptyExpression();
|
||||
}
|
||||
|
||||
List<JsStatement> statements = parseJsCode(argumentExpression);
|
||||
int size = statements.size();
|
||||
|
||||
if (size == 0) {
|
||||
return program().getEmptyStatement();
|
||||
return program().getEmptyExpression();
|
||||
} else if (size > 1) {
|
||||
return new JsBlock(statements);
|
||||
} else {
|
||||
@@ -158,6 +169,9 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
|
||||
JsScope scope = context().scope();
|
||||
StringReader reader = new StringReader((String) jsCode);
|
||||
statements.addAll(JsParser.parse(info, scope, reader, errorReporter, /* insideFunction= */ true));
|
||||
} catch (AbortParsingException e) {
|
||||
/** @see JsCodeErrorReporter#error */
|
||||
return Collections.emptyList();
|
||||
} catch (JsParserException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
@@ -170,12 +184,22 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
|
||||
private class JsCodeErrorReporter implements ErrorReporter {
|
||||
@NotNull
|
||||
private final JetExpression jsCodeExpression;
|
||||
|
||||
private JsCodeErrorReporter(@NotNull JetExpression expression) {
|
||||
jsCodeExpression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
|
||||
ParametrizedDiagnostic<JetExpression> diagnostic = getDiagnostic(ErrorsJs.JSCODE_ERROR, message, line, lineOffset);
|
||||
context().getTrace().report(diagnostic);
|
||||
throw new AbortParsingException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
|
||||
ParametrizedDiagnostic<JetExpression> diagnostic = getDiagnostic(ErrorsJs.JSCODE_WARNING, message, line, lineOffset);
|
||||
context().getTrace().report(diagnostic);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,11 +212,50 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
|
||||
throw new RuntimeException(message);
|
||||
}
|
||||
|
||||
private ParametrizedDiagnostic<JetExpression> getDiagnostic(
|
||||
@NotNull DiagnosticFactory2<JetExpression, String, List<TextRange>> diagnosticFactory,
|
||||
String message,
|
||||
int line,
|
||||
int lineOffset
|
||||
) {
|
||||
String text = (String) getCompileTimeValue(bindingContext(), jsCodeExpression);
|
||||
int offset = jsCodeExpression.getTextOffset() + offsetFromStart(text, line, lineOffset);
|
||||
|
||||
assert jsCodeExpression instanceof JetStringTemplateExpression: "js argument is expected to be compile-time string literal";
|
||||
int quotesLength = jsCodeExpression.getFirstChild().getTextLength();
|
||||
offset += quotesLength;
|
||||
|
||||
TextRange textRange = new TextRange(offset, offset + 1);
|
||||
return diagnosticFactory.on(jsCodeExpression, message, Collections.singletonList(textRange));
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not report warnings
|
||||
* Calculates an offset from the start of a text for a position,
|
||||
* defined by line and offset in that line.
|
||||
*/
|
||||
@Override
|
||||
public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
|
||||
private int offsetFromStart(String text, int line, int offset) {
|
||||
int i = 0;
|
||||
int lineCount = 0;
|
||||
int offsetInLine = 0;
|
||||
|
||||
while (i < text.length()) {
|
||||
char c = text.charAt(i);
|
||||
|
||||
if (lineCount == line && offsetInLine == offset) {
|
||||
return i;
|
||||
}
|
||||
|
||||
if (isEndOfLine(c)) {
|
||||
offsetInLine = 0;
|
||||
lineCount++;
|
||||
assert lineCount <= line;
|
||||
}
|
||||
|
||||
i++;
|
||||
offsetInLine++;
|
||||
}
|
||||
|
||||
return text.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user