Skip to content

Instantly share code, notes, and snippets.

@psibi
Created June 5, 2014 14:40
Show Gist options
  • Save psibi/b218c4681989f6e3e705 to your computer and use it in GitHub Desktop.
Save psibi/b218c4681989f6e3e705 to your computer and use it in GitHub Desktop.
Parser
{-# LANGUAGE BangPatterns #-}
import Data.Attoparsec.Text
import Control.Applicative ((*>),(<*))
import qualified Data.Text.IO as TL
import Data.Text
newtype VehicleId = VehicleId Int deriving Show
newtype Time = Time Int deriving Show
newtype LinkID = LinkID Int deriving Show
newtype NodeID = NodeID Int deriving Show
newtype LaneID = LaneID Int deriving Show
tab :: Parser Char
tab = char '\t'
data Snapshot = Snapshot {
vehicle :: !VehicleId,
time :: !Time,
link :: !LinkID,
node :: !NodeID,
lane :: !LaneID,
distance :: !Double,
velocity :: !Double,
vehtype :: !Int,
acceler :: !Double,
driver :: !Int,
passengers :: !Int,
easting :: !Double,
northing :: !Double,
elevation :: !Double,
azimuth :: !Double,
user :: !Int
} deriving (Show)
snapshotParser :: Parser Snapshot
snapshotParser = do
!sveh <- decimal
tab
!stime <- decimal
tab
!slink <- decimal
tab
!snode <- decimal
tab
!slane <- decimal
tab
!sdistance <- double
tab
!svelocity <- double
tab
!svehtype <- decimal
tab
!sacceler <- double
tab
!sdriver <- decimal
tab
!spassengers <- decimal
tab
!seasting <- double
tab
!snorthing <- double
tab
!selevation <- double
tab
!sazimuth <- double
tab
!suser <- decimal
endOfLine
return $ Snapshot (VehicleId sveh) (Time stime) (LinkID slink) (NodeID snode) (LaneID slane) sdistance svelocity svehtype sacceler sdriver spassengers seasting snorthing selevation sazimuth suser
doStuff :: Text -> [Snapshot] -> IO ()
doStuff text acc = case parse snapshotParser text of
Done !i !r -> doStuff i (r:acc)
Partial _ -> print "done stuff"
Fail i d msg-> print i
main :: IO ()
main = do
text <- TL.readFile "./Snapshot"
doStuff text []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment