[project @ 1999-06-01 16:15:42 by simonmar]
[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 RdrName          ( RdrName )
27 import Panic            ( panic )
28 import Char             ( isDigit, ord )
29
30
31 --UNUSED: type RdrId   = RdrName
32 type SrcLine = Int
33 type SrcFile = FAST_STRING
34 type SrcFun  = RdrName
35 \end{code}
36
37 \begin{code}
38 data RdrBinding
39   =     -- On input we use the Empty/And form rather than a list
40     RdrNullBind
41   | RdrAndBindings      RdrBinding RdrBinding
42
43         -- Value bindings havn't been united with their
44         -- signatures yet
45   | RdrValBinding       RdrNameMonoBinds
46
47         -- Signatures are mysterious; we can't
48         -- tell if its a Sig or a ClassOpSig,
49         -- so we just save the pieces:
50   | RdrSig              RdrNameSig
51
52         -- The remainder all fit into the main HsDecl form
53   | RdrHsDecl           RdrNameHsDecl
54
55 type SigConverter = RdrNameSig -> RdrNameSig
56 \end{code}
57
58 \begin{code}
59 data RdrMatch
60   = RdrMatch
61              [RdrNamePat]
62              (Maybe RdrNameHsType)
63              RdrNameGRHSs
64 \end{code}
65
66 Unscramble strings representing oct/dec/hex integer literals:
67 \begin{code}
68 readInteger :: String -> Integer
69
70 readInteger ('-' : xs)       = - (readInteger xs)
71 readInteger ('0' : 'o' : xs) = chk (stoo 0 xs)
72 readInteger ('0' : 'x' : xs) = chk (stox 0 xs)
73 readInteger ['0']            = 0    -- efficiency shortcut?
74 readInteger ['1']            = 1    -- ditto?
75 readInteger xs               = chk (stoi 0 xs)
76
77 chk (i, "")   = i
78 chk (i, junk) = panic ("readInteger: junk after reading:"++junk)
79
80 stoo, stoi, stox :: Integer -> String -> (Integer, String)
81
82 stoo a (c:cs) | is_oct c  = stoo (a*8 + ord_ c - ord_0) cs
83 stoo a cs                 = (a, cs)
84
85 stoi a (c:cs) | isDigit c = stoi (a*10 + ord_ c - ord_0) cs
86 stoi a cs                 = (a, cs)
87
88 stox a (c:cs) | isDigit c = stox (a_16_ord_c - ord_0)      cs
89               | is_hex  c = stox (a_16_ord_c - ord_a + 10) cs
90               | is_Hex  c = stox (a_16_ord_c - ord_A + 10) cs
91               where a_16_ord_c = a*16 + ord_ c
92 stox a cs = (a, cs)
93
94 is_oct c = c >= '0' && c <= '7'
95 is_hex c = c >= 'a' && c <= 'f'
96 is_Hex c = c >= 'A' && c <= 'F'
97
98 ord_ c = toInteger (ord c)
99
100 ord_0, ord_a, ord_A :: Integer
101 ord_0 = ord_ '0'; ord_a = ord_ 'a'; ord_A = ord_ 'A'
102 \end{code}