-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathMatchObject.java
More file actions
237 lines (190 loc) · 6.85 KB
/
MatchObject.java
File metadata and controls
237 lines (190 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright 2000 Finn Bock
*
* This program contains material copyrighted by:
* Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
*
* This version of the SRE library can be redistributed under CNRI's
* Python 1.6 license. For any other use, please contact Secret Labs
* AB (info@pythonware.com).
*
* Portions of this engine have been developed in cooperation with
* CNRI. Hewlett-Packard provided funding for 1.6 integration and
* other compatibility work.
*/
package org.python.modules.sre;
import org.python.core.*;
import java.math.BigInteger;
public class MatchObject extends PyObject implements Traverseproc {
public PyString string; /* link to the target string */
public PyObject regs; /* cached list of matching spans */
PatternObject pattern; /* link to the regex (pattern) object */
int pos, endpos; /* current target slice */
int lastindex; /* last index marker seen by the engine (-1 if none) */
int groups; /* number of groups (start/end marks) */
int[] mark;
public PyObject expand(PyObject[] args) {
if(args.length == 0) {
throw Py.TypeError("expand() takes exactly 1 argument (0 given)");
}
PyObject mod = imp.importName("re", true);
PyObject func = mod.__getattr__("_expand");
return func.__call__(new PyObject[] {pattern, this, args[0]});
}
public PyObject group(PyObject[] args) {
switch (args.length) {
case 0:
return getslice(Py.Zero, Py.None);
case 1:
return getslice(args[0], Py.None);
default:
PyObject[] result = new PyObject[args.length];
for (int i = 0; i < args.length; i++)
result[i] = getslice(args[i], Py.None);
return new PyTuple(result);
}
}
public PyObject groups(PyObject[] args, String[] kws) {
ArgParser ap = new ArgParser("groups", args, kws, "default");
PyObject def = ap.getPyObject(0, Py.None);
PyObject[] result = new PyObject[groups-1];
for (int i = 1; i < groups; i++) {
result[i-1] = getslice_by_index(i, def);
}
return new PyTuple(result);
}
public PyObject groupdict(PyObject[] args, String[] kws) {
ArgParser ap = new ArgParser("groupdict", args, kws, "default");
PyObject def = ap.getPyObject(0, Py.None);
PyObject result = new PyDictionary();
if (pattern.groupindex == null)
return result;
PyObject keys = pattern.groupindex.invoke("keys");
PyObject key;
for (int i = 0; (key = keys.__finditem__(i)) != null; i++) {
PyObject item = getslice(key, def);
result.__setitem__(key, item);
}
return result;
}
public PyObject start() {
return start(Py.Zero);
}
public PyObject start(PyObject index_) {
int index = getindex(index_);
if (index < 0 || index >= groups)
throw Py.IndexError("no such group");
return Py.newInteger(mark[index*2]);
}
public PyObject end() {
return end(Py.Zero);
}
public PyObject end(PyObject index_) {
int index = getindex(index_);
if (index < 0 || index >= groups)
throw Py.IndexError("no such group");
return Py.newInteger(mark[index*2+1]);
}
public PyTuple span() {
return span(Py.Zero);
}
public PyTuple span(PyObject index_) {
int index = getindex(index_);
if (index < 0 || index >= groups)
throw Py.IndexError("no such group");
int start = mark[index*2];
int end = mark[index*2+1];
return _pair(start, end);
}
public PyObject regs() {
PyObject[] regs = new PyObject[groups];
for (int index = 0; index < groups; index++) {
regs[index] = _pair(mark[index*2], mark[index*2+1]);
}
return new PyTuple(regs);
}
PyTuple _pair(int i1, int i2) {
return new PyTuple(Py.newInteger(i1), Py.newInteger(i2));
}
private PyObject getslice(PyObject index, PyObject def) {
return getslice_by_index(getindex(index), def);
}
private int getindex(PyObject index) {
if (index instanceof PyInteger)
return ((PyInteger) index).getValue();
if (index instanceof PyLong) {
BigInteger idx = ((PyLong) index).getValue();
if (idx.compareTo(PyInteger.MAX_INT) == 1) {
throw Py.IndexError("no such group");
} else {
return idx.intValue();
}
}
int i = -1;
if (pattern.groupindex != null) {
index = pattern.groupindex.__finditem__(index);
if (index != null)
if (index instanceof PyInteger)
return ((PyInteger) index).getValue();
}
return i;
}
private PyObject getslice_by_index(int index, PyObject def) {
if (index < 0 || index >= groups)
throw Py.IndexError("no such group");
index *= 2;
int start = mark[index];
int end = mark[index+1];
//System.out.println("group:" + index + " " + start + " " +
// end + " l:" + string.length());
if (string == null || start < 0)
return def;
return string.__getslice__(Py.newInteger(start), Py.newInteger(end));
}
public PyObject __findattr_ex__(String key) {
//System.out.println("__findattr__:" + key);
if (key == "flags")
return Py.newInteger(pattern.flags);
if (key == "groupindex")
return pattern.groupindex;
if (key == "re")
return pattern;
if (key == "pos")
return Py.newInteger(pos);
if (key == "endpos")
return Py.newInteger(endpos);
if (key == "lastindex")
return lastindex == -1 ? Py.None : Py.newInteger(lastindex);
if (key == "lastgroup"){
if(pattern.indexgroup != null && lastindex >= 0)
return pattern.indexgroup.__getitem__(lastindex);
return Py.None;
}
if ( key == "regs" ){
return regs();
}
return super.__findattr_ex__(key);
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retVal;
if (pattern != null) {
retVal = visit.visit(pattern, arg);
if (retVal != 0) {
return retVal;
}
}
if (string != null) {
retVal = visit.visit(string, arg);
if (retVal != 0) {
return retVal;
}
}
return regs != null ? visit.visit(regs, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == pattern || ob == string || ob == regs);
}
}