KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > dbc > condition > parser > ExpressionParser


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aspects.dbc.condition.parser;
23
24 /**
25  * A very simple expression parser, for the reason that I really,
26  * really cannot get my head around defining grammars in
27  * JavaCC. If this grammar gets more complex I guess JavaCC
28  * is the way forward...
29  *
30  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
31  * @version $Revision: 37406 $
32  */

33 public class ExpressionParser
34 {
35    static final String JavaDoc FORALL = "forall ";
36    static final String JavaDoc EXISTS = "exists ";
37    static final String JavaDoc IMPLIES = " implies ";
38    static final String JavaDoc JAVA = "java: ";
39    static final String JavaDoc IN = " in ";
40    static final String JavaDoc SEPARATOR = " | ";
41    
42    public static Expression parseExpression(String JavaDoc expr)
43    {
44       return parse(expr);
45    }
46    
47    private static Expression parse(String JavaDoc 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 JavaDoc expr)
73    {
74       int in = expr.indexOf(IN);
75       if (in < 0) throw new RuntimeException JavaDoc("forall expressions must have an 'in' clause: " + expr);
76       
77       String JavaDoc declaration = expr.substring(FORALL.length(), in);
78       
79       int sep = expr.indexOf(SEPARATOR);
80       if (sep < 0) throw new RuntimeException JavaDoc("forall expressions must have a '|': " + expr);
81       String JavaDoc collection = expr.substring(in + IN.length(), sep);
82       
83       String JavaDoc 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 JavaDoc expr)
90    {
91       int in = expr.indexOf(IN);
92       if (in < 0) throw new RuntimeException JavaDoc("exists expressions must have an 'in' clause: " + expr);
93       
94       String JavaDoc declaration = expr.substring(EXISTS.length(), in);
95       
96       int sep = expr.indexOf(SEPARATOR);
97       if (sep < 0) throw new RuntimeException JavaDoc("exists expressions must have a '|': " + expr);
98       String JavaDoc collection = expr.substring(in + IN.length(), sep);
99       
100       String JavaDoc 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 JavaDoc expr)
108    {
109       int impl = expr.indexOf(IMPLIES);
110       String JavaDoc exprA = expr.substring(0, impl);
111       String JavaDoc exprB = expr.substring(impl + IMPLIES.length());
112
113       if (exprA.trim().length() == 0 || exprB.trim().length() == 0)
114       {
115          throw new RuntimeException JavaDoc("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 JavaDoc e)
132       {
133          throw new RuntimeException JavaDoc("implies expressions must take two simple boolean expressions: " + expr);
134       }
135    }
136    
137    private static Expression parseJava(String JavaDoc expr)
138    {
139       expr = expr.substring(JAVA.length());
140       return new JavaExpression(expr);
141    }
142    
143    private static Expression parseBoolean(String JavaDoc 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 JavaDoc[] 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 JavaDoc 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