001: /*
002: * RegexpCmd.java --
003: *
004: * This file contains the Jacl implementation of the built-in Tcl
005: * "regexp" command.
006: *
007: * Copyright (c) 1997-1999 Sun Microsystems, Inc.
008: *
009: * See the file "license.terms" for information on usage and
010: * redistribution of this file, and for a DISCLAIMER OF ALL
011: * WARRANTIES.
012: *
013: * RCS: @(#) $Id: RegexpCmd.java,v 1.3 2000/02/23 22:07:23 mo Exp $
014: */
015:
016: package tcl.lang;
017:
018: import sunlabs.brazil.util.regexp.Regexp;
019:
020: /**
021: * This class implements the built-in "regexp" command in Tcl.
022: */
023:
024: class RegexpCmd implements Command {
025:
026: static final private String validOpts[] = { "-indices", "-nocase",
027: "--" };
028: static final private int OPT_INDICES = 0;
029: static final private int OPT_NOCASE = 1;
030: static final private int OPT_LAST = 2;
031:
032: /*
033: *-----------------------------------------------------------------------------
034: *
035: * init --
036: *
037: * This procedure is invoked to connect the regexp and regsub commands to
038: * the CmdProc method of the RegexpCmd and RegsubCmd classes,
039: * respectively. Avoid the AutoloadStub class because regexp and regsub
040: * need a stub with a switch to check for the existence of the tcl.regexp
041: * package.
042: *
043: * Results:
044: * None.
045: *
046: * Side effects:
047: * The regexp and regsub s]commands are now connected to the CmdProc
048: * method of the RegexpCmd and RegsubCmd classes, respectively.
049: *
050: *-----------------------------------------------------------------------------
051: */
052:
053: static void init(Interp interp) // Current interpreter.
054: {
055: interp.createCommand("regexp", new tcl.lang.RegexpCmd());
056: interp.createCommand("regsub", new tcl.lang.RegsubCmd());
057: }
058:
059: /*
060: *-----------------------------------------------------------------------------
061: *
062: * cmdProc --
063: *
064: * This procedure is invoked to process the "regexp" Tcl command.
065: * See the user documentation for details on what it does.
066: *
067: * Results:
068: * A standard Tcl result.
069: *
070: * Side effects:
071: * See the user documentation.
072: *
073: *-----------------------------------------------------------------------------
074: */
075:
076: public void cmdProc(Interp interp, // Current interpreter.
077: TclObject argv[]) // Arguments to "regexp" command.
078: throws TclException {
079: boolean nocase = false;
080: boolean indices = false;
081:
082: try {
083: int i = 1;
084: opts: while (argv[i].toString().startsWith("-")) {
085: int index = TclIndex.get(interp, argv[i], validOpts,
086: "switch", 0);
087: i++;
088: switch (index) {
089: case OPT_INDICES: {
090: indices = true;
091: break;
092: }
093: case OPT_NOCASE: {
094: nocase = true;
095: break;
096: }
097: case OPT_LAST: {
098: break opts;
099: }
100: }
101: }
102:
103: TclObject exp = argv[i++];
104: String string = argv[i++].toString();
105:
106: int matches = argv.length - i;
107:
108: Regexp r = TclRegexp.compile(interp, exp, nocase);
109:
110: int[] args = new int[matches * 2];
111: boolean matched = r.match(string, args);
112: if (matched) {
113: for (int match = 0; i < argv.length; i++) {
114: TclObject obj;
115:
116: int start = args[match++];
117: int end = args[match++];
118: if (indices) {
119: if (end >= 0) {
120: end--;
121: }
122: obj = TclList.newInstance();
123: TclList.append(interp, obj, TclInteger
124: .newInstance(start));
125: TclList.append(interp, obj, TclInteger
126: .newInstance(end));
127: } else {
128: String range = (start >= 0) ? string.substring(
129: start, end) : "";
130: obj = TclString.newInstance(range);
131: }
132: try {
133: interp.setVar(argv[i].toString(), obj, 0);
134: } catch (TclException e) {
135: throw new TclException(interp,
136: "couldn't set variable \"" + argv[i]
137: + "\"");
138: }
139: }
140: }
141: interp.setResult(matched);
142: } catch (IndexOutOfBoundsException e) {
143: throw new TclNumArgsException(interp, 1, argv,
144: "?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?");
145: }
146: }
147:
148: } // end RegexpCmd
|