zsh-workers
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@brasslantern.com>
To: zsh-workers@math.gatech.edu
Subject: PATCH: 3.1.5-pws-3: (subscripts) Re: Problem with associative arrays
Date: Wed, 16 Dec 1998 09:53:09 -0800	[thread overview]
Message-ID: <981216095309.ZM14130@candle.brasslantern.com> (raw)
In-Reply-To: <199812161431.PAA05365@beta.informatik.hu-berlin.de>

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


  reply	other threads:[~1998-12-16 18:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-16 14:31 Sven Wischnowsky
1998-12-16 17:53 ` Bart Schaefer [this message]
1998-12-17 11:53 PATCH: 3.1.5-pws-3: (subscripts) " Sven Wischnowsky
1998-12-17 12:46 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=981216095309.ZM14130@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@math.gatech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).