Skip to content

Commit

Permalink
More support for labels
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed May 1, 2024
1 parent bb3a468 commit 01df92b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/antlr/BoxTemplateGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ while:

// <bx:break> or... <bx:break />
break:
COMPONENT_OPEN PREFIX BREAK (
COMPONENT_OPEN PREFIX BREAK attribute* (
COMPONENT_CLOSE
| COMPONENT_SLASH_CLOSE
);

// <bx:continue> or... <bx:continue />
continue:
COMPONENT_OPEN PREFIX CONTINUE (
COMPONENT_OPEN PREFIX CONTINUE attribute* (
COMPONENT_CLOSE
| COMPONENT_SLASH_CLOSE
);
Expand Down
4 changes: 2 additions & 2 deletions src/main/antlr/CFTemplateGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ while:

// <cfbreak> or... <cfbreak />
break:
COMPONENT_OPEN PREFIX BREAK (
COMPONENT_OPEN PREFIX BREAK attribute* (
COMPONENT_CLOSE
| COMPONENT_SLASH_CLOSE
);

// <cfcontinue> or... <cfcontinue />
continue:
COMPONENT_OPEN PREFIX CONTINUE (
COMPONENT_OPEN PREFIX CONTINUE attribute* (
COMPONENT_CLOSE
| COMPONENT_SLASH_CLOSE
);
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/ortus/boxlang/compiler/parser/BoxTemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,25 @@ private BoxStatement toAst( File file, IncludeContext node ) {
}

private BoxStatement toAst( File file, ContinueContext node ) {
return new BoxContinue( getPosition( node ), getSourceText( node ) );
List<BoxAnnotation> annotations = new ArrayList<>();

for ( var attr : node.attribute() ) {
annotations.add( toAst( file, attr ) );
}
BoxExpression labelSearch = findExprInAnnotations( annotations, "label", false, null, "continue", getPosition( node ) );
String label = getBoxExprAsString( labelSearch, "label", false );
return new BoxContinue( label, getPosition( node ), getSourceText( node ) );
}

private BoxStatement toAst( File file, BreakContext node ) {
return new BoxBreak( getPosition( node ), getSourceText( node ) );
List<BoxAnnotation> annotations = new ArrayList<>();

for ( var attr : node.attribute() ) {
annotations.add( toAst( file, attr ) );
}
BoxExpression labelSearch = findExprInAnnotations( annotations, "label", false, null, "break", getPosition( node ) );
String label = getBoxExprAsString( labelSearch, "label", false );
return new BoxBreak( label, getPosition( node ), getSourceText( node ) );
}

private BoxStatement toAst( File file, WhileContext node ) {
Expand All @@ -569,7 +583,7 @@ private BoxStatement toAst( File file, WhileContext node ) {
body.addAll( toAst( file, node.statements() ) );
}
BoxExpression labelSearch = findExprInAnnotations( annotations, "label", false, null, "while", getPosition( node ) );
String label = getBoxExprAsString( labelSearch, "condition", false );
String label = getBoxExprAsString( labelSearch, "label", false );

return new BoxWhile( label, condition, body, getPosition( node ), getSourceText( node ) );
}
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/ortus/boxlang/compiler/parser/CFTemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,25 @@ private BoxStatement toAst( File file, IncludeContext node ) {
}

private BoxStatement toAst( File file, ContinueContext node ) {
return new BoxContinue( getPosition( node ), getSourceText( node ) );
List<BoxAnnotation> annotations = new ArrayList<>();

for ( var attr : node.attribute() ) {
annotations.add( toAst( file, attr ) );
}
BoxExpression labelSearch = findExprInAnnotations( annotations, "label", false, null, "continue", getPosition( node ) );
String label = getBoxExprAsString( labelSearch, "label", false );
return new BoxContinue( label, getPosition( node ), getSourceText( node ) );
}

private BoxStatement toAst( File file, BreakContext node ) {
return new BoxBreak( getPosition( node ), getSourceText( node ) );
List<BoxAnnotation> annotations = new ArrayList<>();

for ( var attr : node.attribute() ) {
annotations.add( toAst( file, attr ) );
}
BoxExpression labelSearch = findExprInAnnotations( annotations, "label", false, null, "break", getPosition( node ) );
String label = getBoxExprAsString( labelSearch, "label", false );
return new BoxBreak( label, getPosition( node ), getSourceText( node ) );
}

private BoxStatement toAst( File file, WhileContext node ) {
Expand All @@ -692,7 +706,7 @@ private BoxStatement toAst( File file, WhileContext node ) {
}

BoxExpression labelSearch = findExprInAnnotations( annotations, "label", false, null, "while", getPosition( node ) );
String label = getBoxExprAsString( labelSearch, "condition", false );
String label = getBoxExprAsString( labelSearch, "label", false );

return new BoxWhile( label, condition, body, getPosition( node ), getSourceText( node ) );
}
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/TestCases/phase1/LabeledLoopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import ortus.boxlang.compiler.parser.BoxSourceType;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.context.ScriptingRequestBoxContext;
Expand Down Expand Up @@ -70,6 +71,58 @@ public void testSimpleLabeledWhile() {
assertThat( variables.get( result ) ).isEqualTo( 1 );
}

@Test
public void testSimpleLabeledWhileContinue() {

instance.executeSource(
"""
result = 0
mylabel : while( true ) {
result ++
if( result > 2 ) break;
continue mylabel;
result ++
}
""",
context );
assertThat( variables.get( result ) ).isEqualTo( 3 );
}

@Test
public void testSimpleLabeledWhileTag() {

instance.executeSource(
"""
<bx:set result = 0>
<bx:while label="mylabel" condition="true">
<bx:set result ++>
<bx:break label=mylabel>
<bx:set result ++>
</bx:while>
""",
context, BoxSourceType.BOXTEMPLATE );
assertThat( variables.get( result ) ).isEqualTo( 1 );
}

@Test
public void testSimpleLabeledWhileContinueTag() {

instance.executeSource(
"""
<bx:set result = 0>
<bx:while label="mylabel" condition="true">
<bx:set result ++>
<bx:if result GT 2 >
<bx:break>
</bx:if>
<bx:continue label="mylabel">
<bx:set result ++>
</bx:while>
""",
context, BoxSourceType.BOXTEMPLATE );
assertThat( variables.get( result ) ).isEqualTo( 3 );
}

@Test
public void testSimpleLabeledDoWhile() {

Expand Down

0 comments on commit 01df92b

Please sign in to comment.