Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92c8501

Browse files
author
Max Schaefer
committed
JavaScript: Refactor parsing of generator/array comprehensions.
1 parent f3ea810 commit 92c8501

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

javascript/extractor/src/com/semmle/jcorn/CustomParser.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.semmle.js.ast.MemberExpression;
2929
import com.semmle.js.ast.NewExpression;
3030
import com.semmle.js.ast.Node;
31+
import com.semmle.js.ast.ParenthesizedExpression;
3132
import com.semmle.js.ast.Position;
3233
import com.semmle.js.ast.SourceLocation;
3334
import com.semmle.js.ast.Statement;
@@ -137,7 +138,9 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
137138
this.next();
138139
// check whether this is array comprehension or regular array
139140
if (this.type == TokenType._for) {
140-
return (Expression) this.parseComprehension(startLoc, false, null);
141+
ComprehensionExpression c = this.parseComprehension(startLoc, false, null);
142+
this.expect(TokenType.bracketR);
143+
return this.finishNode(c);
141144
}
142145
List<Expression> elements;
143146
if (this.type == TokenType.comma || this.type == TokenType.bracketR ||
@@ -147,7 +150,9 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
147150
Expression firstExpr = this.parseMaybeAssign(true, refDestructuringErrors, null);
148151
// check whether this is a postfix array comprehension
149152
if (this.type == TokenType._for || this.type == TokenType._if) {
150-
return (Expression) this.parseComprehension(startLoc, false, firstExpr);
153+
ComprehensionExpression c = this.parseComprehension(startLoc, false, firstExpr);
154+
this.expect(TokenType.bracketR);
155+
return this.finishNode(c);
151156
} else {
152157
this.eat(TokenType.comma);
153158
elements = new ArrayList<Expression>();
@@ -265,7 +270,7 @@ protected Expression parseMaybeAssign(boolean noIn,
265270
}
266271

267272
// add parsing of comprehensions
268-
protected Node parseComprehension(Position startLoc, boolean isGenerator, Expression body) {
273+
protected ComprehensionExpression parseComprehension(Position startLoc, boolean isGenerator, Expression body) {
269274
List<ComprehensionBlock> blocks = new ArrayList<ComprehensionBlock>();
270275
while (this.type == TokenType._for) {
271276
SourceLocation blockStart = new SourceLocation(this.startLoc);
@@ -287,9 +292,8 @@ protected Node parseComprehension(Position startLoc, boolean isGenerator, Expres
287292
Expression filter = this.eat(TokenType._if) ? this.parseParenExpression() : null;
288293
if (body == null)
289294
body = this.parseExpression(false, null);
290-
this.expect(isGenerator ? TokenType.parenR : TokenType.bracketR);
291295

292-
return this.finishNode(new ComprehensionExpression(new SourceLocation(startLoc), body, blocks, filter, isGenerator));
296+
return new ComprehensionExpression(new SourceLocation(startLoc), body, blocks, filter, isGenerator);
293297
}
294298

295299
@Override
@@ -303,7 +307,9 @@ protected Expression parseParenAndDistinguishExpression(boolean canBeArrow) {
303307
"for".equals(input.substring(m.end(), m.end()+3)) &&
304308
!Identifiers.isIdentifierChar(input.charAt(m.end()+3), true)) {
305309
next();
306-
return (Expression) parseComprehension(startLoc, true, null);
310+
ComprehensionExpression c = parseComprehension(startLoc, true, null);
311+
this.expect(TokenType.parenR);
312+
return this.finishNode(c);
307313
}
308314
}
309315
}

0 commit comments

Comments
 (0)