{-# LANGUAGE ExistentialQuantification, RankNTypes, Rank2Types, Safe #-}{-
This module is part of Chatty.
Copyleft (c) 2014 Marvin Cohrs
All wrongs reversed. Sharing is an act of love, not crime.
Please share Antisplice with everyone you like.
Chatty is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Chatty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}moduleText.Chatty.Expansion.VarswhereimportControl.ApplicativeimportControl.ArrowimportControl.MonadimportControl.Monad.IO.ClassimportControl.Monad.Trans.ClassimportSystem.Environment--hiding (getEnv)--import System.Posix.Env (getEnv, setEnv)importText.Chatty.Expansion-- | Some environment variabledataEnvVar=NotSet-- ^ Not set.|LiteralString-- ^ An embeddable string.|foralla.Showa=>Scalara-- ^ Something we can show.|Array[EnvVar]-- ^ Array of thatinstanceShowEnvVarwhereshow(Scalars)=showsshow(Literals)=sshow(Arrayps)=unwords$mapshowpsshowNotSet=""-- | Environment storage and variable expander.newtypeExpanderTma=Expander{runExpanderT::[(String,EnvVar)]->m(a,[(String,EnvVar)])}instanceMonadm=>Monad(ExpanderTm)wherereturna=Expander$\vs->return(a,vs)(Expandere)>>=f=Expander$\vs->do(a,vs')<-evs;runExpanderT(fa)vs'instanceMonadTransExpanderTwhereliftm=Expander$\vs->doa<-m;return(a,vs)instanceMonadIOm=>MonadIO(ExpanderTm)whereliftIO=lift.liftIOinstanceMonadm=>Functor(ExpanderTm)wherefmapfa=Expander$\vs->do(a',vs')<-runExpanderTavs;return(fa',vs')instanceMonadm=>Applicative(ExpanderTm)where(<*>)=appure=return-- | Run this function inside a blank environment.localEnvironment::Functorm=>ExpanderTma->malocalEnvironmentm=fmapfst$runExpanderTm[]-- | Run this function in a locally modifiable, but not exported environmentforkEnvironment::(Functorm,Monadm,MonadIOm)=>ExpanderTma->maforkEnvironmentm=does<-liftIOgetEnvironmentfmapfst$runExpanderTm$fmap(secondLiteral)es-- | Export this local environment.exportAll::(Monadm,MonadIOm)=>ExpanderTm()exportAll=Expander$\vs->doliftIO$forM_vs$\(k,v)->setEnvk(showv)return((),vs)instanceChExpandIOwhereexpand=expandVarsinstanceChExpandm=>ChExpand(ExpanderTm)whereexpand=lift.expand<=<expandVars-- | Expand $variablesexpandVars::(Monadm,Functorm,ChExpanderEnvm)=>String->mStringexpandVars[]=return[]expandVars('\\':'$':ss)=dor<-expandVarsss;return('$':r)expandVars('$':'{':ss)=letnm=takeBrace0ssrm=drop(lengthnm+1)sstakeBrace::Int->String->StringtakeBrace0('}':ss)=""takeBracen('}':ss)='}':takeBrace(n-1)sstakeBracen('{':ss)='{':takeBrace(n+1)sstakeBracen(s:ss)=s:takeBracenssindov<-fmapshow$mgetvnmr<-expandVarsrmreturn(v++r)expandVars('$':ss)=let(nm,rm)=(takeWhileisAnum&&&dropWhileisAnum)ssindov<-fmapshow$mgetvnmr<-expandVarsrmreturn(v++r)expandVars(s:ss)=doss'<-expandVarsss;return(s:ss')-- | Is alphanumeric?isAnum=(`elem`(['a'..'z']++['A'..'Z']++"_"++['0'..'9']))-- | Typeclass for all environment storages.classMonadee=>ChExpanderEnveewhere-- | Get environment variablemgetv::String->eeEnvVar-- | Put environment variablemputv::String->EnvVar->ee()instanceMonadm=>ChExpanderEnv(ExpanderTm)wheremgetvs=Expander$\vs->return$casefilter((==s).fst)vsof[]->(NotSet,vs)((_,v):_)->(v,vs)mputvkv=Expander$\vs->return((),(k,v):filter((/=k).fst)vs)instanceChExpanderEnvIOwheremgetv=fmapLiteral.getEnvmputvkv=setEnvk(showv)