summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-03-10 20:41:59 -0400
committerJoe Crayne <joe@jerkface.net>2019-03-10 20:41:59 -0400
commit4571dcae244b81a4b6aa0acacd773f728be49772 (patch)
tree11a9879d9334403808ada7749c23e25e18a100d9
parentef0bd9baee906ebf7c3293f0e5ec531bca0b4801 (diff)
Signatures.
-rw-r--r--monkeypatch.hs72
1 files changed, 71 insertions, 1 deletions
diff --git a/monkeypatch.hs b/monkeypatch.hs
index ac67afa..cfa9011 100644
--- a/monkeypatch.hs
+++ b/monkeypatch.hs
@@ -71,6 +71,7 @@ prettyUsingInclude incs (CTranslUnit edecls _) =
71 sysfst (Left ('"':a)) (Left ('<':b)) = Prelude.GT 71 sysfst (Left ('"':a)) (Left ('<':b)) = Prelude.GT
72 sysfst _ _ = Prelude.LT 72 sysfst _ _ = Prelude.LT
73 73
74includeTopLevel :: IncludeStack -> FilePath -> [Char]
74includeTopLevel (IncludeStack incs) f = do 75includeTopLevel (IncludeStack incs) f = do
75 stacks <- maybeToList $ Map.lookup f incs 76 stacks <- maybeToList $ Map.lookup f incs
76 stack <- take 1 stacks 77 stack <- take 1 stacks
@@ -132,6 +133,7 @@ transField (CDecl [CTypeSpec (CSUType (CStruct CStructTag mctyp Nothing [] _) _)
132 133
133transField _ = [] 134transField _ = []
134 135
136transpile :: CExternalDeclaration a -> Maybe (Q Dec)
135transpile (CDeclExt (CDecl [ CTypeSpec (CSUType 137transpile (CDeclExt (CDecl [ CTypeSpec (CSUType
136 (CStruct CStructTag mbIdent (Just fields) [] _) 138 (CStruct CStructTag mbIdent (Just fields) [] _)
137 _) ] 139 _) ]
@@ -158,6 +160,8 @@ data SymbolInformation c = SymbolInformation
158 } 160 }
159 deriving (Eq,Ord,Show,Functor) 161 deriving (Eq,Ord,Show,Functor)
160 162
163symbolInformation :: SymbolInformation
164 [CExternalDeclaration NodeInfo]
161symbolInformation = SymbolInformation 165symbolInformation = SymbolInformation
162 { symbolLocal = False 166 { symbolLocal = False
163 , symbolStatic = False 167 , symbolStatic = False
@@ -168,11 +172,16 @@ data Transpile c = Transpile
168 { syms :: Map String (SymbolInformation c) 172 { syms :: Map String (SymbolInformation c)
169 } 173 }
170 174
175initTranspile :: Transpile c
171initTranspile = Transpile 176initTranspile = Transpile
172 { syms = Map.empty 177 { syms = Map.empty
173 } 178 }
174 179
175-- grokSymbol :: CExternalDeclaration a -> String -> Maybe SymbolInformation -> Maybe SymbolInformation 180-- grokSymbol :: CExternalDeclaration a -> String -> Maybe SymbolInformation -> Maybe SymbolInformation
181grokSymbol :: CExternalDeclaration NodeInfo
182 -> p
183 -> Maybe (SymbolInformation [CExternalDeclaration NodeInfo])
184 -> Maybe (SymbolInformation [CExternalDeclaration NodeInfo])
176grokSymbol d k msi = 185grokSymbol d k msi =
177 let si = fromMaybe symbolInformation msi 186 let si = fromMaybe symbolInformation msi
178 in Just $ si 187 in Just $ si
@@ -200,6 +209,7 @@ hsMkName str = HS.UnQual () (foo () str)
200 foo = HS.Ident -- alternative: HS.Symbol 209 foo = HS.Ident -- alternative: HS.Symbol
201 210
202 211
212notKnown :: String -> Bool
203notKnown "Word8" = False 213notKnown "Word8" = False
204notKnown "Word16" = False 214notKnown "Word16" = False
205notKnown "Word32" = False 215notKnown "Word32" = False
@@ -292,6 +302,7 @@ transField (CDecl [CTypeSpec (CSUType (CStruct CStructTag mctyp Nothing [] _) _)
292hsTransField _ _ = [] 302hsTransField _ _ = []
293-} 303-}
294 304
305extractType :: Decl () -> HS.Type ()
295extractType (HS.TypeDecl _ _ ftyp) = ftyp 306extractType (HS.TypeDecl _ _ ftyp) = ftyp
296extractType (HS.TypeSig _ _ ftyp) = ftyp 307extractType (HS.TypeSig _ _ ftyp) = ftyp
297extractType _ = TyCon () (Special () (UnitCon ())) 308extractType _ = TyCon () (Special () (UnitCon ()))
@@ -326,6 +337,7 @@ tname :: HS.Type () -> String
326tname (HS.TyCon () (HS.UnQual () (HS.Ident () str))) = str 337tname (HS.TyCon () (HS.UnQual () (HS.Ident () str))) = str
327tname _ = "_unkonwn" 338tname _ = "_unkonwn"
328 339
340getPtrType :: HS.Type l -> Maybe (HS.Type l)
329getPtrType (HS.TyApp _ (HS.TyCon _ (HS.UnQual _ (HS.Ident _ "Ptr"))) x) = Just x 341getPtrType (HS.TyApp _ (HS.TyCon _ (HS.UnQual _ (HS.Ident _ "Ptr"))) x) = Just x
330getPtrType _ = Nothing 342getPtrType _ = Nothing
331 343
@@ -368,9 +380,11 @@ sigf f d = f (getReturnValue d) $ do
368 return $ CDeclr s [arg] Nothing [] (node d) 380 return $ CDeclr s [arg] Nothing [] (node d)
369-} 381-}
370 382
383body0 :: CExternalDeclaration a -> Maybe (CStatement a)
371body0 (CFDefExt (CFunDef rs cdeclr [] bdy _)) = Just bdy 384body0 (CFDefExt (CFunDef rs cdeclr [] bdy _)) = Just bdy
372body0 _ = Nothing 385body0 _ = Nothing
373 386
387body :: CExternalDeclaration a -> [CCompoundBlockItem a]
374body (CFDefExt (CFunDef rs cdeclr [] (CCompound [] bdy _) _)) = bdy 388body (CFDefExt (CFunDef rs cdeclr [] (CCompound [] bdy _) _)) = bdy
375body _ = [] 389body _ = []
376 390
@@ -379,6 +393,7 @@ data SideEffect = PointerWrite | FunctionCall
379calls :: Data t => t -> [CExpression NodeInfo] 393calls :: Data t => t -> [CExpression NodeInfo]
380calls = everything (++) (mkQ [] (\case { cc@C.CCall {} -> [cc] ; _ -> [] })) 394calls = everything (++) (mkQ [] (\case { cc@C.CCall {} -> [cc] ; _ -> [] }))
381 395
396mutations1 :: CExpression a -> [CExpression a]
382mutations1 e@(CAssign {}) = [e] 397mutations1 e@(CAssign {}) = [e]
383mutations1 e@(CUnary CPreIncOp _ _) = [e] 398mutations1 e@(CUnary CPreIncOp _ _) = [e]
384mutations1 e@(CUnary CPreDecOp _ _) = [e] 399mutations1 e@(CUnary CPreDecOp _ _) = [e]
@@ -410,6 +425,7 @@ data C2HaskellOptions = C2HaskellOptions
410 , preprocess :: Bool 425 , preprocess :: Bool
411 } 426 }
412 427
428defopts :: C2HaskellOptions
413defopts = C2HaskellOptions 429defopts = C2HaskellOptions
414 { selectFunction = Nothing 430 { selectFunction = Nothing
415 , prettyC = False 431 , prettyC = False
@@ -418,6 +434,7 @@ defopts = C2HaskellOptions
418 , preprocess = False 434 , preprocess = False
419 } 435 }
420 436
437parseOptions :: [String] -> C2HaskellOptions -> C2HaskellOptions
421parseOptions [] opts = opts 438parseOptions [] opts = opts
422parseOptions ("-f":f:args) opts = parseOptions args opts 439parseOptions ("-f":f:args) opts = parseOptions args opts
423 { selectFunction = Just f 440 { selectFunction = Just f
@@ -436,6 +453,8 @@ parseOptions ("-v":args) opts = parseOptions args opts
436 } 453 }
437parseOptions as x = error (show as) 454parseOptions as x = error (show as)
438 455
456tnames :: Show b =>
457 CExternalDeclaration b -> [(String, Maybe String)]
439tnames d = filter (notKnown . fst) $ map (first $ tname . unpointer) $ concatMap (\(t,c) -> map (,c) (types t)) $ sigf hsTransSig d 458tnames d = filter (notKnown . fst) $ map (first $ tname . unpointer) $ concatMap (\(t,c) -> map (,c) (types t)) $ sigf hsTransSig d
440 459
441 460
@@ -454,6 +473,7 @@ getsig (k,si) = do
454 s = sig d 473 s = sig d
455 [(ts,(k,s,d))] 474 [(ts,(k,s,d))]
456 475
476isAcceptableImport :: HS.Type l -> Bool
457isAcceptableImport (HS.TyFun _ (TyCon _ (UnQual _ (HS.Ident _ x))) xs) | not (notKnown x) = isAcceptableImport xs 477isAcceptableImport (HS.TyFun _ (TyCon _ (UnQual _ (HS.Ident _ x))) xs) | not (notKnown x) = isAcceptableImport xs
458isAcceptableImport (HS.TyFun _ (TyApp _ (TyCon _ (UnQual _ (HS.Ident _ "Ptr"))) x) xs) = isAcceptableImport xs 478isAcceptableImport (HS.TyFun _ (TyApp _ (TyCon _ (UnQual _ (HS.Ident _ "Ptr"))) x) xs) = isAcceptableImport xs
459isAcceptableImport (TyCon _ _) = True 479isAcceptableImport (TyCon _ _) = True
@@ -466,6 +486,7 @@ makeFunctionUseIO t@(TyApp a (TyCon b (UnQual c (HS.Ident d "IO"))) x) = t
466makeFunctionUseIO t = TyApp () (TyCon () (UnQual () (HS.Ident () "IO"))) t 486makeFunctionUseIO t = TyApp () (TyCon () (UnQual () (HS.Ident () "IO"))) t
467 487
468 488
489makeAcceptableImport :: HS.Type l -> HS.Type l
469makeAcceptableImport (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) xs) | not (notKnown x) 490makeAcceptableImport (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) xs) | not (notKnown x)
470 = (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) (makeAcceptableImport xs)) 491 = (HS.TyFun a (TyCon b (UnQual c (HS.Ident d x))) (makeAcceptableImport xs))
471makeAcceptableImport (HS.TyFun a (TyApp b (TyCon c (UnQual d (HS.Ident e "Ptr"))) x) xs) 492makeAcceptableImport (HS.TyFun a (TyApp b (TyCon c (UnQual d (HS.Ident e "Ptr"))) x) xs)
@@ -474,8 +495,10 @@ makeAcceptableImport (HS.TyFun a (TyCon c (UnQual d (HS.Ident e x))) xs)
474 = (HS.TyFun a (TyApp c (TyCon c (UnQual d (HS.Ident e "Ptr"))) (TyCon e (UnQual e (HS.Ident e x)))) (makeAcceptableImport xs)) 495 = (HS.TyFun a (TyApp c (TyCon c (UnQual d (HS.Ident e "Ptr"))) (TyCon e (UnQual e (HS.Ident e x)))) (makeAcceptableImport xs))
475makeAcceptableImport t = t 496makeAcceptableImport t = t
476 497
498enumCases :: CExternalDeclaration a
499 -> [(a, [(Ident, Maybe (CExpression a))])]
477enumCases (CDeclExt (CDecl xs _ ni)) = do 500enumCases (CDeclExt (CDecl xs _ ni)) = do
478 CTypeSpec (CEnumType (CEnum _ (Just cs))) <- xs 501 CTypeSpec (CEnumType (CEnum _ (Just cs) _ _) _) <- xs
479 return (ni,cs) 502 return (ni,cs)
480 503
481 504
@@ -614,6 +637,8 @@ makeStatic :: [CDeclarationSpecifier NodeInfo] -> [CDeclarationSpecifier NodeInf
614makeStatic xs = CStorageSpec (CStatic undefNode) : xs 637makeStatic xs = CStorageSpec (CStatic undefNode) : xs
615-- makeStatic xs = CStorageSpec (CStatic ()) : xs 638-- makeStatic xs = CStorageSpec (CStatic ()) : xs
616 639
640makePointer1 :: Maybe (CDeclarator NodeInfo)
641 -> Maybe (CDeclarator NodeInfo)
617makePointer1 (Just (CDeclr a bs c d e)) 642makePointer1 (Just (CDeclr a bs c d e))
618 = (Just (CDeclr a (p:bs) c d e)) 643 = (Just (CDeclr a (p:bs) c d e))
619 where 644 where
@@ -627,6 +652,8 @@ makePointer ((a,b,c):zs) = (makePointer1 a,b,c):zs
627setNull1 :: Maybe (CInitializer NodeInfo) 652setNull1 :: Maybe (CInitializer NodeInfo)
628setNull1 = Just (CInitExpr (CVar (C.Ident "NULL" 0 undefNode) undefNode) undefNode) 653setNull1 = Just (CInitExpr (CVar (C.Ident "NULL" 0 undefNode) undefNode) undefNode)
629 654
655setNull :: [(a, Maybe (CInitializer NodeInfo), c)]
656 -> [(a, Maybe (CInitializer NodeInfo), c)]
630setNull ((a,_,b):zs) = (a,setNull1,b):zs 657setNull ((a,_,b):zs) = (a,setNull1,b):zs
631 658
632makeFunctionPointer :: CExternalDeclaration NodeInfo 659makeFunctionPointer :: CExternalDeclaration NodeInfo
@@ -634,27 +661,47 @@ makeFunctionPointer :: CExternalDeclaration NodeInfo
634makeFunctionPointer d@(CDeclExt (CDecl xs ys pos)) = changeName ("f_"++) $ CDeclExt (CDecl (makeStatic xs) (setNull $ makePointer ys) pos) 661makeFunctionPointer d@(CDeclExt (CDecl xs ys pos)) = changeName ("f_"++) $ CDeclExt (CDecl (makeStatic xs) (setNull $ makePointer ys) pos)
635makeFunctionPointer d = d 662makeFunctionPointer d = d
636 663
664changeName2 :: (String -> String)
665 -> Maybe (CDeclarator a) -> Maybe (CDeclarator a)
637changeName2 f (Just (CDeclr (Just (C.Ident nm n p)) bs c d e)) 666changeName2 f (Just (CDeclr (Just (C.Ident nm n p)) bs c d e))
638 = (Just (CDeclr (Just (C.Ident (f nm) n p)) bs c d e)) 667 = (Just (CDeclr (Just (C.Ident (f nm) n p)) bs c d e))
639changeName2 f d = d 668changeName2 f d = d
640 669
670changeName1 :: (String -> String)
671 -> [(Maybe (CDeclarator a), b, c)]
672 -> [(Maybe (CDeclarator a), b, c)]
641changeName1 f ((a,b,c):zs) = (changeName2 f a,b,c):zs 673changeName1 f ((a,b,c):zs) = (changeName2 f a,b,c):zs
642 674
675changeName :: (String -> String)
676 -> CExternalDeclaration a -> CExternalDeclaration a
643changeName f d@(CDeclExt (CDecl xs ys pos)) = CDeclExt (CDecl xs (changeName1 f ys) pos) 677changeName f d@(CDeclExt (CDecl xs ys pos)) = CDeclExt (CDecl xs (changeName1 f ys) pos)
644changeName f d = d 678changeName f d = d
645 679
680makeAcceptableDecl :: Decl () -> Decl ()
646makeAcceptableDecl (HS.TypeDecl a (DHead b (HS.Ident c signame)) ftyp) 681makeAcceptableDecl (HS.TypeDecl a (DHead b (HS.Ident c signame)) ftyp)
647 = (HS.TypeDecl a (DHead b (HS.Ident c signame)) (makeFunctionUseIO $ makeAcceptableImport ftyp)) 682 = (HS.TypeDecl a (DHead b (HS.Ident c signame)) (makeFunctionUseIO $ makeAcceptableImport ftyp))
648makeAcceptableDecl (HS.TypeSig a b ftyp) = HS.TypeSig a b (makeFunctionUseIO $ makeAcceptableImport ftyp) 683makeAcceptableDecl (HS.TypeSig a b ftyp) = HS.TypeSig a b (makeFunctionUseIO $ makeAcceptableImport ftyp)
649 684
685makeSetter :: CExternalDeclaration NodeInfo
686 -> CExternalDeclaration NodeInfo
650makeSetter d = -- @(CDeclExt (CDecl xs ys pos)) = 687makeSetter d = -- @(CDeclExt (CDecl xs ys pos)) =
651 let name = concatMap identToString $ take 1 $ catMaybes $ sym d 688 let name = concatMap identToString $ take 1 $ catMaybes $ sym d
652 in setBody (setterBody ("f_"++name)) $ changeReturnValue (const voidReturnType) $ changeArgList (const voidp) $ changeName ("setf_"++) d 689 in setBody (setterBody ("f_"++name)) $ changeReturnValue (const voidReturnType) $ changeArgList (const voidp) $ changeName ("setf_"++) d
653 690
691changeArgList1 :: ([CDerivedDeclarator a]
692 -> [CDerivedDeclarator a])
693 -> CDeclarator a -> CDeclarator a
654changeArgList1 f (CDeclr a xs b c d) = CDeclr a (f xs) b c d 694changeArgList1 f (CDeclr a xs b c d) = CDeclr a (f xs) b c d
655 695
696changeArgList2 :: ([CDerivedDeclarator a]
697 -> [CDerivedDeclarator a])
698 -> [(Maybe (CDeclarator a), b, c)]
699 -> [(Maybe (CDeclarator a), b, c)]
656changeArgList2 f ((a,b,c):zs) = (changeArgList3 f a,b,c):zs 700changeArgList2 f ((a,b,c):zs) = (changeArgList3 f a,b,c):zs
657 701
702changeArgList3 :: ([CDerivedDeclarator a]
703 -> [CDerivedDeclarator a])
704 -> Maybe (CDeclarator a) -> Maybe (CDeclarator a)
658changeArgList3 f (Just (CDeclr a x b c d)) = Just (CDeclr a (f x) b c d) 705changeArgList3 f (Just (CDeclr a x b c d)) = Just (CDeclr a (f x) b c d)
659 706
660changeArgList :: ([CDerivedDeclarator a] -> [CDerivedDeclarator a]) 707changeArgList :: ([CDerivedDeclarator a] -> [CDerivedDeclarator a])
@@ -665,13 +712,19 @@ changeArgList f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl xs (changeArgLis
665setPosOfNode :: Position -> NodeInfo -> NodeInfo 712setPosOfNode :: Position -> NodeInfo -> NodeInfo
666setPosOfNode pos n = maybe (mkNodeInfoOnlyPos pos) (mkNodeInfo pos) $ nameOfNode n 713setPosOfNode pos n = maybe (mkNodeInfoOnlyPos pos) (mkNodeInfo pos) $ nameOfNode n
667 714
715setPos :: Position
716 -> CExternalDeclaration NodeInfo -> CExternalDeclaration NodeInfo
668setPos pos (CFDefExt (CFunDef xs ys zs c n)) = (CFDefExt (CFunDef xs ys zs c $ setPosOfNode pos n)) 717setPos pos (CFDefExt (CFunDef xs ys zs c n)) = (CFDefExt (CFunDef xs ys zs c $ setPosOfNode pos n))
669setPos pos (CDeclExt (CDecl xs ys n)) = (CDeclExt (CDecl xs ys $ setPosOfNode pos n)) 718setPos pos (CDeclExt (CDecl xs ys n)) = (CDeclExt (CDecl xs ys $ setPosOfNode pos n))
670 719
720getArgList1 :: CDeclarator a -> [CDerivedDeclarator a]
671getArgList1 (CDeclr a xs b c d) = xs 721getArgList1 (CDeclr a xs b c d) = xs
672 722
723getArgList2 :: [(Maybe (CDeclarator a), b, c)]
724 -> [CDerivedDeclarator a]
673getArgList2 ((a,b,c):zs) = getArgList3 a 725getArgList2 ((a,b,c):zs) = getArgList3 a
674 726
727getArgList3 :: Maybe (CDeclarator a) -> [CDerivedDeclarator a]
675getArgList3 (Just (CDeclr a [CPtrDeclr [] _] b c d)) = [] -- struct prototype, no fields. 728getArgList3 (Just (CDeclr a [CPtrDeclr [] _] b c d)) = [] -- struct prototype, no fields.
676getArgList3 (Just (CDeclr a x b c d)) = x 729getArgList3 (Just (CDeclr a x b c d)) = x
677 730
@@ -685,20 +738,30 @@ getArgList x = let v=getArgList_ x in trace ("getArgList ("++show (u x)++") = "+
685 u :: Functor f => f a -> f () 738 u :: Functor f => f a -> f ()
686 u = fmap (const ()) 739 u = fmap (const ())
687 740
741changeReturnValue :: ([CDeclarationSpecifier a]
742 -> [CDeclarationSpecifier a])
743 -> CExternalDeclaration a -> CExternalDeclaration a
688changeReturnValue f (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef (f xs) ys zs c d)) 744changeReturnValue f (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef (f xs) ys zs c d))
689changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys pos)) 745changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys pos))
690 746
747getReturnValue :: CExternalDeclaration a
748 -> [CDeclarationSpecifier a]
691getReturnValue (CFDefExt (CFunDef xs ys zs c d)) = xs 749getReturnValue (CFDefExt (CFunDef xs ys zs c d)) = xs
692getReturnValue (CDeclExt (CDecl xs ys pos)) = xs 750getReturnValue (CDeclExt (CDecl xs ys pos)) = xs
693 751
752voidReturnType :: [CDeclarationSpecifier NodeInfo]
694voidReturnType = [ CTypeSpec (CVoidType undefNode) ] 753voidReturnType = [ CTypeSpec (CVoidType undefNode) ]
695 754
755setBody :: CStatement a
756 -> CExternalDeclaration a -> CExternalDeclaration a
696setBody bdy (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef xs ys zs bdy d)) 757setBody bdy (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef xs ys zs bdy d))
697setBody bdy (CDeclExt (CDecl xs ys pos)) = (CFDefExt (CFunDef xs v [] bdy pos)) 758setBody bdy (CDeclExt (CDecl xs ys pos)) = (CFDefExt (CFunDef xs v [] bdy pos))
698 where v = case ys of 759 where v = case ys of
699 (Just y,_,_):_ -> y 760 (Just y,_,_):_ -> y
700 _ -> CDeclr Nothing [] Nothing [] pos 761 _ -> CDeclr Nothing [] Nothing [] pos
701 762
763makeStub :: CExternalDeclaration NodeInfo
764 -> CExternalDeclaration NodeInfo
702makeStub d = -- @(CDeclExt (CDecl xs ys pos)) = 765makeStub d = -- @(CDeclExt (CDecl xs ys pos)) =
703 let rval = case getReturnValue d of 766 let rval = case getReturnValue d of
704 [ CTypeSpec (CVoidType _) ] -> False -- void function. 767 [ CTypeSpec (CVoidType _) ] -> False -- void function.
@@ -762,6 +825,8 @@ voidp = [ CFunDeclr
762 where n = undefNode 825 where n = undefNode
763 826
764 827
828stubBody :: String
829 -> [CExpression NodeInfo] -> Bool -> String -> CStatement NodeInfo
765stubBody name vs rval msg = 830stubBody name vs rval msg =
766 CCompound [] 831 CCompound []
767 [ CBlockStmt 832 [ CBlockStmt
@@ -951,15 +1016,20 @@ newtype IncludeStack = IncludeStack
951 } 1016 }
952 deriving Show 1017 deriving Show
953 1018
1019emptyIncludes :: IncludeStack
954emptyIncludes = IncludeStack Map.empty 1020emptyIncludes = IncludeStack Map.empty
955 1021
1022openInclude :: FilePath
1023 -> [FilePath] -> IncludeStack -> IncludeStack
956openInclude fname stack (IncludeStack m) = IncludeStack $ Map.alter go fname m 1024openInclude fname stack (IncludeStack m) = IncludeStack $ Map.alter go fname m
957 where 1025 where
958 go Nothing = Just [stack] 1026 go Nothing = Just [stack]
959 go (Just s) = Just $ stack : s 1027 go (Just s) = Just $ stack : s
960 1028
1029findQuoted :: [Char] -> [Char]
961findQuoted xs = takeWhile (/='"') $ drop 1 $ dropWhile (/='"') xs 1030findQuoted xs = takeWhile (/='"') $ drop 1 $ dropWhile (/='"') xs
962 1031
1032includeStack :: B.ByteString -> IncludeStack
963includeStack bs = foldr go (const emptyIncludes) incs [] 1033includeStack bs = foldr go (const emptyIncludes) incs []
964 where 1034 where
965 incs = filter (\b -> fmap fst (B.uncons b) == Just '#') $ B.lines bs 1035 incs = filter (\b -> fmap fst (B.uncons b) == Just '#') $ B.lines bs