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