6b15a0d685132cb0c4ff4a09ba656ac9acda7adb
[ghc-hetmet.git] / ghc / compiler / reader / PrefixSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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 BasicTypes       ( IfaceFlavour )
27 import Util             ( panic )
28 import SrcLoc           ( SrcLoc )
29 import Char             ( isDigit, ord )
30
31
32 --UNUSED: type RdrId   = RdrName
33 type SrcLine = Int
34 type SrcFile = FAST_STRING
35 type SrcFun  = RdrName
36 \end{code}
37
38 \begin{code}
39 data RdrBinding
40   = RdrNullBind
41   | RdrAndBindings      RdrBinding RdrBinding
42
43   | RdrTyDecl           RdrNameTyDecl
44   | RdrFunctionBinding  SrcLine [RdrMatch]
45   | RdrPatternBinding   SrcLine [RdrMatch]
46   | RdrClassDecl        RdrNameClassDecl
47   | RdrInstDecl         RdrNameInstDecl
48   | RdrDefaultDecl      RdrNameDefaultDecl
49   | RdrForeignDecl      RdrNameForeignDecl
50
51                         -- signatures are mysterious; we can't
52                         -- tell if its a Sig or a ClassOpSig,
53                         -- so we just save the pieces:
54   | RdrSig              RdrNameSig
55
56 type SigConverter = RdrNameSig -> RdrNameSig
57 \end{code}
58
59 \begin{code}
60 data RdrMatch
61   = RdrMatch_NoGuard
62              SrcLine SrcFun
63              RdrNamePat
64              RdrNameHsExpr
65              RdrBinding
66
67   | RdrMatch_Guards
68              SrcLine SrcFun
69              RdrNamePat
70              [([RdrNameStmt], RdrNameHsExpr)]
71              -- (guard,         expr)
72              RdrBinding
73 \end{code}
74
75 Unscramble strings representing oct/dec/hex integer literals:
76 \begin{code}
77 readInteger :: String -> Integer
78
79 readInteger ('-' : xs)       = - (readInteger xs)
80 readInteger ('0' : 'o' : xs) = chk (stoo 0 xs)
81 readInteger ('0' : 'x' : xs) = chk (stox 0 xs)
82 readInteger ['0']            = 0    -- efficiency shortcut?
83 readInteger ['1']            = 1    -- ditto?
84 readInteger xs               = chk (stoi 0 xs)
85
86 chk (i, "")   = i
87 chk (i, junk) = panic ("readInteger: junk after reading:"++junk)
88
89 stoo, stoi, stox :: Integer -> String -> (Integer, String)
90
91 stoo a (c:cs) | is_oct c  = stoo (a*8 + ord_ c - ord_0) cs
92 stoo a cs                 = (a, cs)
93
94 stoi a (c:cs) | isDigit c = stoi (a*10 + ord_ c - ord_0) cs
95 stoi a cs                 = (a, cs)
96
97 stox a (c:cs) | isDigit c = stox (a_16_ord_c - ord_0)      cs
98               | is_hex  c = stox (a_16_ord_c - ord_a + 10) cs
99               | is_Hex  c = stox (a_16_ord_c - ord_A + 10) cs
100               where a_16_ord_c = a*16 + ord_ c
101 stox a cs = (a, cs)
102
103 is_oct c = c >= '0' && c <= '7'
104 is_hex c = c >= 'a' && c <= 'f'
105 is_Hex c = c >= 'A' && c <= 'F'
106
107 ord_ c = toInteger (ord c)
108
109 ord_0, ord_a, ord_A :: Integer
110 ord_0 = ord_ '0'; ord_a = ord_ 'a'; ord_A = ord_ 'A'
111 \end{code}