aboutsummaryrefslogtreecommitdiffhomepage
path: root/scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs
blob: 9555536446ae71741a2b9dc647f709b201857a1f (plain) (blame)
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
47
{-# LANGUAGE OverloadedStrings #-}

module Text.Edifact.Scaffolder.Simples.Representation
  ( -- *
    extractRepresentation
  , representationParser
  ) where

import           Text.Edifact.Scaffolder.Commons
import           Text.Edifact.Scaffolder.Simples.Types

import           Text.Parsec                           as P (char, choice,
                                                             digit, many1,
                                                             option, optional,
                                                             space, string, try)
import           Text.Parsec.String                    (Parser)

extractRepresentation :: FilePath -> Scaffolding (Maybe Representation)
extractRepresentation file =
  let parser = skipBeginning representationParser
  in liftIO (readFile file) >>= maybeParse file parser

contentParser :: Parser Content
contentParser =
  choice [ AlphaNumeric <$ try (P.string "an")
         , Alpha        <$ P.string "a"
         , Numeric      <$ P.string "n"
         ]

cardinalityParser :: Parser Cardinality
cardinalityParser =
  option AnyNumber $
    choice [ Exactly <$> (optional space *> numberParser)
           , UpTo    <$> (dot *> dot *> numberParser)
           ]

numberParser :: Parser Int
numberParser = read <$> many1 digit

dot :: Parser Char
dot = P.char '.'

representationParser :: Parser Representation
representationParser =
  let parser = Representation <$> contentParser
                              <*> cardinalityParser
  in P.string "Repr:" *> space *> parser