aboutsummaryrefslogtreecommitdiffhomepage
path: root/scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs
diff options
context:
space:
mode:
Diffstat (limited to 'scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs')
-rw-r--r--scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs
new file mode 100644
index 0000000..9555536
--- /dev/null
+++ b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs
@@ -0,0 +1,47 @@
1{-# LANGUAGE OverloadedStrings #-}
2
3module Text.Edifact.Scaffolder.Simples.Representation
4 ( -- *
5 extractRepresentation
6 , representationParser
7 ) where
8
9import Text.Edifact.Scaffolder.Commons
10import Text.Edifact.Scaffolder.Simples.Types
11
12import Text.Parsec as P (char, choice,
13 digit, many1,
14 option, optional,
15 space, string, try)
16import Text.Parsec.String (Parser)
17
18extractRepresentation :: FilePath -> Scaffolding (Maybe Representation)
19extractRepresentation file =
20 let parser = skipBeginning representationParser
21 in liftIO (readFile file) >>= maybeParse file parser
22
23contentParser :: Parser Content
24contentParser =
25 choice [ AlphaNumeric <$ try (P.string "an")
26 , Alpha <$ P.string "a"
27 , Numeric <$ P.string "n"
28 ]
29
30cardinalityParser :: Parser Cardinality
31cardinalityParser =
32 option AnyNumber $
33 choice [ Exactly <$> (optional space *> numberParser)
34 , UpTo <$> (dot *> dot *> numberParser)
35 ]
36
37numberParser :: Parser Int
38numberParser = read <$> many1 digit
39
40dot :: Parser Char
41dot = P.char '.'
42
43representationParser :: Parser Representation
44representationParser =
45 let parser = Representation <$> contentParser
46 <*> cardinalityParser
47 in P.string "Repr:" *> space *> parser