Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: 3.1.5-pws-3: (subscripts) Re: Problem with associative arrays



On Dec 16,  3:31pm, Sven Wischnowsky wrote:
} Subject: Problem with associative arrays
}
} And this time it's for real:
} 
}   % typeset -A a
}   % a[x*]=foo
}   zsh: bad math expression: unbalanced stack
}   % echo $a[x*]
}   foo
}   %
} 
} The problem is that isident() uses mathevalarg() to skip (!) over the
} contents of a subscript (unless the subscript contains flags).

isident() seems to be rather inconsistent about strict checking of the
text inside the [ ].  Below is the most obvious fix (#ifdef'd for now).

This has the side-effect of making associative array syntax more like
ksh, in that you can put quoted strings inside the subscript:

    zsh% typeset -A foo
    zsh% foo['a b c']=cba
    zsh% print -l ${(kv)foo}
    a b c
    cba

However, this fails (because of the "balanced brackets" test):

    zsh% foo['a ] c']=cba
    zsh: not an identifier: foo[a ] c]

So a better test might be just strchr(ss, ']') and let other code report
subscripting errors after it knows what kind of array it's accessing.  I
also don't know how widely available strcspn() is; this might break the
build on some older platforms.

Also, this is a bit strange:

    zsh% echo $foo['a b c']

    zsh% echo ${foo[a b c]}	<-- note, must omit the quotes on 'a b c'
    cba

Probably the subscript needs to be untokenized somewhere.

Index: Src/params.c
===================================================================
--- params.c	1998/12/15 06:08:15	1.16
+++ params.c	1998/12/16 17:19:05
@@ -633,6 +633,7 @@
 	if (!iident(*ss))
 	    break;
 
+#if 0
     /* If this exhaust `s' or the next two characters *
      * are [(, then it is a valid identifier.         */
     if (!*ss || (*ss == '[' && ss[1] == '('))
@@ -642,6 +643,7 @@
      * definitely not a valid identifier.              */
     if (*ss != '[')
 	return 0;
+
     noeval = 1;
     (void)mathevalarg(++ss, &ss);
     if (*ss == ',')
@@ -650,6 +652,19 @@
     if (*ss != ']' || ss[1])
 	return 0;
     return 1;
+#else
+    /* If the next character is not [, then it is *
+     * definitely not a valid identifier.              */
+    if (!*ss)
+	return 1;
+    if (*ss != '[')
+	return 0;
+
+    /* Require balanced [ ] pairs */
+    for (s = ss; *(s += strcspn(++ss, "[]")) && s > ss; ss = s)
+	;
+    return (*ss == ']' && !s[1]);
+#endif
 }
 
 static char **garr;


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



Messages sorted by: Reverse Date, Date, Thread, Author