KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > Syntax


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/Syntax.java#15 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2003-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.olap;
11
12 import java.io.PrintWriter JavaDoc;
13
14 /**
15  * Enumerated values describing the syntax of an expression.
16  *
17  * @author jhyde
18  * @since 21 July, 2003
19  * @version $Id: //open/mondrian/src/main/mondrian/olap/Syntax.java#15 $
20  */

21 public enum Syntax {
22     /**
23      * Defines syntax for expression invoked <code>FUNCTION()</code> or
24      * <code>FUNCTION(args)</code>.
25      */

26     Function {
27         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
28             ExpBase.unparseList(pw, args, fun + "(", ", ", ")");
29         }
30     },
31
32     /**
33      * Defines syntax for expression invoked as <code>object.PROPERTY</code>.
34      */

35     Property {
36         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
37             Util.assertTrue(args.length >= 1);
38             args[0].unparse(pw); // 'this'
39
pw.print(".");
40             pw.print(fun);
41         }
42
43         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
44             // e.g. "<Set>.Current"
45
return getTypeDescription(argTypes[0]) + "." + name;
46         }
47     },
48
49     /**
50      * Defines syntax for expression invoked invoked as
51      * <code>object.METHOD()</code> or
52      * <code>object.METHOD(args)</code>.
53      */

54     Method {
55         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
56             Util.assertTrue(args.length >= 1);
57             args[0].unparse(pw); // 'this'
58
pw.print(".");
59             pw.print(fun);
60             pw.print("(");
61             for (int i = 1; i < args.length; i++) {
62                 if (i > 1) {
63                     pw.print(", ");
64                 }
65                 args[i].unparse(pw);
66             }
67             pw.print(")");
68         }
69
70         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
71             // e.g. "<Member>.Lead(<Numeric Expression>)"
72
return (returnType == Category.Unknown ? "" :
73                     getTypeDescription(returnType) + " ") +
74                 getTypeDescription(argTypes[0]) + "." +
75                 name + "(" + getTypeDescriptionCommaList(argTypes, 1) +
76                 ")";
77         }
78     },
79
80     /**
81      * Defines syntax for expression invoked as <code>arg OPERATOR arg</code>
82      * (like '+' or 'AND').
83      */

84     Infix {
85         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
86             if (needParen(args)) {
87                 ExpBase.unparseList(pw, args, "(", " " + fun + " ", ")");
88             } else {
89                 ExpBase.unparseList(pw, args, "", " " + fun + " ", "");
90             }
91         }
92
93         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
94             // e.g. "<Numeric Expression> / <Numeric Expression>"
95
return getTypeDescription(argTypes[0]) + " " + name + " " +
96                 getTypeDescription(argTypes[1]);
97         }
98     },
99
100     /**
101      * Defines syntax for expression invoked as <code>OPERATOR arg</code>
102      * (like unary '-').
103      */

104     Prefix {
105         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
106             if (needParen(args)) {
107                 ExpBase.unparseList(pw, args, "(" + fun + " ", null, ")");
108             } else {
109                 ExpBase.unparseList(pw, args, fun + " ", null, "");
110             }
111         }
112
113         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
114             // e.g. "- <Numeric Expression>"
115
return name + " " + getTypeDescription(argTypes[0]);
116         }
117     },
118
119     /**
120      * Defines syntax for expression invoked as <code>arg OPERATOR</code>
121      * (like <code>IS EMPTY</code>).
122      */

123     Postfix {
124         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
125             if (needParen(args)) {
126                 ExpBase.unparseList(pw, args, "(", null, " " + fun + ")");
127             } else {
128                 ExpBase.unparseList(pw, args, "", null, " " + fun);
129             }
130         }
131
132         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
133             // e.g. "<Expression> IS NULL"
134
return getTypeDescription(argTypes[0]) + " " + name;
135         }
136     },
137
138     /**
139      * Defines syntax for expression invoked as
140      * <code>{ARG, &#46;&#46;&#46;}</code>; that
141      * is, the set construction operator.
142      */

143     Braces {
144         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
145             return "{" + getTypeDescriptionCommaList(argTypes, 0) + "}";
146         }
147
148         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
149             ExpBase.unparseList(pw, args, "{", ", ", "}");
150         }
151     },
152
153     /**
154      * Defines syntax for expression invoked as <code>(ARG)</code> or
155      * <code>(ARG, &#46;&#46;&#46;)</code>; that is, parentheses for grouping
156      * expressions, and the tuple construction operator.
157      */

158     Parentheses {
159         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
160             return "(" + getTypeDescriptionCommaList(argTypes, 0) + ")";
161         }
162
163         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
164             ExpBase.unparseList(pw, args, "(", ", ", ")");
165         }
166     },
167
168     /**
169      * Defines syntax for expression invoked as <code>CASE ... END</code>.
170      */

