Add support for try-catch-finally statement for MGC version. Do not fully support...
authorjzhou <jzhou>
Mon, 10 Jan 2011 18:25:25 +0000 (18:25 +0000)
committerjzhou <jzhou>
Mon, 10 Jan 2011 18:25:25 +0000 (18:25 +0000)
Robust/src/IR/Tree/BuildIR.java
Robust/src/Parse/java14.cup
Robust/src/Tests/TryCatchTest.java [new file with mode: 0644]

index 037232e00e796f25ff48437aa91bf3263d4a2034..e8a3396208a57a88fa7f5fed4310dc5e031a74f3 100644 (file)
@@ -510,6 +510,11 @@ public class BuildIR {
       parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
       return;
     }
+    // in case there are empty node
+    ParseNode emptynode=pn.getChild("empty");
+    if(emptynode != null) {
+      return;
+    }
     throw new Error();
   }
   
@@ -1078,6 +1083,20 @@ public class BuildIR {
           
         }
       }
+    } else if (state.MGC && isNode(pn, "trycatchstatement")) {
+      // TODO add version for normal Java later
+      // Do not fully support exceptions now. Only make sure that if there are no
+      // exceptions thrown, the execution is right
+      ParseNode tpn = pn.getChild("tryblock").getFirstChild();
+      BlockNode bn=parseBlockHelper(tpn);
+      blockstatements.add(new SubBlockNode(bn));
+      
+      ParseNode fbk = pn.getChild("finallyblock");
+      if(fbk != null) {
+        ParseNode fpn = fbk.getFirstChild();
+        BlockNode fbn=parseBlockHelper(fpn);
+        blockstatements.add(new SubBlockNode(fbn));
+      }
     } else if (isNode(pn,"taskexit")) {
       Vector vfe=null;
       if (pn.getChild("flag_effects_list")!=null)
index 2fd93f3f51d686020e65e2a1d416bb5eb048aedb..d0482858a33130298d2392a2a0f96ce8effbe799 100644 (file)
@@ -192,10 +192,10 @@ non terminal ParseNode break_statement, continue_statement;
 non terminal ParseNode return_statement;
 //non terminal ParseNode throw_statement;
 non terminal ParseNode synchronized_statement;
-//non terminal ParseNode try_statement;
-//non terminal ParseNode catches_opt;
-//non terminal ParseNode catches, catch_clause;
-//non terminal ParseNode finally;
+non terminal ParseNode try_statement;
+non terminal ParseNode catches_opt;
+non terminal ParseNode catches, catch_clause;
+non terminal ParseNode finally;
 //non terminal ParseNode assert_statement;
 non terminal ParseNode genreach_statement;
 // 19.12) Expressions
@@ -1339,7 +1339,7 @@ statement_without_trailing_substatement ::=
        |       synchronized_statement:st {: RESULT=st; :}
        |       genreach_statement:st {: RESULT=st; :}
 //     |       throw_statement
-//     |       try_statement
+       |       try_statement:st {: RESULT=st; :}
 //     |       assert_statement
        ;
 empty_statement ::=
@@ -1604,21 +1604,54 @@ sese_statement ::=
               RESULT=pn;
        :}
        ;
-//try_statement ::=
-//             TRY block catches
-//     |       TRY block catches_opt finally
-//     ;
-//catches_opt ::=
-//     |       catches
-//     ;
-//catches ::=  catch_clause
-//     |       catches catch_clause
-//     ;
-//catch_clause ::=
-//             CATCH LPAREN formal_parameter RPAREN block
-//     ;
-//finally ::=  FINALLY block
-//     ;
+try_statement ::=
+               TRY block:bk catches:ca
+               {: 
+               ParseNode pn=new ParseNode("trycatchstatement");
+               pn.addChild("tryblock").addChild(bk); 
+               pn.addChild("catchblock").addChild(ca);
+               RESULT=pn;
+               :}
+       |       TRY block:bk catches_opt:ca finally:fi
+           {: 
+               ParseNode pn=new ParseNode("trycatchstatement");
+               pn.addChild("tryblock").addChild(bk); 
+               pn.addChild("catchblock").addChild(ca);
+               pn.addChild("finallyblock").addChild(fi);
+               RESULT=pn;
+               :}
+       ;
+catches_opt ::=
+    {: RESULT=new ParseNode("empty"); :}
+       |       catches:ca
+       {: RESULT=ca; :}
+       ;
+catches ::=    catch_clause:ca
+        {:
+        ParseNode pn=new ParseNode("catchlist");
+        pn.addChild(ca);
+        RESULT=pn;
+        :}
+       |       catches:cas catch_clause:ca 
+           {:
+           cas.addChild(ca);
+           RESULT=cas;
+           :}
+       ;
+catch_clause ::=
+               CATCH LPAREN formal_parameter:fp RPAREN block:bk
+               {:
+               ParseNode pn=new ParseNode("catchclause");
+               pn.addChild("parameter").addChild(fp);
+               pn.addChild("block").addChild(bk);
+               RESULT=pn;
+               :}
+       ;
+finally ::=    FINALLY block:bk
+    {:
+    RESULT=bk;
+    :}
+       ;
 //assert_statement ::=
 //             ASSERT expression SEMICOLON
 //     |       ASSERT expression COLON expression SEMICOLON
diff --git a/Robust/src/Tests/TryCatchTest.java b/Robust/src/Tests/TryCatchTest.java
new file mode 100644 (file)
index 0000000..08fb306
--- /dev/null
@@ -0,0 +1,12 @@
+public class TryCatchTest {
+  
+  public static void main(String[] args) {
+    try{
+      System.out.println("try block");
+    }catch(Exception e) {
+      System.out.println("catch block");
+    }finally{
+      System.out.println("finally block");
+    }
+  }
+}
\ No newline at end of file