Permalink
Browse files

Restore work-around for unreachable code error

It's tempting to generate code like

case N:
  { <action> }
  break;

but if action is "throw new ...", then this results in unreachable code
on the "break;" line. Instead, generate

case N:
  { <action> }
case <anything-unique>: break;

as it used to be before the port to scala.
  • Loading branch information...
1 parent b3c7784 commit 8a22e1ed405f481fb445d840141276f4c975a002 @moy committed Dec 5, 2011
@@ -1288,8 +1288,7 @@ private void emitActions() {
println(" { "+action.content);
println(" }");
- println(" " + Options.lang.end_case_body());
- println(" "+Options.lang.start_case("" + (i++))+Options.lang.start_case_body()+Options.lang.end_case_body());
+ println(" " + Options.lang.end_case_body(i++));
}
}
@@ -1325,8 +1324,7 @@ private void emitEOFVal() {
}
println(" "+action.content);
println(" }");
- println(" " + Options.lang.end_case_body());
- println(" "+Options.lang.start_case(""+(++last))+Options.lang.start_case_body()+Options.lang.end_case_body());
+ println(" " + Options.lang.end_case_body(++last));
}
}
@@ -180,6 +180,13 @@ public String end_case_body() {
return "break;";
}
+ /* (non-Javadoc)
+ * @see JFlex.Language#end_case_body(int)
+ */
+ public String end_case_body(int last) {
+ return "case " + last + ": break; // work around unreachable code error";
+ }
+
public String start_label_block(String name) {
return name + ": {";
}
@@ -62,6 +62,15 @@ String method_header(boolean overriding, boolean isPublic, boolean overridable,
String add_case(String val);
String start_case_body();
String end_case_body();
+ /**
+ * Same as end_case_body(), but works around "unreachable code"
+ * error by not adding the "break;" statement right away, but
+ * inserting an additional "case <last>:" before it.
+ *
+ * The caller should maintain a counter last to ensure the
+ * uniqueness of the case.
+ */
+ String end_case_body(int last);
String gen_default();
// Exiting loops;
@@ -145,6 +145,10 @@ public String end_case_body() {
return "}";
}
+ public String end_case_body(int last) {
+ return end_case_body();
+ }
+
public String start_label_block(String name) {
return "/*" + name + ":*/ try {";
}

0 comments on commit 8a22e1e

Please sign in to comment.