Skip to content

Commit 9bc7866

Browse files
committed
[ generated .hs ] nicer formatting of alex_action_n
Trim surrounding whitespace, separate by newlines. Input (.x): "a" { \ _ _ -> A } "" { \ _ _ -> Null } Before (.hs): (~ = space) alex_action_0 :: AlexPosn -> String -> Token alex_action_0 = ~\ _ _ -> A~~~~ alex_action_1 :: AlexPosn -> String -> Token alex_action_1 = ~\ _ _ -> Null~ After (.hs): <BLANKLINE> alex_action_0 :: AlexPosn -> String -> Token alex_action_0 = \ _ _ -> A <BLANKLINE> alex_action_1 :: AlexPosn -> String -> Token alex_action_1 = \ _ _ -> Null <BLANKLINE>
1 parent aa6e7c8 commit 9bc7866

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

src/AbsSyn.hs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ encodeStartCodes scan = (scan', 0 : map snd name_code_pairs, sc_hdr)
320320
-- generated file.
321321

322322
extractActions :: Scheme -> Scanner -> (Scanner,ShowS)
323-
extractActions scheme scanner = (scanner{scannerTokens = new_tokens}, decl_str)
323+
extractActions scheme scanner = (scanner{scannerTokens = new_tokens}, decl_str . nl)
324324
where
325325
(new_tokens, decls) = unzip (zipWith f (scannerTokens scanner) act_names)
326326

@@ -333,61 +333,57 @@ extractActions scheme scanner = (scanner{scannerTokens = new_tokens}, decl_str)
333333
str "AlexPosn -> Char -> String -> Int -> ((Int, state) -> "
334334
. str res . str ") -> (Int, state) -> " . str res
335335

336-
mkDecl fun code = case scheme of
337-
Default { defaultTypeInfo = Just (Nothing, actionty) } ->
338-
str fun . str " :: " . str actionty . str "\n"
339-
. str fun . str " = " . str code . nl
340-
Default { defaultTypeInfo = Just (Just tyclasses, actionty) } ->
336+
mkDecl fun code = mkTySig fun
337+
. mkDef fun code
338+
339+
mkDef fun code = str fun . str " = " . str code . nl
340+
341+
mkTySig fun = case scheme of
342+
Default { defaultTypeInfo = Just (Nothing, actionty) } -> nl .
343+
str fun . str " :: " . str actionty . nl
344+
Default { defaultTypeInfo = Just (Just tyclasses, actionty) } -> nl .
341345
str fun . str " :: (" . str tyclasses . str ") => " .
342-
str actionty . str "\n" .
343-
str fun . str " = " . str code . nl
344-
GScan { gscanTypeInfo = Just (Nothing, tokenty) } ->
345-
str fun . str " :: " . gscanActionType tokenty . str "\n"
346-
. str fun . str " = " . str code . nl
347-
GScan { gscanTypeInfo = Just (Just tyclasses, tokenty) } ->
346+
str actionty . nl
347+
GScan { gscanTypeInfo = Just (Nothing, tokenty) } -> nl .
348+
str fun . str " :: " . gscanActionType tokenty . nl
349+
GScan { gscanTypeInfo = Just (Just tyclasses, tokenty) } -> nl .
348350
str fun . str " :: (" . str tyclasses . str ") => " .
349-
gscanActionType tokenty . str "\n" .
350-
str fun . str " = " . str code . nl
351-
Basic { basicStrType = strty, basicTypeInfo = Just (Nothing, tokenty) } ->
351+
gscanActionType tokenty . nl
352+
Basic { basicStrType = strty, basicTypeInfo = Just (Nothing, tokenty) } -> nl .
352353
str fun . str " :: " . str (show strty) . str " -> "
353-
. str tokenty . str "\n"
354-
. str fun . str " = " . str code . nl
354+
. str tokenty . nl
355355
Basic { basicStrType = strty,
356-
basicTypeInfo = Just (Just tyclasses, tokenty) } ->
356+
basicTypeInfo = Just (Just tyclasses, tokenty) } -> nl .
357357
str fun . str " :: (" . str tyclasses . str ") => " .
358-
str (show strty) . str " -> " . str tokenty . str "\n" .
359-
str fun . str " = " . str code . nl
358+
str (show strty) . str " -> " . str tokenty . nl
360359
Posn { posnByteString = isByteString,
361-
posnTypeInfo = Just (Nothing, tokenty) } ->
360+
posnTypeInfo = Just (Nothing, tokenty) } -> nl .
362361
str fun . str " :: AlexPosn -> " . str (strtype isByteString) . str " -> "
363-
. str tokenty . str "\n"
364-
. str fun . str " = " . str code . nl
362+
. str tokenty . nl
365363
Posn { posnByteString = isByteString,
366-
posnTypeInfo = Just (Just tyclasses, tokenty) } ->
364+
posnTypeInfo = Just (Just tyclasses, tokenty) } -> nl .
367365
str fun . str " :: (" . str tyclasses . str ") => AlexPosn -> " .
368-
str (strtype isByteString) . str " -> " . str tokenty . str "\n" .
369-
str fun . str " = " . str code . nl
366+
str (strtype isByteString) . str " -> " . str tokenty . nl
370367
Monad { monadByteString = isByteString,
371-
monadTypeInfo = Just (Nothing, tokenty) } ->
368+
monadTypeInfo = Just (Nothing, tokenty) } -> nl .
372369
let
373370
actintty = if isByteString then "Int64" else "Int"
374371
in
375372
str fun . str " :: AlexInput -> " . str actintty . str " -> Alex ("
376-
. str tokenty . str ")\n"
377-
. str fun . str " = " . str code . nl
373+
. str tokenty . str ")" . nl
378374
Monad { monadByteString = isByteString,
379-
monadTypeInfo = Just (Just tyclasses, tokenty) } ->
375+
monadTypeInfo = Just (Just tyclasses, tokenty) } -> nl .
380376
let
381377
actintty = if isByteString then "Int64" else "Int"
382378
in
383-
str fun . str " :: (" . str tyclasses . str ") => "
379+
str fun . str " :: (" . str tyclasses . str ") =>"
384380
. str " AlexInput -> " . str actintty
385-
. str " -> Alex (" . str tokenty . str ")\n"
386-
. str fun . str " = " . str code . nl
387-
_ -> str fun . str " = " . str code . nl
381+
. str " -> Alex (" . str tokenty . str ")" . nl
382+
_ -> id
388383

389384
act_names = map (\n -> "alex_action_" ++ show (n::Int)) [0..]
390385

386+
decl_str :: ShowS
391387
decl_str = foldr (.) id [ decl | Just decl <- decls ]
392388

393389
-- -----------------------------------------------------------------------------

src/Scan.x

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ code (p,_,_inp) _ = do
175175
go :: AlexInput -> Int -> String -> P Token
176176
go inp 0 cs = do
177177
setInput inp
178-
return (T p (CodeT (reverse (tail cs))))
178+
return $ T p $ CodeT $ triml $ reverse $ triml $ tail cs
179179
go inp n cs = do
180180
case alexGetChar inp of
181181
Nothing -> err inp
@@ -210,6 +210,8 @@ code (p,_,_inp) _ = do
210210

211211
err inp = do setInput inp; lexError "lexical error in code fragment"
212212

213+
triml = dropWhile isSpace
214+
213215
lexError :: String -> P a
214216
lexError s = do
215217
(_,_,_,input) <- getInput

0 commit comments

Comments
 (0)