KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > parser > FieldExpr


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es.parser;
30
31 import com.caucho.es.ESException;
32
33 import java.io.IOException JavaDoc;
34
35 /**
36  * FieldExpr is an intermediate form representing a field reference.
37  *
38  * <p>In JavaScript, foo.bar and foo["bar"] are equivalent.
39  */

40 class FieldExpr extends Expr {
41   Expr lhs;
42   Expr field;
43
44   FieldExpr(Block block, Expr lhs, Expr field)
45   {
46     super(block);
47     
48     this.lhs = lhs;
49     this.field = field;
50
51     if (lhs != null)
52       lhs.setUsed();
53     if (field != null)
54       field.setUsed();
55   }
56
57   /**
58    * Returns the base expression of the field. For foo.bar it returns foo.
59    */

60   Expr getExpr()
61   {
62     return lhs;
63   }
64
65   /**
66    * Returns the field reference.
67    */

68   Expr getField()
69   {
70     return field;
71   }
72
73   /**
74    * Type is always ES.
75    */

76   int getType()
77   {
78     return TYPE_ES;
79   }
80   
81   Expr assign(Expr value)
82     throws ESException
83   {
84     return new AssignExpr(block, lhs, field, value);
85   }
86   
87   Expr delete()
88   {
89     return new DeleteExpr(block, lhs, field);
90   }
91
92   Expr prefix(int op)
93   {
94     if (op == '+')
95       return new PostfixExpr(block, PostfixExpr.PREINC, this);
96     else
97       return new PostfixExpr(block, PostfixExpr.PREDEC, this);
98   }
99   
100   Expr postfix(int op)
101   {
102     if (op == '+')
103       return new PostfixExpr(block, PostfixExpr.POSTINC, this);
104     else
105       return new PostfixExpr(block, PostfixExpr.POSTDEC, this);
106   }
107   
108   CallExpr startCall()
109     throws ESException
110   {
111     return new CallExpr(block, lhs, field, false);
112   }
113
114   CallExpr startNew()
115     throws ESException
116   {
117     return new CallExpr(block, lhs, field, true);
118   }
119
120   void printImpl() throws IOException JavaDoc
121   {
122     lhs.print();
123     cl.print(".getProperty(");
124     field.printStr();
125     cl.print(")");
126   }
127 }
128
Popular Tags