Skip to content

Commit

Permalink
Add support for switch statement w/ breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Nov 6, 2024
1 parent c75ebc9 commit 0d77610
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Rocker Templates by Fizzed
==========================

## TBD
#### 2.0.0 - 2024-11-06

- Java 8 is now minimum supported version (Java 21 will no longer compile < 8)
- Java 7 support dropped. Java 8 is now minimum supported version (Java 21 will no longer compile < 8).
- New switch statement feature (@asaf-wizzdi)
- Merged rocker-test-java7 and rocker-test-java8 modules together into rocker-test-template
- Blaze build system updated to v1.5.1 (which works on Java 21)
- CI automated tests for Java 21
Expand Down
32 changes: 32 additions & 0 deletions docs/SYNTAX.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,38 @@ Note that Rocker has intelligence to skip template content that includes ```{```
and ```}``` characters such as JavaScript or CSS. You will not need to escape
these characters as long as you have matching left and right curly brackets.

### Switch - case - default blocks (@switch)

Standard Java switch - case - defai;t control flow. The left curly character ```{``` indicates
the start of a block and the right curly character ```}``` marks the end.

The ```else if``` option is available as of v0.16.0.

Switch block with case and default example.

@switch (s) {
case ("a") {
i am in the "a" block
}
case ("b") {
i am in the "b" block
}
default {
i am in the "default" block
}
}

Without a default block.

@switch (v) {
case (1) {
i am in the "a" block
}
case (2) {
i am in the "b" block
}
}

### With blocks (set one or more variables) (@with)

As of v0.12.0 a `@with` block sets a variable to a value within a scoped block. Once the block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,33 +975,53 @@ else if(unit instanceof SwitchBlock){

SwitchBlock block = (SwitchBlock) unit;

// break support via try and catch mechanism (works across lambdas!)
tab(w, depth+indent)
.append("try {").append(CRLF);

depth++;

tab(w, depth+indent)
.append("switch ")
.append(block.getExpression())
.append(" {").append(CRLF);

blockEnd.push("}");

depth++;
} else if (unit instanceof SwitchBlockEnd) {
depth--;

tab(w, depth+indent)
.append(blockEnd.pop())
.append(" // switch end ").append(sourceRef(unit)).append(CRLF);

depth--;

// break support via try and catch mechanism (works across lambdas!)
tab(w, depth+indent)
.append("} catch (").append(BreakException.class.getCanonicalName()).append(" e) {") .append(CRLF);

tab(w, depth+indent+1)
.append("// support for breaking switch statements").append(CRLF);

tab(w, depth+indent)
.append("}").append(CRLF);

} else if (unit instanceof SwitchCaseBlock) {

SwitchCaseBlock block = (SwitchCaseBlock) unit;

tab(w, depth+indent)
.append("case ")
.append(block.getExpression())
.append("->")
.append(": ")
.append(" {").append(CRLF);

blockEnd.push("}");

depth++;
}
else if (unit instanceof SwitchCaseBlockEnd) {
} else if (unit instanceof SwitchCaseBlockEnd) {
depth--;

tab(w, depth+indent)
Expand All @@ -1011,7 +1031,7 @@ else if (unit instanceof SwitchCaseBlockEnd) {

tab(w, depth+indent)
.append("default ")
.append("->")
.append(": ")
.append(" {").append(CRLF);

blockEnd.push("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,8 @@ public void enterValue(RockerParser.ValueContext ctx) {
// break, continue
if (expr.equals("break")) {
// verify we're in a "for" loop that hasn't ended yet...
if (!areWeCurrentlyInAForLoop()) {
throw new ParserRuntimeException(sourceRef, "@break used outside @for loop", null);
if (!areWeCurrentlyInAForLoop() && !areWeCurrentlyInASwitchBlock()) {
throw new ParserRuntimeException(sourceRef, "@break used outside @for loop OR @switch block", null);
}
model.getUnits().add(new BreakStatement(sourceRef));
} else if (expr.equals("continue")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,4 +1200,51 @@ public void withBlockElse8() throws Exception {
assertThat(html, is("b"));
}

@Test
public void statementBlock() throws Exception {
String html = new rocker8.SwitchStatement()
.i(1)
.render()
.toString()
.trim();

assertThat(html, is("in 1"));

// case 2 should flow thru 2 & 3
html = new rocker8.SwitchStatement()
.i(2)
.render()
.toString()
.trim();

assertThat(html, is("in 2in 3"));

// case 3
html = new rocker8.SwitchStatement()
.i(3)
.render()
.toString()
.trim();

assertThat(html, is("in 3"));

// case 4 will flow thru to default too
html = new rocker8.SwitchStatement()
.i(4)
.render()
.toString()
.trim();

assertThat(html, is("in 4in default"));

// case 10 is only default
html = new rocker8.SwitchStatement()
.i(10)
.render()
.toString()
.trim();

assertThat(html, is("in default"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@args(int i)
@switch (i) {
case (1) {in 1@break}
case (2) {in 2}
case (3) {in 3@break}
case (4) {in 4}
default {in default}
}

0 comments on commit 0d77610

Please sign in to comment.