rc: clean up parser levels, disallow free carats on lists

This commit is contained in:
Russ Cox 2020-05-05 08:29:45 -04:00
parent ff74f7cdda
commit 601e07b636
4 changed files with 37 additions and 31 deletions

View file

@ -1,16 +1,23 @@
#!/bin/bash #!/bin/bash
aflag=false
if [ "$1" = -a ]; then
aflag=true
shift
fi
files="$@" files="$@"
if [ $# = 0 ]; then if [ $# = 0 ]; then
files=$(echo ./test.rc; files=$(echo ./test.rc;
grep -l '^#!/usr/local/plan9/bin/rc' /usr/local/plan9/bin/{*,*/*} 2>/dev/null; grep -l '^#!/usr/local/plan9/bin/rc' /usr/local/plan9/bin/{*,*/*} 2>/dev/null;
grep -l '^#!/bin/rc' $HOME/pub/plan9/rc/bin/{*,*/*} 2>/dev/null) grep -R -l '^#!/bin/rc' $HOME/pub/plan9 | egrep -v 'plan9/(lib/(oui|pci)|sys/man|sys/lib/man|sys/lib/scsicodes)' 2>/dev/null)
fi fi
for i in $files for i in $files
do do
if ! diff <(./o.rc -DY $i 2>&1) <(./o.rc -D $i 2>&1); then if ! diff <(./o.rc -DY $i 2>&1) <(./o.rc -D $i 2>&1); then
echo '#' $i echo '^^^' $i
exit 1 ! $aflag && exit 1
fi fi
done done

View file

@ -206,7 +206,7 @@ yylex(void)
lastword = 0; lastword = 0;
if(d=='('){ if(d=='('){
advance(); advance();
strcpy(tok, "( [SUB]"); strcpy(tok, "(");
return SUB; return SUB;
} }
if(wordchr(d) || d=='\'' || d=='`' || d=='$' || d=='"'){ if(wordchr(d) || d=='\'' || d=='`' || d=='$' || d=='"'){

View file

@ -7,7 +7,6 @@ static tree* brace(int tok);
static tree* cmd(int tok, int *ptok); static tree* cmd(int tok, int *ptok);
static tree* cmd2(int tok, int *ptok); static tree* cmd2(int tok, int *ptok);
static tree* cmd3(int tok, int *ptok); static tree* cmd3(int tok, int *ptok);
static tree* cmd4(int tok, int *ptok);
static tree* cmds(int tok, int *ptok, int nlok); static tree* cmds(int tok, int *ptok, int nlok);
static tree* epilog(int tok, int *ptok); static tree* epilog(int tok, int *ptok);
static int iswordtok(int tok); static int iswordtok(int tok);
@ -39,9 +38,8 @@ dropsp(int tok)
static void static void
syntax(int tok) syntax(int tok)
{ {
char buf[100]; USED(tok);
snprint(buf, sizeof buf, "syntax error %d", tok); yyerror("syntax error");
yyerror(buf);
longjmp(yyjmp, 1); longjmp(yyjmp, 1);
} }
@ -196,17 +194,6 @@ yyredir(int tok, int *ptok)
static tree* static tree*
cmd(int tok, int *ptok) cmd(int tok, int *ptok)
{
tok = dropsp(tok);
switch(tok) {
default:
return cmd2(tok, ptok);
}
}
static tree*
cmd2(int tok, int *ptok)
{ {
int op; int op;
tree *t1, *t2; tree *t1, *t2;
@ -214,10 +201,11 @@ cmd2(int tok, int *ptok)
// | cmd ANDAND cmd {$$=tree2(ANDAND, $1, $3);} // | cmd ANDAND cmd {$$=tree2(ANDAND, $1, $3);}
// | cmd OROR cmd {$$=tree2(OROR, $1, $3);} // | cmd OROR cmd {$$=tree2(OROR, $1, $3);}
t1 = cmd3(tok, &tok); tok = dropsp(tok);
t1 = cmd2(tok, &tok);
while(tok == ANDAND || tok == OROR) { while(tok == ANDAND || tok == OROR) {
op = tok; op = tok;
t2 = cmd3(dropnl(yylex()), &tok); t2 = cmd2(dropnl(yylex()), &tok);
t1 = tree2(op, t1, t2); t1 = tree2(op, t1, t2);
} }
*ptok = tok; *ptok = tok;
@ -225,15 +213,15 @@ cmd2(int tok, int *ptok)
} }
static tree* static tree*
cmd3(int tok, int *ptok) cmd2(int tok, int *ptok)
{ {
tree *t1, *t2, *t3; tree *t1, *t2, *t3;
// | cmd PIPE cmd {$$=mung2($2, $1, $3);} // | cmd PIPE cmd {$$=mung2($2, $1, $3);}
t1 = cmd4(tok, &tok); t1 = cmd3(tok, &tok);
while(tok == PIPE) { while(tok == PIPE) {
t2 = yylval.tree; t2 = yylval.tree;
t3 = cmd4(dropnl(yylex()), &tok); t3 = cmd3(dropnl(yylex()), &tok);
t1 = mung2(t2, t1, t3); t1 = mung2(t2, t1, t3);
} }
*ptok = tok; *ptok = tok;
@ -241,7 +229,7 @@ cmd3(int tok, int *ptok)
} }
static tree* static tree*
cmd4(int tok, int *ptok) cmd3(int tok, int *ptok)
{ {
tree *t1, *t2, *t3, *t4; tree *t1, *t2, *t3, *t4;
@ -336,16 +324,16 @@ cmd4(int tok, int *ptok)
case SUBSHELL: case SUBSHELL:
// | BANG cmd {$$=mung1($1, $2);} // | BANG cmd {$$=mung1($1, $2);}
// | SUBSHELL cmd {$$=mung1($1, $2);} // | SUBSHELL cmd {$$=mung1($1, $2);}
// Note: cmd3: ! x | y is !{x | y} not {!x} | y. // Note: cmd2: ! x | y is !{x | y} not {!x} | y.
t1 = yylval.tree; t1 = yylval.tree;
return mung1(t1, cmd3(yylex(), ptok)); return mung1(t1, cmd2(yylex(), ptok));
case REDIR: case REDIR:
case DUP: case DUP:
// | redir cmd %prec BANG {$$=mung2($1, $1->child[0], $2);} // | redir cmd %prec BANG {$$=mung2($1, $1->child[0], $2);}
// Note: cmd3: {>x echo a | tr a-z A-Z} writes A to x. // Note: cmd2: {>x echo a | tr a-z A-Z} writes A to x.
t1 = yyredir(tok, &tok); t1 = yyredir(tok, &tok);
t2 = cmd3(tok, ptok); t2 = cmd2(tok, ptok);
return mung2(t1, t1->child[0], t2); return mung2(t1, t1->child[0], t2);
case '{': case '{':
@ -372,9 +360,9 @@ cmd4(int tok, int *ptok)
t1 = yyword(tok, &tok, 0); t1 = yyword(tok, &tok, 0);
if(tok == '=') { if(tok == '=') {
// assignment // assignment
// Note: cmd3: {x=1 true | echo $x} echoes 1. // Note: cmd2: {x=1 true | echo $x} echoes 1.
t1 = tree2('=', t1, yyword(yylex(), &tok, 1)); t1 = tree2('=', t1, yyword(yylex(), &tok, 1));
t2 = cmd3(tok, ptok); t2 = cmd2(tok, ptok);
return mung3(t1, t1->child[0], t1->child[1], t2); return mung3(t1, t1->child[0], t1->child[1], t2);
} }
@ -440,6 +428,9 @@ yyword(int tok, int *ptok, int eqok)
goto out; goto out;
for(;;) { for(;;) {
if(iswordtok(tok)) { if(iswordtok(tok)) {
// No free carats around parens.
if(t->type == PAREN || tok == '(')
syntax(tok);
t = tree2('^', t, word1(tok, &tok)); t = tree2('^', t, word1(tok, &tok));
continue; continue;
} }

View file

@ -83,3 +83,11 @@ x = y
# x y=z # x y=z
# x =y # x =y
# x -flag=y # x -flag=y
>z x | y
# rejected now, was like parens were spaces before.
# echo Formatting Venti arenas and indices (this takes a while).
# echo $STATLINE(1)^$STATLINE(3)' '$STATLINE(2)' '$STATLINE(4)' '$LSLINE(6)