1
0
forked from GitHub/gf-core

Introduce type RawIdent; only 9 imports of Data.ByteString.Char8 remain

The fact that identifiers are represented as ByteStrings is now an internal
implentation detail in module GF.Infra.Ident. Conversion between ByteString
and identifiers is only needed in the lexer and the Binary instances.
This commit is contained in:
hallgren
2013-09-19 20:48:10 +00:00
parent 3d5b9bd1fd
commit 021b5f06d3
14 changed files with 74 additions and 64 deletions

View File

@@ -34,7 +34,7 @@ instance Binary Ident where
get = do bs <- get
if bs == BS.pack "_"
then return identW
else return (identC bs)
else return (identC (rawIdentC bs))
instance Binary SourceGrammar where
put = put . modules
@@ -289,6 +289,9 @@ instance Binary Label where
1 -> fmap LVar get
_ -> decodingError
instance Binary RawIdent where
put = put . rawId2bs
get = fmap rawIdentC get
putGFOVersion = mapM_ (putWord8 . fromIntegral . ord) gfoVersion
getGFOVersion = replicateM (length gfoVersion) (fmap (chr . fromIntegral) getWord8)

View File

@@ -74,7 +74,6 @@ import Data.Array.Unboxed
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.IntMap as IntMap
import qualified Data.ByteString.Char8 as BS
import Text.PrettyPrint
import System.FilePath
import Control.Monad.Identity
@@ -472,7 +471,7 @@ data TInfo =
-- | record label
data Label =
LIdent BS.ByteString
LIdent RawIdent
| LVar Int
deriving (Show, Eq, Ord)
@@ -497,16 +496,15 @@ varLabel :: Int -> Label
varLabel = LVar
tupleLabel, linLabel :: Int -> Label
tupleLabel i = LIdent $! BS.pack ('p':show i)
linLabel i = LIdent $! BS.pack ('s':show i)
tupleLabel i = LIdent $! rawIdentS ('p':show i)
linLabel i = LIdent $! rawIdentS ('s':show i)
theLinLabel :: Label
theLinLabel = LIdent (BS.singleton 's')
theLinLabel = LIdent (rawIdentS "s")
ident2label :: Ident -> Label
ident2label c = LIdent (ident2bs c)
ident2label c = LIdent (ident2raw c)
label2ident :: Label -> Ident
label2ident (LIdent s) = identC s
label2ident (LVar i) = identC (BS.pack ('$':show i))
label2ident (LVar i) = identS ('$':show i)

View File

@@ -278,12 +278,12 @@ getPosn :: P Posn
getPosn = P $ \inp@(AI pos _ _) -> POk pos
alex_action_3 = tok (eitherResIdent (T_Ident . identC))
alex_action_3 = tok (eitherResIdent (T_Ident . identC . rawIdentC))
alex_action_4 = tok (eitherResIdent (T_LString . BS.unpack))
alex_action_5 = tok (eitherResIdent (T_Ident . identC))
alex_action_6 = tok (T_String . unescapeInitTail . BS.unpack)
alex_action_7 = tok (T_Integer . read . BS.unpack)
alex_action_8 = tok (T_Double . read . BS.unpack)
alex_action_5 = tok (eitherResIdent (T_Ident . identC . rawIdentC))
alex_action_6 = tok (T_String . unescapeInitTail . BS.unpack)
alex_action_7 = tok (T_Integer . read . BS.unpack)
alex_action_8 = tok (T_Double . read . BS.unpack)
{-# LINE 1 "templates/GenericTemplate.hs" #-}
{-# LINE 1 "templates/GenericTemplate.hs" #-}
{-# LINE 1 "<built-in>" #-}

View File

@@ -16,8 +16,6 @@
module GF.Grammar.Lockfield (lockRecType, unlockRecord, lockLabel, isLockLabel) where
import qualified Data.ByteString.Char8 as BS
import GF.Infra.Ident
import GF.Grammar.Grammar
import GF.Grammar.Macros
@@ -41,12 +39,12 @@ unlockRecord c ft = do
_ -> return $ mkAbs xs (ExtR t lock)
lockLabel :: Ident -> Label
lockLabel c = LIdent $! BS.append lockPrefix (ident2bs c)
lockLabel c = LIdent $! prefixRawIdent lockPrefix (ident2raw c)
isLockLabel :: Label -> Bool
isLockLabel l = case l of
LIdent c -> BS.isPrefixOf lockPrefix c
LIdent c -> isPrefixOf lockPrefix c
_ -> False
lockPrefix = BS.pack "lock_"
lockPrefix = rawIdentS "lock_"

View File

@@ -26,7 +26,6 @@ import GF.Grammar.Values
import GF.Grammar.Macros
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Text.PrettyPrint
{-
@@ -238,11 +237,11 @@ qualifTerm m = qualif [] where
Cn c -> Q (m,c)
Con c -> QC (m,c)
_ -> composSafeOp (qualif xs) t
chV x = string2var $ ident2bs x
chV x = string2var $ ident2raw x
string2var :: BS.ByteString -> Ident
string2var s = case BS.unpack s of
c:'_':i -> identV (BS.singleton c) (readIntArg i) ---
string2var :: RawIdent -> Ident
string2var s = case showRawIdent s of
c:'_':i -> identV (rawIdentS [c]) (readIntArg i) ---
_ -> identC s
-- | reindex variables so that they tell nesting depth level
@@ -254,7 +253,7 @@ reindexTerm = qualif (0,[]) where
Vr x -> Vr $ look x g
_ -> composSafeOp (qualif dg) t
look x = maybe x id . lookup x --- if x is not in scope it is unchanged
ind x d = identC $ ident2bs x `BS.append` BS.singleton '_' `BS.append` BS.pack (show d)
ind x d = identC $ ident2raw x `prefixRawIdent` rawIdentS "_" `prefixRawIdent` rawIdentS (show d)
{-
-- this method works for context-free abstract syntax

View File

@@ -507,11 +507,11 @@ Patt3
PattAss :: { [(Label,Patt)] }
PattAss
: ListIdent '=' Patt { [(LIdent (ident2bs i),$3) | i <- $1] }
: ListIdent '=' Patt { [(LIdent (ident2raw i),$3) | i <- $1] }
Label :: { Label }
Label
: Ident { LIdent (ident2bs $1) }
: Ident { LIdent (ident2raw $1) }
| '$' Integer { LVar (fromIntegral $2) }
Sort :: { Ident }

View File

@@ -30,9 +30,9 @@ $u = [\0-\255] -- universal: any character
"{-" ([$u # \-] | \- [$u # \}])* ("-")+ "}" ;
$white+ ;
@rsyms { tok (eitherResIdent (T_Ident . identC)) }
@rsyms { tok (eitherResIdent (T_Ident . identC . rawIdentC)) }
\' ($u # \')* \' { tok (eitherResIdent (T_LString . BS.unpack)) }
(\_ | $l)($l | $d | \_ | \')* { tok (eitherResIdent (T_Ident . identC)) }
(\_ | $l)($l | $d | \_ | \')* { tok (eitherResIdent (T_Ident . identC . rawIdentC)) }
\" ([$u # [\" \\ \n]] | (\\ (\" | \\ | \' | n | t)))* \" { tok (T_String . unescapeInitTail . BS.unpack) }