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