171     Case {
172         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
173             if (fun.equals("_CaseTest")) {
174                 pw.print("CASE");
175                 int j = 0;
176                 int clauseCount = (args.length - j) / 2;
177                 for (int i = 0; i < clauseCount; i++) {
178                     pw.print(" WHEN ");
179                     args[j++].unparse(pw);
180                     pw.print(" THEN ");
181                     args[j++].unparse(pw);
182                 }
183                 if (j < args.length) {
184                     pw.print(" ELSE ");
185                     args[j++].unparse(pw);
186                 }
187                 Util.assertTrue(j == args.length);
188                 pw.print(" END");
189             } else {
190                 Util.assertTrue(fun.equals("_CaseMatch"));
191
192                 pw.print("CASE ");
193                 int j = 0;
194                 args[j++].unparse(pw);
195                 int clauseCount = (args.length - j) / 2;
196                 for (int i = 0; i < clauseCount; i++) {
197                     pw.print(" WHEN ");
198                     args[j++].unparse(pw);
199                     pw.print(" THEN ");
200                     args[j++].unparse(pw);
201                 }
202                 if (j < args.length) {
203                     pw.print(" ELSE ");
204                     args[j++].unparse(pw);
205                 }
206                 Util.assertTrue(j == args.length);
207                 pw.print(" END");
208             }
209         }
210
211         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
212             String JavaDoc s = getTypeDescription(argTypes[0]);
213             if (argTypes[0] == Category.Logical) {
214                 return "CASE WHEN " + s + " THEN <Expression> ... END";
215             } else {
216                 return "CASE " + s + " WHEN " + s + " THEN <Expression> ... END";
217             }
218         }
219     },
220
221     /**
222      * Defines syntax for expression generated by the Mondrian system which
223      * cannot be specified syntactically.
224      */

225     Internal,
226
227     /**
228      * Defines syntax for a CAST expression
229      * <code>CAST(expression AS type)</code>.
230      */

231     Cast {
232         public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
233             pw.print("CAST(");
234             args[0].unparse(pw);
235             pw.print(" AS ");
236             args[1].unparse(pw);
237             pw.print(")");
238         }
239
240         public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
241             return "CAST(<Expression> AS <Type>)";
242         }
243     },
244
245     /**
246      * Defines syntax for expression invoked <code>object&#46;&PROPERTY</code>
247      * (a variant of {@link #Property}).
248      */

249     QuotedProperty,
250
251     /**
252      * Defines syntax for expression invoked <code>object&#46;[&PROPERTY]</code>
253      * (a variant of {@link #Property}).
254      */

255     AmpersandQuotedProperty;
256
257     /**
258      * Converts a call to a function of this syntax into source code.
259      *
260      * @param fun Function name
261      * @param args Arguments to the function
262      * @param pw Writer
263      */

264     public void unparse(String JavaDoc fun, Exp[] args, PrintWriter JavaDoc pw) {
265         throw new UnsupportedOperationException JavaDoc();
266     }
267
268     /**
269      * Returns a description of the signature of a function call, for
270      * example, "CoalesceEmpty(<Numeric Expression>, <String Expression>)".
271      *
272      * @param name Function name
273      * @param returnType Function's return category
274      * @param argTypes Categories of the function's arguments
275      * @return Function signature
276      */

277     public String JavaDoc getSignature(String JavaDoc name, int returnType, int[] argTypes) {
278         // e.g. "StripCalculatedMembers(<Set>)"
279
return (returnType == Category.Unknown ? "" :
280                 getTypeDescription(returnType) + " ") +
281             name + "(" + getTypeDescriptionCommaList(argTypes, 0) +
282             ")";
283     }
284
285     private static boolean needParen(Exp[] args) {
286         return !(args.length == 1 &&
287                 args[0] instanceof FunCall &&
288                 ((FunCall) args[0]).getSyntax() == Syntax.Parentheses);
289     }
290
291     private static String JavaDoc getTypeDescription(int type) {
292         return "<" + Category.instance.getDescription(type & Category.Mask) + ">";
293     }
294
295     private static String JavaDoc getTypeDescriptionCommaList(int[] types, int start) {
296         int initialSize = (types.length - start) * 16;
297         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(initialSize > 0 ? initialSize : 16);
298         for (int i = start; i < types.length; i++) {
299             if (i > start) {
300                 sb.append(", ");
301             }
302             sb.append("<")
303                     .append(Category.instance.getDescription(types[i] & Category.Mask))
304                     .append(">");
305         }
306         return sb.toString();
307     }
308 }
309
310 // End Syntax.java
311
Popular Tags