Skip to content

Commit

Permalink
Make it possible to throw SyntaxError in IRFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchida committed Aug 27, 2023
1 parent 07b7ca1 commit d378847
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 178 deletions.
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -2515,7 +2515,7 @@ private ScriptNode parse(
}
}

IRFactory irf = new IRFactory(compilerEnv, compilationErrorReporter);
IRFactory irf = new IRFactory(compilerEnv, sourceString, compilationErrorReporter);
ScriptNode tree = irf.transformTree(ast);

if (compilerEnv.isGeneratingSource()) {
Expand Down
389 changes: 267 additions & 122 deletions src/org/mozilla/javascript/IRFactory.java

Large diffs are not rendered by default.

90 changes: 43 additions & 47 deletions src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public class Parser {
private boolean parseFinished; // set when finished to prevent reuse

private TokenStream ts;
CurrentPositionReporter currentPos;
private int currentFlaggedToken = Token.EOF;
private int currentToken;
private int syntaxErrorCount;
Expand Down Expand Up @@ -156,7 +157,7 @@ public class Parser {
private boolean defaultUseStrictDirective;

// Exception to unwind
private static class ParserException extends RuntimeException {
public static class ParserException extends RuntimeException {
private static final long serialVersionUID = 5882582646773765630L;
}

Expand All @@ -178,25 +179,15 @@ public Parser(CompilerEnvirons compilerEnv, ErrorReporter errorReporter) {

// Add a strict warning on the last matched token.
void addStrictWarning(String messageId, String messageArg) {
int beg = -1, end = -1;
if (ts != null) {
beg = ts.tokenBeg;
end = ts.tokenEnd - ts.tokenBeg;
}
addStrictWarning(messageId, messageArg, beg, end);
addStrictWarning(messageId, messageArg, currentPos.getPosition(), currentPos.getLength());
}

void addStrictWarning(String messageId, String messageArg, int position, int length) {
if (compilerEnv.isStrictMode()) addWarning(messageId, messageArg, position, length);
}

void addWarning(String messageId, String messageArg) {
int beg = -1, end = -1;
if (ts != null) {
beg = ts.tokenBeg;
end = ts.tokenEnd - ts.tokenBeg;
}
addWarning(messageId, messageArg, beg, end);
addWarning(messageId, messageArg, currentPos.getPosition(), currentPos.getLength());
}

void addWarning(String messageId, int position, int length) {
Expand All @@ -209,31 +200,26 @@ void addWarning(String messageId, String messageArg, int position, int length) {
addError(messageId, messageArg, position, length);
} else if (errorCollector != null) {
errorCollector.warning(message, sourceURI, position, length);
} else if (ts != null) {
errorReporter.warning(message, sourceURI, ts.getLineno(), ts.getLine(), ts.getOffset());
} else {
errorReporter.warning(message, sourceURI, 1, "", 1);
errorReporter.warning(
message,
sourceURI,
currentPos.getLineno(),
currentPos.getLine(),
currentPos.getOffset());
}
}

void addError(String messageId) {
if (ts == null) {
addError(messageId, 0, 0);
} else {
addError(messageId, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg);
}
addError(messageId, currentPos.getPosition(), currentPos.getLength());
}

void addError(String messageId, int position, int length) {
addError(messageId, null, position, length);
}

void addError(String messageId, String messageArg) {
if (ts == null) {
addError(messageId, messageArg, 0, 0);
} else {
addError(messageId, messageArg, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg);
}
addError(messageId, messageArg, currentPos.getPosition(), currentPos.getLength());
}

void addError(String messageId, int c) {
Expand All @@ -247,14 +233,12 @@ void addError(String messageId, String messageArg, int position, int length) {
if (errorCollector != null) {
errorCollector.error(message, sourceURI, position, length);
} else {
int lineno = 1, offset = 1;
String line = "";
if (ts != null) { // happens in some regression tests
lineno = ts.getLineno();
line = ts.getLine();
offset = ts.getOffset();
}
errorReporter.error(message, sourceURI, lineno, line, offset);
errorReporter.error(
message,
sourceURI,
currentPos.getLineno(),
currentPos.getLine(),
currentPos.getOffset());
}
}

Expand Down Expand Up @@ -321,11 +305,7 @@ void reportError(String messageId) {
}

void reportError(String messageId, String messageArg) {
if (ts == null) { // happens in some regression tests
reportError(messageId, messageArg, 1, 1);
} else {
reportError(messageId, messageArg, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg);
}
reportError(messageId, messageArg, currentPos.getPosition(), currentPos.getLength());
}

void reportError(String messageId, int position, int length) {
Expand Down Expand Up @@ -556,7 +536,7 @@ public AstRoot parse(String sourceString, String sourceURI, int lineno) {
if (compilerEnv.isIdeMode()) {
this.sourceChars = sourceString.toCharArray();
}
this.ts = new TokenStream(this, null, sourceString, lineno);
currentPos = ts = new TokenStream(this, null, sourceString, lineno);
try {
return parse();
} catch (IOException iox) {
Expand All @@ -582,7 +562,7 @@ public AstRoot parse(Reader sourceReader, String sourceURI, int lineno) throws I
}
try {
this.sourceURI = sourceURI;
ts = new TokenStream(this, sourceReader, null, lineno);
currentPos = ts = new TokenStream(this, sourceReader, null, lineno);
return parse();
} finally {
parseFinished = true;
Expand Down Expand Up @@ -651,12 +631,7 @@ private AstRoot parse() throws IOException {
inUseStrictDirective = savedStrictMode;
}

if (this.syntaxErrorCount != 0) {
String msg = String.valueOf(this.syntaxErrorCount);
msg = lookupMessage("msg.got.syntax.errors", msg);
if (!compilerEnv.isIdeMode())
throw errorReporter.runtimeError(msg, sourceURI, baseLineno, null, 0);
}
reportErrorsIfExists(baseLineno);

// add comments to root in lexical order
if (scannedComments != null) {
Expand Down Expand Up @@ -4267,4 +4242,25 @@ public void setDefaultUseStrictDirective(boolean useStrict) {
public boolean inUseStrictDirective() {
return inUseStrictDirective;
}

public void reportErrorsIfExists(int baseLineno) {
if (this.syntaxErrorCount != 0) {
String msg = String.valueOf(this.syntaxErrorCount);
msg = lookupMessage("msg.got.syntax.errors", msg);
if (!compilerEnv.isIdeMode())
throw errorReporter.runtimeError(msg, sourceURI, baseLineno, null, 0);
}
}

public interface CurrentPositionReporter {
public int getPosition();

public int getLength();

public int getLineno();

public String getLine();

public int getOffset();
}
}
19 changes: 15 additions & 4 deletions src/org/mozilla/javascript/TokenStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author Mike McCabe
* @author Brendan Eich
*/
class TokenStream {
class TokenStream implements Parser.CurrentPositionReporter {
/*
* For chars - because we need something out-of-range
* to check. (And checking EOF by exception is annoying.)
Expand Down Expand Up @@ -580,7 +580,8 @@ final String getSourceString() {
return sourceString;
}

final int getLineno() {
@Override
public int getLineno() {
return lineno;
}

Expand Down Expand Up @@ -2097,7 +2098,8 @@ private void skipLine() throws IOException {
}

/** Returns the offset into the current line. */
final int getOffset() {
@Override
public int getOffset() {
int n = sourceCursor - lineStart;
if (lineEndChar >= 0) {
--n;
Expand Down Expand Up @@ -2140,7 +2142,8 @@ private final String substring(int beginIndex, int endIndex) {
return new String(sourceBuffer, beginIndex, count);
}

final String getLine() {
@Override
public String getLine() {
int lineEnd = sourceCursor;
if (lineEndChar >= 0) {
// move cursor before newline sequence
Expand Down Expand Up @@ -2293,6 +2296,14 @@ private static String convertLastCharToHex(String str) {
return buf.toString();
}

public int getPosition() {
return tokenBeg;
}

public int getLength() {
return tokenEnd - tokenBeg;
}

// stuff other than whitespace since start of line
private boolean dirtyLine;

Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/optimizer/ClassCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Object[] compileToClassFiles(
String source, String sourceLocation, int lineno, String mainClassName) {
Parser p = new Parser(compilerEnv);
AstRoot ast = p.parse(source, sourceLocation, lineno);
IRFactory irf = new IRFactory(compilerEnv);
IRFactory irf = new IRFactory(compilerEnv, source);
ScriptNode tree = irf.transformTree(ast);

if (compilerEnv.isGeneratingSource()) {
Expand Down
2 changes: 1 addition & 1 deletion testsrc/org/mozilla/javascript/tests/Bug708801Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected ScriptNode compile(CharSequence source) {
ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
Parser p = new Parser(compilerEnv, compilationErrorReporter);
AstRoot ast = p.parse(source.toString(), "<eval>", 1);
IRFactory irf = new IRFactory(compilerEnv);
IRFactory irf = new IRFactory(compilerEnv, source.toString());
ScriptNode tree = irf.transformTree(ast);

Codegen codegen = new Codegen();
Expand Down
2 changes: 1 addition & 1 deletion testsrc/org/mozilla/javascript/tests/Bug782363Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected ScriptNode compile(CharSequence source) {
compilerEnv.initFromContext(cx);
Parser p = new Parser(compilerEnv);
AstRoot ast = p.parse(source.toString(), "<eval>", 1);
IRFactory irf = new IRFactory(compilerEnv);
IRFactory irf = new IRFactory(compilerEnv, source.toString());
ScriptNode tree = irf.transformTree(ast);

Codegen codegen = new Codegen();
Expand Down
2 changes: 1 addition & 1 deletion testsrc/org/mozilla/javascript/tests/ReadCommentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void readComments() throws IOException {
testJs = scriptIn.lines().collect(Collectors.joining(System.lineSeparator()));
}
AstRoot ast = p.parse(testJs, "test", 1);
IRFactory irf = new IRFactory(compilerEnv);
IRFactory irf = new IRFactory(compilerEnv, testJs);
ScriptNode tree = irf.transformTree(ast);

Assert.assertEquals(1, tree.getFunctions().size());
Expand Down

0 comments on commit d378847

Please sign in to comment.