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 0000000..90aaf33 Binary files /dev/null and b/hie.yaml differ diff --git a/resources/testAll.rosetta b/resources/testAll.rosetta index 29c327b..8721b95 100644 --- a/resources/testAll.rosetta +++ b/resources/testAll.rosetta @@ -1,3 +1,6 @@ +namespace cdm.main : <"Something"> +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