From 31ff23cd2e6b5190dbe92f81b7d5dc732adb75d6 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Mon, 9 Aug 2004 18:04:18 +0000 Subject: [PATCH] Simpler SyntxError handling: Since changes to fix bug 254778 made the parser the sole source of syntax error reports, it removed the need to check for errors after tree transformation. The patch removes those checks and moves all reporting about syntax errors into omj/Parser.java. --- .../mozilla/javascript/CompilerEnvirons.java | 44 ---------- .../src/org/mozilla/javascript/Context.java | 83 +++++++++---------- .../javascript/DefaultErrorReporter.java | 54 +++++++++--- .../org/mozilla/javascript/Interpreter.java | 1 - .../org/mozilla/javascript/NativeScript.java | 4 +- .../src/org/mozilla/javascript/Parser.java | 52 +++++++----- .../org/mozilla/javascript/ScriptRuntime.java | 6 +- .../org/mozilla/javascript/TokenStream.java | 22 ++--- .../javascript/optimizer/ClassCompiler.java | 82 ++++++++---------- 9 files changed, 166 insertions(+), 182 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/CompilerEnvirons.java b/js/rhino/src/org/mozilla/javascript/CompilerEnvirons.java index 0b8761456efd..610a309278a7 100644 --- a/js/rhino/src/org/mozilla/javascript/CompilerEnvirons.java +++ b/js/rhino/src/org/mozilla/javascript/CompilerEnvirons.java @@ -45,8 +45,6 @@ public class CompilerEnvirons public CompilerEnvirons() { this.errorReporter = DefaultErrorReporter.instance; - this.syntaxErrorCount = 0; - this.fromEval = false; this.languageVersion = Context.VERSION_DEFAULT; this.generateDebugInfo = true; this.useDynamicScope = false; @@ -74,11 +72,6 @@ public class CompilerEnvirons this.activationNames = cx.activationNames; } - public final int getSyntaxErrorCount() - { - return syntaxErrorCount; - } - public final ErrorReporter getErrorReporter() { return errorReporter; @@ -90,40 +83,6 @@ public class CompilerEnvirons this.errorReporter = errorReporter; } - final void reportSyntaxError(String message, - String sourceName, int lineno, - String lineText, int lineOffset) - { - ++syntaxErrorCount; - if (fromEval) { - // We're probably in an eval. Need to throw an exception. - throw ScriptRuntime.constructError( - "SyntaxError", message, sourceName, - lineno, lineText, lineOffset); - } else { - getErrorReporter().error(message, sourceName, lineno, - lineText, lineOffset); - } - } - - final void reportSyntaxWarning(String message, - String sourceName, int lineno, - String lineText, int lineOffset) - { - getErrorReporter().warning(message, sourceName, - lineno, lineText, lineOffset); - } - - final boolean isFromEval() - { - return fromEval; - } - - final void setFromEval(boolean fromEval) - { - this.fromEval = fromEval; - } - public final int getLanguageVersion() { return languageVersion; @@ -191,9 +150,6 @@ public class CompilerEnvirons } private ErrorReporter errorReporter; - private int syntaxErrorCount; - - private boolean fromEval; int languageVersion = Context.VERSION_DEFAULT; boolean generateDebugInfo = true; diff --git a/js/rhino/src/org/mozilla/javascript/Context.java b/js/rhino/src/org/mozilla/javascript/Context.java index 62d445551cb0..ec6a0dd8f513 100644 --- a/js/rhino/src/org/mozilla/javascript/Context.java +++ b/js/rhino/src/org/mozilla/javascript/Context.java @@ -1197,11 +1197,10 @@ public class Context boolean errorseen = false; CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.initFromContext(this); - compilerEnv.setErrorReporter(DefaultErrorReporter.instance); // no source name or source text manager, because we're just // going to throw away the result. compilerEnv.setGeneratingSource(false); - Parser p = new Parser(compilerEnv); + Parser p = new Parser(compilerEnv, DefaultErrorReporter.instance); try { p.parse(source, null, 1); } catch (EvaluatorException ee) { @@ -1255,7 +1254,7 @@ public class Context "Line number can not be negative:"+lineno); } return (Script) compile(null, in, null, sourceName, lineno, - securityDomain, false, false); + securityDomain, false, null); } /** @@ -1281,16 +1280,18 @@ public class Context throw new IllegalArgumentException( "Line number can not be negative:"+lineno); } - return compileString(source, false, sourceName, lineno, securityDomain); + return compileString(source, null, sourceName, lineno, securityDomain); } - final Script compileString(String source, boolean fromEval, + final Script compileString(String source, + ErrorReporter compilationErrorReporter, String sourceName, int lineno, Object securityDomain) { try { return (Script) compile(null, null, source, sourceName, lineno, - securityDomain, false, fromEval); + securityDomain, false, + compilationErrorReporter); } catch (IOException ex) { // Should not happen when dealing with source as string throw new RuntimeException(); @@ -1320,7 +1321,7 @@ public class Context { try { return (Function) compile(scope, null, source, sourceName, lineno, - securityDomain, true, false); + securityDomain, true, null); } catch (IOException ioe) { // Should never happen because we just made the reader @@ -2290,7 +2291,7 @@ public class Context Reader sourceReader, String sourceString, String sourceName, int lineno, Object securityDomain, boolean returnFunction, - boolean fromEval) + ErrorReporter compilationErrorReporter) throws IOException { if (securityDomain != null && securityController == null) { @@ -2305,7 +2306,9 @@ public class Context CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.initFromContext(this); - compilerEnv.setFromEval(fromEval); + if (compilationErrorReporter == null) { + compilationErrorReporter = compilerEnv.getErrorReporter(); + } if (debugger != null) { if (sourceReader != null) { @@ -2314,50 +2317,40 @@ public class Context } } - Parser p = new Parser(compilerEnv); + Parser p = new Parser(compilerEnv, compilationErrorReporter); ScriptOrFnNode tree; if (sourceString != null) { tree = p.parse(sourceString, sourceName, lineno); } else { tree = p.parse(sourceReader, sourceName, lineno); } - int syntaxErrorCount = compilerEnv.getSyntaxErrorCount(); - if (syntaxErrorCount == 0) { - if (returnFunction) { - if (!(tree.getFunctionCount() == 1 - && tree.getFirstChild() != null - && tree.getFirstChild().getType() == Token.FUNCTION)) - { - // XXX: the check just look for the first child - // and allows for more nodes after it for compatibility - // with sources like function() {};;; - throw new IllegalArgumentException( - "compileFunction only accepts source with single JS function: "+sourceString); - } - } - - Interpreter compiler = createCompiler(); - - String encodedSource = p.getEncodedSource(); - - Object result = compiler.compile(scope, compilerEnv, - tree, encodedSource, - returnFunction, - securityDomain); - syntaxErrorCount = compilerEnv.getSyntaxErrorCount(); - if (syntaxErrorCount == 0) { - if (debugger != null) { - if (sourceString == null) Kit.codeBug(); - compiler.notifyDebuggerCompilationDone(this, result, - sourceString); - } - return result; + if (returnFunction) { + if (!(tree.getFunctionCount() == 1 + && tree.getFirstChild() != null + && tree.getFirstChild().getType() == Token.FUNCTION)) + { + // XXX: the check just look for the first child + // and allows for more nodes after it for compatibility + // with sources like function() {};;; + throw new IllegalArgumentException( + "compileFunction only accepts source with single JS function: "+sourceString); } } - String msg = Context.getMessage1("msg.got.syntax.errors", - String.valueOf(syntaxErrorCount)); - throw compilerEnv.getErrorReporter(). - runtimeError(msg, sourceName, lineno, null, 0); + + Interpreter compiler = createCompiler(); + + String encodedSource = p.getEncodedSource(); + + Object result = compiler.compile(scope, compilerEnv, + tree, encodedSource, + returnFunction, + securityDomain); + if (debugger != null) { + if (sourceString == null) Kit.codeBug(); + compiler.notifyDebuggerCompilationDone(this, result, + sourceString); + } + return result; } private static Class codegenClass = Kit.classOrNull( diff --git a/js/rhino/src/org/mozilla/javascript/DefaultErrorReporter.java b/js/rhino/src/org/mozilla/javascript/DefaultErrorReporter.java index 798ad3620fd1..24a43771fdbb 100644 --- a/js/rhino/src/org/mozilla/javascript/DefaultErrorReporter.java +++ b/js/rhino/src/org/mozilla/javascript/DefaultErrorReporter.java @@ -44,24 +44,56 @@ class DefaultErrorReporter implements ErrorReporter { static final DefaultErrorReporter instance = new DefaultErrorReporter(); - public void warning(String message, String sourceName, int line, - String lineSource, int lineOffset) + private boolean forEval; + private ErrorReporter chainedReporter; + + private DefaultErrorReporter() { } + + static ErrorReporter forEval(ErrorReporter reporter) { - // do nothing + DefaultErrorReporter r = new DefaultErrorReporter(); + r.forEval = true; + r.chainedReporter = reporter; + return r; } - public void error(String message, String sourceName, int line, - String lineSource, int lineOffset) + public void warning(String message, String sourceURI, int line, + String lineText, int lineOffset) { - throw new EvaluatorException(message, sourceName, line, - lineSource, lineOffset); + if (chainedReporter != null) { + chainedReporter.warning( + message, sourceURI, line, lineText, lineOffset); + } else { + // Do nothing + } } - public EvaluatorException runtimeError(String message, String sourceName, - int line, String lineSource, + public void error(String message, String sourceURI, int line, + String lineText, int lineOffset) + { + if (forEval) { + throw ScriptRuntime.constructError( + "SyntaxError", message, sourceURI, line, lineText, lineOffset); + } + if (chainedReporter != null) { + chainedReporter.error( + message, sourceURI, line, lineText, lineOffset); + } else { + throw runtimeError( + message, sourceURI, line, lineText, lineOffset); + } + } + + public EvaluatorException runtimeError(String message, String sourceURI, + int line, String lineText, int lineOffset) { - throw new EvaluatorException(message, sourceName, line, - lineSource, lineOffset); + if (chainedReporter != null) { + return chainedReporter.runtimeError( + message, sourceURI, line, lineText, lineOffset); + } else { + return new EvaluatorException( + message, sourceURI, line, lineText, lineOffset); + } } } diff --git a/js/rhino/src/org/mozilla/javascript/Interpreter.java b/js/rhino/src/org/mozilla/javascript/Interpreter.java index 3e502e800921..c4b8575346b1 100644 --- a/js/rhino/src/org/mozilla/javascript/Interpreter.java +++ b/js/rhino/src/org/mozilla/javascript/Interpreter.java @@ -298,7 +298,6 @@ public class Interpreter generateFunctionICode(); return createFunction(cx, scope, itsData, false); } else { - itsData.itsFromEvalCode = compilerEnv.isFromEval(); generateICodeFromTree(scriptOrFn); return new InterpretedScript(itsData); } diff --git a/js/rhino/src/org/mozilla/javascript/NativeScript.java b/js/rhino/src/org/mozilla/javascript/NativeScript.java index d316edc956cd..5b7b1ea3ae22 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeScript.java +++ b/js/rhino/src/org/mozilla/javascript/NativeScript.java @@ -196,7 +196,9 @@ class NativeScript extends NativeFunction implements Script filename = "