Separate NondecreasingIndentation out into its own extension
[ghc-hetmet.git] / compiler / parser / Lexer.x
index e52880b..6b73981 100644 (file)
@@ -211,7 +211,7 @@ $tab+         { warn Opt_WarnTabs (text "Warning: Tab character") }
 -- context if the curly brace is missing.
 -- Careful! This stuff is quite delicate.
 <layout, layout_do> {
-  \{ / { notFollowedBy '-' }           { pop_and open_brace }
+  \{ / { notFollowedBy '-' }           { hopefully_open_brace }
        -- we might encounter {-# here, but {- has been handled already
   \n                                   ;
   ^\# (line)?                          { begin line_prag1 }
@@ -757,6 +757,19 @@ pop :: Action
 pop _span _buf _len = do _ <- popLexState
                          lexToken
 
+hopefully_open_brace :: Action
+hopefully_open_brace span buf len
+ = do relaxed <- extension relaxedLayout
+      ctx <- getContext
+      (AI l _) <- getInput
+      let offset = srcLocCol l
+          isOK = relaxed ||
+                 case ctx of
+                 Layout prev_off : _ -> prev_off < offset
+                 _                   -> True
+      if isOK then pop_and open_brace span buf len
+              else failSpanMsgP span (text "Missing block")
+
 pop_and :: Action -> Action
 pop_and act span buf len = do _ <- popLexState
                               act span buf len
@@ -1107,8 +1120,8 @@ new_layout_context strict span _buf _len = do
     (AI l _) <- getInput
     let offset = srcLocCol l
     ctx <- getContext
-    relaxed <- extension relaxedLayout
-    let strict' = strict || not relaxed
+    nondecreasing <- extension nondecreasingIndentation
+    let strict' = strict || not nondecreasing
     case ctx of
        Layout prev_off : _  | 
           (strict'     && prev_off >= offset  ||
@@ -1765,6 +1778,8 @@ alternativeLayoutRuleBit :: Int
 alternativeLayoutRuleBit = 23
 relaxedLayoutBit :: Int
 relaxedLayoutBit = 24
+nondecreasingIndentationBit :: Int
+nondecreasingIndentationBit = 25
 
 always :: Int -> Bool
 always           _     = True
@@ -1810,6 +1825,8 @@ alternativeLayoutRule :: Int -> Bool
 alternativeLayoutRule flags = testBit flags alternativeLayoutRuleBit
 relaxedLayout :: Int -> Bool
 relaxedLayout flags = testBit flags relaxedLayoutBit
+nondecreasingIndentation :: Int -> Bool
+nondecreasingIndentation flags = testBit flags nondecreasingIndentationBit
 
 -- PState for parsing options pragmas
 --
@@ -1864,6 +1881,7 @@ mkPState flags buf loc =
                .|. newQualOpsBit `setBitIf` xopt Opt_NewQualifiedOperators flags
                .|. alternativeLayoutRuleBit `setBitIf` xopt Opt_AlternativeLayoutRule flags
                .|. relaxedLayoutBit `setBitIf` xopt Opt_RelaxedLayout flags
+               .|. nondecreasingIndentationBit `setBitIf` xopt Opt_NondecreasingIndentation flags
       --
       setBitIf :: Int -> Bool -> Int
       b `setBitIf` cond | cond      = bit b