zsh-workers
 help / color / mirror / code / Atom feed
* Problem with associative arrays
@ 1998-12-16 14:31 Sven Wischnowsky
  1998-12-16 17:53 ` PATCH: 3.1.5-pws-3: (subscripts) " Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Sven Wischnowsky @ 1998-12-16 14:31 UTC (permalink / raw)
  To: zsh-workers


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).

I don't have the time to fix this now since our LAN will soon be
upgraded but if noone else gives it a try I'll have a lokk at it.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 4+ messages in thread

* PATCH: 3.1.5-pws-3: (subscripts) Re: Problem with associative arrays
  1998-12-16 14:31 Problem with associative arrays Sven Wischnowsky
@ 1998-12-16 17:53 ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 1998-12-16 17:53 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: PATCH: 3.1.5-pws-3: (subscripts) Re: Problem with associative arrays
  1998-12-17 11:53 Sven Wischnowsky
@ 1998-12-17 12:46 ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 1998-12-17 12:46 UTC (permalink / raw)
  To: zsh-workers

On Dec 17, 12:53pm, Sven Wischnowsky wrote:
} Subject: Re:  PATCH: 3.1.5-pws-3: (subscripts) Re: Problem with associativ
}
} ...minus one `s', it seems:
} 
} *** os/params.c	Thu Dec 17 12:20:23 1998
} --- Src/params.c	Thu Dec 17 12:49:07 1998
} ***************
} *** 663,669 ****
} !     return (*ss == ']' && !s[1]);
} --- 663,669 ----
} !     return (*ss == ']' && !ss[1]);

How odd.  I have `ss' in my copy ... I must have accidentally whacked it
somehow when scanning through the patch after I piped diff into my mail
buffer.  Sorry about that.

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re:  PATCH: 3.1.5-pws-3: (subscripts) Re: Problem with associative arrays
@ 1998-12-17 11:53 Sven Wischnowsky
  1998-12-17 12:46 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Sven Wischnowsky @ 1998-12-17 11:53 UTC (permalink / raw)
  To: zsh-workers


Bart Schaefer wrote:

> 
> 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).

...minus one `s', it seems:

  % typeset -A a
  % a[--foo--]=bar
  zsh: not an identifier: a[--foo--]

Bye
 Sven

*** os/params.c	Thu Dec 17 12:20:23 1998
--- Src/params.c	Thu Dec 17 12:49:07 1998
***************
*** 663,669 ****
      /* Require balanced [ ] pairs */
      for (s = ss; *(s += strcspn(++ss, "[]")) && s > ss; ss = s)
  	;
!     return (*ss == ']' && !s[1]);
  #endif
  }
  
--- 663,669 ----
      /* Require balanced [ ] pairs */
      for (s = ss; *(s += strcspn(++ss, "[]")) && s > ss; ss = s)
  	;
!     return (*ss == ']' && !ss[1]);
  #endif
  }
  

--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1998-12-17 12:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-16 14:31 Problem with associative arrays Sven Wischnowsky
1998-12-16 17:53 ` PATCH: 3.1.5-pws-3: (subscripts) " Bart Schaefer
1998-12-17 11:53 Sven Wischnowsky
1998-12-17 12:46 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).