Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: parsing empty alternatives: case foo|) :;;
- X-seq: zsh-workers 41509
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: "zsh-workers@xxxxxxx" <zsh-workers@xxxxxxx>
- Subject: Re: parsing empty alternatives: case foo|) :;;
- Date: Mon, 7 Aug 2017 19:56:20 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ntlworld.com; s=meg.feb2017; t=1502132180; bh=NnjO+tNtuyW1kQGP2HYueUxwpc9dXTF3djFT4n1ItM0=; h=Date:From:To:Subject:In-Reply-To:References; b=VFLitLjyWzLu/YSsbw0KxiRjofz5nCWYnyJ7e73MVrG0oqzEQG7pFSsWcfeNaPK8P rrDT/oxIzpj6cHXJOe1dpn9gRLatp6w+E3vT6/TiNs0BasO7n2f4WoyojIxw0CNRHX oy/Xxkxqp0hQKPnjk0EV8Aura3BMj78yT+0PF8bc3ZFJ+r52MrUZiwnjCxIXwNPRwA 3AefnltmR5EtCyIEMN7WsqH+xADGvAltJkDi0gTsn8qOcceWg1YSU+wgtIR2726Jjg wAkEyZiMrBWbZrCcHB9BD+E5jlQupzhvOL6+4m++cG18etMopFSFbQtmgXWJlu9Isc oSJX7AEeUUoSA==
- In-reply-to: <20170807192103.77258330@ntlworld.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CGME20170807135641epcas3p4bc0a64f832ae7fcd76051ac198722045@epcas3p4.samsung.com> <20170807135559.odtceysgqn5qeqql@tarpaulin.shahaf.local2> <20170807152649.6a5e7d70@pwslap01u.europe.root.pri> <CAH+w=7ZhywOm=W-jZqtGaA=fkHjsoOaBonhBrnEyyEYHzkaR3A@mail.gmail.com> <20170807155856.7882f89f@pwslap01u.europe.root.pri> <20170807192103.77258330@ntlworld.com>
On Mon, 7 Aug 2017 19:21:03 +0100
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> 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.
Looks like this isn't too difficult, either.
pws
diff --git a/Src/lex.c b/Src/lex.c
index b2d9b3f..8493d47 100644
--- a/Src/lex.c
+++ b/Src/lex.c
@@ -760,7 +760,7 @@ gettok(void)
return AMPER;
case LX1_BAR:
d = hgetc();
- if (d == '|')
+ if (d == '|' && !incasepat)
return DBAR;
else if (d == '&')
return BARAMP;
@@ -1058,7 +1058,7 @@ gettokstr(int c, int sub)
if (isset(SHGLOB)) {
if (sub || in_brace_param)
break;
- if (incasepat && !lexbuf.len)
+ if (incasepat > 0 && !lexbuf.len)
return INPAR;
if (!isset(KSHGLOB) && lexbuf.len)
goto brk;
@@ -1859,7 +1859,7 @@ exalias(void)
Reswd rw;
hwend();
- if (interact && isset(SHINSTDIN) && !strin && !incasepat &&
+ if (interact && isset(SHINSTDIN) && !strin && incasepat <= 0 &&
tok == STRING && !nocorrect && !(inbufflags & INP_ALIAS) &&
(isset(CORRECTALL) || (isset(CORRECT) && incmdpos)))
spckword(&tokstr, 1, incmdpos, 1);
diff --git a/Src/parse.c b/Src/parse.c
index ba9cd61..2705252 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -48,7 +48,11 @@ mod_export int incond;
/**/
mod_export int inredir;
-/* != 0 if we are about to read a case pattern */
+/*
+ * 1 if we are about to read a case pattern
+ * -1 if we are not quite sure
+ * 0 otherwise
+ */
/**/
int incasepat;
@@ -1194,6 +1198,7 @@ par_case(int *cmplx)
for (;;) {
char *str;
+ int skip_zshlex;
while (tok == SEPER)
zshlex();
@@ -1201,11 +1206,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);
@@ -1243,10 +1254,11 @@ par_case(int *cmplx)
* this doesn't affect our ability to match a | or ) as
* these are valid on command lines.
*/
- incasepat = 0;
+ incasepat = -1;
incmdpos = 1;
- for (;;) {
+ if (!skip_zshlex)
zshlex();
+ for (;;) {
if (tok == OUTPAR) {
ecstr(str);
ecadd(ecnpats++);
@@ -1302,10 +1314,26 @@ 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;
+ }
}
+ incasepat = 0;
par_save_list(cmplx);
if (tok == SEMIAMP)
type = WC_CASE_AND;
diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst
index 9625a15..0302c96 100644
--- a/Test/A01grammar.ztst
+++ b/Test/A01grammar.ztst
@@ -820,6 +820,43 @@
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
+ case '' in
+ gurgle) print No.
+ ;;
+ wurgle||jurgle) print Yikes!
+ ;;
+ durgle|) print Hmm.
+ ;;
+ |zurgle) print Hah.
+ ;;
+ esac
+ case '' in
+ # Useless doubled empty string to check special case.
+ ||jurgle) print Ok.
+ ;;
+ esac
+0: case with no opening parentheses and empty string
+>Yes!
+>Wow!
+>Yikes!
+>Ok.
+
x=1
x=2 | echo $x
echo $x
Messages sorted by:
Reverse Date,
Date,
Thread,
Author