mirror of
https://github.com/macocianradu/RosettaHaskellCompiler.git
synced 2026-03-18 13:00:08 +00:00
added headers to files
This commit is contained in:
@@ -39,6 +39,7 @@ library
|
||||
PrettyPrinter.Enum
|
||||
PrettyPrinter.Function
|
||||
PrettyPrinter.General
|
||||
PrettyPrinter.Header
|
||||
PrettyPrinter.RosettaObject
|
||||
PrettyPrinter.Type
|
||||
Semantic.ExpressionChecker
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -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
|
||||
|
||||
36
src/PrettyPrinter/Header.hs
Normal file
36
src/PrettyPrinter/Header.hs
Normal 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
|
||||
Reference in New Issue
Block a user