In [58]:
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}

import Text.Parsec

type Parser = Parsec String ()

parse' rule = parse rule "(source)"
In [59]:
data Feature
  = Feature
  { row        :: String
  , col        :: String
  , value      :: String
  , descriptor :: Maybe String
  } deriving (Show)

anythingUntil p = manyTill anyToken (p *> return ())

signal = string "$$"
kvSep = string ":"
descSep = string "|"

descP :: Parser (Maybe String)
descP = optionMaybe $ try $ manyTill anyToken descSep

featureP :: Parser Feature
featureP = do
  row <- anythingUntil signal
  desc <- descP
  col <- anythingUntil kvSep
  value <- manyTill anyToken eof
  Feature <$> return row <*> return col <*> return value <*> return desc



parseFeature :: String -> Feature
parseFeature input =
  case parse' featureP input of
    Left err -> error (show err)
    Right r -> r
In [60]:
input :: IO [String]
input = lines <$> readFile "./some_data.txt"
In [61]:
main = do
  f <- input
  let r = words $ unlines f
  return $ fmap parseFeature r
In [62]:
parseFeature "1$$stuff|1231234:1"
Feature {row = "1", col = "1231234", value = "1", descriptor = Just "stuff"}
In [63]:
main
[Feature {row = "8", col = "3462", value = "1", descriptor = Just "stuff"},Feature {row = "8", col = "10842", value = "1", descriptor = Nothing},Feature {row = "9", col = "something", value = "4.12", descriptor = Nothing},Feature {row = "7", col = "derp", value = "7.1", descriptor = Nothing},Feature {row = "8", col = "12", value = "1", descriptor = Nothing},Feature {row = "9", col = "something", value = "4.12", descriptor = Just "some_data"},Feature {row = "7", col = "derp", value = "7.1", descriptor = Nothing},Feature {row = "8", col = "123", value = "1", descriptor = Nothing},Feature {row = "8", col = "12842", value = "1", descriptor = Nothing},Feature {row = "9", col = "knows", value = "3.2", descriptor = Just "who"},Feature {row = "7", col = "derp", value = "7.1", descriptor = Nothing},Feature {row = "7", col = "thing", value = "0.1", descriptor = Nothing},Feature {row = "8", col = "1234", value = "1", descriptor = Nothing},Feature {row = "8", col = "13842", value = "1", descriptor = Nothing},Feature {row = "9", col = "somethin", value = "4.12", descriptor = Nothing},Feature {row = "7", col = "derr", value = "7.2", descriptor = Nothing}]
In [ ]: