@@ -35,11 +35,14 @@ import UCD2Haskell.Common (
3535import UCD2Haskell.Generator (
3636 BitmapType (.. ),
3737 FileRecipe (.. ),
38+ ShamochuCode (.. ),
3839 apacheLicense ,
3940 generateShamochuBitmaps ,
41+ mkImports' ,
4042 toLookupBitMapName ,
4143 unlinesBB ,
4244 word32ToWord8s ,
45+ (<+>) ,
4346 )
4447
4548recipe :: PropertyValuesAliases -> FileRecipe Prop. Entry
@@ -56,6 +59,13 @@ genScriptsModule moduleName aliases = Fold step mempty done
5659 done ranges =
5760 let scripts = Set. toList
5861 (foldr addScript (Set. singleton Defaults. defaultScript) ranges)
62+ ShamochuCode {.. } = if length scripts <= 0xff
63+ then mkCharScripts scripts ranges
64+ else error " Cannot encode scripts"
65+ imports' = imports <+> Map. fromList
66+ [ ( " GHC.Exts"
67+ , Set. fromList [" Addr#" , " Int(..)" , " nullAddr#" ] )
68+ , ( " Data.Ix" , Set. singleton " Ix" )]
5969 in unlinesBB
6070 [ " {-# LANGUAGE PatternSynonyms #-}"
6171 , " {-# OPTIONS_HADDOCK hide #-}"
@@ -71,13 +81,7 @@ genScriptsModule moduleName aliases = Fold step mempty done
7181 , " , pattern ScriptCharMaskComplement )"
7282 , " where"
7383 , " "
74- , " import Data.Char (ord)"
75- , " import Data.Int (Int8)"
76- , " import Data.Ix (Ix)"
77- , " import Data.Word (Word16)"
78- , " import GHC.Exts (Addr#, Int#, Int(..), Ptr(..), nullAddr#, andI#, iShiftL#, iShiftRL#, (+#), (-#))"
79- , " import Unicode.Internal.Bits.Scripts (lookupWord8AsInt#, lookupWord16AsInt#)"
80- , " "
84+ , mkImports' " Scripts" imports'
8185 , " -- | Unicode [script](https://www.unicode.org/reports/tr24/)."
8286 , " --"
8387 , " -- The constructors descriptions are the original Unicode values"
@@ -127,9 +131,7 @@ genScriptsModule moduleName aliases = Fold step mempty done
127131 , " -- | Script of a character."
128132 , " --"
129133 , " -- @since 0.1.0"
130- , if length scripts <= 0xff
131- then mkCharScripts scripts ranges
132- else error " Cannot encode scripts"
134+ , code
133135 , " "
134136 ]
135137
@@ -297,7 +299,7 @@ genScriptsModule moduleName aliases = Fold step mempty done
297299 encodeBytes = foldr addByte " " . word32ToWord8s
298300 addByte n acc = BB. char7 ' \\ ' <> BB. word8Dec n <> acc
299301
300- mkCharScripts :: [BS. ShortByteString ] -> [Prop. Entry ] -> BB. Builder
302+ mkCharScripts :: [BS. ShortByteString ] -> [Prop. Entry ] -> ShamochuCode
301303 mkCharScripts scripts scriptsRanges =
302304 let charScripts = L. sort (foldMap (rangeToCharScripts getScript) scriptsRanges)
303305 charScripts' = reverse (fst (foldl' addMissing (mempty , '\ 0 ') charScripts))
@@ -325,28 +327,32 @@ genScriptsModule moduleName aliases = Fold step mempty done
325327 assert (fromEnum (length scripts) < 0xff )
326328 (fromIntegral . fromEnum )
327329 bitmap0To1 = " scriptPlanes0To1"
328- in mconcat
329- [ " {-# INLINE script #-}\n "
330- , " script :: Char -> Int#\n "
331- , " script c\n "
332- , " -- Planes 0-1\n "
333- , " | cp < 0x" , showPaddedHeXB boundPlanes0To1
334- , " = " , toLookupBitMapName bitmap0To1, " cp#\n "
335- , mkScriptsBounds def (scripts !! ) otherPlanes
336- , " -- Default: " , BB. shortByteString Defaults. defaultScript, " \n "
337- , " | otherwise = " , BB. intDec def, " #\n "
338- , " where\n "
339- , " !cp@(I# cp#) = ord c\n "
340- , " \n "
341- , generateShamochuBitmaps
342- bitmap0To1
343- True
344- ByteMap
345- (NE. singleton 3 )
346- [5 ]
347- toWord8
348- planes0To1
349- ]
330+ ShamochuCode {.. } = generateShamochuBitmaps
331+ bitmap0To1
332+ True
333+ ByteMap
334+ (NE. singleton 3 )
335+ [5 ]
336+ toWord8
337+ planes0To1
338+ in ShamochuCode
339+ { code = mconcat
340+ [ " {-# INLINE script #-}\n "
341+ , " script :: Char -> Int#\n "
342+ , " script c\n "
343+ , " -- Planes 0-1\n "
344+ , " | cp < 0x" , showPaddedHeXB boundPlanes0To1
345+ , " = " , toLookupBitMapName bitmap0To1, " cp#\n "
346+ , mkScriptsBounds def (scripts !! ) otherPlanes
347+ , " -- Default: " , BB. shortByteString Defaults. defaultScript, " \n "
348+ , " | otherwise = " , BB. intDec def, " #\n "
349+ , " where\n "
350+ , " !cp@(I# cp#) = ord c\n "
351+ , " \n "
352+ , code
353+ ]
354+ , imports = imports }
355+
350356
351357 mkScriptsBounds :: Int -> (Int -> BS. ShortByteString ) -> [(Int ,Char )] -> BB. Builder
352358 mkScriptsBounds def getScriptName
0 commit comments