Skip to content

Commit

Permalink
fix: loop expression
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Sep 27, 2023
1 parent d8746a2 commit 82df77c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ build/

# stryker temp files
.stryker-tmp

Mac
.DS_Store
13 changes: 11 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const EMPTY_EXPR = { type: SyntaxType.EMPTY };
export class JsonTemplateParser {
private lexer: JsonTemplateLexer;
private options?: EngineOptions;
// indicates currently how many loops being parsed
private loopCount = 0;

constructor(lexer: JsonTemplateLexer, options?: EngineOptions) {
this.lexer = lexer;
Expand Down Expand Up @@ -451,9 +453,13 @@ export class JsonTemplateParser {
}

private parseLoopControlExpr(): LoopControlExpression {
const control = this.lexer.value();
if (!this.loopCount) {
throw new JsonTemplateParserError(`encounted loop control outside loop: ${control}`);
}
return {
type: SyntaxType.LOOP_CONTROL_EXPR,
control: this.lexer.value(),
control,
};
}

Expand Down Expand Up @@ -512,6 +518,7 @@ export class JsonTemplateParser {
}

private parseLoopExpr(): LoopExpression {
this.loopCount++;
this.lexer.ignoreTokens(1);
let init: Expression | undefined;
let test: Expression | undefined;
Expand All @@ -531,12 +538,14 @@ export class JsonTemplateParser {
}
this.lexer.expect(')');
}
const body = this.parseCurlyBlockExpr({ blockEnd: '}', parentType: SyntaxType.LOOP_EXPR });
this.loopCount--;
return {
type: SyntaxType.LOOP_EXPR,
init,
test,
update,
body: this.parseCurlyBlockExpr({ blockEnd: '}', parentType: SyntaxType.LOOP_EXPR }),
body,
};
}

Expand Down
4 changes: 3 additions & 1 deletion test/scenarios/loops/break_without_condition.jt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
continue;
for {
break;
}
2 changes: 2 additions & 0 deletions test/scenarios/loops/break_without_loop.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

(.num < 0) ? break;
4 changes: 3 additions & 1 deletion test/scenarios/loops/continue_without_condition.jt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
continue;
for {
continue;
}
2 changes: 2 additions & 0 deletions test/scenarios/loops/continue_without_loop.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

(.num < 0) ? continue;
8 changes: 8 additions & 0 deletions test/scenarios/loops/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const data: Scenario[] = [
error:
'return, throw, continue and break statements are only allowed as last statements in conditional expressions',
},
{
templatePath: 'break_without_loop.jt',
error: 'encounted loop control outside loop',
},
{
templatePath: 'complex_loop.jt',
output: 10,
Expand All @@ -15,6 +19,10 @@ export const data: Scenario[] = [
error:
'return, throw, continue and break statements are only allowed as last statements in conditional expressions',
},
{
templatePath: 'continue_without_loop.jt',
error: 'encounted loop control outside loop',
},
{
templatePath: 'continue.jt',
input: {
Expand Down
8 changes: 5 additions & 3 deletions test/scenarios/loops/statement_after_break.jt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.num > 1 ? {
continue;
let count = 0;
for {
.num > 1 ? {
break;
let count = 0;
}
}
8 changes: 5 additions & 3 deletions test/scenarios/loops/statement_after_continue.jt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.num > 1 ? {
continue;
let count = 0;
for {
.num > 1 ? {
continue;
let count = 0;
}
}

0 comments on commit 82df77c

Please sign in to comment.