~jojo/Carth

ref: 55fb4f948f1f3797078b584dc60b4f7dd68b37ed Carth/app/Prebaked.hs -rw-r--r-- 788 bytes
55fb4f94JoJo Check `cast` in Infer instead of Gen 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Prebaked (readCompilerVersion) where

import Language.Haskell.TH
import Language.Haskell.TH.Syntax (Lift(..))
import Data.Maybe
import qualified Text.Megaparsec as M
import qualified Text.Megaparsec.Char as MC
import qualified Text.Megaparsec.Char.Lexer as ML
import Data.Void

type Parser = M.Parsec Void String

readCompilerVersion :: Q Exp
readCompilerVersion = do
    s <- runIO (readFile "carth.cabal")
    let (_, major, minor, patch) =
            head (catMaybes (map (M.parseMaybe pversion) (lines s)))
    lift (major, minor, patch)

pversion :: Parser (Int, Int, Int, Int)
pversion = do
    MC.string "version:" >> MC.space
    a <- num
    b <- dot >> num
    c <- dot >> num
    d <- dot >> num
    pure (a, b, c, d)
  where
    num = ML.decimal
    dot = MC.char '.'