[project @ 1996-06-26 10:26:00 by partain]
[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 #include "HsVersions.h"
12
13 module PrefixSyn (
14         RdrBinding(..),
15         SYN_IE(RdrId),
16         RdrMatch(..),
17         SYN_IE(SigConverter),
18         SYN_IE(SrcFile),
19         SYN_IE(SrcFun),
20         SYN_IE(SrcLine),
21
22         readInteger
23     ) where
24
25 IMP_Ubiq()
26 IMPORT_1_3(Char(isDigit))
27
28 import HsSyn
29 import RdrHsSyn
30 import Util             ( panic )
31
32 #ifdef REALLY_HASKELL_1_3
33 ord = fromEnum :: Char -> Int
34 #endif
35
36 type RdrId   = RdrName
37 type SrcLine = Int
38 type SrcFile = FAST_STRING
39 type SrcFun  = RdrName
40 \end{code}
41
42 \begin{code}
43 data RdrBinding
44   = RdrNullBind
45   | RdrAndBindings      RdrBinding RdrBinding
46
47   | RdrTyDecl           RdrNameTyDecl
48   | RdrFunctionBinding  SrcLine [RdrMatch]
49   | RdrPatternBinding   SrcLine [RdrMatch]
50   | RdrClassDecl        RdrNameClassDecl
51   | RdrInstDecl         RdrNameInstDecl
52   | RdrDefaultDecl      RdrNameDefaultDecl
53
54                         -- signatures are mysterious; we can't
55                         -- tell if its a Sig or a ClassOpSig,
56                         -- so we just save the pieces:
57   | RdrTySig            [RdrName]           -- vars getting sigs
58                         RdrNamePolyType     -- the type
59                         SrcLoc
60
61   -- user pragmas come in in a Sig-ish way/form...
62   | RdrSpecValSig       [RdrNameSig]
63   | RdrInlineValSig     RdrNameSig
64   | RdrDeforestSig      RdrNameSig
65   | RdrMagicUnfoldingSig RdrNameSig
66   | RdrSpecInstSig      RdrNameSpecInstSig
67   | RdrSpecDataSig      RdrNameSpecDataSig
68
69 type SigConverter = RdrBinding {- a Sig -} -> [RdrNameSig]
70 \end{code}
71
72 \begin{code}
73 data RdrMatch
74   = RdrMatch_NoGuard
75              SrcLine SrcFun
76              RdrNamePat
77              RdrNameHsExpr
78              RdrBinding
79
80   | RdrMatch_Guards
81              SrcLine SrcFun
82              RdrNamePat
83              [(RdrNameHsExpr, RdrNameHsExpr)]
84              -- (guard,         expr)
85              RdrBinding
86 \end{code}
87
88 Unscramble strings representing oct/dec/hex integer literals:
89 \begin{code}
90 readInteger :: String -> Integer
91
92 readInteger ('-' : xs)       = - (readInteger xs)
93 readInteger ('0' : 'o' : xs) = chk (stoo 0 xs)
94 readInteger ('0' : 'x' : xs) = chk (stox 0 xs)
95 readInteger ['0']            = 0    -- efficiency shortcut?
96 readInteger ['1']            = 1    -- ditto?
97 readInteger xs               = chk (stoi 0 xs)
98
99 chk (i, "")   = i
100 chk (i, junk) = panic ("readInteger: junk after reading:"++junk)
101
102 stoo, stoi, stox :: Integer -> String -> (Integer, String)
103
104 stoo a (c:cs) | is_oct c  = stoo (a*8 + ord_ c - ord_0) cs
105 stoo a cs                 = (a, cs)
106
107 stoi a (c:cs) | isDigit c = stoi (a*10 + ord_ c - ord_0) cs
108 stoi a cs                 = (a, cs)
109
110 stox a (c:cs) | isDigit c = stox (a_16_ord_c - ord_0)      cs
111               | is_hex  c = stox (a_16_ord_c - ord_a + 10) cs
112               | is_Hex  c = stox (a_16_ord_c - ord_A + 10) cs
113               where a_16_ord_c = a*16 + ord_ c
114 stox a cs = (a, cs)
115
116 is_oct c = c >= '0' && c <= '7'
117 is_hex c = c >= 'a' && c <= 'f'
118 is_Hex c = c >= 'A' && c <= 'F'
119
120 ord_ c = toInteger (ord c)
121
122 ord_0, ord_a, ord_A :: Integer
123 ord_0 = ord_ '0'; ord_a = ord_ 'a'; ord_A = ord_ 'A'
124 \end{code}