From 6e4c4dca3a6e146e1eb97f0dd5774d37703c94e8 Mon Sep 17 00:00:00 2001 From: jzhou Date: Mon, 10 Jan 2011 18:25:25 +0000 Subject: [PATCH] Add support for try-catch-finally statement for MGC version. Do not fully support exceptions yet. Only guarantee the execution when there are no exceptions --- Robust/src/IR/Tree/BuildIR.java | 19 ++++++++ Robust/src/Parse/java14.cup | 73 ++++++++++++++++++++++-------- Robust/src/Tests/TryCatchTest.java | 12 +++++ 3 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 Robust/src/Tests/TryCatchTest.java diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index 037232e0..e8a33962 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -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) diff --git a/Robust/src/Parse/java14.cup b/Robust/src/Parse/java14.cup index 2fd93f3f..d0482858 100644 --- a/Robust/src/Parse/java14.cup +++ b/Robust/src/Parse/java14.cup @@ -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 index 00000000..08fb3065 --- /dev/null +++ b/Robust/src/Tests/TryCatchTest.java @@ -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 -- 2.34.1