]> source.dussan.org Git - javassist.git/commitdiff
enabled the parser to parse switch and synchronized statements.
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Tue, 7 Oct 2003 06:26:21 +0000 (06:26 +0000)
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Tue, 7 Oct 2003 06:26:21 +0000 (06:26 +0000)
The code generator has not supported them yet.

git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@52 30ef5769-5b8d-40dd-aea6-55b5d6557bb3

src/main/javassist/CtClass.java
src/main/javassist/compiler/CodeGen.java
src/main/javassist/compiler/Parser.java

index 6140981842c0dce3c172f1354485c9226713a433..6304a7da9682bcab7db4f2ae1670ce5489414ad3 100644 (file)
@@ -35,7 +35,7 @@ public abstract class CtClass {
     /**
      * The version number of this release.
      */
-    public static final String version = "2.7 beta 1";
+    public static final String version = "2.7 beta 2";
 
     /**
      * Prints the version number and the copyright notice.
index 54f14477c453528471c9bb3d965602a0c9c36527..6a0bd3dc2f8ad64cd8fafa3df4580873ca8bf4ba 100644 (file)
@@ -339,6 +339,14 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId {
             atThrowStmnt(st);
         else if (op == TRY)
             atTryStmnt(st);
+        else if (op == SYNCHRONIZED) {
+            hasReturned = false;
+            throw new CompileError("sorry, synchronized is not supported");
+        }
+        else if (op == SWITCH) {
+            hasReturned = false;
+            throw new CompileError("sorry, switch is not supported");
+        }
         else {
             // LABEL, SWITCH label stament might be null?.
             hasReturned = false;
index 0e3945101bb44e3c7506a69a410cd6b4d2eaab94..dcc14a2427363f31af0dab28fd0ad6af7deff5b7 100644 (file)
@@ -302,13 +302,7 @@ public final class Parser implements TokenId {
      */
     private Stmnt parseIf(SymbolTable tbl) throws CompileError {
         int t = lex.get();      // IF
-        if (lex.get() != '(')
-            throw new SyntaxError(lex);
-
-        ASTree expr = parseExpression(tbl);
-        if (lex.get() != ')')
-            throw new SyntaxError(lex);
-
+        ASTree expr = parseParExpression(tbl);
         Stmnt thenp = parseStatement(tbl);
         Stmnt elsep;
         if (lex.lookAhead() == ELSE) {
@@ -327,13 +321,7 @@ public final class Parser implements TokenId {
         throws CompileError
     {
         int t = lex.get();      // WHILE
-        if (lex.get() != '(')
-            throw new SyntaxError(lex);
-
-        ASTree expr = parseExpression(tbl);
-        if (lex.get() != ')')
-            throw new SyntaxError(lex);
-
+        ASTree expr = parseParExpression(tbl);
         Stmnt body = parseStatement(tbl);
         return new Stmnt(t, expr, body);
     }
@@ -396,20 +384,82 @@ public final class Parser implements TokenId {
 
     /* switch.statement : SWITCH "(" expression ")" "{" switch.block "}"
      *
-     * swtich.block : ( switch.label* statement )*
+     * swtich.block : ( switch.label statement* )*
      *
      * swtich.label : DEFAULT ":"
      *              | CASE const.expression ":"
      */
     private Stmnt parseSwitch(SymbolTable tbl) throws CompileError {
-        throw new CompileError("switch is not supported", lex);
+        int t = lex.get();     // SWITCH
+        ASTree expr = parseParExpression(tbl);
+        Stmnt body = parseSwitchBlock(tbl);
+        return new Stmnt(t, expr, body);
+    }
+
+    private Stmnt parseSwitchBlock(SymbolTable tbl) throws CompileError {
+        if (lex.get() != '{')
+            throw new SyntaxError(lex);
+
+        SymbolTable tbl2 = new SymbolTable(tbl);
+        Stmnt s = parseStmntOrCase(tbl2);
+        if (s == null)
+            throw new CompileError("empty switch block", lex);
+
+        int op = s.getOperator();
+        if (op != CASE && op != DEFAULT)
+            throw new CompileError("no case or default in a switch block",
+                                   lex);
+
+        Stmnt body = new Stmnt(BLOCK, s);
+        while (lex.lookAhead() != '}') {
+            Stmnt s2 = parseStmntOrCase(tbl2);
+            if (s2 != null) {
+                int op2 = s2.getOperator();
+                if (op2 == CASE || op2 == DEFAULT) {
+                    body = (Stmnt)ASTList.concat(body, new Stmnt(BLOCK, s2));
+                    s = s2;
+                }
+                else
+                    s = (Stmnt)ASTList.concat(s, new Stmnt(BLOCK, s2));
+            }
+        }
+
+        lex.get();      // '}'
+        return body;
+    }
+
+    private Stmnt parseStmntOrCase(SymbolTable tbl) throws CompileError {
+        int t = lex.lookAhead();
+        if (t != CASE && t != DEFAULT)
+            return parseStatement(tbl);
+
+        lex.get();
+        Stmnt s;
+        if (t == CASE)
+            s = new Stmnt(t, parseExpression(tbl));
+        else
+            s = new Stmnt(DEFAULT);
+
+        if (lex.get() != ':')
+            throw new CompileError(": is missing", lex);
+
+        return s;
     }
 
     /* synchronized.statement :
      *     SYNCHRONIZED "(" expression ")" block.statement
      */
     private Stmnt parseSynchronized(SymbolTable tbl) throws CompileError {
-        throw new CompileError("synchronized is not supported", lex);
+        int t = lex.get();     // SYNCHRONIZED
+        if (lex.get() != '(')
+            throw new SyntaxError(lex);
+
+        ASTree expr = parseExpression(tbl);
+        if (lex.get() != ')')
+            throw new SyntaxError(lex);
+
+        Stmnt body = parseBlock(tbl);
+        return new Stmnt(t, expr, body);
     }
 
     /* try.statement
@@ -617,6 +667,19 @@ public final class Parser implements TokenId {
         throw new CompileError("array initializer is not supported", lex);
     }
 
+    /* par.expression : '(' expression ')'
+     */
+    private ASTree parseParExpression(SymbolTable tbl) throws CompileError {
+        if (lex.get() != '(')
+            throw new SyntaxError(lex);
+
+        ASTree expr = parseExpression(tbl);
+        if (lex.get() != ')')
+            throw new SyntaxError(lex);
+
+        return expr;
+    }
+
     /* expression : conditional.expr
      *            | conditional.expr assign.op expression (right-to-left)
      */