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