trifecta

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{-# LANGUAGE OverloadedStrings #-}
module Book where

import           Control.Applicative
import           Text.Trifecta

data NumberOrString =
    NOSS String
  | NOSI Integer
  deriving (Show, Eq)

type Major = Integer
type Minor = Integer
type Patch = Integer
type Release = [NumberOrString]
type Metadata = [NumberOrString]

data SemVer = SemVer Major Minor Patch Release Metadata deriving (Eq, Show)

parseNoS :: Parser NumberOrString
parseNoS = do
  char '.'
  v <- (NOSI <$> decimal) <|> (NOSS <$> some letter)
  return v

parseSemVer :: Parser SemVer
parseSemVer = do
  major <- integer
  char '.'
  minor <- integer
  char '.'
  patch <- integer
  rel <- (char '-' *> many parseNoS) <|> return []
  meta <- (char '+' *> many parseNoS) <|> return []
  return $ SemVer major minor patch rel meta

main :: IO ()
main = return ()

{-
EXAMPLES

"0.1.1" -> Success (SemVer 0 1 1 [] [])
"1.0.0-beta.2" -> Success (SemVer 1 0 0 [NOSS "beta", NOSI 2] [])
"1.23.0-beta+exp.sha.5114f" -> Success (SemVer 1 23 0 [NOSS "beta"] [ NOSS "exp", NOSS "sha", NOSI 5114, NOSS "f"])
-}