Skip to content

Commit

Permalink
Handle for loops over ranges with unnamed arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Jun 2, 2024
1 parent 18b1e2b commit 41aee75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,4 +1064,16 @@ describe("Parser", () => {
echo(x);
`);
});

it("Can parse for loops with unnamed variables", () => {
doParse(`
for ([-1, 1]){
echo("should print two times");
}
intersection_for([-1, 1]){
echo("this also should print two times");
}
`)
})
});
15 changes: 10 additions & 5 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export default class Parser {
}
}
let isForLoop = name === "for" || name === "intersection_for";
const args = this.args(!isForLoop);
const args = this.args(true, isForLoop ? AssignmentNodeRole.VARIABLE_DECLARATION : null);
const secondParen = this.previous();
return new ModuleInstantiationStmt(name, args, null, {
firstParen,
Expand All @@ -462,7 +462,10 @@ export default class Parser {
* The initial paren must be consumed.
* @param allowPositional Set to true when in call mode, positional arguments will be allowed.
*/
protected args(allowPositional = false): AssignmentNode[] {
protected args(
allowPositional = false,
forceType: AssignmentNodeRole | null = null
): AssignmentNode[] {
this.consumeUselessCommas();
const args: AssignmentNode[] = [];
if (this.matchToken(TokenType.RightParen)) {
Expand Down Expand Up @@ -498,9 +501,11 @@ export default class Parser {
const arg = new AssignmentNode(
name,
value,
allowPositional
? AssignmentNodeRole.ARGUMENT_ASSIGNMENT
: AssignmentNodeRole.ARGUMENT_DECLARATION,
forceType == null
? allowPositional
? AssignmentNodeRole.ARGUMENT_ASSIGNMENT
: AssignmentNodeRole.ARGUMENT_DECLARATION
: forceType,
{
name: nameToken,
equals,
Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/Parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46391,7 +46391,7 @@ polyhedron [[i1,i2,i3],[i2,i3,i4],...]",
"__c": "AssignmentNode",
"docComment": null,
"name": "i",
"role": 1,
"role": 0,
"tokens": {
"__c": "Object",
"equals": {
Expand Down

0 comments on commit 41aee75

Please sign in to comment.