eeb639e3e23d7ef6438de2aa855265cdd47af181
[ghc-hetmet.git] / ghc / compiler / reader / PrefixSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[PrefixSyn]{``Prefix-form'' syntax}
5
6 This module contains an algebraic data type into which a prefix form
7 string from the current Haskell parser is converted.  Given in an
8 order that follows the \tr{Prefix_Form} document.
9
10 \begin{code}
11 module PrefixSyn (
12         RdrBinding(..),
13         RdrMatch(..),
14         SigConverter,
15         SrcFile,
16         SrcFun,
17         SrcLine,
18
19         readInteger
20     ) where
21
22 #include "HsVersions.h"
23
24 import HsSyn
25 import RdrHsSyn
26 import Panic            ( panic )
27 import Char             ( isDigit, ord )
28
29
30 --UNUSED: type RdrId   = RdrName
31 type SrcLine = Int
32 type SrcFile = FAST_STRING
33 type SrcFun  = RdrName
34 \end{code}
35
36 \begin{code}
37 data RdrBinding
38   = RdrNullBind
39   | RdrAndBindings      RdrBinding RdrBinding
40   | RdrTyClDecl         RdrNameTyClDecl
41   | RdrValBinding       RdrNameMonoBinds        -- Pattern or function binding
42   | RdrInstDecl         RdrNameInstDecl
43   | RdrDefaultDecl      RdrNameDefaultDecl
44   | RdrForeignDecl      RdrNameForeignDecl
45
46                         -- signatures are mysterious; we can't
47                         -- tell if its a Sig or a ClassOpSig,
48                         -- so we just save the pieces:
49   | RdrSig              RdrNameSig
50
51 type SigConverter = RdrNameSig -> RdrNameSig
52 \end{code}
53
54 \begin{code}
55 data RdrMatch
56   = RdrMatch
57              [RdrNamePat]
58              (Maybe RdrNameHsType)
59              RdrNameGRHSs
60 \end{code}
61
62 Unscramble strings representing oct/dec/hex integer literals:
63 \begin{code}
64 readInteger :: String -> Integer
65
66 readInteger ('-' : xs)       = - (readInteger xs)
67 readInteger ('0' : 'o' : xs) = chk (stoo 0 xs)
68 readInteger ('0' : 'x' : xs) = chk (stox 0 xs)
69 readInteger ['0']            = 0    -- efficiency shortcut?
70 readInteger ['1']            = 1    -- ditto?
71 readInteger xs               = chk (stoi 0 xs)
72
73 chk (i, "")   = i
74 chk (i, junk) = panic ("readInteger: junk after reading:"++junk)
75
76 stoo, stoi, stox :: Integer -> String -> (Integer, String)
77
78 stoo a (c:cs) | is_oct c  = stoo (a*8 + ord_ c - ord_0) cs
79 stoo a cs                 = (a, cs)
80
81 stoi a (c:cs) | isDigit c = stoi (a*10 + ord_ c - ord_0) cs
82 stoi a cs                 = (a, cs)
83
84 stox a (c:cs) | isDigit c = stox (a_16_ord_c - ord_0)      cs
85               | is_hex  c = stox (a_16_ord_c - ord_a + 10) cs
86               | is_Hex  c = stox (a_16_ord_c - ord_A + 10) cs
87               where a_16_ord_c = a*16 + ord_ c
88 stox a cs = (a, cs)
89
90 is_oct c = c >= '0' && c <= '7'
91 is_hex c = c >= 'a' && c <= 'f'
92 is_Hex c = c >= 'A' && c <= 'F'
93
94 ord_ c = toInteger (ord c)
95
96 ord_0, ord_a, ord_A :: Integer
97 ord_0 = ord_ '0'; ord_a = ord_ 'a'; ord_A = ord_ 'A'
98 \end{code}