Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/ParseCmd.3 @ 42

Last change on this file since 42 was 25, checked in by landauf, 17 years ago

added tcl to libs

File size: 20.7 KB
RevLine 
[25]1'\"
2'\" Copyright (c) 1997 Sun Microsystems, Inc.
3'\"
4'\" See the file "license.terms" for information on usage and redistribution
5'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6'\"
7'\" RCS: @(#) $Id: ParseCmd.3,v 1.27 2007/12/13 15:22:31 dgp Exp $
8'\"
9.so man.macros
10.TH Tcl_ParseCommand 3 8.3 Tcl "Tcl Library Procedures"
11.BS
12.SH NAME
13Tcl_ParseCommand, Tcl_ParseExpr, Tcl_ParseBraces, Tcl_ParseQuotedString, Tcl_ParseVarName, Tcl_ParseVar, Tcl_FreeParse, Tcl_EvalTokens, Tcl_EvalTokensStandard \- parse Tcl scripts and expressions
14.SH SYNOPSIS
15.nf
16\fB#include <tcl.h>\fR
17.sp
18int
19\fBTcl_ParseCommand\fR(\fIinterp, start, numBytes, nested, parsePtr\fR)
20.sp
21int
22\fBTcl_ParseExpr\fR(\fIinterp, start, numBytes, parsePtr\fR)
23.sp
24int
25\fBTcl_ParseBraces\fR(\fIinterp, start, numBytes, parsePtr, append, termPtr\fR)
26.sp
27int
28\fBTcl_ParseQuotedString\fR(\fIinterp, start, numBytes, parsePtr, append, termPtr\fR)
29.sp
30int
31\fBTcl_ParseVarName\fR(\fIinterp, start, numBytes, parsePtr, append\fR)
32.sp
33const char *
34\fBTcl_ParseVar\fR(\fIinterp, start, termPtr\fR)
35.sp
36\fBTcl_FreeParse\fR(\fIusedParsePtr\fR)
37.sp
38Tcl_Obj *
39\fBTcl_EvalTokens\fR(\fIinterp, tokenPtr, numTokens\fR)
40.sp
41int
42\fBTcl_EvalTokensStandard\fR(\fIinterp, tokenPtr, numTokens\fR)
43.SH ARGUMENTS
44.AS Tcl_Interp *usedParsePtr out
45.AP Tcl_Interp *interp out
46For procedures other than \fBTcl_FreeParse\fR, \fBTcl_EvalTokens\fR
47and \fBTcl_EvalTokensStandard\fR, used only for error reporting;
48if NULL, then no error messages are left after errors.
49For \fBTcl_EvalTokens\fR and \fBTcl_EvalTokensStandard\fR,
50determines the context for evaluating the
51script and also is used for error reporting; must not be NULL.
52.AP "const char" *start in
53Pointer to first character in string to parse.
54.AP int numBytes in
55Number of bytes in string to parse, not including any terminating null
56character.  If less than 0 then the script consists of all characters
57following \fIstart\fR up to the first null character.
58.AP int nested in
59Non-zero means that the script is part of a command substitution so an
60unquoted close bracket should be treated as a command terminator.  If zero,
61close brackets have no special meaning.
62.AP int append in
63Non-zero means that \fI*parsePtr\fR already contains valid tokens; the new
64tokens should be appended to those already present.  Zero means that
65\fI*parsePtr\fR is uninitialized; any information in it is ignored.
66This argument is normally 0.
67.AP Tcl_Parse *parsePtr out
68Points to structure to fill in with information about the parsed
69command, expression, variable name, etc.
70Any previous information in this structure
71is ignored, unless \fIappend\fR is non-zero in a call to
72\fBTcl_ParseBraces\fR, \fBTcl_ParseQuotedString\fR,
73or \fBTcl_ParseVarName\fR.
74.AP "const char" **termPtr out
75If not NULL, points to a location where
76\fBTcl_ParseBraces\fR, \fBTcl_ParseQuotedString\fR, and
77\fBTcl_ParseVar\fR will store a pointer to the character
78just after the terminating character (the close-brace, the last
79character of the variable name, or the close-quote (respectively))
80if the parse was successful.
81.AP Tcl_Parse *usedParsePtr in
82Points to structure that was filled in by a previous call to
83\fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseVarName\fR, etc.
84.BE
85
86.SH DESCRIPTION
87.PP
88These procedures parse Tcl commands or portions of Tcl commands such as
89expressions or references to variables.
90Each procedure takes a pointer to a script (or portion thereof)
91and fills in the structure pointed to by \fIparsePtr\fR
92with a collection of tokens describing the information that was parsed.
93The procedures normally return \fBTCL_OK\fR.
94However, if an error occurs then they return \fBTCL_ERROR\fR,
95leave an error message in \fIinterp\fR's result
96(if \fIinterp\fR is not NULL),
97and leave nothing in \fIparsePtr\fR.
98.PP
99\fBTcl_ParseCommand\fR is a procedure that parses Tcl
100scripts.  Given a pointer to a script, it
101parses the first command from the script.  If the command was parsed
102successfully, \fBTcl_ParseCommand\fR returns \fBTCL_OK\fR and fills in the
103structure pointed to by \fIparsePtr\fR with information about the
104structure of the command (see below for details).
105If an error occurred in parsing the command then
106\fBTCL_ERROR\fR is returned, an error message is left in \fIinterp\fR's
107result, and no information is left at \fI*parsePtr\fR.
108.PP
109\fBTcl_ParseExpr\fR parses Tcl expressions.
110Given a pointer to a script containing an expression,
111\fBTcl_ParseExpr\fR parses the expression.
112If the expression was parsed successfully,
113\fBTcl_ParseExpr\fR returns \fBTCL_OK\fR and fills in the
114structure pointed to by \fIparsePtr\fR with information about the
115structure of the expression (see below for details).
116If an error occurred in parsing the command then
117\fBTCL_ERROR\fR is returned, an error message is left in \fIinterp\fR's
118result, and no information is left at \fI*parsePtr\fR.
119.PP
120\fBTcl_ParseBraces\fR parses a string or command argument
121enclosed in braces such as
122\fB{hello}\fR or \fB{string \et with \et tabs}\fR
123from the beginning of its argument \fIstart\fR.
124The first character of \fIstart\fR must be \fB{\fR.
125If the braced string was parsed successfully,
126\fBTcl_ParseBraces\fR returns \fBTCL_OK\fR,
127fills in the structure pointed to by \fIparsePtr\fR
128with information about the structure of the string
129(see below for details),
130and stores a pointer to the character just after the terminating \fB}\fR
131in the location given by \fI*termPtr\fR.
132If an error occurs while parsing the string
133then \fBTCL_ERROR\fR is returned,
134an error message is left in \fIinterp\fR's result,
135and no information is left at \fI*parsePtr\fR or \fI*termPtr\fR.
136.PP
137\fBTcl_ParseQuotedString\fR parses a double-quoted string such as
138\fB"sum is [expr {$a+$b}]"\fR
139from the beginning of the argument \fIstart\fR.
140The first character of \fIstart\fR must be \fB\N'34'\fR.
141If the double-quoted string was parsed successfully,
142\fBTcl_ParseQuotedString\fR returns \fBTCL_OK\fR,
143fills in the structure pointed to by \fIparsePtr\fR
144with information about the structure of the string
145(see below for details),
146and stores a pointer to the character just after the terminating \fB\N'34'\fR
147in the location given by \fI*termPtr\fR.
148If an error occurs while parsing the string
149then \fBTCL_ERROR\fR is returned,
150an error message is left in \fIinterp\fR's result,
151and no information is left at \fI*parsePtr\fR or \fI*termPtr\fR.
152.PP
153\fBTcl_ParseVarName\fR parses a Tcl variable reference such as
154\fB$abc\fR or \fB$x([expr {$index + 1}])\fR from the beginning of its
155\fIstart\fR argument.
156The first character of \fIstart\fR must be \fB$\fR.
157If a variable name was parsed successfully, \fBTcl_ParseVarName\fR
158returns \fBTCL_OK\fR and fills in the structure pointed to by
159\fIparsePtr\fR with information about the structure of the variable name
160(see below for details).  If an error
161occurs while parsing the command then \fBTCL_ERROR\fR is returned, an
162error message is left in \fIinterp\fR's result (if \fIinterp\fR is not
163NULL), and no information is left at \fI*parsePtr\fR.
164.PP
165\fBTcl_ParseVar\fR parse a Tcl variable reference such as \fB$abc\fR
166or \fB$x([expr {$index + 1}])\fR from the beginning of its \fIstart\fR
167argument.  The first character of \fIstart\fR must be \fB$\fR.  If
168the variable name is parsed successfully, \fBTcl_ParseVar\fR returns a
169pointer to the string value of the variable.  If an error occurs while
170parsing, then NULL is returned and an error message is left in
171\fIinterp\fR's result.
172.PP
173The information left at \fI*parsePtr\fR
174by \fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseBraces\fR,
175\fBTcl_ParseQuotedString\fR, and \fBTcl_ParseVarName\fR
176may include dynamically allocated memory.
177If these five parsing procedures return \fBTCL_OK\fR
178then the caller must invoke \fBTcl_FreeParse\fR to release
179the storage at \fI*parsePtr\fR.
180These procedures ignore any existing information in
181\fI*parsePtr\fR (unless \fIappend\fR is non-zero),
182so if repeated calls are being made to any of them
183then \fBTcl_FreeParse\fR must be invoked once after each call.
184.PP
185\fBTcl_EvalTokensStandard\fR evaluates a sequence of parse tokens from
186a Tcl_Parse structure.  The tokens typically consist
187of all the tokens in a word or all the tokens that make up the index for
188a reference to an array variable.  \fBTcl_EvalTokensStandard\fR performs the
189substitutions requested by the tokens and concatenates the
190resulting values.
191The return value from \fBTcl_EvalTokensStandard\fR is a Tcl completion
192code with one of the values \fBTCL_OK\fR, \fBTCL_ERROR\fR,
193\fBTCL_RETURN\fR, \fBTCL_BREAK\fR, or \fBTCL_CONTINUE\fR, or possibly
194some other integer value originating in an extension.
195In addition, a result value or error message is left in \fIinterp\fR's
196result; it can be retrieved using \fBTcl_GetObjResult\fR.
197.PP
198\fBTcl_EvalTokens\fR differs from \fBTcl_EvalTokensStandard\fR only in
199the return convention used: it returns the result in a new Tcl_Obj.
200The reference count of the object returned as result has been
201incremented, so the caller must
202invoke \fBTcl_DecrRefCount\fR when it is finished with the object.
203If an error or other exception occurs while evaluating the tokens
204(such as a reference to a non-existent variable) then the return value
205is NULL and an error message is left in \fIinterp\fR's result. The use
206of \fBTcl_EvalTokens\fR is deprecated.
207
208.SH "TCL_PARSE STRUCTURE"
209.PP
210\fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseBraces\fR,
211\fBTcl_ParseQuotedString\fR, and \fBTcl_ParseVarName\fR
212return parse information in two data structures, Tcl_Parse and Tcl_Token:
213.CS
214typedef struct Tcl_Parse {
215        const char *\fIcommentStart\fR;
216        int \fIcommentSize\fR;
217        const char *\fIcommandStart\fR;
218        int \fIcommandSize\fR;
219        int \fInumWords\fR;
220        Tcl_Token *\fItokenPtr\fR;
221        int \fInumTokens\fR;
222        ...
223} Tcl_Parse;
224
225typedef struct Tcl_Token {
226        int \fItype\fR;
227        const char *\fIstart\fR;
228        int \fIsize\fR;
229        int \fInumComponents\fR;
230} Tcl_Token;
231.CE
232.PP
233The first five fields of a Tcl_Parse structure
234are filled in only by \fBTcl_ParseCommand\fR.
235These fields are not used by the other parsing procedures.
236.PP
237\fBTcl_ParseCommand\fR fills in a Tcl_Parse structure
238with information that describes one Tcl command and any comments that
239precede the command.
240If there are comments,
241the \fIcommentStart\fR field points to the \fB#\fR character that begins
242the first comment and \fIcommentSize\fR indicates the number of bytes
243in all of the comments preceding the command, including the newline
244character that terminates the last comment.
245If the command is not preceded by any comments, \fIcommentSize\fR is 0.
246\fBTcl_ParseCommand\fR also sets the \fIcommandStart\fR field
247to point to the first character of the first
248word in the command (skipping any comments and leading space) and
249\fIcommandSize\fR gives the total number of bytes in the command,
250including the character pointed to by \fIcommandStart\fR up to and
251including the newline, close bracket, or semicolon character that
252terminates the command.  The \fInumWords\fR field gives the
253total number of words in the command.
254.PP
255All parsing procedures set the remaining fields,
256\fItokenPtr\fR and \fInumTokens\fR.
257The \fItokenPtr\fR field points to the first in an array of Tcl_Token
258structures that describe the components of the entity being parsed.
259The \fInumTokens\fR field gives the total number of tokens
260present in the array.
261Each token contains four fields.
262The \fItype\fR field selects one of several token types
263that are described below.  The \fIstart\fR field
264points to the first character in the token and the \fIsize\fR field
265gives the total number of characters in the token.  Some token types,
266such as \fBTCL_TOKEN_WORD\fR and \fBTCL_TOKEN_VARIABLE\fR, consist of
267several component tokens, which immediately follow the parent token;
268the \fInumComponents\fR field describes how many of these there are.
269The \fItype\fR field has one of the following values:
270.TP 20
271\fBTCL_TOKEN_WORD\fR
272This token ordinarily describes one word of a command
273but it may also describe a quoted or braced string in an expression.
274The token describes a component of the script that is
275the result of concatenating together a sequence of subcomponents,
276each described by a separate subtoken.
277The token starts with the first non-blank
278character of the component (which may be a double-quote or open brace)
279and includes all characters in the component up to but not including the
280space, semicolon, close bracket, close quote, or close brace that
281terminates the component.  The \fInumComponents\fR field counts the total
282number of sub-tokens that make up the word, including sub-tokens
283of \fBTCL_TOKEN_VARIABLE\fR and \fBTCL_TOKEN_BS\fR tokens.
284.TP
285\fBTCL_TOKEN_SIMPLE_WORD\fR
286This token has the same meaning as \fBTCL_TOKEN_WORD\fR, except that
287the word is guaranteed to consist of a single \fBTCL_TOKEN_TEXT\fR
288sub-token.  The \fInumComponents\fR field is always 1.
289.TP
290\fBTCL_TOKEN_EXPAND_WORD\fR
291.VS 8.5
292This token has the same meaning as \fBTCL_TOKEN_WORD\fR, except that
293the command parser notes this word began with the expansion
294prefix \fB{*}\fR, indicating that after substitution,
295the list value of this word should be expanded to form multiple
296arguments in command evaluation.  This
297token type can only be created by Tcl_ParseCommand.
298.VE 8.5
299.TP
300\fBTCL_TOKEN_TEXT\fR
301The token describes a range of literal text that is part of a word.
302The \fInumComponents\fR field is always 0.
303.TP
304\fBTCL_TOKEN_BS\fR
305The token describes a backslash sequence such as \fB\en\fR or \fB\e0xa3\fR.
306The \fInumComponents\fR field is always 0.
307.TP
308\fBTCL_TOKEN_COMMAND\fR
309The token describes a command whose result must be substituted into
310the word.  The token includes the square brackets that surround the
311command.  The \fInumComponents\fR field is always 0 (the nested command
312is not parsed; call \fBTcl_ParseCommand\fR recursively if you want to
313see its tokens).
314.TP
315\fBTCL_TOKEN_VARIABLE\fR
316The token describes a variable substitution, including the
317\fB$\fR, variable name, and array index (if there is one) up through the
318close parenthesis that terminates the index.  This token is followed
319by one or more additional tokens that describe the variable name and
320array index.  If \fInumComponents\fR  is 1 then the variable is a
321scalar and the next token is a \fBTCL_TOKEN_TEXT\fR token that gives the
322variable name.  If \fInumComponents\fR is greater than 1 then the
323variable is an array: the first sub-token is a \fBTCL_TOKEN_TEXT\fR
324token giving the array name and the remaining sub-tokens are
325\fBTCL_TOKEN_TEXT\fR, \fBTCL_TOKEN_BS\fR, \fBTCL_TOKEN_COMMAND\fR, and
326\fBTCL_TOKEN_VARIABLE\fR tokens that must be concatenated to produce the
327array index. The \fInumComponents\fR field includes nested sub-tokens
328that are part of \fBTCL_TOKEN_VARIABLE\fR tokens in the array index.
329.TP
330\fBTCL_TOKEN_SUB_EXPR\fR
331The token describes one subexpression of an expression
332(or an entire expression).
333A subexpression may consist of a value
334such as an integer literal, variable substitution,
335or parenthesized subexpression;
336it may also consist of an operator and its operands.
337The token starts with the first non-blank character of the subexpression
338up to but not including the space, brace, close-paren, or bracket
339that terminates the subexpression.
340This token is followed by one or more additional tokens
341that describe the subexpression.
342If the first sub-token after the \fBTCL_TOKEN_SUB_EXPR\fR token
343is a \fBTCL_TOKEN_OPERATOR\fR token,
344the subexpression consists of an operator and its token operands.
345If the operator has no operands, the subexpression consists of
346just the \fBTCL_TOKEN_OPERATOR\fR token.
347Each operand is described by a \fBTCL_TOKEN_SUB_EXPR\fR token.
348Otherwise, the subexpression is a value described by
349one of the token types \fBTCL_TOKEN_WORD\fR, \fBTCL_TOKEN_TEXT\fR,
350\fBTCL_TOKEN_BS\fR, \fBTCL_TOKEN_COMMAND\fR,
351\fBTCL_TOKEN_VARIABLE\fR, and \fBTCL_TOKEN_SUB_EXPR\fR.
352The \fInumComponents\fR field
353counts the total number of sub-tokens that make up the subexpression;
354this includes the sub-tokens for any nested \fBTCL_TOKEN_SUB_EXPR\fR tokens.
355.TP
356\fBTCL_TOKEN_OPERATOR\fR
357The token describes one operator of an expression
358such as \fB&&\fR or \fBhypot\fR.
359A \fBTCL_TOKEN_OPERATOR\fR token is always preceded by a
360\fBTCL_TOKEN_SUB_EXPR\fR token
361that describes the operator and its operands;
362the \fBTCL_TOKEN_SUB_EXPR\fR token's \fInumComponents\fR field
363can be used to determine the number of operands.
364A binary operator such as \fB*\fR
365is followed by two \fBTCL_TOKEN_SUB_EXPR\fR tokens
366that describe its operands.
367A unary operator like \fB\-\fR
368is followed by a single \fBTCL_TOKEN_SUB_EXPR\fR token
369for its operand.
370If the operator is a math function such as \fBlog10\fR,
371the \fBTCL_TOKEN_OPERATOR\fR token will give its name and
372the following \fBTCL_TOKEN_SUB_EXPR\fR tokens will describe
373its operands;
374if there are no operands (as with \fBrand\fR),
375no \fBTCL_TOKEN_SUB_EXPR\fR tokens follow.
376There is one trinary operator, \fB?\fR,
377that appears in if-then-else subexpressions
378such as \fIx\fB?\fIy\fB:\fIz\fR;
379in this case, the \fB?\fR \fBTCL_TOKEN_OPERATOR\fR token
380is followed by three \fBTCL_TOKEN_SUB_EXPR\fR tokens for the operands
381\fIx\fR, \fIy\fR, and \fIz\fR.
382The \fInumComponents\fR field for a \fBTCL_TOKEN_OPERATOR\fR token
383is always 0.
384.PP
385After \fBTcl_ParseCommand\fR returns, the first token pointed to by
386the \fItokenPtr\fR field of the
387Tcl_Parse structure always has type \fBTCL_TOKEN_WORD\fR or
388.VS 8.5
389\fBTCL_TOKEN_SIMPLE_WORD\fR or \fBTCL_TOKEN_EXPAND_WORD\fR. 
390It is followed by the sub-tokens
391that must be concatenated to produce the value of that word.
392The next token is the \fBTCL_TOKEN_WORD\fR or \fBTCL_TOKEN_SIMPLE_WORD\fR
393of \fBTCL_TOKEN_EXPAND_WORD\fR token for the second word,
394followed by sub-tokens for that
395word, and so on until all \fInumWords\fR have been accounted
396for.
397.VE 8.5
398.PP
399After \fBTcl_ParseExpr\fR returns, the first token pointed to by
400the \fItokenPtr\fR field of the
401Tcl_Parse structure always has type \fBTCL_TOKEN_SUB_EXPR\fR.
402It is followed by the sub-tokens that must be evaluated
403to produce the value of the expression.
404Only the token information in the Tcl_Parse structure
405is modified: the \fIcommentStart\fR, \fIcommentSize\fR,
406\fIcommandStart\fR, and \fIcommandSize\fR fields are not modified
407by \fBTcl_ParseExpr\fR.
408.PP
409After \fBTcl_ParseBraces\fR returns,
410the array of tokens pointed to by the \fItokenPtr\fR field of the
411Tcl_Parse structure will contain a single \fBTCL_TOKEN_TEXT\fR token
412if the braced string does not contain any backslash-newlines.
413If the string does contain backslash-newlines,
414the array of tokens will contain one or more
415\fBTCL_TOKEN_TEXT\fR or \fBTCL_TOKEN_BS\fR sub-tokens
416that must be concatenated to produce the value of the string.
417If the braced string was just \fB{}\fR
418(that is, the string was empty),
419the single \fBTCL_TOKEN_TEXT\fR token will have a \fIsize\fR field
420containing zero;
421this ensures that at least one token appears
422to describe the braced string.
423Only the token information in the Tcl_Parse structure
424is modified: the \fIcommentStart\fR, \fIcommentSize\fR,
425\fIcommandStart\fR, and \fIcommandSize\fR fields are not modified
426by \fBTcl_ParseBraces\fR.
427.PP
428After \fBTcl_ParseQuotedString\fR returns,
429the array of tokens pointed to by the \fItokenPtr\fR field of the
430Tcl_Parse structure depends on the contents of the quoted string.
431It will consist of one or more \fBTCL_TOKEN_TEXT\fR, \fBTCL_TOKEN_BS\fR,
432\fBTCL_TOKEN_COMMAND\fR, and \fBTCL_TOKEN_VARIABLE\fR sub-tokens.
433The array always contains at least one token;
434for example, if the argument \fIstart\fR is empty,
435the array returned consists of a single \fBTCL_TOKEN_TEXT\fR token
436with a zero \fIsize\fR field.
437Only the token information in the Tcl_Parse structure
438is modified: the \fIcommentStart\fR, \fIcommentSize\fR,
439\fIcommandStart\fR, and \fIcommandSize\fR fields are not modified.
440.PP
441After \fBTcl_ParseVarName\fR returns, the first token pointed to by
442the \fItokenPtr\fR field of the
443Tcl_Parse structure always has type \fBTCL_TOKEN_VARIABLE\fR.  It
444is followed by the sub-tokens that make up the variable name as
445described above.  The total length of the variable name is
446contained in the \fIsize\fR field of the first token.
447As in \fBTcl_ParseExpr\fR,
448only the token information in the Tcl_Parse structure
449is modified by \fBTcl_ParseVarName\fR:
450the \fIcommentStart\fR, \fIcommentSize\fR,
451\fIcommandStart\fR, and \fIcommandSize\fR fields are not modified.
452.PP
453All of the character pointers in the
454Tcl_Parse and Tcl_Token structures refer
455to characters in the \fIstart\fR argument passed to
456\fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseBraces\fR,
457\fBTcl_ParseQuotedString\fR, and \fBTcl_ParseVarName\fR.
458.PP
459There are additional fields in the Tcl_Parse structure after the
460\fInumTokens\fR field, but these are for the private use of
461\fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseBraces\fR,
462\fBTcl_ParseQuotedString\fR, and \fBTcl_ParseVarName\fR; they should not be
463referenced by code outside of these procedures.
464
465.SH KEYWORDS
466backslash substitution, braces, command, expression, parse, token, variable substitution
Note: See TracBrowser for help on using the repository browser.