]> git.immae.eu Git - github/fretlink/edi-parser.git/blob - scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs
Release code as open source
[github/fretlink/edi-parser.git] / scaffolder / src / Text / Edifact / Scaffolder / Simples / Representation.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 module Text.Edifact.Scaffolder.Simples.Representation
4 ( -- *
5 extractRepresentation
6 , representationParser
7 ) where
8
9 import Text.Edifact.Scaffolder.Commons
10 import Text.Edifact.Scaffolder.Simples.Types
11
12 import Text.Parsec as P (char, choice,
13 digit, many1,
14 option, optional,
15 space, string, try)
16 import Text.Parsec.String (Parser)
17
18 extractRepresentation :: FilePath -> Scaffolding (Maybe Representation)
19 extractRepresentation file =
20 let parser = skipBeginning representationParser
21 in liftIO (readFile file) >>= maybeParse file parser
22
23 contentParser :: Parser Content
24 contentParser =
25 choice [ AlphaNumeric <$ try (P.string "an")
26 , Alpha <$ P.string "a"
27 , Numeric <$ P.string "n"
28 ]
29
30 cardinalityParser :: Parser Cardinality
31 cardinalityParser =
32 option AnyNumber $
33 choice [ Exactly <$> (optional space *> numberParser)
34 , UpTo <$> (dot *> dot *> numberParser)
35 ]
36
37 numberParser :: Parser Int
38 numberParser = read <$> many1 digit
39
40 dot :: Parser Char
41 dot = P.char '.'
42
43 representationParser :: Parser Representation
44 representationParser =
45 let parser = Representation <$> contentParser
46 <*> cardinalityParser
47 in P.string "Repr:" *> space *> parser