Last active
August 23, 2020 19:49
-
-
Save edwintorok/b76b7118490840a6306d17aa64f86c35 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let prologue = | |
{| | |
{-# language DeriveGeneric #-} | |
module Parser where | |
import Data.Csv | |
import qualified Data.Vector as V | |
import qualified Data.ByteString.Char8 as BS | |
import qualified Data.ByteString.Lazy.Char8 as BSL | |
import GHC.Generics | |
import Control.Monad | |
import Text.Read(readEither) | |
import Data.Time.Calendar | |
instance FromField DayOfWeek where | |
parseField f = case readEither (BS.unpack f) of | |
Left errMsg -> fail errMsg | |
Right r -> pure r | |
|} | |
let epilogue = | |
{| | |
instance ToRecord InputRecord where | |
instance FromRecord InputRecord where | |
instance ToField DayOfWeek where | |
main = do | |
input <- BSL.readFile "input.csv" | |
either putStrLn (V.mapM_ (print :: InputRecord -> IO ())) $ decode HasHeader input | |
|} | |
let () = | |
print_endline prologue ; | |
print_endline {|data InputRecord = InputRecord { |} ; | |
let inpath = Sys.argv.(1) in | |
Csv.Rows.load ~has_header:true inpath | |
|> List.map Csv.Row.to_list | |
|> List.mapi (fun i -> function | |
| [field; typ; _parser] -> | |
let i = i + 2 in | |
let field = String.lowercase_ascii field in | |
Printf.sprintf "{-# LINE %d \"%s\" #-}\n" i inpath ^ | |
(if i > 2 then "," else "") ^ | |
Printf.sprintf "%s :: %s" field typ | |
| line -> | |
failwith ("Unknown line format: " ^ String.concat "," line)) | |
|> List.iter print_endline ; | |
print_endline {|} deriving (Generic, Show)|} ; | |
print_endline epilogue | |
Excellent. Now try to compile generated code with GHC, or generate parser in OCaml
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With this
dune
file:Generates this output: