40afdb0c4e751cb6a6dc166d37b2c8f736013535
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPDBGEvalString.java
1 package net.sourceforge.phpdt.internal.debug.core.model;
2
3 /*
4  * Created on 17.04.2004
5  *
6  * To change the template for this generated file go to
7  * Window - Preferences - Java - Code Generation - Code and Comments
8  */
9 /**
10  * @author Chris Admin
11  *
12  * To change the template for this generated type comment go to
13  * Window - Preferences - Java - Code Generation - Code and Comments
14  */
15
16 import java.util.Vector;
17
18 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
19
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.DebugException;
22
23
24 public class PHPDBGEvalString {
25
26         String workStr;
27         private PHPStackFrame fStackFrame;
28
29         public PHPDBGEvalString(PHPStackFrame stack,String dataStr) {
30                 fStackFrame=stack;
31                 workStr=dataStr;
32         }
33
34         String ExtractSubStr(char chstart, char chend,int startIdx) throws DebugException {
35                 int idx=startIdx;
36                 String rslt;
37                 if (idx >= (workStr.length() - 1) || workStr.charAt(idx) != chstart) {
38                         Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"worng startIdx!",null);
39                         throw new DebugException(status);
40                 }
41                 int i = ++idx;
42                 i = workStr.indexOf(chend, i);
43                 if (i==-1){
44                         Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"endchar not found!",null);
45                         throw new DebugException(status);
46                 }
47                 rslt=workStr.substring(idx,i);
48
49                 workStr=workStr.substring(i+1);
50                 return rslt;
51         }
52
53         String ExtractQuotedSubStr(int slen,int startIdx) throws DebugException {
54                 int idx=startIdx;
55                 String rslt;
56                 if (idx+slen+1 >= workStr.length() ||
57                                 workStr.charAt(idx)!= '"' ||
58                                 workStr.charAt(idx+slen+1) != '"') {
59                         Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"no quoted substring found!",null);
60                         throw new DebugException(status);
61                 }
62                 rslt=workStr.substring(idx+1, idx+1+slen);
63                 workStr=workStr.substring(idx+2+slen);
64                 return rslt;
65         }
66
67
68         int ExtractInt(char chstart, char chend,int startIdx) throws DebugException {
69                 String subs;
70                 int rslt;
71                 subs=ExtractSubStr(chstart, chend,startIdx);
72                 return (Integer.parseInt(subs));
73         }
74
75         PHPVariable ParseEvalArray(String name, PHPVariable parent,Vector list, Vector var_list, String classname, int atype)  throws DebugException{
76                 long arritems;
77                 String itemname;
78                 PHPVariable item;
79                 Vector subitems=null;
80
81                 arritems= ExtractInt(':', ':',0);
82                 if ((workStr.length()>0)&&(workStr.charAt(0)!='{')) {
83                         Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"no array startcharecter!",null);
84                         throw new DebugException(status);
85                 }
86                 workStr=workStr.substring(1);
87                 item= new PHPVariable(fStackFrame,name,parent,classname,atype,null);
88                 list.add(item);
89                 if (var_list!=null)
90                         var_list.add(item);
91                 if (arritems > 0) {
92                         subitems = new Vector();
93                 } else
94                         if (workStr.charAt(0)!='}')
95                         {
96                                 Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"no array endcharecter!",null);
97                                 throw new DebugException(status);
98                         }
99                 while ((workStr.length()>0) && (workStr.charAt(0)!='}')) {
100                         Vector tmplst=new Vector();
101                         // name
102                         parse("",null, tmplst, null, false,0);
103                         if(tmplst.size()!=1){
104                                 Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"no name found!",null);
105                                 throw new DebugException(status);
106                         }
107                         // value
108                         parse(((PHPVariable)tmplst.elementAt(0)).getValue().getValueString(),item, subitems, var_list, true,0);
109
110                 }
111                 ((PHPValue)item.getValue()).addVariable(subitems);
112                 workStr=workStr.substring(1);
113                 return item;
114         }
115
116         void ParseEvalNULL(String name,PHPVariable parent,Vector list, Vector var_list, int startIdx) throws DebugException {
117                 int idx=startIdx;
118                 if (idx >= workStr.length() || workStr.charAt(idx) != ';') {
119                         Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"NULL not found!",null);
120                         throw new DebugException(status);
121                 }
122                 workStr=workStr.substring(1);
123                 PHPVariable item= new PHPVariable(fStackFrame, name,parent,"NULL",PHPValue.PEVT_UNKNOWN,null);
124                 list.add(item);
125                 if (var_list!=null)
126                         var_list.add(item);
127         }
128
129         boolean ParseEvalInt( String name, PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
130                 String subs=null;
131                 PHPVariable item;
132                 subs = ExtractSubStr(':',';',startIdx);
133                 item= new PHPVariable(fStackFrame, name,parent,subs,PHPValue.PEVT_LONG,null);
134                 list.add(item);
135                 if (var_list!=null)
136                         var_list.add(item);
137                 return true;
138         }
139
140         boolean ParseEvalDouble(String name,PHPVariable parent, Vector list, Vector var_list, int startIdx) throws DebugException {
141                 String subs=null;
142                 PHPVariable item;
143                 subs = ExtractSubStr(':',';',startIdx);
144                 item= new PHPVariable(fStackFrame, name,parent,subs,PHPValue.PEVT_DOUBLE,null);
145                 list.add(item);
146                 if (var_list!=null)
147                         var_list.add(item);
148                 return true;
149         }
150
151         boolean ParseEvalString(String name,PHPVariable parent, Vector list, Vector var_list, boolean MakePhpStr, int startIdx)
152                                         throws DebugException{
153                 int slen;
154                 slen= ExtractInt( ':', ':',startIdx);
155                 if ((workStr.length()<=slen)||(workStr.charAt(0)!='"')) {
156                         Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"no String startcharecter!",null);
157                         throw new DebugException(status);
158                 }
159                 workStr=workStr.substring(1);
160                 String subs = workStr.substring(0,slen);
161                 // replace \\ with \
162                 subs=subs.replaceAll("\\\\\\\\","\\\\");
163                 if (workStr.charAt(slen)!='"') {
164                         Status status= new Status(Status.ERROR,PHPDebugCorePlugin.getUniqueIdentifier(),Status.OK,"no String endcharecter!",null);
165                         throw new DebugException(status);
166                 }
167                 workStr=workStr.substring(slen+2);
168
169 /*              if (MakePhpStr) {
170                         ConvertToPhpString(subs, &subs);
171                 }
172 */
173                 PHPVariable item= new PHPVariable(fStackFrame, name,parent,subs,PHPValue.PEVT_STRING,null);
174                 list.add(item);
175                 if (var_list!=null)
176                         var_list.add(item);
177                 return true;
178         }
179
180         boolean ParseEvalBool(String name,PHPVariable parent, Vector list, Vector var_list, int startIdx)
181                         throws DebugException{
182                 long v;
183                 v=ExtractInt(':', ';',startIdx);
184                 PHPVariable item= new PHPVariable(fStackFrame, name,parent,(v==0)?("FALSE"):("TRUE"),PHPValue.PEVT_BOOLEAN,null);
185                 list.add(item);
186                 if (var_list!=null)
187                         list.add(item);
188                 return true;
189         }
190
191         boolean ParseEvalObject(String name,PHPVariable parent, Vector list, Vector var_list, int startIdx)
192                         throws DebugException{
193                 int slen;
194                 String classname;
195
196                 slen=ExtractInt(':', ':',startIdx);
197                 classname= ExtractQuotedSubStr(slen, startIdx);
198                 if ((int)classname.length()!=slen) return false;
199                 ParseEvalArray(name,parent, list, var_list, classname,PHPValue.PEVT_OBJECT);
200                 return true;
201         }
202
203         boolean ParseEvalResource(String name,PHPVariable parent, Vector list, Vector var_list, int startIdx)
204                         throws DebugException{
205                 int v, slen;
206                 String restype, val;
207
208                 slen=ExtractInt(':', ':',startIdx);
209                 restype=ExtractQuotedSubStr(slen, startIdx);
210                 v=ExtractInt(':', ';',startIdx);
211 //              std_sprintf(val, "%ld", v);
212 //              list->Add(var_list, name, val, ptResource, restype);
213                 return true;
214         }
215
216
217         boolean ParseEvalRef(String name,PHPVariable parent, Vector list, Vector var_list, boolean isSoftRef, int startIdx)
218                         throws DebugException{
219                 int v;
220
221                 v=ExtractInt(':', ';',startIdx);
222
223                 PHPVariable item= new PHPVariable(fStackFrame, name,parent,"",(isSoftRef)? (PHPValue.PEVT_SOFTREF): (PHPValue.PEVT_REF),null);
224                 v--; // ref ID is 1-based, EvalList is 0-based
225
226
227                 if ((var_list==null) || (v<0) || (v >= var_list.size())) {
228 //                      item.ref = item; // self-resolving
229                         return true;
230                 }       else {
231                         PHPVariable var_item=(PHPVariable)var_list.get(v);
232                         try {
233                                 item.setValue(var_item.getValue());
234                                 item.setReferenceType(var_item.getReferenceType());
235                                 ((PHPValue)item.getValue()).setParent(item);
236                         } catch (DebugException e) {
237                                 // TODO Auto-generated catch block
238                                 e.printStackTrace();
239                         }
240                         list.add(item);
241                 }
242
243
244                 return true;
245         }
246
247         /**
248          *
249          * @return The array of PHPVariables
250          */
251         public PHPVariable[] getVars () {
252                 Vector list     = new Vector ();
253                 Vector var_list = new Vector ();
254
255                 parse ("", null, list, var_list, false, 0);
256
257                 return (PHPVariable[]) list.toArray (new PHPVariable[list.size ()]);    // Convert the list to an array and return the array
258         }
259
260         /**
261          *
262          * @return The array of PHPVariables
263          */
264         public Vector getVariables () {
265                 Vector list     = new Vector ();
266                 Vector var_list = new Vector ();
267
268                 parse ("", null, list, var_list, false, 0);
269
270                 return list;                                                                                                                    // return the PHPVariable list
271         }
272
273         /**
274          *
275          * @param name
276          * @param parent
277          * @param list
278          * @param var_list
279          * @param MakePhpStr
280          * @param startIdx
281          */
282         boolean parse (String name, PHPVariable parent, Vector list, Vector var_list, boolean MakePhpStr, int startIdx) {
283                 boolean ret_val = false;
284                 char    ch;
285
286                 if (startIdx >= workStr.length ()) {
287                         return false;
288                 }
289
290                 ch              = workStr.charAt (startIdx);
291                 workStr = workStr.substring (1);
292
293                 try {
294                         switch (ch) {
295                                 case 'N': ParseEvalNULL         (name, parent, list, var_list, startIdx);                                break;
296                                 case 'i': ParseEvalInt          (name, parent, list, var_list, startIdx);                                break;
297                                 case 'd': ParseEvalDouble       (name, parent, list, var_list, startIdx);                                break;
298                                 case 's': ParseEvalString       (name, parent, list, var_list, MakePhpStr, startIdx);    break;
299                                 case 'a': ParseEvalArray        (name, parent, list, var_list, "", PHPValue.PEVT_ARRAY); break;
300                                 case 'O': ParseEvalObject       (name, parent, list, var_list, startIdx);                                break;
301                                 case 'b': ParseEvalBool         (name, parent, list, var_list, startIdx);                                break;
302                                 case 'z': ParseEvalResource (name, parent, list, var_list, startIdx);                            break;
303                                 case 'R': ParseEvalRef          (name, parent, list, var_list, false, startIdx);                 break;
304                                 case 'r': ParseEvalRef          (name, parent, list, var_list, true, startIdx);                  break;
305                         }
306                 } catch (DebugException e) {
307                         // TODO Auto-generated catch block
308                         e.printStackTrace();
309                 }
310
311 /*              if (!ret_val) { // try to recover
312                         unsigned int i=*startIdx;
313                         while (i<str.length() && str[i]!='{' && str[i]!=';') i++;
314                         if (i<str.length() && str[i] == '{') {
315                                 unsigned int cnt=1;
316                                 i++;
317                                 while (i<str.length() && cnt!=0) {
318                                         if (str[i] == '{')
319                                                 cnt++;
320                                         else if (str[i] == '}')
321                                                 cnt--;
322                                         i++;
323                                 }
324                         }
325                         *startIdx = i;
326                 }
327 */
328                 return  ret_val;
329         }
330 }