zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: parsing empty alternatives: case foo|) :;;
Date: Mon, 7 Aug 2017 19:21:03 +0100	[thread overview]
Message-ID: <20170807192103.77258330@ntlworld.com> (raw)
In-Reply-To: <20170807155856.7882f89f@pwslap01u.europe.root.pri>

On Mon, 07 Aug 2017 15:58:56 +0100
Peter Stephenson <p.stephenson@samsung.com> wrote:
> I think this might be some fix-up code that got added for the old case
> (nothing to do with pipes in that case).

It's complicated because of the two cases, but I'm not sure this ever
worked.

It's straightforward to fix, but note I can't fix "foo||bar)" without
more work because || is a different token.  I think we can probably
avoid parsing it that way here if we want to go down that route.

I deliberately haven't tried to squeeze this into 5.4.

pws

diff --git a/Src/parse.c b/Src/parse.c
index ba9cd61..d99826d 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -1194,6 +1194,7 @@ par_case(int *cmplx)
 
     for (;;) {
 	char *str;
+	int skip_zshlex;
 
 	while (tok == SEPER)
 	    zshlex();
@@ -1201,11 +1202,17 @@ par_case(int *cmplx)
 	    break;
 	if (tok == INPAR)
 	    zshlex();
-	if (tok != STRING)
-	    YYERRORV(oecused);
-	if (!strcmp(tokstr, "esac"))
-	    break;
-	str = dupstring(tokstr);
+	if (tok == BAR) {
+	    str = dupstring("");
+	    skip_zshlex = 1;
+	} else {
+	    if (tok != STRING)
+		YYERRORV(oecused);
+	    if (!strcmp(tokstr, "esac"))
+		break;
+	    str = dupstring(tokstr);
+	    skip_zshlex = 0;
+	}
 	type = WC_CASE_OR;
 	pp = ecadd(0);
 	palts = ecadd(0);
@@ -1245,8 +1252,9 @@ par_case(int *cmplx)
 	 */
 	incasepat = 0;
 	incmdpos = 1;
-	for (;;) {
+	if (!skip_zshlex)
 	    zshlex();
+	for (;;) {
 	    if (tok == OUTPAR) {
 		ecstr(str);
 		ecadd(ecnpats++);
@@ -1302,9 +1310,24 @@ par_case(int *cmplx)
 	    }
 
 	    zshlex();
-	    if (tok != STRING)
+	    switch (tok) {
+	    case STRING:
+		/* Normal case */
+		str = dupstring(tokstr);
+		zshlex();
+		break;
+
+	    case OUTPAR:
+	    case BAR:
+		/* Empty string */
+		str = dupstring("");
+		break;
+
+	    default:
+		/* Oops. */
 		YYERRORV(oecused);
-	    str = dupstring(tokstr);
+		break;
+	    }
 	}
 	par_save_list(cmplx);
 	if (tok == SEMIAMP)
diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst
index 9625a15..db62875 100644
--- a/Test/A01grammar.ztst
+++ b/Test/A01grammar.ztst
@@ -820,6 +820,26 @@
 0:case keeps exit status of last command executed in compound-list
 >37
 
+  case '' in
+    burble) print No.
+    ;;
+    spurble|) print Yes!
+    ;;
+    |burble) print Not quite.
+    ;;
+  esac
+  case '' in
+    burble) print No.
+    ;;
+    |burble) print Wow!
+    ;;
+    spurble|) print Sorry.
+    ;;
+  esac
+0: case with no opening parentheses and empty string 
+>Yes!
+>Wow!
+
   x=1
   x=2 | echo $x
   echo $x


  parent reply	other threads:[~2017-08-07 18:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20170807135641epcas3p4bc0a64f832ae7fcd76051ac198722045@epcas3p4.samsung.com>
2017-08-07 13:55 ` Daniel Shahaf
2017-08-07 14:26   ` Peter Stephenson
2017-08-07 14:44     ` Bart Schaefer
2017-08-07 14:58       ` Peter Stephenson
2017-08-07 18:03         ` 5.4 almost released Peter Stephenson
2017-08-07 18:25           ` Eric Cook
2017-08-07 18:34             ` Peter Stephenson
2017-08-08 10:26               ` Martijn Dekker
2017-08-08 11:00                 ` Peter Stephenson
2017-08-07 18:21         ` Peter Stephenson [this message]
2017-08-07 18:56           ` parsing empty alternatives: case foo|) :;; Peter Stephenson

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=20170807192103.77258330@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@zsh.org \
    /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).