Menu

#2914 Possible parsing problem. "list" command inccorectly assigns

final: 8.3.5
closed-invalid
5
2004-10-19
2004-10-14
Anonymous
No

Synopsis of the bug:

Possible issue with the way tcl parses list commands.

1. If the list being passed to one of the list commands
is followed by any nonspace "terminator" character
(such as "[", ",", ".", "{", "}", ")", "\" and etc. ) and then
by any number of nonspace characters then space , all
those characters are being appended to the very last
element of the list while executing the command.

Example:
----------
input: join [list 1 2 3]\[ |
output: 1|2|3[

input: join [list 1 2 3]] |
output: 1|2|3]

2. If the list being passed to one of the list commands
is followed by any nonspace "terminator" character
(such as "[", ",", ".", "{", "}", ")", "\" and etc. ) and then
by any number of nonspace characters, then by "\",
then by a space character, and then again any number
of nonspace characters, all those characters before "\ "
are being appended to the very last element of the
passed list and all characters that goes after "\ " are
treated as an additional last element of the list while
executing the command.

Example:
----------
input: join $l\extra1\ extra2 |
output: 1|2|3extra1|extra2

Setup information:
OS: Windows 2000 Professional,
TCL: Tcl version 8.3

Steps to reproduce the bug:
1. Bring up tclsh command prompt

2. Initialize the list as below...
input: set l [list 1 2 3]
output: 1 2 3

3. Try one of the operation below (join is used only as
an example any other list operation will manifest the
same issue)

input: join $l] |
output: 1|2|3]

input: join $l{ |
output: 1|2|3{

input: join $l} |
output: 1|2|3}

input: join $l) |
output: 1|2|3)

input: join $l. |
output: 1|2|3.

input: join $l, |
output: 1|2|3,

input: join $l\asdf |
output: 1|2|3sdf

input: join $l\( |
output: 1|2|3(

input: join $l\[ |
output: 1|2|3[

input: join $l\] |
output: 1|2|3]

input: join $l\\ |
output: 1|2|3\

input: join [list 1 2 3]\[ |
output: 1|2|3[

input: join $l\extra1\ extra2 |
output: 1|2|3extra1|extra2

Expected result: Parsing error.

Discussion

  • Nobody/Anonymous

    Logged In: NO

    I am not sure about cases containing chars like [ and { but
    at least this:

    input: join $l, |
    output: 1|2|3,

    is IMHO perfectly OK - list $l is converted to string, then
    comma is
    appended to it while forming the first arg to 'join', then
    it is converted
    back to list because 'join' wants a list.

    In fact it has nothing to do with lists at all, the only
    question might be
    whether all those combinations of special characters in
    arguments
    are written in the best possible quoting style and whether
    parser could enforce a better style, for example:

    input: join $l} |
    output: 1|2|3}

    is rather unfortunate when used as part of bigger script, I
    would
    rather backslash-escape the brace:

    input: join $l\} |
    output: 1|2|3}

    Vaclav Hanzl

     
  • Donal K. Fellows

    Logged In: YES
    user_id=79902

    These are all not bugs; just funny (and inefficient) ways to
    construct lists.

    Tcl, by definition, concatenates lists and characters by
    converting the list to a string and concatenating strings.
    Then the join command (or any other list-consuming command)
    reinterprets the resulting string as a list again. Of the
    characters you list ("[", ",", ".", "{", "}", ")", "\"), all
    except the {curly braces} and backslash have no meaning to
    the list parsing engine at all. When a curly brace starts a
    word in a list, that word extends up to the matching closing
    curly (which must not have any trailing chars) but that is
    the only time they are significant. Backslashes are special
    in front of some characters, but you have to distinguish
    between quoting them to the command you're typing in and the
    actual list engine.

    To summarize, what you've written is all correct and
    intended (if perhaps surprising the first time you encounter
    it.)

     
  • Donal K. Fellows

    • labels: 105658 --> 14. List Object
    • milestone: 141255 --> final: 8.3.5
    • status: open --> closed-invalid