root/compiler/parser/ParserCoreUtils.hs

Revision b00e3a6c0a82a8af3238d677f798d812cd7fd49f, 1.3 KB (checked in by Ian Lynagh <igloo@…>, 18 months ago)

Replace uses of the old catch function with the new one

  • Property mode set to 100644
Line 
1module ParserCoreUtils where
2
3import Exception
4import System.IO
5
6data ParseResult a = OkP a | FailP String
7type P a = String -> Int -> ParseResult a
8
9thenP :: P a -> (a -> P b) -> P b
10m `thenP`  k = \ s l -> 
11  case m s l of 
12    OkP a -> k a s l
13    FailP s -> FailP s
14
15returnP :: a -> P a
16returnP m _ _ = OkP m
17
18failP :: String -> P a
19failP s s' _ = FailP (s ++ ":" ++ s')
20
21getCoreModuleName :: FilePath -> IO String
22getCoreModuleName fpath = 
23   catchIO (do
24     h  <- openFile fpath ReadMode
25     ls <- hGetContents h
26     let mo = findMod (words ls)
27      -- make sure we close up the file right away.
28     (length mo) `seq` return ()
29     hClose h
30     return mo)
31    (\ _ -> return "Main")
32 where
33   findMod [] = "Main"
34   -- TODO: this should just return the module name, without the package name
35   findMod ("%module":m:_) = m
36   findMod (_:xs) = findMod xs
37
38
39data Token =
40   TKmodule
41 | TKdata
42 | TKnewtype
43 | TKforall
44 | TKrec
45 | TKlet
46 | TKin
47 | TKcase
48 | TKof
49 | TKcast
50 | TKnote
51 | TKexternal
52 | TKlocal
53 | TKwild
54 | TKoparen
55 | TKcparen
56 | TKobrace
57 | TKcbrace
58 | TKhash
59 | TKeq
60 | TKcolon
61 | TKcoloncolon
62 | TKcoloneqcolon
63 | TKstar
64 | TKrarrow
65 | TKlambda
66 | TKat
67 | TKdot
68 | TKquestion
69 | TKsemicolon
70 | TKname String
71 | TKcname String
72 | TKinteger Integer
73 | TKrational Rational
74 | TKstring String
75 | TKchar Char
76 | TKEOF
Note: See TracBrowser for help on using the browser.