From 50498e53d581c4d7a884d60497801e2a375add7a Mon Sep 17 00:00:00 2001 From: Macocian Adrian Radu <34056556+macocianradu@users.noreply.github.com> Date: Thu, 17 Feb 2022 23:26:47 +0100 Subject: [PATCH] added headers to files --- RosettaParser.cabal | 1 + app/Main.hs | 8 +++++--- hie.yaml | Bin 0 -> 496 bytes resources/testAll.rosetta | 3 +++ src/Parser/Header.hs | 7 ++++--- src/PrettyPrinter/Header.hs | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 hie.yaml create mode 100644 src/PrettyPrinter/Header.hs diff --git a/RosettaParser.cabal b/RosettaParser.cabal index 4be877a..9334cf4 100644 --- a/RosettaParser.cabal +++ b/RosettaParser.cabal @@ -39,6 +39,7 @@ library PrettyPrinter.Enum PrettyPrinter.Function PrettyPrinter.General + PrettyPrinter.Header PrettyPrinter.RosettaObject PrettyPrinter.Type Semantic.ExpressionChecker diff --git a/app/Main.hs b/app/Main.hs index e5afb21..e6ca9a8 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -19,6 +19,8 @@ import Model.Enum import Data.Either import Model.Header import Parser.Header +import PrettyPrinter.Header +import Data.Tuple (fst, snd) -- :set args resources/testAll.rosetta resources/Generated/testAll.hs -- :l resources/Generated/testAll.hs @@ -30,12 +32,12 @@ main = do case parse rosettaParser "" (Text.pack rosettaString) of Left errorBundle -> print (errorBundlePretty errorBundle) Right objs -> do - writeFile (args !! 1) (printObjects (definedTypes, definedFunctions) objs) + writeFile (args !! 1) (printHeader (fst objs) ++ printObjects (definedTypes, definedFunctions) (snd objs)) where -- |Adds all the function definitions from the file into the symbol table - definedFunctions = addNewFunctions (definedTypes, defaultMap) objs + definedFunctions = addNewFunctions (definedTypes, defaultMap) (snd objs) -- |Adds all the new data types into the symbol table - definedTypes = addNewTypes [] objs + definedTypes = addNewTypes [] (snd objs) -- |Reads a rosetta string from the first input argument, parses that string and then writes a haskell output to the file given as a second argument printObjects :: ([Type], [Symbol]) -> [RosettaObject] -> String diff --git a/hie.yaml b/hie.yaml new file mode 100644 index 0000000000000000000000000000000000000000..90aaf332e66a93e3f5ac088eb0c793c349549cce GIT binary patch literal 496 zcmb`DF%H5o6hq&P#2u>cEqm{Pgt$Qom8d9fm1N-bfRhl!Qdp`eLu`NB`Ok;J(sE{? zqhZU2LdW5S8m?bhz$@KZjOQYzprmFewUxH~Ob2B+hKZZ>6QkTmO{JNm5GTaPl!N?i zd|UAYJ*T% +version "${version.ok}" + enum PeriodEnum: <"The enumerated values to specified the period, e.g. day, week."> D displayName "day" <"Day"> M displayName "month" <"Month"> diff --git a/src/Parser/Header.hs b/src/Parser/Header.hs index dd930bc..708b781 100644 --- a/src/Parser/Header.hs +++ b/src/Parser/Header.hs @@ -11,11 +11,12 @@ import Text.ParserCombinators.ReadP (many1) headerParser :: Parser Header headerParser = do _ <- lexeme $ string "namespace" - name <- namespaceParser + name <- lexeme namespaceParser + _ <- lexeme $ char ':' desc <- optional descriptionParser _ <- lexeme $ string "version" - vers <- between (char '\"') (char '\"') (many (letterChar <|> char '.' <|> char '$' <|> digitChar)) - imports <- many importParser + vers <- lexeme $ between (string "\"${") (string "}\"") (many (letterChar <|> char '.' <|> char '$' <|> digitChar)) + imports <- many $ lexeme importParser return $ MakeHeader name desc vers imports importParser :: Parser String diff --git a/src/PrettyPrinter/Header.hs b/src/PrettyPrinter/Header.hs new file mode 100644 index 0000000..7f5e401 --- /dev/null +++ b/src/PrettyPrinter/Header.hs @@ -0,0 +1,36 @@ +{-# LANGUAGE OverloadedStrings #-} + +module PrettyPrinter.Header where + +import Model.Header +import PrettyPrinter.General +import Prettyprinter +import Data.Char + +-- |Converts a Header into a haskell valid String +printHeader :: Header -> String +printHeader (MakeHeader name (Just description) _ imports) = + show $ vcat ["module" <+> pretty (convertFirst name) <+> "where", + enclose "{-" "-}" (pretty description), + emptyDoc, + vcat (map printImport imports), + emptyDoc] +printHeader (MakeHeader name Nothing _ imports) = + show $ vcat ["module" <+> pretty (convertFirst name) <+> "where", + emptyDoc, + vcat (map printImport imports), + emptyDoc] + +-- |Converts an import name into an import prettyprinter doc +printImport :: String -> Doc a +printImport name = "import" <+> pretty name + +convertName :: String -> String +convertName [] = [] +convertName (c:cs) + | c == '.' = c : convertFirst cs + | otherwise = c : convertName cs + +convertFirst :: String -> String +convertFirst [] = [] +convertFirst (c:cs) = toUpper c : convertName cs \ No newline at end of file