Debian Codegraph @

c2hs/c/CParserMonad.hs

raw symbols SCIP
1 -- C -> Haskell Compiler: Lexer for C Header Files
2 --
3 -- Author : Manuel M T Chakravarty, Duncan Coutts
4 -- Created: 12 February 2007
5 --
6 -- Copyright (c) [1999..2004] Manuel M T Chakravarty
7 -- Copyright (c) 2005-2007 Duncan Coutts
8 --
9 -- This file is free software; you can redistribute it and/or modify
10 -- it under the terms of the GNU General Public License as published by
11 -- the Free Software Foundation; either version 2 of the License, or
12 -- (at your option) any later version.
13 --
14 -- This file is distributed in the hope that it will be useful,
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 -- GNU General Public License for more details.
18 --
19 --- DESCRIPTION ---------------------------------------------------------------
20 --
21 -- Monad for the C lexer and parser
22 --
23 --- DOCU ----------------------------------------------------------------------
24 --
25 -- language: Haskell 98
26 --
27 -- This monad has to be usable with Alex and Happy. Some things in it are
28 -- dictated by that, eg having to be able to remember the last token.
29 --
30 -- The monad also provides a unique name supply (via the Names module)
31 --
32 -- For parsing C we have to maintain a set of identifiers that we know to be
33 -- typedef'ed type identifiers. We also must deal correctly with scope so we
34 -- keep a list of sets of identifiers so we can save the outer scope when we
35 -- enter an inner scope.
36 --
37 --- TODO ----------------------------------------------------------------------
38 --
39 --
40 {-# LANGUAGE CPP #-}
41
42 module CParserMonad (
43 P,
44 execParser,
45 failP,
46 getNewName, -- :: P Name
47 addTypedef, -- :: Ident -> P ()
48 shadowTypedef, -- :: Ident -> P ()
49 isTypeIdent, -- :: Ident -> P Bool
50 enterScope, -- :: P ()
51 leaveScope, -- :: P ()
52 setPos, -- :: Position -> P ()
53 getPos, -- :: P Position
54 getInput, -- :: P String
55 setInput, -- :: String -> P ()
56 getLastToken, -- :: P CToken
57 setLastToken, -- :: CToken -> P ()
58 ) where
59
60 import Position (Position(..), Pos(posOf))
61 import Errors (interr)
62 import UNames (Name)
63 import Idents (Ident, lexemeToIdent, identToLexeme)
64
65 import Control.Applicative (Applicative(..))
66 import Control.Monad (liftM, ap)
67 import Control.Monad.Fail (MonadFail (..))
68 import Data.Set (Set)
69 import qualified Data.Set as Set (fromList, insert, member, delete)
70
71 import CTokens (CToken)
72
73 data ParseResult a
74 = POk !PState a
75 | PFailed [String] Position -- The error message and position
76
77 data PState = PState {
78 curPos :: !Position, -- position at current input location
79 curInput :: !String, -- the current input
80 prevToken :: CToken, -- the previous token
81 namesupply :: ![Name], -- the name unique supply
82 tyidents :: !(Set Ident), -- the set of typedef'ed identifiers
83 scopes :: ![Set Ident] -- the tyident sets for outer scopes
84 }
85
86 newtype P a = P { unP :: PState -> ParseResult a }
87
88 instance MonadFail P where
89 fail m = getPos >>= \pos -> failP pos [m]
90
91 instance Functor P where
92 fmap = liftM
93
94 instance Applicative P where
95 pure = returnP
96 (<*>) = ap
97
98 instance Monad P where
99 return = pure
100 (>>=) = thenP
101
102 #if !MIN_VERSION_base(4,13,0)
103 fail m = getPos >>= \pos -> failP pos [m]
104 #endif
105
106 execParser :: P a -> String -> Position -> [Ident] -> [Name]
107 -> Either a ([String], Position)
108 execParser (P parser) input pos builtins names =
109 case parser initialState of
110 POk _ result -> Left result
111 PFailed message pos -> Right (message, pos)
112 where initialState = PState {
113 curPos = pos,
114 curInput = input,
115 prevToken = interr "CLexer.execParser: Touched undefined token!",
116 namesupply = names,
117 tyidents = Set.fromList builtins,
118 scopes = []
119 }
120
121 {-# INLINE returnP #-}
122 returnP :: a -> P a
123 returnP a = P $ \s -> POk s a
124
125 {-# INLINE thenP #-}
126 thenP :: P a -> (a -> P b) -> P b
127 (P m) `thenP` k = P $ \s ->
128 case m s of
129 POk s' a -> (unP (k a)) s'
130 PFailed err pos -> PFailed err pos
131
132 failP :: Position -> [String] -> P a
133 failP pos msg = P $ \_ -> PFailed msg pos
134
135 getNewName :: P Name
136 getNewName = P $ \s@PState{namesupply=(n:ns)} -> POk s{namesupply=ns} n
137
138 setPos :: Position -> P ()
139 setPos pos = P $ \s -> POk s{curPos=pos} ()
140
141 getPos :: P Position
142 getPos = P $ \s@PState{curPos=pos} -> POk s pos
143
144 addTypedef :: Ident -> P ()
145 addTypedef ident = (P $ \s@PState{tyidents=tyidents} ->
146 POk s{tyidents = ident `Set.insert` tyidents} ())
147
148 shadowTypedef :: Ident -> P ()
149 shadowTypedef ident = (P $ \s@PState{tyidents=tyidents} ->
150 -- optimisation: mostly the ident will not be in
151 -- the tyident set so do a member lookup to avoid
152 -- churn induced by calling delete
153 POk s{tyidents = if ident `Set.member` tyidents
154 then ident `Set.delete` tyidents
155 else tyidents } ())
156
157 isTypeIdent :: Ident -> P Bool
158 isTypeIdent ident = P $ \s@PState{tyidents=tyidents} ->
159 POk s $! Set.member ident tyidents
160
161 enterScope :: P ()
162 enterScope = P $ \s@PState{tyidents=tyidents,scopes=ss} ->
163 POk s{scopes=tyidents:ss} ()
164
165 leaveScope :: P ()
166 leaveScope = P $ \s@PState{scopes=ss} ->
167 case ss of
168 [] -> interr "leaveScope: already in global scope"
169 (tyidents:ss') -> POk s{tyidents=tyidents, scopes=ss'} ()
170
171 getInput :: P String
172 getInput = P $ \s@PState{curInput=i} -> POk s i
173
174 setInput :: String -> P ()
175 setInput i = P $ \s -> POk s{curInput=i} ()
176
177 getLastToken :: P CToken
178 getLastToken = P $ \s@PState{prevToken=tok} -> POk s tok
179
180 setLastToken :: CToken -> P ()
181 setLastToken tok = P $ \s -> POk s{prevToken=tok} ()