From b0b048c03930b826ab3dbf88b56fd664fca26886 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 22 May 2020 15:32:31 +0300 Subject: Add script command redirect aliases --- .../script/lexer+command-line.test.testscript | 36 +++-- libbuild2/build/script/lexer.cxx | 10 +- libbuild2/build/script/lexer.hxx | 6 +- .../script/parser+here-document.test.testscript | 102 +++++++++++--- .../script/parser+here-string.test.testscript | 24 +++- .../build/script/parser+pipe-expr.test.testscript | 4 +- .../build/script/parser+redirect.test.testscript | 152 ++++++++++++++++----- .../build/script/parser+regex.test.testscript | 87 ++++++------ libbuild2/build/script/parser.cxx | 6 +- 9 files changed, 308 insertions(+), 119 deletions(-) (limited to 'libbuild2/build') diff --git a/libbuild2/build/script/lexer+command-line.test.testscript b/libbuild2/build/script/lexer+command-line.test.testscript index 1777583..3eceae8 100644 --- a/libbuild2/build/script/lexer+command-line.test.testscript +++ b/libbuild2/build/script/lexer+command-line.test.testscript @@ -47,24 +47,36 @@ test.arguments = command-line : str : - $* <"cmd b" >>EOO + $* <"cmd <<<=a 1>>>?b" >>EOO 'cmd' - < + <<<= 'a' '1' - > + >>>? 'b' EOO : str-nn : - $* <"cmd <:a 1>:b" >>EOO + $* <"cmd <<<=:a 1>>>?:b" >>EOO 'cmd' - <: + <<<=: 'a' '1' - >: + >>>?: + 'b' + + EOO + + : str-nn-alias + : + $* <"cmd <<<:a 1>>>?:b" >>EOO + 'cmd' + <<<: + 'a' + '1' + >>>?: 'b' EOO @@ -83,26 +95,26 @@ test.arguments = command-line : doc-nn : - $* <"cmd <<:EOI 1>>:EOO" >>EOO + $* <"cmd <<:EOI 1>>?:EOO" >>EOO 'cmd' <<: 'EOI' '1' - >>: + >>?: 'EOO' EOO : file-cmp : - $* <"cmd <<>>out 2>>>err" >>EOO + $* <"cmd <=in >?out 2>?err" >>EOO 'cmd' - <<< + <= 'in' - >>> + >? 'out' '2' - >>> + >? 'err' EOO diff --git a/libbuild2/build/script/lexer.cxx b/libbuild2/build/script/lexer.cxx index de93b6b..716d898 100644 --- a/libbuild2/build/script/lexer.cxx +++ b/libbuild2/build/script/lexer.cxx @@ -13,6 +13,14 @@ namespace build2 { using type = token_type; + build2::script::redirect_aliases lexer::redirect_aliases { + type (type::in_file), + type (type::in_doc), + type (type::in_str), + type (type::out_file_ovr), + type (type::out_file_app), + nullopt}; + void lexer:: mode (build2::lexer_mode m, char ps, @@ -192,7 +200,7 @@ namespace build2 m == lexer_mode::first_token || m == lexer_mode::second_token) { - if (optional t = next_cmd_op (c, sep, m)) + if (optional t = next_cmd_op (c, sep)) return move (*t); } diff --git a/libbuild2/build/script/lexer.hxx b/libbuild2/build/script/lexer.hxx index f755cea..7d919e5 100644 --- a/libbuild2/build/script/lexer.hxx +++ b/libbuild2/build/script/lexer.hxx @@ -48,7 +48,8 @@ namespace build2 const char* escapes = nullptr) : base_lexer (is, name, line, nullptr /* escapes */, - false /* set_mode */) + false /* set_mode */, + redirect_aliases) { mode (m, '\0', escapes); } @@ -62,6 +63,9 @@ namespace build2 virtual token next () override; + public: + static redirect_aliases_type redirect_aliases; + private: token next_line (); diff --git a/libbuild2/build/script/parser+here-document.test.testscript b/libbuild2/build/script/parser+here-document.test.testscript index 8ea6373..f56a5e1 100644 --- a/libbuild2/build/script/parser+here-document.test.testscript +++ b/libbuild2/build/script/parser+here-document.test.testscript @@ -6,49 +6,85 @@ { : missing-newline : + $* <'cmd <<=' 2>>EOE != 0 + buildfile:11:8: error: expected here-document end marker + EOE + + : missing-newline-alias + : $* <'cmd <<' 2>>EOE != 0 buildfile:11:7: error: expected here-document end marker EOE : missing-exit : + $* <'cmd <<= != 0' 2>>EOE != 0 + buildfile:11:9: error: expected here-document end marker + EOE + + : missing-exit-alias + : $* <'cmd << != 0' 2>>EOE != 0 buildfile:11:8: error: expected here-document end marker EOE : missing-empty : + $* <'cmd <<=""' 2>>EOE != 0 + buildfile:11:8: error: expected here-document end marker + EOE + + : missing-empty-alias + : $* <'cmd <<""' 2>>EOE != 0 buildfile:11:7: error: expected here-document end marker EOE : unseparated-expansion : + $* <'cmd <<=FOO$foo' 2>>EOE != 0 + buildfile:11:11: error: here-document end marker must be literal + EOE + + : unseparated-expansion-alias + : $* <'cmd <>EOE != 0 buildfile:11:10: error: here-document end marker must be literal EOE : quoted-single-partial : - $* <"cmd <>EOE != 0 - buildfile:11:7: error: partially-quoted here-document end marker + $* <"cmd <<=F'O'O" 2>>EOE != 0 + buildfile:11:8: error: partially-quoted here-document end marker EOE : quoted-double-partial : - $* <'cmd <<"FO"O' 2>>EOE != 0 - buildfile:11:7: error: partially-quoted here-document end marker + $* <'cmd <<="FO"O' 2>>EOE != 0 + buildfile:11:8: error: partially-quoted here-document end marker EOE : quoted-mixed : - $* <"cmd <<\"FO\"'O'" 2>>EOE != 0 - buildfile:11:7: error: partially-quoted here-document end marker + $* <"cmd <<=\"FO\"'O'" 2>>EOE != 0 + buildfile:11:8: error: partially-quoted here-document end marker EOE : unseparated : $* <>EOO + cmd <<=EOF!=0 + foo + EOF + EOI + cmd <<=EOF != 0 + foo + EOF + EOO + + : unseparated-alias + : + $* <>EOO cmd <>EOO + cmd <<='EOF' + foo + EOF + EOI + cmd <<=EOF + foo + EOF + EOO + + : quoted-single-alias + : + $* <>EOO cmd <<'EOF' foo EOF @@ -73,6 +121,18 @@ : quoted-double : $* <>EOO + cmd <<="EOF" + foo + EOF + EOI + cmd <<=EOF + foo + EOF + EOO + + : quoted-double-alias + : + $* <>EOO cmd <<"EOF" foo EOF @@ -89,13 +149,13 @@ : basic : $* <>EOO - cmd <>EOO - cmd <>EOO - cmd <>EOO x = foo bar - cmd <<"EOF" + cmd <<="EOF" $x EOF EOI - cmd <>EOO x = foo - cmd <<"EOF" + cmd <<="EOF" $x bar $x EOF EOI - cmd <>EOE != 0 - cmd <>EOO -cmd <>EOO -cmd <<"EOF" +cmd <<="EOF" 'single' "double" b'o't"h" ('single' "double") EOF EOI -cmd <>EOO -cmd <"" +cmd <<<="" EOI -cmd <'' +cmd <<<='' EOO : empty-nn : $* <>EOO -cmd <:"" +cmd <<<=:"" EOI -cmd <:'' +cmd <<<=:'' +EOO + +: empty-alias +: +$* <>EOO +cmd <<<"" +EOI +cmd <<<'' +EOO + +: empty-nn-alias +: +$* <>EOO +cmd <<<:"" +EOI +cmd <<<:'' EOO diff --git a/libbuild2/build/script/parser+pipe-expr.test.testscript b/libbuild2/build/script/parser+pipe-expr.test.testscript index 3dd6b1b..a6ca12e 100644 --- a/libbuild2/build/script/parser+pipe-expr.test.testscript +++ b/libbuild2/build/script/parser+pipe-expr.test.testscript @@ -36,7 +36,7 @@ EOO : here-doc : $* <>EOO -cmd1 <>EOO2 && cmd3 <&1 | cmd4 2>>EOE4 >>EOO4 +cmd1 <<=EOI1 | cmd2 >>?EOO2 && cmd3 <<=EOI3 2>&1 | cmd4 2>>?EOE4 >>?EOO4 input one EOI1 @@ -53,7 +53,7 @@ output four EOO4 EOI -cmd1 <>EOO2 && cmd3 <&1 | cmd4 >>EOO4 2>>EOE4 +cmd1 <<=EOI1 | cmd2 >>?EOO2 && cmd3 <<=EOI3 2>&1 | cmd4 >>?EOO4 2>>?EOE4 input one EOI1 diff --git a/libbuild2/build/script/parser+redirect.test.testscript b/libbuild2/build/script/parser+redirect.test.testscript index 641381e..82c04ea 100644 --- a/libbuild2/build/script/parser+redirect.test.testscript +++ b/libbuild2/build/script/parser+redirect.test.testscript @@ -21,9 +21,9 @@ : portable-path : $* <>EOO - cmd /bar 2>/baz + cmd <<<=/foo >>>?/bar 2>>>?/baz EOI - cmd /bar 2>/baz + cmd <<<=/foo >>>?/bar 2>>>?/baz EOO } @@ -33,9 +33,9 @@ : portable-path : $* <>EOO - cmd >/~%foo% 2>/~%bar% + cmd >>>?/~%foo% 2>>>?/~%bar% EOI - cmd >/~%foo% 2>/~%bar% + cmd >>>?/~%foo% 2>>>?/~%bar% EOO } } @@ -49,7 +49,7 @@ : portable-path : $* <>EOO - cmd </EOO_ 2>/EOE_ + cmd <<=/EOI_ >>?/EOO_ 2>>?/EOE_ foo EOI_ bar @@ -57,7 +57,7 @@ baz EOE_ EOI - cmd </EOO_ 2>/EOE_ + cmd <<=/EOI_ >>?/EOO_ 2>>?/EOE_ foo EOI_ bar @@ -72,11 +72,35 @@ : in-out : $* <>EOO - cmd <<:/EOF >>:/EOF + cmd <<=:/EOF >>?:/EOF foo EOF EOI - cmd <<:/EOF >>:/EOF + cmd <<=:/EOF >>?:/EOF + foo + EOF + EOO + + : in-alias-out + : + $* <>EOO + cmd <<:/EOF >>?:/EOF + foo + EOF + EOI + cmd <<:/EOF >>?:/EOF + foo + EOF + EOO + + : out-in-alias + : + $* <>EOO + cmd >>?:/EOF <<:/EOF + foo + EOF + EOI + cmd <<:/EOF >>?:/EOF foo EOF EOO @@ -87,21 +111,21 @@ : modifiers : $* <>EOE != 0 - cmd <<:/EOF >>:EOF + cmd <<=:/EOF >>?:EOF foo EOF EOI - buildfile:11:16: error: different modifiers for shared here-document 'EOF' + buildfile:11:18: error: different modifiers for shared here-document 'EOF' EOE : quoting : $* <>EOE != 0 - cmd <>"EOF" + cmd <<=EOF >>?"EOF" foo EOF EOI - buildfile:11:13: error: different quoting for shared here-document 'EOF' + buildfile:11:15: error: different quoting for shared here-document 'EOF' EOE } } @@ -113,13 +137,13 @@ : portable-path : $* <>EOO - cmd >/~%EOF% 2>/~%EOE% + cmd >>?/~%EOF% 2>>?/~%EOE% foo EOF bar EOE EOI - cmd >/~%EOF% 2>/~%EOE% + cmd >>?/~%EOF% 2>>?/~%EOE% foo EOF bar @@ -132,11 +156,11 @@ : in-out : $* <>EOO - cmd >>~/EOF/ 2>>~/EOF/ + cmd >>?~/EOF/ 2>>?~/EOF/ foo EOF EOI - cmd >>~/EOF/ 2>>~/EOF/ + cmd >>?~/EOF/ 2>>?~/EOF/ foo EOF EOO @@ -147,21 +171,21 @@ : introducers : $* <>EOE != 0 - cmd >>~/EOF/ 2>>~%EOF% + cmd >>?~/EOF/ 2>>?~%EOF% foo EOF EOI - buildfile:11:18: error: different introducers for shared here-document regex 'EOF' + buildfile:11:20: error: different introducers for shared here-document regex 'EOF' EOE : flags : $* <>EOE != 0 - cmd >>~/EOF/ 2>>~/EOF/i + cmd >>?~/EOF/ 2>>?~/EOF/i foo EOF EOI - buildfile:11:18: error: different global flags for shared here-document regex 'EOF' + buildfile:11:20: error: different global flags for shared here-document regex 'EOF' EOE } } @@ -179,23 +203,23 @@ : string : $* <>EOO - cmd >>EOF >bar + cmd >>?EOF >>>?bar foo EOF EOI - cmd >bar + cmd >>>?bar EOO : regex : $* <>EOO - cmd >>FOO >>~/BAR/ + cmd >>?FOO >>?~/BAR/ foo FOO bar BAR EOI - cmd >>~/BAR/ + cmd >>?~/BAR/ bar BAR EOO @@ -215,11 +239,11 @@ : different-modifiers : $* <>EOE != 0 - cmd >>EOF >>/EOF + cmd >>?EOF >>?/EOF foo EOF EOI - buildfile:11:14: error: different modifiers for shared here-document 'EOF' + buildfile:11:16: error: different modifiers for shared here-document 'EOF' EOE } } @@ -260,9 +284,9 @@ : cmp : $* <>EOO - cmd 0<<>>b 2>>>c + cmd 0<=a 1>?b 2>?c EOI - cmd <<>>b 2>>>c + cmd <=a >?b 2>?c EOO : write @@ -276,9 +300,9 @@ : quote : $* <>EOO - cmd 0<<<"a f" 1>="b f" 2>+"c f" + cmd 0<="a f" 1>="b f" 2>+"c f" EOI - cmd <<<'a f' >='b f' 2>+'c f' + cmd <='a f' >='b f' 2>+'c f' EOO : in @@ -287,17 +311,37 @@ : missed : $* <>EOE !=0 - cmd <<< + cmd <= EOI - buildfile:11:8: error: missing stdin file + buildfile:11:7: error: missing stdin file EOE : empty : $* <>EOE !=0 - cmd <<<"" + cmd <="" EOI - buildfile:11:8: error: empty stdin redirect path + buildfile:11:7: error: empty stdin redirect path + EOE + } + + : in-alias + : + { + : missed + : + $* <>EOE !=0 + cmd < + EOI + buildfile:11:6: error: missing stdin file + EOE + + : empty + : + $* <>EOE !=0 + cmd <"" + EOI + buildfile:11:6: error: empty stdin redirect path EOE } @@ -321,6 +365,26 @@ EOE } + : out-alias + : + { + : missed + : + $* <>EOE !=0 + cmd > + EOI + buildfile:11:6: error: missing stdout file + EOE + + : empty + : + $* <>EOE !=0 + cmd >"" + EOI + buildfile:11:6: error: empty stdout redirect path + EOE + } + : err : { @@ -340,6 +404,26 @@ buildfile:11:8: error: empty stderr redirect path EOE } + + : err-alias + : + { + : missed + : + $* <>EOE !=0 + cmd 2> + EOI + buildfile:11:7: error: missing stderr file + EOE + + : empty + : + $* <>EOE !=0 + cmd 2>"" + EOI + buildfile:11:7: error: empty stderr redirect path + EOE + } } : merge diff --git a/libbuild2/build/script/parser+regex.test.testscript b/libbuild2/build/script/parser+regex.test.testscript index 4a47e73..625bfdf 100644 --- a/libbuild2/build/script/parser+regex.test.testscript +++ b/libbuild2/build/script/parser+regex.test.testscript @@ -9,61 +9,61 @@ { : missed : - $* <'cmd >~' 2>>EOE != 0 - buildfile:11:7: error: missing stdout here-string regex + $* <'cmd >>>?~' 2>>EOE != 0 + buildfile:11:10: error: missing stdout here-string regex EOE : no-introducer : - $* <'cmd >~""' 2>>EOE != 0 - buildfile:11:7: error: no introducer character in stdout regex redirect + $* <'cmd >>>?~""' 2>>EOE != 0 + buildfile:11:10: error: no introducer character in stdout regex redirect EOE : no-term-introducer : - $* <'cmd >~/' 2>>EOE != 0 - buildfile:11:7: error: no closing introducer character in stdout regex redirect + $* <'cmd >>>?~/' 2>>EOE != 0 + buildfile:11:10: error: no closing introducer character in stdout regex redirect EOE : portable-path-introducer : - $* <'cmd >/~/foo/' 2>>EOE != 0 - buildfile:11:8: error: portable path modifier and '/' introducer in stdout regex redirect + $* <'cmd >>>?/~/foo/' 2>>EOE != 0 + buildfile:11:11: error: portable path modifier and '/' introducer in stdout regex redirect EOE : empty : - $* <'cmd >~//' 2>>EOE != 0 - buildfile:11:7: error: stdout regex redirect is empty + $* <'cmd >>>?~//' 2>>EOE != 0 + buildfile:11:10: error: stdout regex redirect is empty EOE : no-flags : - $* <'cmd >~/fo*/' >'cmd >~/fo*/' + $* <'cmd >>>?~/fo*/' >'cmd >>>?~/fo*/' : idot : - $* <'cmd >~/fo*/d' >'cmd >~/fo*/d' + $* <'cmd >>>?~/fo*/d' >'cmd >>>?~/fo*/d' : icase : - $* <'cmd >~/fo*/i' >'cmd >~/fo*/i' + $* <'cmd >>>?~/fo*/i' >'cmd >>>?~/fo*/i' : invalid-flags1 : - $* <'cmd >~/foo/z' 2>>EOE != 0 - buildfile:11:7: error: junk at the end of stdout regex redirect + $* <'cmd >>>?~/foo/z' 2>>EOE != 0 + buildfile:11:10: error: junk at the end of stdout regex redirect EOE : invalid-flags2 : - $* <'cmd >~/foo/iz' 2>>EOE != 0 - buildfile:11:7: error: junk at the end of stdout regex redirect + $* <'cmd >>>?~/foo/iz' 2>>EOE != 0 + buildfile:11:10: error: junk at the end of stdout regex redirect EOE : no-newline : - $* <'cmd >:~/fo*/' >'cmd >:~/fo*/' + $* <'cmd >>>?:~/fo*/' >'cmd >>>?:~/fo*/' } : stderr @@ -71,8 +71,8 @@ { : missed : - $* <'cmd 2>~' 2>>EOE != 0 - buildfile:11:8: error: missing stderr here-string regex + $* <'cmd 2>>>?~' 2>>EOE != 0 + buildfile:11:11: error: missing stderr here-string regex EOE : no-introducer @@ -81,15 +81,15 @@ : All we need is to make sure that the proper description is passed to : the parse_regex() function. : - $* <'cmd 2>~""' 2>>EOE != 0 - buildfile:11:8: error: no introducer character in stderr regex redirect + $* <'cmd 2>>>?~""' 2>>EOE != 0 + buildfile:11:11: error: no introducer character in stderr regex redirect EOE } : modifier-last : - $* <'cmd >~/x' 2>>EOE != 0 - buildfile:11:7: error: no closing introducer character in stdout regex redirect + $* <'cmd >>>?~/x' 2>>EOE != 0 + buildfile:11:10: error: no closing introducer character in stdout regex redirect EOE } @@ -101,14 +101,14 @@ { : missed : - $* <'cmd >>~' 2>>EOE != 0 - buildfile:11:8: error: expected here-document regex end marker + $* <'cmd >>?~' 2>>EOE != 0 + buildfile:11:9: error: expected here-document regex end marker EOE : portable-path-introducer : $* <>EOE != 0 - cmd >>/~/EOO/ + cmd >>?/~/EOO/ foo EOO EOI @@ -118,7 +118,7 @@ : unterminated-line-char : $* <>EOE != 0 - cmd >>~/EOO/ + cmd >>?~/EOO/ / EOO EOI @@ -128,7 +128,7 @@ : empty : $* <>EOE != 0 - cmd >>:~/EOO/ + cmd >>?:~/EOO/ EOO EOI buildfile:12:1: error: empty here-document regex @@ -137,7 +137,7 @@ : no-flags : $* <>EOO - cmd 2>>~/EOE/ + cmd 2>>?~/EOE/ foo /? /foo/ @@ -149,7 +149,7 @@ //* EOE EOI - cmd 2>>~/EOE/ + cmd 2>>?~/EOE/ foo /? /foo/ @@ -162,15 +162,18 @@ EOE EOO - : no-newline + : no-newline-str + : + $* <'cmd >>>?:~/fo*/' >'cmd >>>?:~/fo*/' + + : no-newline-doc : - $* <'cmd >:~/fo*/' >'cmd >:~/fo*/' $* <>EOO - cmd 2>>:~/EOE/ + cmd 2>>?:~/EOE/ foo EOE EOI - cmd 2>>:~/EOE/ + cmd 2>>?:~/EOE/ foo EOE EOO @@ -181,11 +184,11 @@ : idot : $* <>EOO - cmd 2>>~/EOE/d + cmd 2>>?~/EOE/d foo EOE EOI - cmd 2>>~/EOE/d + cmd 2>>?~/EOE/d foo EOE EOO @@ -193,11 +196,11 @@ : icase : $* <>EOO - cmd 2>>~/EOE/i + cmd 2>>?~/EOE/i foo EOE EOI - cmd 2>>~/EOE/i + cmd 2>>?~/EOE/i foo EOE EOO @@ -209,14 +212,14 @@ { : missed : - $* <'cmd 2>>~' 2>>EOE != 0 - buildfile:11:9: error: expected here-document regex end marker + $* <'cmd 2>>?~' 2>>EOE != 0 + buildfile:11:10: error: expected here-document regex end marker EOE } : modifier-last : - $* <'cmd >>~:/FOO/' 2>>EOE != 0 + $* <'cmd >>?~:/FOO/' 2>>EOE != 0 buildfile:11:5: error: no closing introducer character in here-document regex end marker EOE } diff --git a/libbuild2/build/script/parser.cxx b/libbuild2/build/script/parser.cxx index 86019ba..60000c2 100644 --- a/libbuild2/build/script/parser.cxx +++ b/libbuild2/build/script/parser.cxx @@ -141,7 +141,7 @@ namespace build2 pair p; if (lt != line_type::cmd_else && lt != line_type::cmd_end) - p = parse_command_expr (t, tt); + p = parse_command_expr (t, tt, lexer::redirect_aliases); if (tt != type::newline) fail (t) << "expected newline instead of " << t; @@ -232,7 +232,9 @@ namespace build2 // assert (!pre_parse_); - pair p (parse_command_expr (t, tt)); + pair p ( + parse_command_expr (t, tt, lexer::redirect_aliases)); + assert (tt == type::newline); parse_here_documents (t, tt, p); -- cgit v1.1