-
-
Save psibi/b218c4681989f6e3e705 to your computer and use it in GitHub Desktop.
Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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