42257025b2a296b7537fc22f168d0e0c67864e66
[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         public PHPVariable[] getVars(){
248                 Vector list=new Vector();
249                 Vector var_list=new Vector();
250                 parse("",null,list,var_list,false,0);
251                 return (PHPVariable[])list.toArray(new PHPVariable[list.size()]);
252         }
253         
254         boolean parse(String name,PHPVariable parent, Vector list, Vector var_list, boolean MakePhpStr,int startIdx) {
255                 boolean ret_val = false;
256
257                 if (startIdx >= workStr.length()) return false;
258                 char ch = workStr.charAt(startIdx);
259                 workStr=workStr.substring(1);
260                 try {
261                         switch (ch) {
262                                 case 'N': 
263                                         ParseEvalNULL(name,parent, list, var_list, startIdx);
264                                         break;
265                                 case 'i': 
266                                         ParseEvalInt(name,parent, list, var_list, startIdx);
267                                         break;
268                                 case 'd': 
269                                         ParseEvalDouble(name,parent, list, var_list, startIdx);
270                                         break;
271                                 case 's': 
272                                         ParseEvalString(name,parent, list, var_list, MakePhpStr, startIdx);
273                                         break;
274                                 case 'a': 
275                                         ParseEvalArray(name,parent, list, var_list, "", PHPValue.PEVT_ARRAY);
276                                         break;
277                                 case 'O': 
278                                         ParseEvalObject(name,parent, list, var_list, startIdx);
279                                         break;
280                                 case 'b': 
281                                         ParseEvalBool(name,parent, list, var_list, startIdx);
282                                         break;
283                                 case 'z': 
284                                         ParseEvalResource(name,parent, list, var_list, startIdx);
285                                         break;
286                                 case 'R': 
287                                         ParseEvalRef(name,parent, list, var_list, false, startIdx);
288                                         break;
289                                 case 'r': 
290                                         ParseEvalRef(name,parent, list, var_list, true, startIdx);
291                                         break;
292                         }
293                 } catch (DebugException e) {
294                         // TODO Auto-generated catch block
295                         e.printStackTrace();
296                 }
297 /*              if (!ret_val) { // try to recover
298                         unsigned int i=*startIdx;
299                         while (i<str.length() && str[i]!='{' && str[i]!=';') i++;
300                         if (i<str.length() && str[i] == '{') {
301                                 unsigned int cnt=1;
302                                 i++;
303                                 while (i<str.length() && cnt!=0) {
304                                         if (str[i] == '{')
305                                                 cnt++;
306                                         else if (str[i] == '}') 
307                                                 cnt--;
308                                         i++;
309                                 }
310                         }
311                         *startIdx = i;
312                 }
313 */
314                 return  ret_val;
315         }
316
317         
318 }