diff options
Diffstat (limited to 'scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs')
-rw-r--r-- | scaffolder/src/Text/Edifact/Scaffolder/Simples/Representation.hs | 47 |
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 | |||
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 | ||