aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/compiler/Parser.java
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2003-10-07 06:26:21 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2003-10-07 06:26:21 +0000
commitff4f2c43c1fbba039e4c905a56f5706ed7cc4724 (patch)
treee51974866aaaba4e1791a78efe6fe283b441bf58 /src/main/javassist/compiler/Parser.java
parentc7d2341f4d0d5110a4347458dc4236af704986df (diff)
downloadjavassist-ff4f2c43c1fbba039e4c905a56f5706ed7cc4724.tar.gz
javassist-ff4f2c43c1fbba039e4c905a56f5706ed7cc4724.zip
enabled the parser to parse switch and synchronized statements.
The code generator has not supported them yet. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@52 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/compiler/Parser.java')
-rw-r--r--src/main/javassist/compiler/Parser.java97
1 files changed, 80 insertions, 17 deletions
diff --git a/src/main/javassist/compiler/Parser.java b/src/main/javassist/compiler/Parser.java
index 0e394510..dcc14a24 100644
--- a/src/main/javassist/compiler/Parser.java
+++ b/src/main/javassist/compiler/Parser.java
@@ -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)
*/