Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ccbfd904981a68a00f9a8784350498e1849105de
Ticket: 219345ffffffffffffffffffffffffffffffffff
Small parser change would really improve TclJava interface.
User & Date: nobody on 2000-10-26 05:11:03
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 5423 RFE
    Version: 8.4a1
    SubmitDate: '2000-05-10'
    LastModified: '2000-10-25'
    Severity: MED
    Status: UnAssn
    Submitter: techsupp
    OS: Linux
    FixedDate: '2000-10-25'
    ClosedDate: '2000-10-25'
    
    
    Name:
    
    mo
    
    
    CVS:
    
    Tue May  9
    
    
    CustomShell:
    
    The parser now treats [] as a literal
    
    
    Comments:
    
    Why is this needed? Well people often write in asking
    
        why the following TclJava code is broken.
    
    
        set strarr [java::new String[] {2}]
    
    
        The problem is that the [] on the end of String gets
    
        silently replaced with "". One is expected to use {String[]}
    
        instead, but that is really confusing for new users.
    
    
    DesiredBehavior:
    
    It would really be great if the Tcl parser could treat the string [] as
    
    a literal string instead of doing a command evaluation of the empty
    
    string. It seems extremely unlikely that anyone has actually written
    
    code like "set h []" and expected it to return the empty string. At
    
    any rate, here is the patch to do it.
    
    
    Patch:
    
    Index: ChangeLog
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/ChangeLog,v
    
    retrieving revision 1.235
    
    diff -u -r1.235 ChangeLog
    
    --- ChangeLog2000/05/09 19:13:551.235
    
    +++ ChangeLog2000/05/10 05:47:44
    
    @@ -1,3 +1,14 @@
    
    +2000-05-09  Mo DeJong  <[email protected]>
    
    +
    
    +* doc/Tcl.n: Added note about [] case.
    
    +* generic/tclParse.c (ParseTokens): Changed the
    
    +Parser so that it no longer invokes Tcl_ParseCommand
    
    +when the literal sequence [] is encountered. The
    
    +sequence [] is not treated as a literal [].
    
    +* tests/expr.test: Added expr test case for empty
    
    +brackets string.
    
    +* tests/parse.test: Added parser test for [].
    
    +
    
     2000-05-09  Andreas Kupries  <[email protected]>
    
     operating as proxy for David Gravereaux <[email protected]>
    
    
    Index: doc/Tcl.n
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/doc/Tcl.n,v
    
    retrieving revision 1.3
    
    diff -u -r1.3 Tcl.n
    
    --- Tcl.n1999/04/16 00:46:331.3
    
    +++ Tcl.n2000/05/10 05:47:44
    
    @@ -73,6 +73,9 @@
    
     substituted into the word in place of the brackets and all of the
    
     characters between them.
    
     There may be any number of command substitutions in a single word.
    
    +The only special case if an open bracket is immediately followed by
    
    +a close bracket as in the string []. In this case no command
    
    +substitution is performed and the brackets are treated as normal text.
    
     Command substitution is not performed on words enclosed in braces.
    
     .IP [7]
    
     If a word contains a dollar-sign (``$'') then Tcl performs \fIvariable
    
    Index: generic/tclParse.c
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/generic/tclParse.c,v
    
    retrieving revision 1.13
    
    diff -u -r1.13 tclParse.c
    
    --- tclParse.c1999/11/10 02:51:571.13
    
    +++ tclParse.c2000/05/10 05:47:46
    
    @@ -591,42 +591,55 @@
    
         }
    
         src += parsePtr->tokenPtr[varToken].size;
    
     } else if (*src == '[') {
    
    +    src++;
    
    +
    
         /*
    
    -     * Command substitution.  Call Tcl_ParseCommand recursively
    
    -     * (and repeatedly) to parse the nested command(s), then
    
    -     * throw away the parse information.
    
    +     * In the case of [] there is really no point in doing an eval
    
    +     * because it would just return the empty string. Just leave the
    
    +     * literal [] text as is.
    
          */
    
    +    if (*src == ']') {    
    
    +        tokenPtr->type = TCL_TOKEN_TEXT;
    
    +        tokenPtr->size = 2;
    
    +        parsePtr->numTokens++;
    
    +        src++;
    
    +    } else {
    
    +        /*
    
    +         * Command substitution.  Call Tcl_ParseCommand recursively
    
    +         * (and repeatedly) to parse the nested command(s), then
    
    +         * throw away the parse information.
    
    +         */
    
    
    -    src++;
    
    -    while (1) {
    
    -if (Tcl_ParseCommand(parsePtr->interp, src,
    
    -parsePtr->end - src, 1, &nested) != TCL_OK) {
    
    -    parsePtr->errorType = nested.errorType;
    
    -    parsePtr->term = nested.term;
    
    -    parsePtr->incomplete = nested.incomplete;
    
    -    return TCL_ERROR;
    
    -}
    
    -src = nested.commandStart + nested.commandSize;
    
    -if (nested.tokenPtr != nested.staticTokens) {
    
    -    ckfree((char *) nested.tokenPtr);
    
    -}
    
    -if ((*nested.term == ']') && !nested.incomplete) {
    
    -    break;
    
    -}
    
    -if (src == parsePtr->end) {
    
    -    if (parsePtr->interp != NULL) {
    
    -Tcl_SetResult(parsePtr->interp,
    
    -    "missing close-bracket", TCL_STATIC);
    
    +        while (1) {
    
    +    if (Tcl_ParseCommand(parsePtr->interp, src,
    
    +    parsePtr->end - src, 1, &nested) != TCL_OK) {
    
    +        parsePtr->errorType = nested.errorType;
    
    +        parsePtr->term = nested.term;
    
    +        parsePtr->incomplete = nested.incomplete;
    
    +        return TCL_ERROR;
    
    +    }
    
    +    src = nested.commandStart + nested.commandSize;
    
    +    if (nested.tokenPtr != nested.staticTokens) {
    
    +        ckfree((char *) nested.tokenPtr);
    
    +    }
    
    +    if ((*nested.term == ']') && !nested.incomplete) {
    
    +        break;
    
    +    }
    
    +    if (src == parsePtr->end) {
    
    +        if (parsePtr->interp != NULL) {
    
    +    Tcl_SetResult(parsePtr->interp,
    
    +        "missing close-bracket", TCL_STATIC);
    
    +        }
    
    +        parsePtr->errorType = TCL_PARSE_MISSING_BRACKET;
    
    +        parsePtr->term = tokenPtr->start;
    
    +        parsePtr->incomplete = 1;
    
    +        return TCL_ERROR;
    
         }
    
    -    parsePtr->errorType = TCL_PARSE_MISSING_BRACKET;
    
    -    parsePtr->term = tokenPtr->start;
    
    -    parsePtr->incomplete = 1;
    
    -    return TCL_ERROR;
    
    -}
    
    +        }
    
    +        tokenPtr->type = TCL_TOKEN_COMMAND;
    
    +        tokenPtr->size = src - tokenPtr->start;
    
    +        parsePtr->numTokens++;
    
         }
    
    -    tokenPtr->type = TCL_TOKEN_COMMAND;
    
    -    tokenPtr->size = src - tokenPtr->start;
    
    -    parsePtr->numTokens++;
    
     } else if (*src == '\\') {
    
         /*
    
          * Backslash substitution.
    
    Index: tests/expr.test
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/tests/expr.test,v
    
    retrieving revision 1.10
    
    diff -u -r1.10 expr.test
    
    --- expr.test2000/05/09 00:00:371.10
    
    +++ expr.test2000/05/10 05:47:48
    
    @@ -656,6 +656,9 @@
    
     test expr-16.2 {GetToken: check for string literal in braces} {
    
         expr {{1}}
    
     } {1}
    
    +test expr-16.3 {compare escaped brackets and empty command} {
    
    +    expr {"\[\]" == "[]"}
    
    +} 1
    
    
     # Check "expr" and computed command names.
    
    
    Index: tests/parse.test
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/tests/parse.test,v
    
    retrieving revision 1.7
    
    diff -u -r1.7 parse.test
    
    --- parse.test2000/04/10 17:19:021.7
    
    +++ parse.test2000/05/10 05:47:49
    
    @@ -209,6 +209,9 @@
    
     test parse-6.17 {ParseTokens procedure, null characters} {
    
         testparser [bytestring "foo\0zz"] 0
    
     } "- [bytestring foo\0zz] 1 word [bytestring foo\0zz] 3 text foo 0 text [bytestring \0] 0 text zz 0 {}"
    
    +test parse-6.18 {ParseTokens procedure, null characters} {
    
    +    testparser [] 0
    
    +} {- {[]} 1 simple {[]} 1 text {[]} 0 {}}
    
    
     test parse-7.1 {Tcl_FreeParse and ExpandTokenArray procedures} {
    
         testparser {$a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) $a(b) } 0
    
    
    PatchFiles:
    
    doc/Tcl.n
    
        generic/tclParse.c
    
        tests/expr.test
    
        tests/parse.test
    
    
    
    
    
    
  4. foundin changed to: "obsolete: 8.4a1"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "69. Other"
  10. title changed to:
    Small parser change would really improve TclJava interface.
    
  11. type changed to: "RFE"