FileDocCategorySizeDatePackage
ExpressionParser.javaAPI DocJBoss 4.2.15848Fri Jul 13 21:02:28 BST 2007org.jboss.aspects.dbc.condition.parser

ExpressionParser

public class ExpressionParser extends Object
A very simple expression parser, for the reason that I really, really cannot get my head around defining grammars in JavaCC. If this grammar gets more complex I guess JavaCC is the way forward...
author
Kabir Khan
version
$Revision: 57186 $

Fields Summary
static final String
FORALL
static final String
EXISTS
static final String
IMPLIES
static final String
JAVA
static final String
IN
static final String
SEPARATOR
Constructors Summary
Methods Summary
public static voiddoit(java.lang.String str)

      System.out.println(str);
      System.out.println();
      Expression expr = ExpressionParser.parseExpression(str);
      BeanshellGenerator gen = new BeanshellGenerator(expr);
      System.out.println(gen.createBeanshellCode());
      System.out.println("-----------------------");
   
public static voidmain(java.lang.String[] args)

      doit("a == b");
      doit("a == b implies b == a");
      doit("forall IEmployee e in getEmployees() | getRooms().contains(e.getOffice());");
      doit("exists IEmployee e in getEmployees() | !getRooms().contains(e.getOffice())");
      doit("forall IEmployee e1 in getEmployees() | forall IEmployee e2 in getEmployees() | (e1 != e2) implies e1.getOffice() != e2.getOffice()");
      doit("java: for (int i = 0){i > 0;}");
   
private static Expressionparse(java.lang.String expr)

      expr = expr.trim();
      if (expr.startsWith(FORALL))
      {
         return parseForAll(expr);
      }
      else if (expr.startsWith(EXISTS))
      {
         return parseExists(expr);         
      }
      else if (expr.indexOf(IMPLIES) > 0)
      {
         return parseImplies(expr);         
      }
      else if (expr.startsWith(JAVA))
      {
         return parseJava(expr);
      }
      else
      {
         return parseBoolean(expr);         
      }
   
private static ExpressionparseBoolean(java.lang.String expr)

      if (expr.endsWith(";"))
      {
         expr = expr.substring(0, expr.length() - 1);
      }
      return new BooleanExpression(expr);      
   
private static ExpressionparseExists(java.lang.String expr)

      int in = expr.indexOf(IN);
      if (in < 0) throw new RuntimeException("exists expressions must have an 'in' clause: " + expr);
      
      String declaration = expr.substring(EXISTS.length(), in);
      
      int sep = expr.indexOf(SEPARATOR);
      if (sep < 0) throw new RuntimeException("exists expressions must have a '|': " + expr);
      String collection = expr.substring(in + IN.length(), sep);
      
      String body = expr.substring(sep + SEPARATOR.length());
      Expression sub = parse(body);
      ExistsExpression exists = new ExistsExpression(declaration, collection, sub);
      return exists;
   
public static ExpressionparseExpression(java.lang.String expr)

   
       
   
      return parse(expr);
   
private static ExpressionparseForAll(java.lang.String expr)

      int in = expr.indexOf(IN);
      if (in < 0) throw new RuntimeException("forall expressions must have an 'in' clause: " + expr);
      
      String declaration = expr.substring(FORALL.length(), in);
      
      int sep = expr.indexOf(SEPARATOR);
      if (sep < 0) throw new RuntimeException("forall expressions must have a '|': " + expr);
      String collection = expr.substring(in + IN.length(), sep);
      
      String body = expr.substring(sep + SEPARATOR.length());
      Expression sub = parse(body);
      ForAllExpression forAll = new ForAllExpression(declaration, collection, sub);
      return forAll;
   
private static ExpressionparseImplies(java.lang.String expr)

      int impl = expr.indexOf(IMPLIES);
      String exprA = expr.substring(0, impl);
      String exprB = expr.substring(impl + IMPLIES.length());

      if (exprA.trim().length() == 0 || exprB.trim().length() == 0)
      {
         throw new RuntimeException("implies expressions must take two simple boolean expressions: " + expr);
      }
      
      if (exprA.endsWith(";"))
      {
         exprA = expr.substring(0, expr.length() - 1);
      }

      try
      {
         Expression condA = parse(exprA);
         Expression condB = parse(exprB);
         
         ImpliesExpression implies = new ImpliesExpression((BooleanExpression)condA, (BooleanExpression)condB);
         return implies;
      }
      catch (ClassCastException e)
      {
         throw new RuntimeException("implies expressions must take two simple boolean expressions: " + expr);
      }      
   
private static ExpressionparseJava(java.lang.String expr)

      expr = expr.substring(JAVA.length());
      return new JavaExpression(expr);