e083902767571ad9f12ba17441e20fded1edc91e
[org.ibex.core.git] / src / org / ibex / js / JSRegexp.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.js;
3
4 import gnu.regexp.*;
5
6 /** A JavaScript regular expression object */
7 public class JSRegexp extends JS {
8     private boolean global;
9     private RE re;
10     private int lastIndex;
11
12     private JS pattern;
13     private int flags;
14     
15     public JSRegexp(JS arg0, JS arg1) throws JSExn {
16         if(arg0 instanceof JSRegexp) {
17             JSRegexp r = (JSRegexp) arg0;
18             this.global = r.global;
19             this.re = r.re;
20             this.lastIndex = r.lastIndex;
21             this.pattern = pattern;
22             this.flags = flags;
23         } else {
24             String pattern = JS.toString(arg0);
25             String sFlags = null;
26             int flags = 0;
27             if(arg1 != null) sFlags = JS.toString(arg1);
28             if(sFlags == null) sFlags = "";
29             for(int i=0;i<sFlags.length();i++) {
30                 switch(sFlags.charAt(i)) {
31                     case 'i': flags |= RE.REG_ICASE; break;
32                     case 'm': flags |= RE.REG_MULTILINE; break;
33                     case 'g': global = true; break;
34                     default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
35                 }
36             }
37             re = newRE(pattern,flags);
38             this.pattern = JS.S(pattern);
39             this.flags = flags;
40         }
41     }
42
43     public JS callMethod(JS method_, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
44         if(!JS.isString(method_)) return super.callMethod(method_, a0, a1, a2, rest, nargs);
45         String method = JS.toString(method_);
46         switch(nargs) {
47             case 1: {
48                 //#switch(method)
49                 case "exec": {
50                     String s = JS.toString(a0);
51                     int start = global ? lastIndex : 0;
52                     if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
53                     REMatch match = re.getMatch(s,start);
54                     if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
55                     return match == null ? null : matchToExecResult(match,re,s);
56                 }
57                 case "test": {
58                     String s = JS.toString(a0);
59                     if (!global) return B(re.getMatch(s) != null);
60                     int start = global ? lastIndex : 0;
61                     if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
62                     REMatch match = re.getMatch(s,start);
63                     lastIndex = match != null ? s.length() : match.getEndIndex();
64                     return B(match != null);
65                 }
66                 case "toString": return JS.S(a0.coerceToString());
67                 case "stringMatch": return stringMatch(a0,a1);
68                 case "stringSearch": return stringSearch(a0,a1);
69                 //#end
70                 break;
71             }
72             case 2: {
73                 //#switch(method)
74                 case "stringReplace": return stringReplace(a0, a1,a2);
75                 //#end
76                 break;
77             }
78         }
79         return super.callMethod(method_, a0, a1, a2, rest, nargs);
80     }
81     
82     public JS get(JS key_) throws JSExn {
83         if(!JS.isString(key_)) return super.get(key_);
84         String key = JS.toString(key_);
85         //#switch(key)
86         case "exec": return METHOD;
87         case "test": return METHOD;
88         case "toString": return METHOD;
89         case "lastIndex": return N(lastIndex);
90         case "source": return pattern;
91         case "global": return JS.B(global);
92         case "ignoreCase": return B(flags & RE.REG_ICASE);
93         case "multiline": return B(flags & RE.REG_MULTILINE);
94         //#end
95         return super.get(key_);
96     }
97     
98     public void put(JS key, JS value) throws JSExn {
99         if(JS.isString(key)) {
100             if(JS.toString(key).equals("lastIndex")) {
101                 lastIndex = JS.toInt(value);
102                 return;
103             }
104         }
105         super.put(key,value);
106     }
107   
108     private static JS matchToExecResult(REMatch match, RE re, String s) {
109         try {
110             JS ret = new JS.O();
111             ret.put(JS.S("index"), N(match.getStartIndex()));
112             ret.put(JS.S("input"),JS.S(s));
113             int n = re.getNumSubs();
114             ret.put(JS.S("length"), N(n+1));
115             ret.put(ZERO,JS.S(match.toString()));
116             for(int i=1;i<=n;i++) ret.put(JS.N(i),JS.S(match.toString(i)));
117             return ret;
118         } catch (JSExn e) {
119             throw new Error("this should never happen");
120         }
121     }
122     
123     String coerceToString() {
124         StringBuffer sb = new StringBuffer();
125         sb.append('/');
126         sb.append(pattern);
127         sb.append('/');
128         if(global) sb.append('g');
129         if((flags & RE.REG_ICASE) != 0) sb.append('i');
130         if((flags & RE.REG_MULTILINE) != 0) sb.append('m');
131         return sb.toString();
132     }
133     
134     public static JS stringMatch(JS o, JS arg0) throws JSExn {
135         String s = JS.toString(o);
136         RE re;
137         JSRegexp regexp = null;
138         if(arg0 instanceof JSRegexp) {
139             regexp = (JSRegexp) arg0;
140             re = regexp.re;
141         } else {
142             re = newRE(JS.toString(arg0),0);
143         }
144         
145         if(regexp == null) {
146             REMatch match = re.getMatch(s);
147             return matchToExecResult(match,re,s);
148         }
149         if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1);
150         
151         JSArray ret = new JSArray();
152         REMatch[] matches = re.getAllMatches(s);
153         for(int i=0;i<matches.length;i++) ret.addElement(JS.S(matches[i].toString()));
154         regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
155         return ret;
156     }
157     
158     public static JS stringSearch(JS o, JS arg0) throws JSExn  {
159         String s = JS.toString(o);
160         RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0);
161         REMatch match = re.getMatch(s);
162         return match == null ? N(-1) : N(match.getStartIndex());
163     }
164     
165     public static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
166         String s = JS.toString(o);
167         RE re;
168         JSFunction replaceFunc = null;
169         String replaceString = null;
170         JSRegexp regexp = null;
171         if(arg0 instanceof JSRegexp) {
172             regexp = (JSRegexp) arg0;
173             re = regexp.re;
174         } else {
175             re = newRE(arg0.toString(),0);
176         }
177         if(arg1 instanceof JSFunction)
178             replaceFunc = (JSFunction) arg1;
179         else
180             replaceString = JS.toString(arg1);
181         REMatch[] matches;
182         if(regexp != null && regexp.global) {
183             matches = re.getAllMatches(s);
184             if(regexp != null) {
185                 if(matches.length > 0)
186                     regexp.lastIndex = matches[matches.length-1].getEndIndex();
187                 else
188                     regexp.lastIndex = s.length();
189             }
190         } else {
191             REMatch match = re.getMatch(s);
192             if(match != null)
193                 matches = new REMatch[]{ match };
194             else
195                 matches = new REMatch[0];
196         }
197         
198         StringBuffer sb = new StringBuffer(s.length());
199         int pos = 0;
200         char[] sa = s.toCharArray();
201         for(int i=0;i<matches.length;i++) {
202             REMatch match = matches[i];
203             sb.append(sa,pos,match.getStartIndex()-pos);
204             pos = match.getEndIndex();
205             if(replaceFunc != null) {
206                 int n = (regexp == null ? 0 : re.getNumSubs());
207                 int numArgs = 3 + n;
208                 JS[] rest = new JS[numArgs - 3];
209                 JS a0 = JS.S(match.toString());
210                 JS a1 = null;
211                 JS a2 = null;
212                 for(int j=1;j<=n;j++)
213                     switch(j) {
214                         case 1: a1 = JS.S(match.toString(j)); break;
215                         case 2: a2 = JS.S(match.toString(j)); break;
216                         default: rest[j - 3] = JS.S(match.toString(j)); break;
217                     }
218                 switch(numArgs) {
219                     case 3:
220                         a1 = N(match.getStartIndex());
221                         a2 = JS.S(s);
222                         break;
223                     case 4:
224                         a2 = N(match.getStartIndex());
225                         rest[0] = JS.S(s);
226                         break;
227                     default:
228                         rest[rest.length - 2] = N(match.getStartIndex());
229                         rest[rest.length - 1] = JS.S(s);
230                 }
231
232                 // note: can't perform pausing operations in here
233                 sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs)));
234
235             } else {
236                 sb.append(mySubstitute(match,replaceString,s));
237             }
238         }
239         int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
240         sb.append(sa,end,sa.length-end);
241         return JS.S(sb.toString());
242     }
243     
244     private static String mySubstitute(REMatch match, String s, String source) {
245         StringBuffer sb = new StringBuffer();
246         int i,n;
247         char c,c2;
248         for(i=0;i<s.length()-1;i++) {
249            c = s.charAt(i);
250             if(c != '$') {
251                 sb.append(c);
252                 continue;
253             }
254             i++;
255             c = s.charAt(i);
256             switch(c) {
257                 case '0': case '1': case '2': case '3': case '4':
258                 case '5': case '6': case '7': case '8': case '9':
259                     if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
260                         n = (c - '0') * 10 + (c2 - '0');
261                         i++;
262                     } else {
263                         n = c - '0';
264                     }
265                     if(n > 0)
266                         sb.append(match.toString(n));
267                     break;
268                 case '$':
269                     sb.append('$'); break;
270                 case '&':
271                     sb.append(match.toString()); break;
272                 case '`':
273                     sb.append(source.substring(0,match.getStartIndex())); break;
274                 case '\'':
275                     sb.append(source.substring(match.getEndIndex())); break;
276                 default:
277                     sb.append('$');
278                     sb.append(c);
279             }
280         }
281         if(i < s.length()) sb.append(s.charAt(i));
282         return sb.toString();
283     }
284                     
285     
286     public static JS stringSplit(JS s_, JS arg0, JS arg1, int nargs) throws JSExn {
287         String s = JS.toString(s_);
288         int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
289         if(limit < 0) limit = Integer.MAX_VALUE;
290         if(limit == 0) return new JSArray();
291         
292         RE re = null;
293         JSRegexp regexp = null;
294         String sep = null;
295         JSArray ret = new JSArray();
296         int p = 0;
297         
298         if(arg0 instanceof JSRegexp) {
299             regexp = (JSRegexp) arg0;
300             re = regexp.re;
301         } else {
302             sep = JS.toString(arg0);
303         }
304         
305         // special case this for speed. additionally, the code below doesn't properly handle
306         // zero length strings
307         if(sep != null && sep.length()==0) {
308             int len = s.length();
309             for(int i=0;i<len;i++)
310                 ret.addElement(JS.S(s.substring(i,i+1)));
311             return ret;
312         }
313         
314         OUTER: while(p < s.length()) {
315             if(re != null) {
316                 REMatch m = re.getMatch(s,p);
317                 if(m == null) break OUTER;
318                 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
319                 ret.addElement(JS.S(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex())));
320                 p = zeroLength ? p + 1 : m.getEndIndex();
321                 if(!zeroLength) {
322                     for(int i=1;i<=re.getNumSubs();i++) {
323                         ret.addElement(JS.S(m.toString(i)));
324                         if(ret.length() == limit) break OUTER;
325                     }
326                 }
327             } else {
328                 int x = s.indexOf(sep,p);
329                 if(x == -1) break OUTER;
330                 ret.addElement(JS.S(s.substring(p,x)));
331                 p = x + sep.length();
332             }
333             if(ret.length() == limit) break;
334         }
335         if(p < s.length() && ret.length() != limit)
336             ret.addElement(JS.S(s.substring(p)));
337         return ret;
338     }
339     
340     public static RE newRE(String pattern, int flags) throws JSExn {
341         try {
342             return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
343         } catch(REException e) {
344             throw new JSExn(e.toString());
345         }
346     }
347 }