haskell / happy

The Happy parser generator for Haskell
Other
291 stars 84 forks source link

Code blocks should strip alignment of opening brace #305

Open sgraf812 opened 2 months ago

sgraf812 commented 2 months ago

Motivated by https://github.com/haskell/happy/issues/303, I think we should offer a command-line flag such that a code block

         | 'pattern' pattern_synonym_lhs '<-' pat_syn_pat where_decls
             {% do { let (name, args, as) = $2
                   ; mg <- mkPatSynMatchGroup name $51 }

is turned into

 do { let (name, args, as) = $2
    ; mg <- mkPatSynMatchGroup name $51 }

instead of

 do { let (name, args, as) = $2
                   ; mg <- mkPatSynMatchGroup name $51 }

That is, try and trim a whitespace prefix from subsequent lines that corresponds to the alignment of {%.

I think we can make code blocks entirely insensitive to indentation by wrapping them in do{...}. Example:

x :: Int
x = do{ 42 } -- works on pure expressions as well

-- code block: {%do putStrLn "this is the"
--                  putStrLn "code block"}

main =                do
            putStrLn "hi"
            do{
do putStrLn "this is the"
   putStrLn "code block"}
            putStrLn "end"