KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > expr > Expr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.expr;
31
32 import com.caucho.quercus.Location;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.env.Value;
35 import com.caucho.quercus.parser.QuercusParser;
36 import com.caucho.quercus.program.Statement;
37 import com.caucho.util.L10N;
38
39 import java.io.IOException JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Represents a PHP expression.
44  */

45 abstract public class Expr {
46   private static final L10N L = new L10N(Expr.class);
47   private static final Logger JavaDoc log = Logger.getLogger(Expr.class.getName());
48
49   public static final int COMPILE_ARG_MAX = 5;
50
51   private final Location _location;
52
53   public Expr(Location location)
54   {
55     _location = location;
56   }
57
58   public Expr()
59   {
60     _location = Location.UNKNOWN;
61   }
62
63   /**
64    * Returns the location.
65    */

66   final public Location getLocation()
67   {
68     return _location;
69   }
70
71   /**
72    * Returns the filename.
73    */

74   public String JavaDoc getFileName()
75   {
76     if (_location != Location.UNKNOWN)
77       return _location.getFileName();
78     else
79       return null;
80   }
81
82   /**
83    * Returns the filename.
84    */

85   public int getLine()
86   {
87     return _location.getLineNumber();
88   }
89
90   /**
91    * Returns the function name.
92    */

93   public String JavaDoc getFunctionLocation()
94   {
95     return "";
96   }
97   
98   /**
99    * Returns the location if known.
100    */

101   public String JavaDoc getLocationLine()
102   {
103     if (_location != Location.UNKNOWN)
104       return _location.getFileName() + ":" + getLine() + ": ";
105     else
106       return "";
107   }
108
109   /**
110    * Returns true for a reference.
111    */

112   public boolean isRef()
113   {
114     return false;
115   }
116
117   /**
118    * Returns true for a constant expression.
119    */

120   public boolean isConstant()
121   {
122     return isLiteral();
123   }
124
125   /**
126    * Returns true for a literal expression.
127    */

128   public boolean isLiteral()
129   {
130     return false;
131   }
132
133   /**
134    * Returns true if a static true value.
135    */

136   public boolean isTrue()
137   {
138     return false;
139   }
140
141   /**
142    * Returns true if a static false value.
143    */

144   public boolean isFalse()
145   {
146     return false;
147   }
148
149   /**
150    * Returns true for an expression that can be read (only $a[] uses this)
151    */

152   public boolean canRead()
153   {
154     return true;
155   }
156
157   public Expr createAssign(QuercusParser parser, Expr value)
158     throws IOException JavaDoc
159   {
160     String JavaDoc msg = (L.l("{0} is an invalid left-hand side of an assignment.",
161               this));
162
163     if (parser != null)
164       throw parser.error(msg);
165     else
166       throw new IOException JavaDoc(msg);
167   }
168
169   /**
170    * Mark as an assignment for a list()
171    */

172   public void assign(QuercusParser parser)
173     throws IOException JavaDoc
174   {
175     String JavaDoc msg = L.l("{0} is an invalid left-hand side of an assignment.",
176              this);
177
178     if (parser != null)
179       throw parser.error(msg);
180     else
181       throw new IOException JavaDoc(msg);
182   }
183
184   public Expr createAssignRef(QuercusParser parser, Expr value)
185     throws IOException JavaDoc
186   {
187     // XXX: need real exception
188
String JavaDoc msg = L.l("{0} is an invalid left-hand side of an assignment.",
189              this);
190
191     if (parser != null)
192       throw parser.error(msg);
193     else
194       throw new IOException JavaDoc(msg);
195   }
196
197   /**
198    * Creates a reference.
199    * @param location
200    */

201   public Expr createRef(QuercusParser parser)
202     throws IOException JavaDoc
203   {
204     return this;
205   }
206
207   public Expr createDeref(ExprFactory factory)
208     throws IOException JavaDoc
209   {
210     return this;
211   }
212
213   /**
214    * Creates a assignment
215    * @param location
216    */

217   public Expr createCopy(ExprFactory factory)
218   {
219     return this;
220   }
221
222   /**
223    * Copy for things like $a .= "test";
224    * @param location
225    */

226   /*
227   public Expr copy()
228   {
229     return this;
230   }
231   */

232
233   /**
234    * Creates a field ref
235    */

236   public Expr createFieldGet(ExprFactory factory, String JavaDoc name)
237   {
238     return factory.createFieldGet(this, name);
239   }
240
241   /**
242    * Creates a field ref
243    */

244   public Expr createFieldGet(ExprFactory factory, Expr name)
245   {
246     return factory.createFieldVarGet(this, name);
247   }
248
249   /**
250    * Creates a assignment
251    */

252   public Statement createUnset(ExprFactory factory, Location location)
253     throws IOException JavaDoc
254   {
255     throw new IOException JavaDoc(L.l("{0} is an illegal value to unset",
256                   this));
257   }
258
259   /**
260    * Creates an isset expression
261    */

262   public Expr createIsset(ExprFactory factory)
263     throws IOException JavaDoc
264   {
265     throw new IOException JavaDoc(L.l("{0} is an illegal value to isset",
266                   this));
267   }
268
269   /**
270    * Returns true if the expression evaluates to a boolean.
271    */

272   public boolean isBoolean()
273   {
274     return false;
275   }
276
277   /**
278    * Returns true if the expression evaluates to a long.
279    */

280   public boolean isLong()
281   {
282     return false;
283   }
284
285   /**
286    * Returns true if the expression evaluates to a double.
287    */

288   public boolean isDouble()
289   {
290     return false;
291   }
292
293   /**
294    * Returns true if the expression evaluates to a number.
295    */

296   public boolean isNumber()
297   {
298     return isLong() || isDouble();
299   }
300
301   /**
302    * Returns true if the expression evaluates to a string.
303    */

304   public boolean isString()
305   {
306     return false;
307   }
308
309   /**
310    * Evaluates the expression as a constant.
311    *
312    * @return the expression value.
313    */

314   public Value evalConstant()
315     throws IOException JavaDoc
316   {
317     throw new IOException JavaDoc(L.l("{0} is not a constant expression", this));
318   }
319
320   /**
321    * Evaluates the expression.
322    *
323    * @param env the calling environment.
324    *
325    * @return the expression value.
326    */

327   abstract public Value eval(Env env);
328
329   /**
330    * Evaluates the expression, with the object expected to be modified,
331    * e.g. from an unset.
332    *
333    * @param env the calling environment.
334    *
335    * @return the expression value.
336    */

337   public Value evalDirty(Env env)
338   {
339     return eval(env);
340   }
341
342   /**
343    * Evaluates the expression, creating an array for unassigned values.
344    *
345    * @param env the calling environment.
346    *
347    * @return the expression value.
348    */

349   public Value evalArray(Env env)
350   {
351     return eval(env);
352   }
353
354   /**
355    * Evaluates the expression, creating an object for unassigned values.
356    *
357    * @param env the calling environment.
358    *
359    * @return the expression value.
360    */

361   public Value evalObject(Env env)
362   {
363     return eval(env);
364   }
365
366   /**
367    * Evaluates the expression as a reference.
368    *
369    * @param env the calling environment.
370    *
371    * @return the expression value.
372    */

373   public Value evalRef(Env env)
374   {
375     return eval(env);
376   }
377
378   /**
379    * Evaluates the expression as a copy
380    *
381    * @param env the calling environment.
382    *
383    * @return the expression value.
384    */

385   public Value evalCopy(Env env)
386   {
387     return eval(env);
388   }
389
390   /**
391    * Evaluates the expression as a function argument where it is unknown
392    * if the value is a reference.
393    *
394    * @param env the calling environment.
395    *
396    * @return the expression value.
397    */

398   public Value evalArg(Env env)
399   {
400     return eval(env);
401   }
402
403   /**
404    * Needed to handle list.
405    */

406   public void evalAssign(Env env, Value value)
407   {
408     throw new RuntimeException JavaDoc(L.l("{0} is an invalid left-hand side of an assignment.",
409                    this));
410   }
411
412   /**
413    * Evaluates the expression as a string
414    *
415    * @param env the calling environment.
416    *
417    * @return the expression value.
418    */

419   public String JavaDoc evalString(Env env)
420   {
421     return eval(env).toString();
422   }
423
424   /**
425    * Evaluates the expression as a string
426    *
427    * @param env the calling environment.
428    *
429    * @return the expression value.
430    */

431   public char evalChar(Env env)
432   {
433     return eval(env).toChar();
434   }
435
436   /**
437    * Evaluates the expression as a boolean.
438    *
439    * @param env the calling environment.
440    *
441    * @return the expression value.
442    */

443   public boolean evalBoolean(Env env)
444   {
445     return eval(env).toBoolean();
446   }
447
448   /**
449    * Evaluates the expression as a long
450    *
451    * @param env the calling environment.
452    *
453    * @return the expression value.
454    */

455   public long evalLong(Env env)
456   {
457     return eval(env).toLong();
458   }
459
460   /**
461    * Evaluates the expression as a double
462    *
463    * @param env the calling environment.
464    *
465    * @return the expression value.
466    */

467   public double evalDouble(Env env)
468   {
469     return eval(env).toDouble();
470   }
471
472   /**
473    * Prints to the output as an echo.
474    */

475   public void print(Env env)
476     throws IOException JavaDoc
477   {
478     eval(env).print(env);
479   }
480
481   public String JavaDoc toString()
482   {
483     return "Expr[]";
484   }
485 }
486
487
Popular Tags