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