JS parser: pass ErrorReporter to JsParser

This commit is contained in:
Alexey Tsvetkov
2014-11-27 17:25:18 +03:00
parent 3658698f4f
commit 7c6629e38a
2 changed files with 46 additions and 31 deletions
@@ -25,6 +25,7 @@ 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;
@@ -39,9 +40,11 @@ public class JsParser {
public static List<JsStatement> parse(
SourceInfo rootSourceInfo,
JsScope scope, Reader r, boolean insideFunction
JsScope scope, Reader r,
ErrorReporter errorReporter,
boolean insideFunction
) throws IOException, JsParserException {
return new JsParser(scope.getProgram()).parseImpl(rootSourceInfo, scope, r, insideFunction);
return new JsParser(scope.getProgram()).parseImpl(rootSourceInfo, scope, r, errorReporter, insideFunction);
}
private final Stack<JsScope> scopeStack = new Stack<JsScope>();
@@ -57,31 +60,12 @@ public class JsParser {
List<JsStatement> parseImpl(
final SourceInfo rootSourceInfo, JsScope scope,
Reader r, boolean insideFunction
Reader r,
ErrorReporter errorReporter,
boolean insideFunction
) throws JsParserException, IOException {
// Create a custom error handler so that we can throw our own exceptions.
Context.enter().setErrorReporter(new ErrorReporter() {
@Override
public void error(String msg, String loc, int ln, String src, int col) {
throw new UncheckedJsParserException(new JsParserException(msg, ln,
src, col, SOURCE_NAME_STUB));
}
@Override
public EvaluatorException runtimeError(
String msg, String loc, int ln,
String src, int col
) {
// Never called, but just in case.
throw new UncheckedJsParserException(new JsParserException(msg, ln,
src, col, SOURCE_NAME_STUB));
}
@Override
public void warning(String msg, String loc, int ln, String src, int col) {
// Ignore warnings.
}
});
Context.enter().setErrorReporter(errorReporter);
try {
// Parse using the Rhino parser.
//
@@ -128,9 +128,7 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
JetExpression argumentExpression = arguments.get(0).getArgumentExpression();
assert argumentExpression != null;
Object jsCode = BindingUtils.getCompileTimeValue(bindingContext(), argumentExpression);
assert jsCode instanceof String: "jsCode must be compile time string";
List<JsStatement> statements = parseJsCode((String) jsCode);
List<JsStatement> statements = parseJsCode(argumentExpression);
int size = statements.size();
if (size == 0) {
@@ -148,14 +146,18 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
}
@NotNull
private List<JsStatement> parseJsCode(@NotNull String jsCode) {
private List<JsStatement> parseJsCode(@NotNull JetExpression jsCodeExpression) {
Object jsCode = getCompileTimeValue(bindingContext(), jsCodeExpression);
assert jsCode instanceof String: "jsCode must be compile time string";
List<JsStatement> statements = new ArrayList<JsStatement>();
ErrorReporter errorReporter = new JsCodeErrorReporter(jsCodeExpression);
try {
SourceInfoImpl info = new SourceInfoImpl(null, 0, 0, 0, 0);
JsScope scope = context().scope();
StringReader reader = new StringReader(jsCode);
statements.addAll(JsParser.parse(info, scope, reader, /* insideFunction= */ true));
StringReader reader = new StringReader((String) jsCode);
statements.addAll(JsParser.parse(info, scope, reader, errorReporter, /* insideFunction= */ true));
} catch (JsParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
@@ -164,4 +166,33 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
return statements;
}
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) {
}
/**
* TODO: seems not called anywhere, so remove
*/
@Override
public EvaluatorException runtimeError(
String message, String sourceName, int line, String lineSource, int lineOffset
) {
throw new RuntimeException(message);
}
/**
* Do not report warnings
*/
@Override
public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
}
}
}