added headers to files

This commit is contained in:
Macocian Adrian Radu
2022-02-17 23:26:47 +01:00
parent 8743dc0874
commit 50498e53d5
6 changed files with 49 additions and 6 deletions

View File

@@ -39,6 +39,7 @@ library
PrettyPrinter.Enum PrettyPrinter.Enum
PrettyPrinter.Function PrettyPrinter.Function
PrettyPrinter.General PrettyPrinter.General
PrettyPrinter.Header
PrettyPrinter.RosettaObject PrettyPrinter.RosettaObject
PrettyPrinter.Type PrettyPrinter.Type
Semantic.ExpressionChecker Semantic.ExpressionChecker

View File

@@ -19,6 +19,8 @@ import Model.Enum
import Data.Either import Data.Either
import Model.Header import Model.Header
import Parser.Header import Parser.Header
import PrettyPrinter.Header
import Data.Tuple (fst, snd)
-- :set args resources/testAll.rosetta resources/Generated/testAll.hs -- :set args resources/testAll.rosetta resources/Generated/testAll.hs
-- :l resources/Generated/testAll.hs -- :l resources/Generated/testAll.hs
@@ -30,12 +32,12 @@ main = do
case parse rosettaParser "" (Text.pack rosettaString) of case parse rosettaParser "" (Text.pack rosettaString) of
Left errorBundle -> print (errorBundlePretty errorBundle) Left errorBundle -> print (errorBundlePretty errorBundle)
Right objs -> do Right objs -> do
writeFile (args !! 1) (printObjects (definedTypes, definedFunctions) objs) writeFile (args !! 1) (printHeader (fst objs) ++ printObjects (definedTypes, definedFunctions) (snd objs))
where where
-- |Adds all the function definitions from the file into the symbol table -- |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 -- |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 -- |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 printObjects :: ([Type], [Symbol]) -> [RosettaObject] -> String

BIN
hie.yaml Normal file

Binary file not shown.

View File

@@ -1,3 +1,6 @@
namespace cdm.main : <"Something">
version "${version.ok}"
enum PeriodEnum: <"The enumerated values to specified the period, e.g. day, week."> enum PeriodEnum: <"The enumerated values to specified the period, e.g. day, week.">
D displayName "day" <"Day"> D displayName "day" <"Day">
M displayName "month" <"Month"> M displayName "month" <"Month">

View File

@@ -11,11 +11,12 @@ import Text.ParserCombinators.ReadP (many1)
headerParser :: Parser Header headerParser :: Parser Header
headerParser = do headerParser = do
_ <- lexeme $ string "namespace" _ <- lexeme $ string "namespace"
name <- namespaceParser name <- lexeme namespaceParser
_ <- lexeme $ char ':'
desc <- optional descriptionParser desc <- optional descriptionParser
_ <- lexeme $ string "version" _ <- lexeme $ string "version"
vers <- between (char '\"') (char '\"') (many (letterChar <|> char '.' <|> char '$' <|> digitChar)) vers <- lexeme $ between (string "\"${") (string "}\"") (many (letterChar <|> char '.' <|> char '$' <|> digitChar))
imports <- many importParser imports <- many $ lexeme importParser
return $ MakeHeader name desc vers imports return $ MakeHeader name desc vers imports
importParser :: Parser String importParser :: Parser String

View File

@@ -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