JS parser: added insideFunction flag

This commit is contained in:
Alexey Tsvetkov
2014-10-20 19:04:20 +04:00
parent 420df315b4
commit 5256a99c54
2 changed files with 11 additions and 9 deletions
@@ -33,9 +33,9 @@ public class JsParser {
public static List<JsStatement> parse( public static List<JsStatement> parse(
SourceInfo rootSourceInfo, SourceInfo rootSourceInfo,
JsScope scope, Reader r JsScope scope, Reader r, boolean insideFunction
) throws IOException, JsParserException { ) throws IOException, JsParserException {
return new JsParser().parseImpl(rootSourceInfo, scope, r); return new JsParser().parseImpl(rootSourceInfo, scope, r, insideFunction);
} }
private final Stack<JsScope> scopeStack = new Stack<JsScope>(); private final Stack<JsScope> scopeStack = new Stack<JsScope>();
@@ -50,7 +50,7 @@ public class JsParser {
List<JsStatement> parseImpl( List<JsStatement> parseImpl(
final SourceInfo rootSourceInfo, JsScope scope, final SourceInfo rootSourceInfo, JsScope scope,
Reader r Reader r, boolean insideFunction
) throws JsParserException, IOException { ) throws JsParserException, IOException {
// Create a custom error handler so that we can throw our own exceptions. // Create a custom error handler so that we can throw our own exceptions.
Context.enter().setErrorReporter(new ErrorReporter() { Context.enter().setErrorReporter(new ErrorReporter() {
@@ -79,7 +79,7 @@ public class JsParser {
// Parse using the Rhino parser. // Parse using the Rhino parser.
// //
TokenStream ts = new TokenStream(r, sourceNameStub, rootSourceInfo.getLine()); TokenStream ts = new TokenStream(r, sourceNameStub, rootSourceInfo.getLine());
Parser parser = new Parser(new IRFactory(ts)); Parser parser = new Parser(new IRFactory(ts), insideFunction);
Node topNode = (Node) parser.parse(ts); Node topNode = (Node) parser.parse(ts);
// Map the Rhino AST to ours. // Map the Rhino AST to ours.
@@ -52,9 +52,9 @@ import java.io.IOException;
*/ */
public class Parser { public class Parser {
public Parser(IRFactory nf, boolean insideFunction) {
public Parser(IRFactory nf) { this.nf = nf;
this.nf = nf; this.insideFunction = insideFunction;
} }
private void mustMatchToken(TokenStream ts, int toMatch, String messageId) private void mustMatchToken(TokenStream ts, int toMatch, String messageId)
@@ -729,8 +729,9 @@ public class Parser {
sourceAdd((char) ts.RETURN); sourceAdd((char) ts.RETURN);
// bail if we're not in a (toplevel) function // bail if we're not in a (toplevel) function
if ((ts.flags & ts.TSF_FUNCTION) == 0) if ((!insideFunction) && ((ts.flags & ts.TSF_FUNCTION) == 0)) {
reportError(ts, "msg.bad.return"); reportError(ts, "msg.bad.return");
}
/* This is ugly, but we don't want to require a semicolon. */ /* This is ugly, but we don't want to require a semicolon. */
ts.flags |= ts.TSF_REGEXP; ts.flags |= ts.TSF_REGEXP;
@@ -1516,4 +1517,5 @@ public class Parser {
private char[] sourceBuffer = new char[128]; private char[] sourceBuffer = new char[128];
private int sourceTop; private int sourceTop;
private int functionNumber; private int functionNumber;
private final boolean insideFunction;
} }