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