From 1e02e1f746ade63007fbe9caa03e89511a1da66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Tue, 25 Aug 2015 11:28:41 +0200 Subject: [PATCH 16/19] Shellvars: allow &&/|| in commands --- lenses/shellvars.aug | 80 ++++++++++++++++++++++++----------------- lenses/tests/test_shellvars.aug | 42 ++++++++++++++++++++-- 2 files changed, 87 insertions(+), 35 deletions(-) diff --git a/lenses/shellvars.aug b/lenses/shellvars.aug index f68f5bb..f7f4c47 100644 --- a/lenses/shellvars.aug +++ b/lenses/shellvars.aug @@ -106,28 +106,66 @@ module Shellvars = . Util.del_str "return" . ( Util.del_ws_spc . store Rx.integer )? + let action (operator:string) (lbl:string) (sto:lens) = + [ del (Rx.opt_space . operator . Rx.opt_space) (" " . operator . " ") + . label ("@".lbl) . sto ] + + let action_pipe = action "|" "pipe" + let action_and = action "&&" "and" + let action_or = action "||" "or" + let condition = - let action (operator:string) (lbl:string) = - [ Sep.opt_space . Util.del_str operator . Sep.opt_space - . label lbl . sto_to_semicol ] - in let cond (start:string) (end:string) = [ label "type" . store start ] - . Util.del_ws_spc . sto_to_semicol - . Util.del_ws_spc . Util.del_str end - . ( action "&&" "@and" | action "||" "@or" )* + let cond (start:string) (end:string) = [ label "type" . store start ] + . Util.del_ws_spc . sto_to_semicol + . Util.del_ws_spc . Util.del_str end + . ( action_and sto_to_semicol | action_or sto_to_semicol )* in Util.indent . label "@condition" . (cond "[" "]" | cond "[[" "]]") (* Entry types *) let entry_eol_item (item:lens) = [ item . comment_or_eol ] let entry_item (item:lens) = [ item ] + let entry_eol_nocommand = + entry_eol_item source + | entry_eol_item kv + | entry_eol_item unset + | entry_eol_item bare_export + | entry_eol_item builtin + | entry_eol_item return + | entry_eol_item condition + | entry_eol_item eval + | entry_eol_item alias + + let entry_noeol_nocommand = + entry_item source + | entry_item kv + | entry_item unset + | entry_item bare_export + | entry_item builtin + | entry_item return + | entry_item condition + | entry_item eval + | entry_item alias + (* Command *) let rec command = let reserved_key = /exit|shift|return|ulimit|unset|export|source|\.|if|for|select|while|until|then|else|fi|done|case|eval|alias/ in let word = /[A-Za-z0-9_.-\/]+/ - in let pipe = del /[ \t]*\|[ \t]*/ " | " + in let entry_eol = entry_eol_nocommand | entry_eol_item command + in let entry_noeol = entry_noeol_nocommand | entry_item command + in let entry = entry_eol | entry_noeol + in let pipe = action_pipe (entry_eol_item command | entry_item command) + in let and = action_and entry + in let or = action_or entry in Util.indent . label "@command" . store (word - reserved_key) . [ Sep.space . label "@arg" . sto_to_semicol]? - . (pipe . (entry_eol_item command | entry_item command) )* + . ( pipe | and | or )? + + let entry_eol = entry_eol_nocommand + | entry_eol_item command + + let entry_noeol = entry_noeol_nocommand + | entry_item command (************************************************************************ * Group: CONDITIONALS AND LOOPS @@ -185,30 +223,6 @@ module Shellvars = . entry+ . Util.indent . Util.del_str "}" . eol ] - let entry_eol = - entry_eol_item source - | entry_eol_item kv - | entry_eol_item unset - | entry_eol_item bare_export - | entry_eol_item builtin - | entry_eol_item return - | entry_eol_item condition - | entry_eol_item eval - | entry_eol_item alias - | entry_eol_item command - - let entry_noeol = - entry_item source - | entry_item kv - | entry_item unset - | entry_item bare_export - | entry_item builtin - | entry_item return - | entry_item condition - | entry_item eval - | entry_item alias - | entry_item command - let rec rec_entry = let entry = comment | entry_eol | rec_entry in cond_if entry diff --git a/lenses/tests/test_shellvars.aug b/lenses/tests/test_shellvars.aug index e0bf7fb..2c810cb 100644 --- a/lenses/tests/test_shellvars.aug +++ b/lenses/tests/test_shellvars.aug @@ -627,8 +627,46 @@ test Shellvars.lns get "echo foobar 'and this is baz' test Shellvars.lns get "echo \"$STRING\" | grep foo\n" = { "@command" = "echo" { "@arg" = "\"$STRING\"" } - { "@command" = "grep" - { "@arg" = "foo" } } } + { "@pipe" + { "@command" = "grep" + { "@arg" = "foo" } } } } + +(* Test: Shellvars.lns + Support && and || after command + GH #215 *) +test Shellvars.lns get "grep -q \"Debian\" /etc/issue && echo moo\n" = + { "@command" = "grep" + { "@arg" = "-q \"Debian\" /etc/issue" } + { "@and" + { "@command" = "echo" + { "@arg" = "moo" } } } } + +test Shellvars.lns get "grep -q \"Debian\" /etc/issue || echo baa\n" = + { "@command" = "grep" + { "@arg" = "-q \"Debian\" /etc/issue" } + { "@or" + { "@command" = "echo" + { "@arg" = "baa" } } } } + +test Shellvars.lns get "grep -q \"Debian\" /etc/issue && DEBIAN=1\n" = + { "@command" = "grep" + { "@arg" = "-q \"Debian\" /etc/issue" } + { "@and" + { "DEBIAN" = "1" } } } + +test Shellvars.lns get "cat /etc/issue | grep -q \"Debian\" && echo moo || echo baa\n" = + { "@command" = "cat" + { "@arg" = "/etc/issue" } + { "@pipe" + { "@command" = "grep" + { "@arg" = "-q \"Debian\"" } + { "@and" + { "@command" = "echo" + { "@arg" = "moo" } + { "@or" + { "@command" = "echo" + { "@arg" = "baa" } } } } } } } } + (* Local Variables: *) (* mode: caml *) -- 2.5.0