Skip to content

Instantly share code, notes, and snippets.

@remysucre
Created November 6, 2017 04:37
Show Gist options
  • Save remysucre/0eae2ee40e4abb4c48a2c8b429eb5569 to your computer and use it in GitHub Desktop.
Save remysucre/0eae2ee40e4abb4c48a2c8b429eb5569 to your computer and use it in GitHub Desktop.
srcloc for bangs
{-# LANGUAGE BangPatterns #-}
module Main where
import Language.Haskell.Exts
import Language.Haskell.Exts.Syntax
import Data.Generics.Uniplate.DataOnly
main :: IO ()
main = do
ParseOk s <- parseFile "/Users/rem/exp/app/Main.hs"
let !locs = [ l | PBangPat l p <- universeBi s] :: [SrcSpanInfo]
print locs
@remysucre
Copy link
Author

This programs prints out a list of the location of the bang patterns in a file. When ran on itself, it prints out [SrcSpanInfo {srcInfoSpan = SrcSpan "/Users/rem/exp/app/Main.hs" 12 7 12 12, srcInfoPoints = [SrcSpan "/Users/rem/exp/app/Main.hs" 12 7 12 8]}], which means there's a bang around the pattern on line 12 from column 7 to column 12 (loc). Then to find out the exact location of the bang itself in the source just take the first column number and -1.

@remysucre
Copy link
Author

Nvm you don't need to -1, the first column is exactly where the bang is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment