From 3d5b9bd1fd46a51651cbfbd45f03e5b878aebbac Mon Sep 17 00:00:00 2001 From: hallgren Date: Thu, 19 Sep 2013 18:23:47 +0000 Subject: Make Ident abstract; imports of Data.ByteString.Char8 down from 29 to 16 modules Most of the explicit uses of ByteStrings were eliminated by using identS, identS = identC . BS.pack which was found in GF.Grammar.CF and moved to GF.Infra.Ident. The function prefixIdent :: String -> Ident -> Ident allowed one additional import of ByteString to be eliminated. The functions isArgIdent :: Ident -> Bool getArgIndex :: Ident -> Maybe Int were needed to eliminate explicit pattern matching on Ident from two modules. --- src/compiler/GF/Infra/Ident.hs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/compiler/GF/Infra/Ident.hs') diff --git a/src/compiler/GF/Infra/Ident.hs b/src/compiler/GF/Infra/Ident.hs index 967945be9..bb26ea98c 100644 --- a/src/compiler/GF/Infra/Ident.hs +++ b/src/compiler/GF/Infra/Ident.hs @@ -13,9 +13,10 @@ ----------------------------------------------------------------------------- module GF.Infra.Ident (-- * Identifiers - Ident(..), ident2bs, showIdent, ppIdent, - identC, identV, identA, identAV, identW, - argIdent, varStr, varX, isWildIdent, varIndex, + Ident, ident2bs, showIdent, ppIdent, prefixIdent, + identS, identC, identV, identA, identAV, identW, + argIdent, isArgIdent, getArgIndex, + varStr, varX, isWildIdent, varIndex, -- * refreshing identifiers IdState, initIdStateN, initIdState, lookVar, refVar, refVarPlus @@ -23,6 +24,7 @@ module GF.Infra.Ident (-- * Identifiers import GF.Data.Operations import qualified Data.ByteString.Char8 as BS +import Data.Char(isDigit) import Text.PrettyPrint @@ -54,6 +56,9 @@ showIdent i = BS.unpack $! ident2bs i ppIdent :: Ident -> Doc ppIdent = text . showIdent +identS :: String -> Ident +identS = identC . BS.pack + identC :: BS.ByteString -> Ident identV :: BS.ByteString -> Int -> Ident identA :: BS.ByteString -> Int -> Ident @@ -62,6 +67,10 @@ identW :: Ident (identC, identV, identA, identAV, identW) = (IC, IV, IA, IAV, IW) + +prefixIdent :: String -> Ident -> Ident +prefixIdent pref = identC . BS.append (BS.pack pref) . ident2bs + -- normal identifier -- ident s = IC s @@ -70,6 +79,16 @@ argIdent :: Int -> Ident -> Int -> Ident argIdent 0 (IC c) i = identA c i argIdent b (IC c) i = identAV c b i +isArgIdent IA{} = True +isArgIdent IAV{} = True +isArgIdent _ = False + +getArgIndex (IA _ i) = Just i +getArgIndex (IAV _ _ i) = Just i +getArgIndex (IC s) + | isDigit (BS.last s) = (Just . read . BS.unpack . snd . BS.spanEnd isDigit) s +getArgIndex x = Nothing + -- | used in lin defaults varStr :: Ident varStr = identA (BS.pack "str") 0 -- cgit v1.2.3