From af3fa90e8bb41c306c5fe2d2cf105db6bbabd1d9 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 19 Oct 2021 10:53:45 -0700 Subject: Feat: word operators and more robust crashing Added the length and concatenate operators. Slightly improved the robustness on syntax errors. --- sys/cmd/rc/syntax.y | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'sys/cmd/rc/syntax.y') diff --git a/sys/cmd/rc/syntax.y b/sys/cmd/rc/syntax.y index a580cb0..469260f 100644 --- a/sys/cmd/rc/syntax.y +++ b/sys/cmd/rc/syntax.y @@ -1,5 +1,5 @@ -%token Tword Tif Telse Tbang -%token Targs +%token Tword Twords Tif Telse Tbang +%token Targs Tindex %token Tbasic Tparen Tblock %define parse.error verbose @@ -12,18 +12,18 @@ %} /* operator precendence: lowest first */ -%right Telse -%left Twhile +%left Twhile Telse %left '\n' %left Tbang -%right '$' +%right '$' Tcount Tflat +%right Tindex /* semantic types */ %union{ struct Tree *tree; } %type line cmds cmdsln body paren block ifbody assign; -%type cmd basic executable nonkeyword keyword word atom arg args; +%type cmd basic executable nonkeyword keyword word words wordsnl atom arg args; %type Tbang; %type Tword Tif Telse; @@ -74,21 +74,21 @@ cmd: basic: executable -| basic word { $$ = maketree2(Targs, $1, $2); } +| basic word { $$ = maketree2(Targs, $1, $2); } arg: word args: arg -| args arg { $$ = maketree2(Targs, $1, $2); } +| args arg { $$ = maketree2(Targs, $1, $2); } atom: nonkeyword -| keyword { $$ = maketree1(Tword, $1); } +| keyword { $$ = maketree1(Tword, $1); } word: atom -| word '^' atom { $$ = maketree2('^', $1, $3); } +| word '^' atom { $$ = maketree2('^', $1, $3); } executable: nonkeyword @@ -96,11 +96,24 @@ executable: nonkeyword: Tword -| '$' atom { $$ = maketree1('$', $2); } +| '$' atom { $$ = maketree1('$', $2); } +| '(' wordsnl ')' { $$ = $2; } +| Tcount atom { $$ = maketree1(Tcount, $2); } +| Tflat atom { $$ = maketree1(Tflat, $2); } +//| '`' block { $$ = maketree1('`', $2); } keyword: Tif|Telse +words: +/* empty */ { $$ = nil; } +| words word { $$ = maketree2(Twords, $1, $2); } + +wordsnl: +/* empty */ { $$ = nil; } +| wordsnl '\n' /* empty */ +| wordsnl word {$$ = (!$1) ? ((!$2) ? nil : $2) : ((!$2) ? $1 : maketree2(Twords, $1, $2)); } + nl: /*empty*/ | nl '\n' -- cgit v1.2.1