commit 1128baa58acaff32e4538d5878b916fba091c574
parent b3b9943da25aa0f86e2542c6846142aeeaa0cfe5
Author: Toby Goodwin <toby@paccrat.org>
Date: Mon, 5 Mar 2018 07:13:14 +0000
Merge branch 'master' into ifnot
Diffstat:
2 files changed, 18 insertions(+), 7 deletions(-)
diff --git a/ChangeLog b/ChangeLog
@@ -903,3 +903,11 @@ Changes since rc-1.5b2
2015-05-13
Release: rc-1.7.4.
+
+[... needs completing]
+
+2018-03-05
+
+ Bug: the grammar disallowed local assignments and redirections in the true
+ branch of `if` when there was an `else` clause (thanks Dražen Borković and
+ Bert Münnich; github #31).
diff --git a/parse.y b/parse.y
@@ -17,7 +17,8 @@ Node *parsetree; /* not using yylval because bison declares it as an auto */
%token OROR PIPE REDIR SREDIR SUB SUBSHELL SWITCH TWIDDLE WHILE WORD HUH
%left '^' '='
-%left WHILE ')' ELSE
+%right ELSE TWIDDLE
+%left WHILE ')'
%left ANDAND OROR '\n'
%left BANG SUBSHELL
%left PIPE
@@ -43,7 +44,7 @@ Node *parsetree; /* not using yylval because bison declares it as an auto */
%type <keyword> keyword
%type <node> assign body brace case cbody cmd cmdsa cmdsan comword epilog
first line nlwords paren redir sword simple iftail word words
- arg args
+ arg args else
%start rc
@@ -96,8 +97,10 @@ cbody : cmd { $$ = mk(nCbody, $1, NULL); }
| case cbody { $$ = mk(nCbody, $1, $2); }
| cmdsan cbody { $$ = mk(nCbody, $1, $2); }
-iftail : cmd %prec ELSE
- | brace ELSE optnl cmd { $$ = mk(nElse,$1,$4); }
+iftail : cmd else { $$ = $2 != NULL ? mk(nElse, $1, $2) : $1; }
+
+else : /* empty */ %prec ELSE { $$ = NULL; }
+ | ELSE optnl cmd { $$ = $3; }
cmd : /* empty */ %prec WHILE { $$ = NULL; }
| simple
@@ -117,13 +120,13 @@ cmd : /* empty */ %prec WHILE { $$ = NULL; }
| BANG optcaret cmd { $$ = mk(nBang,$3); }
| SUBSHELL optcaret cmd { $$ = mk(nSubshell,$3); }
| FN words brace { $$ = mk(nNewfn,$2,$3); }
- | FN words { $$ = mk(nRmfn,$2); }
+ | FN words %prec ELSE { $$ = mk(nRmfn,$2); }
optcaret : /* empty */ %prec '^'
| '^'
-simple : first
- | first args { $$ = ($2 != NULL ? mk(nArgs,$1,$2) : $1); }
+simple : first %prec ELSE
+ | first args %prec ELSE { $$ = ($2 != NULL ? mk(nArgs,$1,$2) : $1); }
args : arg
| args arg { $$ = ($2 != NULL ? mk(nArgs,$1,$2) : $1); }