diff options
author | Frédéric Menou <frederic.menou@fretlink.com> | 2016-12-08 10:19:15 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@fretlink.com> | 2022-05-17 18:01:51 +0200 |
commit | a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7 (patch) | |
tree | adf3186fdccaeef19151026cdfbd38a530cf9ecb /scaffolder/src/Text/Edifact/Scaffolder/Simples | |
download | edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.tar.gz edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.tar.zst edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.zip |
Diffstat (limited to 'scaffolder/src/Text/Edifact/Scaffolder/Simples')
5 files changed, 138 insertions, 0 deletions
diff --git a/scaffolder/src/Text/Edifact/Scaffolder/Simples/Elements.hs b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Elements.hs new file mode 100644 index 0000000..328a5d0 --- /dev/null +++ b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Elements.hs | |||
@@ -0,0 +1,26 @@ | |||
1 | module Text.Edifact.Scaffolder.Simples.Elements | ||
2 | ( listSimples | ||
3 | ) where | ||
4 | |||
5 | import Text.Edifact.Scaffolder.Commons | ||
6 | |||
7 | import Text.Parsec (digit, eof, oneOf, string, | ||
8 | (<?>)) | ||
9 | import Text.Parsec.String (Parser) | ||
10 | |||
11 | listSimples :: Scaffolding [ElementWithDefinition SimpleCode] | ||
12 | listSimples = listElements "simples" simpleCodeParser | ||
13 | |||
14 | simpleCodeParser :: Parser SimpleCode | ||
15 | simpleCodeParser = | ||
16 | let codeParser = | ||
17 | sequence [ oneOf ['1'..'9'] | ||
18 | , digit | ||
19 | , digit | ||
20 | , digit | ||
21 | ] | ||
22 | in | ||
23 | SimpleCode <$> codeParser | ||
24 | <* string ".txt" | ||
25 | <* eof | ||
26 | <?> "SimpleCode" | ||
diff --git a/scaffolder/src/Text/Edifact/Scaffolder/Simples/Implementation.hs b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Implementation.hs new file mode 100644 index 0000000..6cfb2ab --- /dev/null +++ b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Implementation.hs | |||
@@ -0,0 +1,23 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | |||
3 | module Text.Edifact.Scaffolder.Simples.Implementation | ||
4 | ( -- * | ||
5 | toImplementation | ||
6 | ) where | ||
7 | |||
8 | import Text.Edifact.Scaffolder.Commons | ||
9 | import Text.Edifact.Scaffolder.Simples.Types | ||
10 | |||
11 | import Formatting | ||
12 | |||
13 | toImplementation :: Representation -> Text | ||
14 | toImplementation (Representation content (UpTo n) ) = sformat (fContent % " `upTo` " % int) content n | ||
15 | toImplementation (Representation content (Exactly n)) = sformat (fContent % " `exactly` " % int) content n | ||
16 | toImplementation (Representation content AnyNumber ) = sformat ("many " % fContent) content | ||
17 | |||
18 | fContent :: Format t (Content -> t) | ||
19 | fContent = | ||
20 | let display AlphaNumeric = "alphaNumeric" | ||
21 | display Alpha = "alpha" | ||
22 | display Numeric = "numeric" | ||
23 | in mapf display stext | ||
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 | ||
diff --git a/scaffolder/src/Text/Edifact/Scaffolder/Simples/Specification.hs b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Specification.hs new file mode 100644 index 0000000..0651cbd --- /dev/null +++ b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Specification.hs | |||
@@ -0,0 +1,28 @@ | |||
1 | module Text.Edifact.Scaffolder.Simples.Specification | ||
2 | ( -- * | ||
3 | specificationParser | ||
4 | ) where | ||
5 | |||
6 | import Text.Edifact.Scaffolder.Commons | ||
7 | |||
8 | import Text.Parsec as P (anyChar, count, digit, | ||
9 | endOfLine, manyTill, | ||
10 | oneOf, skipMany, string, | ||
11 | try) | ||
12 | import Text.Parsec.String (Parser) | ||
13 | |||
14 | specificationParser :: Parser (SimpleCode, SimpleName) | ||
15 | specificationParser = scanUntil [ simpleParser ] | ||
16 | |||
17 | simpleParser :: Parser (SimpleCode, SimpleName) | ||
18 | simpleParser = do | ||
19 | _ <- count 3 (oneOf "+*#|-X ") | ||
20 | skipMany (string " ") | ||
21 | code <- simpleCodeParser | ||
22 | _ <- string " " | ||
23 | skipMany (string " ") | ||
24 | name <- SimpleName <$> manyTill anyChar (() <$ try endOfLine) | ||
25 | pure (code, name) | ||
26 | |||
27 | simpleCodeParser :: Parser SimpleCode | ||
28 | simpleCodeParser = fromString <$> count 4 digit | ||
diff --git a/scaffolder/src/Text/Edifact/Scaffolder/Simples/Types.hs b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Types.hs new file mode 100644 index 0000000..08b6ca5 --- /dev/null +++ b/scaffolder/src/Text/Edifact/Scaffolder/Simples/Types.hs | |||
@@ -0,0 +1,14 @@ | |||
1 | module Text.Edifact.Scaffolder.Simples.Types where | ||
2 | |||
3 | data Representation = Representation Content Cardinality | ||
4 | deriving Show | ||
5 | |||
6 | data Content = AlphaNumeric | ||
7 | | Alpha | ||
8 | | Numeric | ||
9 | deriving Show | ||
10 | |||
11 | data Cardinality = UpTo Int | ||
12 | | Exactly Int | ||
13 | | AnyNumber | ||
14 | deriving Show | ||