📄 parsecmd.3
字号:
'\"'\" Copyright (c) 1997 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" RCS: @(#) $Id: ParseCmd.3,v 1.10 2002/08/05 03:24:39 dgp Exp $'\" .so man.macros.TH Tcl_ParseCommand 3 8.3 Tcl "Tcl Library Procedures".BS.SH NAMETcl_ParseCommand, Tcl_ParseExpr, Tcl_ParseBraces, Tcl_ParseQuotedString, Tcl_ParseVarName, Tcl_ParseVar, Tcl_FreeParse, Tcl_EvalTokens, Tcl_EvalTokensStandard \- parse Tcl scripts and expressions.SH SYNOPSIS.nf\fB#include <tcl.h>\fR.spint\fBTcl_ParseCommand\fR(\fIinterp, string, numBytes, nested, parsePtr\fR).spint\fBTcl_ParseExpr\fR(\fIinterp, string, numBytes, parsePtr\fR).spint\fBTcl_ParseBraces\fR(\fIinterp, string, numBytes, parsePtr, append, termPtr\fR).spint\fBTcl_ParseQuotedString\fR(\fIinterp, string, numBytes, parsePtr, append, termPtr\fR).spint\fBTcl_ParseVarName\fR(\fIinterp, string, numBytes, parsePtr, append\fR).spCONST char *\fBTcl_ParseVar\fR(\fIinterp, string, termPtr\fR).sp\fBTcl_FreeParse\fR(\fIusedParsePtr\fR).spTcl_Obj *\fBTcl_EvalTokens\fR(\fIinterp, tokenPtr, numTokens\fR).spTcl_Obj *\fBTcl_EvalTokensStandard\fR(\fIinterp, tokenPtr, numTokens\fR).SH ARGUMENTS.AS Tcl_Interp *usedParsePtr.AP Tcl_Interp *interp outFor procedures other than \fBTcl_FreeParse\fR, \fBTcl_EvalTokens\fRand \fBTcl_EvalTokensStandard\fR, used only for error reporting;if NULL, then no error messages are left after errors.For \fBTcl_EvalTokens\fR and \fBTcl_EvalTokensStandard\fR,determines the context for evaluating thescript and also is used for error reporting; must not be NULL..AP "CONST char" *string inPointer to first character in string to parse..AP int numBytes inNumber of bytes in \fIstring\fR, not including any terminating nullcharacter. If less than 0 then the script consists of all charactersin \fIstring\fR up to the first null character..AP int nested inNon-zero means that the script is part of a command substitution so anunquoted close bracket should be treated as a command terminator. If zero,close brackets have no special meaning. .AP int append inNon-zero means that \fI*parsePtr\fR already contains valid tokens; the newtokens should be appended to those already present. Zero means that\fI*parsePtr\fR is uninitialized; any information in it is ignored.This argument is normally 0..AP Tcl_Parse *parsePtr outPoints to structure to fill in with information about the parsedcommand, expression, variable name, etc.Any previous information in this structureis ignored, unless \fIappend\fR is non-zero in a call to\fBTcl_ParseBraces\fR, \fBTcl_ParseQuotedString\fR,or \fBTcl_ParseVarName\fR..AP "CONST char" **termPtr outIf not NULL, points to a location where\fBTcl_ParseBraces\fR, \fBTcl_ParseQuotedString\fR, and\fBTcl_ParseVar\fR will store a pointer to the characterjust after the terminating character (the close-brace, the lastcharacter of the variable name, or the close-quote (respectively))if the parse was successful..AP Tcl_Parse *usedParsePtr inPoints to structure that was filled in by a previous call to\fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseVarName\fR, etc..BE.SH DESCRIPTION.PPThese procedures parse Tcl commands or portions of Tcl commands such asexpressions or references to variables.Each procedure takes a pointer to a script (or portion thereof)and fills in the structure pointed to by \fIparsePtr\fRwith a collection of tokens describing the information that was parsed.The procedures normally return \fBTCL_OK\fR.However, if an error occurs then they return \fBTCL_ERROR\fR,leave an error message in \fIinterp's\fR result(if \fIinterp\fR is not NULL),and leave nothing in \fIparsePtr\fR..PP\fBTcl_ParseCommand\fR is a procedure that parses Tclscripts. Given a pointer to a script, itparses the first command from the script. If the command was parsedsuccessfully, \fBTcl_ParseCommand\fR returns \fBTCL_OK\fR and fills in thestructure pointed to by \fIparsePtr\fR with information about thestructure of the command (see below for details).If an error occurred in parsing the command then\fBTCL_ERROR\fR is returned, an error message is left in \fIinterp\fR'sresult, and no information is left at \fI*parsePtr\fR..PP\fBTcl_ParseExpr\fR parses Tcl expressions.Given a pointer to a script containing an expression,\fBTcl_ParseCommand\fR parses the expression.If the expression was parsed successfully,\fBTcl_ParseExpr\fR returns \fBTCL_OK\fR and fills in thestructure pointed to by \fIparsePtr\fR with information about thestructure of the expression (see below for details).If an error occurred in parsing the command then\fBTCL_ERROR\fR is returned, an error message is left in \fIinterp\fR'sresult, and no information is left at \fI*parsePtr\fR..PP\fBTcl_ParseBraces\fR parses a string or command argumentenclosed in braces such as\fB{hello}\fR or \fB{string \\t with \\t tabs}\fRfrom the beginning of its argument \fIstring\fR.The first character of \fIstring\fR must be \fB{\fR. If the braced string was parsed successfully,\fBTcl_ParseBraces\fR returns \fBTCL_OK\fR,fills in the structure pointed to by \fIparsePtr\fRwith information about the structure of the string(see below for details),and stores a pointer to the character just after the terminating \fB}\fRin the location given by \fI*termPtr\fR.If an error occurs while parsing the stringthen \fBTCL_ERROR\fR is returned,an error message is left in \fIinterp\fR's result,and no information is left at \fI*parsePtr\fR or \fI*termPtr\fR..PP\fBTcl_ParseQuotedString\fR parses a double-quoted string such as\fB"sum is [expr $a+$b]"\fRfrom the beginning of the argument \fIstring\fR.The first character of \fIstring\fR must be \fB"\fR. If the double-quoted string was parsed successfully,\fBTcl_ParseQuotedString\fR returns \fBTCL_OK\fR,fills in the structure pointed to by \fIparsePtr\fRwith information about the structure of the string(see below for details),and stores a pointer to the character just after the terminating \fB"\fRin the location given by \fI*termPtr\fR.If an error occurs while parsing the stringthen \fBTCL_ERROR\fR is returned,an error message is left in \fIinterp\fR's result,and no information is left at \fI*parsePtr\fR or \fI*termPtr\fR..PP\fBTcl_ParseVarName\fR parses a Tcl variable reference such as\fB$abc\fR or \fB$x([expr $index + 1])\fR from the beginning of its\fIstring\fR argument.The first character of \fIstring\fR must be \fB$\fR. If a variable name was parsed successfully, \fBTcl_ParseVarName\fRreturns \fBTCL_OK\fR and fills in the structure pointed to by\fIparsePtr\fR with information about the structure of the variable name(see below for details). If an erroroccurs while parsing the command then \fBTCL_ERROR\fR is returned, anerror message is left in \fIinterp\fR's result (if \fIinterp\fR isn'tNULL), and no information is left at \fI*parsePtr\fR..PP\fBTcl_ParseVar\fR parse a Tcl variable reference such as \fB$abc\fRor \fB$x([expr $index + 1])\fR from the beginning of its \fIstring\fRargument. The first character of \fIstring\fR must be \fB$\fR. Ifthe variable name is parsed successfully, \fBTcl_ParseVar\fR returns apointer to the string value of the variable. If an error occurs whileparsing, then NULL is returned and an error message is left in\fIinterp\fR's result..PPThe information left at \fI*parsePtr\fRby \fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseBraces\fR,\fBTcl_ParseQuotedString\fR, and \fBTcl_ParseVarName\fRmay include dynamically allocated memory.If these five parsing procedures return \fBTCL_OK\fRthen the caller must invoke \fBTcl_FreeParse\fR to releasethe storage at \fI*parsePtr\fR.These procedures ignore any existing information in\fI*parsePtr\fR (unless \fIappend\fR is non-zero),so if repeated calls are being made to any of themthen \fBTcl_FreeParse\fR must be invoked once after each call..PP\fBTcl_EvalTokensStandard\fR evaluates a sequence of parse tokens froma Tcl_Parse structure. The tokens typically consistof all the tokens in a word or all the tokens that make up the index fora reference to an array variable. \fBTcl_EvalTokensStandard\fR performs thesubstitutions requested by the tokens and concatenates theresulting values. The return value from \fBTcl_EvalTokensStandard\fR is a Tcl completioncode with one of the values \fBTCL_OK\fR, \fBTCL_ERROR\fR,\fBTCL_RETURN\fR, \fBTCL_BREAK\fR, or \fBTCL_CONTINUE\fR.In addition, a result value or error message is left in \fIinterp\fR'sresult; it can be retrieved using \fBTcl_GetObjResult\fR..PP\fBTcl_EvalTokens\fR differs from \fBTcl_EvalTokensStandard\fR only inthe return convention used: it returns the result in a new Tcl_Obj.The reference count of the object returned as result has beenincremented, so the caller mustinvoke \fBTcl_DecrRefCount\fR when it is finished with the object.If an error or other exception occurs while evaluating the tokens(such as a reference to a non-existent variable) then the return valueis NULL and an error message is left in \fIinterp\fR's result. The useof \fBTcl_EvalTokens\fR is deprecated..SH "TCL_PARSE STRUCTURE".PP\fBTcl_ParseCommand\fR, \fBTcl_ParseExpr\fR, \fBTcl_ParseBraces\fR,\fBTcl_ParseQuotedString\fR, and \fBTcl_ParseVarName\fRreturn parse information in two data structures, Tcl_Parse and Tcl_Token:.CStypedef struct Tcl_Parse { char *\fIcommentStart\fR; int \fIcommentSize\fR; char *\fIcommandStart\fR; int \fIcommandSize\fR; int \fInumWords\fR; Tcl_Token *\fItokenPtr\fR; int \fInumTokens\fR; ...} Tcl_Parse;typedef struct Tcl_Token { int \fItype\fR; char *\fIstart\fR;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -