001: /*
002: * Copyright 2000 Finn Bock
003: *
004: * This program contains material copyrighted by:
005: * Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
006: *
007: * This version of the SRE library can be redistributed under CNRI's
008: * Python 1.6 license. For any other use, please contact Secret Labs
009: * AB (info@pythonware.com).
010: *
011: * Portions of this engine have been developed in cooperation with
012: * CNRI. Hewlett-Packard provided funding for 1.6 integration and
013: * other compatibility work.
014: */
015:
016: package org.python.modules.sre;
017:
018: import org.python.core.ArgParser;
019: import org.python.core.Py;
020: import org.python.core.PyDictionary;
021: import org.python.core.PyInteger;
022: import org.python.core.PyObject;
023: import org.python.core.PyString;
024: import org.python.core.PyTuple;
025: import org.python.core.imp;
026:
027: public class MatchObject extends PyObject {
028: public PyString string; /* link to the target string */
029: public PyObject regs; /* cached list of matching spans */
030: PatternObject pattern; /* link to the regex (pattern) object */
031: int pos, endpos; /* current target slice */
032: int lastindex; /* last index marker seen by the engine (-1 if none) */
033: int groups; /* number of groups (start/end marks) */
034: int[] mark;
035:
036: public PyObject expand(PyObject[] args) {
037: if (args.length == 0) {
038: throw Py
039: .TypeError("expand() takes exactly 1 argument (0 given)");
040: }
041: PyObject mod = imp.importName("sre", true);
042: PyObject func = mod.__getattr__("_expand");
043: return func.__call__(new PyObject[] { pattern, this , args[0] });
044: }
045:
046: public PyObject group(PyObject[] args) {
047: switch (args.length) {
048: case 0:
049: return getslice(Py.Zero, Py.None);
050: case 1:
051: return getslice(args[0], Py.None);
052: default:
053: PyObject[] result = new PyObject[args.length];
054: for (int i = 0; i < args.length; i++)
055: result[i] = getslice(args[i], Py.None);
056: return new PyTuple(result);
057: }
058: }
059:
060: public PyObject groups(PyObject[] args, String[] kws) {
061: ArgParser ap = new ArgParser("groups", args, kws, "default");
062: PyObject def = ap.getPyObject(0, Py.None);
063:
064: PyObject[] result = new PyObject[groups - 1];
065: for (int i = 1; i < groups; i++) {
066: result[i - 1] = getslice_by_index(i, def);
067: }
068: return new PyTuple(result);
069: }
070:
071: public PyObject groupdict(PyObject[] args, String[] kws) {
072: ArgParser ap = new ArgParser("groupdict", args, kws, "default");
073: PyObject def = ap.getPyObject(0, Py.None);
074:
075: PyObject result = new PyDictionary();
076:
077: if (pattern.groupindex == null)
078: return result;
079:
080: PyObject keys = pattern.groupindex.invoke("keys");
081:
082: PyObject key;
083: for (int i = 0; (key = keys.__finditem__(i)) != null; i++) {
084: PyObject item = getslice(key, def);
085: result.__setitem__(key, item);
086: }
087: return result;
088: }
089:
090: public PyObject start() {
091: return start(Py.Zero);
092: }
093:
094: public PyObject start(PyObject index_) {
095: int index = getindex(index_);
096:
097: if (index < 0 || index >= groups)
098: throw Py.IndexError("no such group");
099:
100: return Py.newInteger(mark[index * 2]);
101: }
102:
103: public PyObject end() {
104: return end(Py.Zero);
105: }
106:
107: public PyObject end(PyObject index_) {
108: int index = getindex(index_);
109:
110: if (index < 0 || index >= groups)
111: throw Py.IndexError("no such group");
112:
113: return Py.newInteger(mark[index * 2 + 1]);
114: }
115:
116: public PyTuple span() {
117: return span(Py.Zero);
118: }
119:
120: public PyTuple span(PyObject index_) {
121: int index = getindex(index_);
122:
123: if (index < 0 || index >= groups)
124: throw Py.IndexError("no such group");
125:
126: int start = mark[index * 2];
127: int end = mark[index * 2 + 1];
128:
129: return _pair(start, end);
130: }
131:
132: public PyObject regs() {
133:
134: PyObject[] regs = new PyObject[groups];
135:
136: for (int index = 0; index < groups; index++) {
137: regs[index] = _pair(mark[index * 2], mark[index * 2 + 1]);
138: }
139:
140: return new PyTuple(regs);
141: }
142:
143: PyTuple _pair(int i1, int i2) {
144: return new PyTuple(new PyObject[] { Py.newInteger(i1),
145: Py.newInteger(i2) });
146: }
147:
148: private PyObject getslice(PyObject index, PyObject def) {
149: return getslice_by_index(getindex(index), def);
150: }
151:
152: private int getindex(PyObject index) {
153: if (index instanceof PyInteger)
154: return ((PyInteger) index).getValue();
155:
156: int i = -1;
157:
158: if (pattern.groupindex != null) {
159: index = pattern.groupindex.__finditem__(index);
160: if (index != null)
161: if (index instanceof PyInteger)
162: return ((PyInteger) index).getValue();
163: }
164: return i;
165: }
166:
167: private PyObject getslice_by_index(int index, PyObject def) {
168: if (index < 0 || index >= groups)
169: throw Py.IndexError("no such group");
170:
171: index *= 2;
172: int start = mark[index];
173: int end = mark[index + 1];
174:
175: //System.out.println("group:" + index + " " + start + " " +
176: // end + " l:" + string.length());
177:
178: if (string == null || start < 0)
179: return def;
180: return string.__getslice__(Py.newInteger(start), Py
181: .newInteger(end));
182:
183: }
184:
185: public PyObject __findattr__(String key) {
186: //System.out.println("__findattr__:" + key);
187: if (key == "flags")
188: return Py.newInteger(pattern.flags);
189: if (key == "groupindex")
190: return pattern.groupindex;
191: if (key == "re")
192: return pattern;
193: if (key == "pos")
194: return Py.newInteger(pos);
195: if (key == "endpos")
196: return Py.newInteger(endpos);
197: if (key == "lastindex")
198: return lastindex == -1 ? Py.None : Py.newInteger(lastindex);
199: if (key == "lastgroup") {
200: if (pattern.indexgroup != null && lastindex >= 0)
201: return pattern.indexgroup.__getitem__(lastindex);
202: return Py.None;
203: }
204: if (key == "regs") {
205: return regs();
206: }
207: return super.__findattr__(key);
208: }
209: }
|