remove empty dir
[ghc-hetmet.git] / ghc / compiler / parser / ParserCoreUtils.hs
1 module ParserCoreUtils where
2
3 import IO 
4
5 data ParseResult a = OkP a | FailP String
6 type P a = String -> Int -> ParseResult a
7
8 thenP :: P a -> (a -> P b) -> P b
9 m `thenP`  k = \ s l -> 
10   case m s l of 
11     OkP a -> k a s l
12     FailP s -> FailP s
13
14 returnP :: a -> P a
15 returnP m _ _ = OkP m
16
17 failP :: String -> P a
18 failP s s' _ = FailP (s ++ ":" ++ s')
19
20 getCoreModuleName :: FilePath -> IO String
21 getCoreModuleName fpath = 
22    catch (do 
23      h  <- openFile fpath ReadMode
24      ls <- hGetContents h
25      let mo = findMod (words ls)
26       -- make sure we close up the file right away.
27      (length mo) `seq` return ()
28      hClose h
29      return mo)
30     (\ _ -> return "Main")
31  where
32    findMod [] = "Main"
33    findMod ("%module":m:_) = m
34    findMod (_:xs) = findMod xs
35
36
37 data Token =
38    TKmodule
39  | TKdata
40  | TKnewtype
41  | TKforall
42  | TKrec
43  | TKlet
44  | TKin
45  | TKcase
46  | TKof
47  | TKcoerce
48  | TKnote
49  | TKexternal
50  | TKwild
51  | TKoparen
52  | TKcparen
53  | TKobrace
54  | TKcbrace
55  | TKhash
56  | TKeq
57  | TKcoloncolon
58  | TKstar
59  | TKrarrow
60  | TKlambda
61  | TKat
62  | TKdot
63  | TKquestion
64  | TKsemicolon
65  | TKname String
66  | TKcname String
67  | TKinteger Integer
68  | TKrational Rational
69  | TKstring String
70  | TKchar Char
71  | TKEOF
72