1+ {-# LANGUAGE CPP #-}
12{-# OPTIONS_HADDOCK hide #-}
23-----------------------------------------------------------------------------
34-- |
@@ -18,7 +19,7 @@ module Language.Haskell.Exts.ParseMonad(
1819 -- * Generic Parsing
1920 Parseable (.. ),
2021 -- * Parsing
21- P , ParseResult (.. ), atSrcLoc , LexContext (.. ),
22+ P , ParseResult (.. ), atSrcLoc , LexContext (.. ), LayoutKind ( .. ),
2223 ParseMode (.. ), defaultParseMode , fromParseResult ,
2324 runParserWithMode , runParserWithModeComments , runParser ,
2425 getSrcLoc , pushCurrentContext , popContext ,
@@ -51,6 +52,10 @@ import Data.Semigroup (Semigroup(..))
5152-- To avoid import warnings for Control.Applicative, Data.Monoid, and Data.Semigroup
5253import Prelude
5354
55+ #ifdef DEBUG
56+ import Debug.Trace
57+ #endif
58+
5459-- | Class providing function for parsing at many different types.
5560--
5661-- Note that for convenience of implementation, the default methods have
@@ -115,22 +120,27 @@ instance ( Monoid m , Semigroup m) => Monoid (ParseResult m) where
115120data ParseStatus a = Ok ParseState a | Failed SrcLoc String
116121 deriving Show
117122
118- data LexContext = NoLayout | Layout Int
123+ data LayoutKind
124+ = BindLayout
125+ | StmtLayout
126+ deriving (Eq , Ord , Show )
127+
128+ data LexContext = NoLayout | Layout LayoutKind Int
119129 deriving (Eq ,Ord ,Show )
120130
121131data ExtContext = CodeCtxt | HarpCtxt | TagCtxt | ChildCtxt
122132 | CloseTagCtxt | CodeTagCtxt
123133 deriving (Eq ,Ord ,Show )
124134
125135type CtxtFlag = (Bool ,Bool )
126- -- (True,_) = We're in a do context.
127- -- (_, True)= Next token must be a virtual closing brace.
136+ -- (True, _) = We're in a do context.
137+ -- (_, True) = Next token must be a virtual closing brace.
128138
129139type ParseState = ([LexContext ],[[KnownExtension ]],[ExtContext ],CtxtFlag ,[Comment ])
130140
131141indentOfParseState :: ParseState -> Int
132- indentOfParseState (Layout n: _,_,_,_,_) = n
133- indentOfParseState _ = 0
142+ indentOfParseState (Layout _ n: _,_,_,_,_) = n
143+ indentOfParseState _ = 0
134144
135145-- | Static parameters governing a parse.
136146-- Note that the various parse functions in "Language.Haskell.Exts.Parser"
@@ -278,30 +288,35 @@ getModuleName = P $ \_i _x _y _l _ch s m ->
278288-- (So if the source loc is not to the right of the current indent, an
279289-- empty list {} will be inserted.)
280290
281- pushCurrentContext :: P ()
282- pushCurrentContext = do
291+ pushCurrentContext :: LayoutKind -> P ()
292+ pushCurrentContext layoutKind = do
283293 lc <- getSrcLoc
284294 indent <- currentIndent
285295 dob <- pullDoStatus
286296 let loc = srcColumn lc
287297 when (dob && loc < indent
288298 || not dob && loc <= indent) pushCtxtFlag
289- pushContext (Layout loc)
299+ pushContext (Layout layoutKind loc)
290300
291301currentIndent :: P Int
292302currentIndent = P $ \ _r _x _y _ _ stk _mode -> Ok stk (indentOfParseState stk)
293303
294304pushContext :: LexContext -> P ()
295305pushContext ctxt =
296- -- trace ("pushing lexical scope: " ++ show ctxt ++"\n") $
306+ #ifdef DEBUG
307+ trace (" pushing lexical scope: " ++ show ctxt) $
308+ #endif
297309 P $ \ _i _x _y _l _ (s, exts, e, p, c) _m -> Ok (ctxt: s, exts, e, p, c) ()
298310
299311popContext :: P ()
300312popContext = P $ \ _i _x _y loc _ stk _m ->
301- case stk of
302- (_: s, exts, e, p, c) -> -- trace ("popping lexical scope, context now "++show s ++ "\n") $
303- Ok (s, exts, e, p, c) ()
304- ([] ,_,_,_,_) -> Failed loc " Unexpected }" -- error "Internal error: empty context in popContext"
313+ case stk of
314+ (_: s, exts, e, p, c) ->
315+ #ifdef DEBUG
316+ trace (" popping lexical scope, context now " ++ show s) $
317+ #endif
318+ Ok (s, exts, e, p, c) ()
319+ ([] ,_,_,_,_) -> Failed loc " Unexpected }"
305320
306321{-
307322-- HaRP/Hsx
@@ -323,9 +338,13 @@ getExtensions = P $ \_i _x _y _l _ s m ->
323338
324339pushCtxtFlag :: P ()
325340pushCtxtFlag =
326- P $ \ _i _x _y _l _ (s, exts, e, (d,c), cs) _m -> case c of
327- False -> Ok (s, exts, e, (d,True ), cs) ()
328- _ -> error " Internal error: context flag already pushed"
341+ P $ \ _i _x _y _l _ (s, exts, e, (d,c), cs) _m ->
342+ #ifdef DEBUG
343+ trace " pushing context switch" $
344+ #endif
345+ case c of
346+ False -> Ok (s, exts, e, (d,True ), cs) ()
347+ _ -> error " Internal error: context flag already pushed"
329348
330349pullDoStatus :: P Bool
331350pullDoStatus = P $ \ _i _x _y _l _ (s, exts, e, (d,c), cs) _m -> Ok (s,exts,e,(False ,c),cs) d
@@ -364,6 +383,9 @@ instance Fail.MonadFail (Lex r) where
364383getInput :: Lex r String
365384getInput = Lex $ \ cont -> P $ \ r -> runP (cont r) r
366385
386+ parserL :: P a -> Lex r a
387+ parserL p = Lex (p >>= )
388+
367389-- | Discard some input characters (these must not include tabs or newlines).
368390
369391discard :: Int -> Lex r ()
@@ -480,16 +502,16 @@ setSrcLineL y = Lex $ \cont -> P $ \i x _ ->
480502 runP (cont () ) i x y
481503
482504pushContextL :: LexContext -> Lex a ()
483- pushContextL ctxt = Lex $ \ cont -> P $ \ r x y loc ch (stk, exts, e, pst, cs) ->
484- runP (cont () ) r x y loc ch (ctxt: stk, exts, e, pst, cs)
505+ pushContextL = parserL . pushContext
485506
486507popContextL :: String -> Lex a ()
487- popContextL _ = Lex $ \ cont -> P $ \ r x y loc ch stk m -> case stk of
488- (_: ctxt, exts, e, pst, cs) -> runP (cont () ) r x y loc ch (ctxt, exts, e, pst, cs) m
489- ([] , _, _, _, _) -> Failed loc " Unexpected }"
508+ popContextL _ = parserL popContext
490509
491510pullCtxtFlag :: Lex a Bool
492511pullCtxtFlag = Lex $ \ cont -> P $ \ r x y loc ch (ct, exts, e, (d,c), cs) ->
512+ #ifdef DEBUG
513+ trace " pulling context switch" $
514+ #endif
493515 runP (cont c) r x y loc ch (ct, exts, e, (d,False ), cs)
494516
495517
0 commit comments