001: /*
002: * TestExprParserCmd.java --
003: *
004: * This procedure implements the "testexprparser" command. It is
005: * used for testing Parser.parseCommand.
006: *
007: * Copyright (c) 1997 by Sun Microsystems, Inc.
008: *
009: * See the file "license.terms" for information on usage and redistribution
010: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
011: *
012: * RCS: @(#) $Id: TestExprParserCmd.java,v 1.2 2006/03/11 22:35:54 mdejong Exp $
013: */
014:
015: package tcl.lang;
016:
017: public class TestExprParserCmd implements Command {
018:
019: /*
020: *----------------------------------------------------------------------
021: *
022: * TestexprparserObjCmd -> cmdProc
023: *
024: * This procedure implements the "testexprparser" command. It is
025: * used for testing the new Tcl expression parser in Tcl 8.1.
026: *
027: * Results:
028: * None.
029: *
030: * Side effects:
031: * None.
032: *
033: *----------------------------------------------------------------------
034: */
035:
036: public void cmdProc(Interp interp, // Current interpreter.
037: TclObject objv[]) // The argument objects.
038: throws TclException {
039: CharPointer script;
040: String string;
041: int length;
042: TclParse parse = null;
043:
044: if (objv.length != 3) {
045: throw new TclNumArgsException(interp, 1, objv,
046: "expr length");
047: }
048:
049: string = objv[1].toString();
050: script = new CharPointer(string);
051:
052: length = TclInteger.get(interp, objv[2]);
053: if (length == 0) {
054: length = string.length();
055: }
056:
057: parse = ParseExpr.parseExpr(interp, script.array, script.index,
058: length);
059:
060: if (parse.result != TCL.OK) {
061: interp.addErrorInfo("\n (remainder of script: \"");
062: interp
063: .addErrorInfo(new String(parse.string,
064: parse.termIndex,
065: (parse.endIndex - parse.termIndex)));
066: interp.addErrorInfo("\")");
067: throw new TclException(TCL.ERROR);
068: }
069:
070: // The parse completed successfully. Just print out the contents
071: // of the parse structure into the interpreter's result.
072:
073: try {
074: TclObject tlist = TclList.newInstance();
075: PrintParse(interp, parse, tlist);
076: interp.setResult(tlist);
077: } finally {
078: parse.release();
079: }
080: return;
081: }
082:
083: /*
084: *----------------------------------------------------------------------
085: *
086: * PrintParse -> PrintParse
087: *
088: * This procedure prints out the contents of a TclParse structure
089: * in the result of an interpreter.
090: *
091: * Results:
092: * Appends to the TclList obj containing the contents of parse.
093: *
094: * Side effects:
095: * None.
096: *
097: *----------------------------------------------------------------------
098: */
099:
100: static void PrintParse(Interp interp, TclParse parse, TclObject obj)
101: throws TclException {
102: String typeString;
103: TclToken token;
104: int i;
105:
106: if (parse.commentStart != -1) {
107: TclList.append(interp, obj, TclString
108: .newInstance(new String(parse.string,
109: parse.commentStart, parse.commentSize)));
110: } else {
111: TclList.append(interp, obj, TclString.newInstance("-"));
112: }
113: if (parse.commandStart != -1) {
114: TclList.append(interp, obj, TclString
115: .newInstance(new String(parse.string,
116: parse.commandStart, parse.commandSize)));
117: } else {
118: TclList.append(interp, obj, TclString.newInstance(""));
119: }
120: TclList.append(interp, obj, TclInteger
121: .newInstance(parse.numWords));
122:
123: for (i = 0; i < parse.numTokens; i++) {
124: token = parse.getToken(i);
125: switch (token.type) {
126: case Parser.TCL_TOKEN_WORD:
127: typeString = "word";
128: break;
129: case Parser.TCL_TOKEN_SIMPLE_WORD:
130: typeString = "simple";
131: break;
132: case Parser.TCL_TOKEN_TEXT:
133: typeString = "text";
134: break;
135: case Parser.TCL_TOKEN_BS:
136: typeString = "backslash";
137: break;
138: case Parser.TCL_TOKEN_COMMAND:
139: typeString = "command";
140: break;
141: case Parser.TCL_TOKEN_VARIABLE:
142: typeString = "variable";
143: break;
144: case Parser.TCL_TOKEN_SUB_EXPR:
145: typeString = "subexpr";
146: break;
147: case Parser.TCL_TOKEN_OPERATOR:
148: typeString = "operator";
149: break;
150: default:
151: typeString = "??";
152: break;
153: }
154: TclList.append(interp, obj, TclString
155: .newInstance(typeString));
156: TclList.append(interp, obj, TclString
157: .newInstance(new String(token.script_array,
158: token.script_index, token.size)));
159: TclList.append(interp, obj, TclInteger
160: .newInstance(token.numComponents));
161: }
162: if (parse.commandStart != -1) {
163: int index = parse.commandStart + parse.commandSize;
164: // Append rest of command string, going past parse termination index.
165: //int len = parse.endIndex - index;
166: int len = (parse.string.length - 1) - index;
167: TclList.append(interp, obj, TclString
168: .newInstance(new String(parse.string, index, len)));
169: } else {
170: TclList.append(interp, obj, TclString.newInstance(""));
171: }
172: }
173:
174: } // end class TestExprParserCmd
|