From 512cacb051d7e7c72192462fd19dada8342204e2 Mon Sep 17 00:00:00 2001 From: Jason Dusek Date: Sun, 17 Mar 2013 00:23:30 +0000 Subject: [PATCH] Provide a safe/defaulting element lookup. --- hs/Language/Bash/PrettyPrinter.hs | 4 ++++ hs/Language/Bash/Syntax.hs | 3 +++ hs/bash.cabal | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/hs/Language/Bash/PrettyPrinter.hs b/hs/Language/Bash/PrettyPrinter.hs index 2c2c845..8091eca 100644 --- a/hs/Language/Bash/PrettyPrinter.hs +++ b/hs/Language/Bash/PrettyPrinter.hs @@ -72,6 +72,8 @@ instance (Annotation t) => PP (Expression t) where pp (ARGVLength) = word "$#" pp (Elements ident) = (word . quote . braces) (bytes ident `append` "[@]") + pp (ElementsSafe ident) = (word . quote . braces_) + (bytes ident `append` "[@]") pp (Keys ident) = (word . quote . braces) ('!' `cons` bytes ident `append` "[@]") pp (Length ident) = (word . quote . braces) @@ -188,6 +190,8 @@ braces b = "${" `append` b `snoc` '}' braces0 b = "${" `append` b `append` ":-}" +braces_ b = concat ["${", b, ":+${", b, "}}"] + brackets b = cons '[' b `snoc` ']' identpart (VarSpecial special) = (drop 1 . bytes) special diff --git a/hs/Language/Bash/Syntax.hs b/hs/Language/Bash/Syntax.hs index 75b84a0..bc19d7b 100644 --- a/hs/Language/Bash/Syntax.hs +++ b/hs/Language/Bash/Syntax.hs @@ -157,6 +157,7 @@ data Expression t = Literal Esc.Bash | ARGVElements | ARGVLength | Elements Identifier + | ElementsSafe Identifier | Keys Identifier | Length VarName | Trim Trim VarName (Expression t) @@ -186,6 +187,7 @@ instance Functor Expression where ARGVElements -> ARGVElements ARGVLength -> ARGVLength Elements ident -> Elements ident + ElementsSafe ident -> Elements ident Keys ident -> Keys ident Length ident -> Length ident Trim trim v expr -> Trim trim v (fmap f expr) @@ -208,6 +210,7 @@ instance Foldable Expression where ARGVElements -> mempty ARGVLength -> mempty Elements _ -> mempty + ElementsSafe _ -> mempty Keys _ -> mempty Length _ -> mempty Trim _ _ expr -> foldMap f expr diff --git a/hs/bash.cabal b/hs/bash.cabal index da6e8ad..20e8f1e 100644 --- a/hs/bash.cabal +++ b/hs/bash.cabal @@ -1,5 +1,5 @@ name : bash -version : 0.1.6 +version : 0.1.8 category : Language license : BSD3 license-file : LICENSE