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

Skip to content

Commit d3ae295

Browse files
author
Max Schaefer
committed
JavaScript: Add support for parsing postfix generator comprehensions.
1 parent bb93cef commit d3ae295

3 files changed

Lines changed: 667 additions & 507 deletions

File tree

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,29 @@ protected Expression parseParenAndDistinguishExpression(boolean canBeArrow) {
314314
}
315315
}
316316

317-
return super.parseParenAndDistinguishExpression(canBeArrow);
317+
Expression res = super.parseParenAndDistinguishExpression(canBeArrow);
318+
if (res instanceof ParenthesizedExpression) {
319+
ParenthesizedExpression p = (ParenthesizedExpression) res;
320+
if (p.getExpression() instanceof ComprehensionExpression) {
321+
ComprehensionExpression c = (ComprehensionExpression) p.getExpression();
322+
if (c.isGenerator())
323+
return new ComprehensionExpression(p.getLoc(), c.getBody(), c.getBlocks(), c.getFilter(), c.isGenerator());
324+
}
325+
}
326+
return res;
327+
}
328+
329+
@Override
330+
protected boolean parseParenthesisedExpression(DestructuringErrors refDestructuringErrors,
331+
boolean allowTrailingComma, ParenthesisedExpressions parenExprs, boolean first) {
332+
boolean cont = super.parseParenthesisedExpression(refDestructuringErrors, allowTrailingComma, parenExprs, first);
333+
if (options.mozExtensions() && parenExprs.exprList.size() == 1 && this.type == TokenType._for) {
334+
Expression body = parenExprs.exprList.remove(0);
335+
ComprehensionExpression c = parseComprehension(body.getLoc().getStart(), true, body);
336+
parenExprs.exprList.add(this.finishNode(c));
337+
return false;
338+
}
339+
return cont;
318340
}
319341

320342
// add parsing of for-each loops
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(for (year of years) if (year > 2000) year);
22
year;
33
(for (i of numbers) for (j of letters) i+j);
4-
(for (i of numbers) for (j of letters) if (i<j) i+j);
4+
(for (i of numbers) for (j of letters) if (i<j) i+j);
5+
(x.p for (x in xs));

0 commit comments

Comments
 (0)