1 22 package org.jboss.aspects.dbc.condition.parser; 23 24 33 public class ExpressionParser 34 { 35 static final String FORALL = "forall "; 36 static final String EXISTS = "exists "; 37 static final String IMPLIES = " implies "; 38 static final String JAVA = "java: "; 39 static final String IN = " in "; 40 static final String SEPARATOR = " | "; 41 42 public static Expression parseExpression(String expr) 43 { 44 return parse(expr); 45 } 46 47 private static Expression parse(String expr) 48 { 49 expr = expr.trim(); 50 if (expr.startsWith(FORALL)) 51 { 52 return parseForAll(expr); 53 } 54 else if (expr.startsWith(EXISTS)) 55 { 56 return parseExists(expr); 57 } 58 else if (expr.indexOf(IMPLIES) > 0) 59 { 60 return parseImplies(expr); 61 } 62 else if (expr.startsWith(JAVA)) 63 { 64 return parseJava(expr); 65 } 66 else 67 { 68 return parseBoolean(expr); 69 } 70 } 71 72 private static Expression parseForAll(String expr) 73 { 74 int in = expr.indexOf(IN); 75 if (in < 0) throw new RuntimeException ("forall expressions must have an 'in' clause: " + expr); 76 77 String declaration = expr.substring(FORALL.length(), in); 78 79 int sep = expr.indexOf(SEPARATOR); 80 if (sep < 0) throw new RuntimeException ("forall expressions must have a '|': " + expr); 81 String collection = expr.substring(in + IN.length(), sep); 82 83 String body = expr.substring(sep + SEPARATOR.length()); 84 Expression sub = parse(body); 85 ForAllExpression forAll = new ForAllExpression(declaration, collection, sub); 86 return forAll; 87 } 88 89 private static Expression parseExists(String expr) 90 { 91 int in = expr.indexOf(IN); 92 if (in < 0) throw new RuntimeException ("exists expressions must have an 'in' clause: " + expr); 93 94 String declaration = expr.substring(EXISTS.length(), in); 95 96 int sep = expr.indexOf(SEPARATOR); 97 if (sep < 0) throw new RuntimeException ("exists expressions must have a '|': " + expr); 98 String collection = expr.substring(in + IN.length(), sep); 99 100 String body = expr.substring(sep + SEPARATOR.length()); 101 Expression sub = parse(body); 102 ExistsExpression exists = new ExistsExpression(declaration, collection, sub); 103 return exists; 104 } 105 106 107 private static Expression parseImplies(String expr) 108 { 109 int impl = expr.indexOf(IMPLIES); 110 String exprA = expr.substring(0, impl); 111 String exprB = expr.substring(impl + IMPLIES.length()); 112 113 if (exprA.trim().length() == 0 || exprB.trim().length() == 0) 114 { 115 throw new RuntimeException ("implies expressions must take two simple boolean expressions: " + expr); 116 } 117 118 if (exprA.endsWith(";")) 119 { 120 exprA = expr.substring(0, expr.length() - 1); 121 } 122 123 try 124 { 125 Expression condA = parse(exprA); 126 Expression condB = parse(exprB); 127 128 ImpliesExpression implies = new ImpliesExpression((BooleanExpression)condA, (BooleanExpression)condB); 129 return implies; 130 } 131 catch (ClassCastException e) 132 { 133 throw new RuntimeException ("implies expressions must take two simple boolean expressions: " + expr); 134 } 135 } 136 137 private static Expression parseJava(String expr) 138 { 139 expr = expr.substring(JAVA.length()); 140 return new JavaExpression(expr); 141 } 142 143 private static Expression parseBoolean(String expr) 144 { 145 if (expr.endsWith(";")) 146 { 147 expr = expr.substring(0, expr.length() - 1); 148 } 149 return new BooleanExpression(expr); 150 } 151 152 public static void main(String [] args) 153 { 154 doit("a == b"); 155 doit("a == b implies b == a"); 156 doit("forall IEmployee e in getEmployees() | getRooms().contains(e.getOffice());"); 157 doit("exists IEmployee e in getEmployees() | !getRooms().contains(e.getOffice())"); 158 doit("forall IEmployee e1 in getEmployees() | forall IEmployee e2 in getEmployees() | (e1 != e2) implies e1.getOffice() != e2.getOffice()"); 159 doit("java: for (int i = 0){i > 0;}"); 160 } 161 162 public static void doit(String str) 163 { 164 System.out.println(str); 165 System.out.println(); 166 Expression expr = ExpressionParser.parseExpression(str); 167 BeanshellGenerator gen = new BeanshellGenerator(expr); 168 System.out.println(gen.createBeanshellCode()); 169 System.out.println("-----------------------"); 170 } 171 } 172 | Popular Tags |