Added haddock documentation

This commit is contained in:
macocianradu
2021-11-30 22:33:44 +01:00
parent 70baa17a4e
commit ff25395b68
18 changed files with 136 additions and 73 deletions

View File

@@ -7,6 +7,8 @@ import Text.Megaparsec.Char
import Text.Megaparsec
import Model.Enum
-- |Parses a complete Rosetta enum into a EnumType
enumParser :: Parser EnumType
enumParser =
do
@@ -15,6 +17,8 @@ enumParser =
values <- some enumValueParser
return (MakeEnum eName eDescription values)
-- |Parses a Rosetta enum value into a EnumValue
enumValueParser :: Parser EnumValue
enumValueParser =
do
@@ -23,6 +27,8 @@ enumValueParser =
vDescription <- optional descriptionParser
return (MakeEnumValue vName vDescription dName)
-- |Parses the display name of a Rosetta enum value into a String
enumValueDisplayNameParser :: Parser String
enumValueDisplayNameParser =
do
@@ -30,6 +36,8 @@ enumValueDisplayNameParser =
_ <- char '"'
lexeme $ anySingle `manyTill` char '"'
-- |Parses the name of a Rosetta enum into a String
enumNameParser :: Parser String
enumNameParser =
do

View File

@@ -8,6 +8,8 @@ import qualified Data.Text as Text
import Text.Megaparsec
import Text.Megaparsec.Char
-- |Parses a complete Rosetta expression into an Expression type
expressionParser :: Parser Expression
expressionParser =
choice [ ifParser,
@@ -18,6 +20,7 @@ expressionParser =
-- Command Structures ----------------------
--------------------------------------------
-- |Parses a function call in Rosetta into an Expression
functionCallParser :: Parser Expression
functionCallParser =
do
@@ -30,6 +33,7 @@ functionCallParser =
Nothing -> return $ Function f []
Just at -> return $ Function f (ats ++ [at])
-- |Parses an if statement in Rosetta into an Expression
ifParser :: Parser Expression
ifParser =
do
@@ -42,6 +46,7 @@ ifParser =
Left _ -> return (IfSimple condition expr)
Right _ -> expressionParser >>= \expr2 -> return (IfElse condition expr expr2)
-- |Parses an expression between parentheses in Rosetta into an Expression
parens :: Parser a -> Parser a
parens = between (char '(') (char ')')
@@ -49,6 +54,7 @@ parens = between (char '(') (char ')')
-- Terminals -------------------------------
--------------------------------------------
-- |Parses a list in Rosetta into an Expression
listParser :: Parser Expression
listParser =
do
@@ -58,6 +64,7 @@ listParser =
_ <- lexeme $ char ']'
return $ List (expressions ++ [lastExpr])
-- |Parses a variable in Rosetta into an Expression
variableParser :: Parser Expression
variableParser =
do
@@ -65,18 +72,21 @@ variableParser =
inner <- many innerVariableParser
return $ Variable (var ++ concatMap ("->" ++) inner)
-- |Parses an inner variable (a -> b) in Rosetta into an Expression
innerVariableParser :: Parser String
innerVariableParser =
do
_ <- lexeme $ string "->"
camelNameParser
-- |Parses an integer in Rosetta into an Expression
integerParser :: Parser Expression
integerParser =
do
nr <- lexeme $ some digitChar
return $ Int nr
-- |Parses a real number in Rosetta into an Expression
decimalParser :: Parser Expression
decimalParser =
do
@@ -84,19 +94,22 @@ decimalParser =
_ <- char '.'
real <- lexeme $ many digitChar
return $ Real $ nr ++ "." ++ real
-- |Parses a boolean in Rosetta into an Expression
booleanParser :: Parser Expression
booleanParser =
do
bol <- lexeme (string "True" <|> string "False")
return $ Boolean $ Text.unpack bol
-- |Parses the empty statement in Rosetta into an Expression
emptyParser :: Parser Expression
emptyParser =
do
_ <- lexeme $ string "empty"
return Empty
-- |Parses any of the terminal statements in Rosetta into an Expression
terminalParser :: Parser Expression
terminalParser =
do
@@ -115,12 +128,18 @@ terminalParser =
-- Expressions -----------------------------
--------------------------------------------
-- |Parses an prefix function statement in Rosetta into an Expression
prefixParser :: Parser Expression
prefixParser =
do
op <- lexeme $ choice $ fmap (try . string . Text.pack) prefixOperators
PrefixExp (Text.unpack op) <$> expressionParser
-- |List of prefix operators
prefixOperators :: [String]
prefixOperators = ["-", "not"]
-- |Parses an equality statement in Rosetta into an Expression
eqParser :: Parser Expression
eqParser =
do
@@ -130,9 +149,11 @@ eqParser =
Left _ -> return s
Right o -> eqParser >>= \ex -> return $ InfixExp (Text.unpack o) s ex
-- |The list of equality statements in Rosetta
eqFunctions :: [String]
eqFunctions = ["=", "<", "<=", ">", ">=", "<>", "all =", "all <>", "any =", "any <>"]
-- |Parses a sum statement in Rosetta into an Expression
sumParser :: Parser Expression
sumParser =
do
@@ -142,6 +163,7 @@ sumParser =
Left _ -> return f
Right o -> sumParser >>= \ex -> return $ reverseExpression $ InfixExp [o] f ex
-- |Parses a multiplication or division statement in Rosetta into an Expression
factorParser :: Parser Expression
factorParser =
do
@@ -151,6 +173,7 @@ factorParser =
Left _ -> return p
Right o -> factorParser >>= \ex -> return $ reverseExpression $ InfixExp [o] p ex
-- |Parses a boolean statement in Rosetta into an Expression
boolOpParser :: Parser Expression
boolOpParser =
do
@@ -160,6 +183,7 @@ boolOpParser =
Left _ -> return p
Right o -> boolOpParser >>= \ex -> return $ InfixExp (Text.unpack o) p ex
-- |Parses a power statement in Rosetta into an Expression
powerParser :: Parser Expression
powerParser =
do
@@ -168,7 +192,8 @@ powerParser =
case op of
Left _ -> return p
Right _ -> powerParser >>= \ex -> return $ InfixExp "^" p ex
-- |Parses a postfix function in Rosetta into an Expression
postfixParser :: Parser Expression
postfixParser =
do
@@ -178,6 +203,7 @@ postfixParser =
Left _ -> return t
Right o -> return $ PostfixExp (Text.unpack o) t
-- |The list of existing postfix Rosetta functions
postfixFunctions :: [String]
postfixFunctions = ["exists", "is absent", "count", "only-element", "single exists", "multiple exists"]
@@ -185,12 +211,15 @@ postfixFunctions = ["exists", "is absent", "count", "only-element", "single exis
-- Auxiliary ------------------------------
--------------------------------------------
-- |Reverses the order of operations for left-associative functions
reverseExpression :: Expression -> Expression
reverseExpression (InfixExp op t1 (InfixExp op2 t2 e))
| precedence op == precedence op2 = InfixExp op2 (reverseExpression (InfixExp op t1 t2)) e
| otherwise = InfixExp op t1 (InfixExp op2 t2 e)
reverseExpression e = e
-- |The precedence of existing infix functions (higher goes first)
precedence :: String -> Int
precedence "or" = 1
precedence "and" = 10
@@ -199,7 +228,4 @@ precedence "-" = 2
precedence "*" = 3
precedence "/" = 3
precedence "^" = 4
precedence _ = 100
prefixOperators :: [String]
prefixOperators = ["-", "not"]
precedence _ = 100

View File

@@ -10,6 +10,7 @@ import Text.Megaparsec
import Text.Megaparsec.Char
import Parser.General
-- |Parses a function statement in Rosetta into a Function
functionParser :: Parser Function
functionParser =
do
@@ -21,6 +22,7 @@ functionParser =
fOutput <- outputAttributeParser
MakeFunction fName fDescription fInput fOutput <$> assignmentParser
-- |Parses the output assignment statement from a function in Rosetta into an Expression
assignmentParser :: Parser Expression
assignmentParser =
do
@@ -28,18 +30,21 @@ assignmentParser =
_ <- lexeme $ char ':'
expressionParser
-- |Parses the input attributes from a function statement in Rosetta into a list of TypeAttributes
inputAttributesParser :: Parser [TypeAttribute]
inputAttributesParser =
do
_ <- lexeme $ string "inputs:"
many $ try attributeParser
-- |Parses the output attribute of a function statement in Rosetta into a TypeAttribute
outputAttributeParser :: Parser TypeAttribute
outputAttributeParser =
do
_ <- lexeme $ string "output:"
attributeParser
-- |Auxiliary function that parses an attribute into a TypeAttribute
attributeParser :: Parser TypeAttribute
attributeParser =
do

View File

@@ -10,19 +10,22 @@ import Data.Text
type Parser = Parsec Void Text
-- |Auxiliary parser that eliminates trailing white space
spaceConsumer :: Parser ()
spaceConsumer = L.space space1 (L.skipLineComment "//") (L.skipBlockComment "/*" "*/")
-- |Auxiliary parser that runs a parser and eliminates trailing white space
lexeme :: Parser a -> Parser a
lexeme = L.lexeme spaceConsumer
-- |Parses a description in Rosetta into a String
descriptionParser :: Parser String
descriptionParser =
do
_ <- string "<\""
lexeme $ anySingle `manyTill` string "\">"
-- |Parses a pascal case name into a String (PascalCase)
pascalNameParser :: Parser String
pascalNameParser =
do
@@ -30,6 +33,7 @@ pascalNameParser =
rest <- lexeme $ many allowedChars
if first:rest `notElem` restrictedNames then return (first:rest) else fail ((first:rest) ++ " is a restricted name")
-- |Parses a camel case name into a String (camelCase)
camelNameParser :: Parser String
camelNameParser =
do
@@ -37,6 +41,7 @@ camelNameParser =
rest <- lexeme $ many allowedChars
if first:rest `notElem` restrictedNames then return (first:rest) else fail ((first:rest) ++ " is a restricted name")
-- |Parses any name that starts with a letter or '_' into a String
nameParser :: Parser String
nameParser =
do
@@ -44,9 +49,12 @@ nameParser =
rest <- lexeme $ many allowedChars
if first:rest `notElem` restrictedNames then return (first:rest) else fail ((first:rest) ++ " is a restricted name")
-- |Parses a character allowed in names in Rosetta into a Char
allowedChars :: Parser Char
allowedChars = letterChar <|> digitChar <|> char '_'
-- |List of restricted names used by Rosetta
restrictedNames :: [String]
restrictedNames = [
"displayName",

View File

@@ -7,6 +7,7 @@ import Text.Megaparsec.Char
import Text.Megaparsec
import Parser.General
-- |Parses a type declaration statement in Rosetta into an Type
typeParser :: Parser Type
typeParser =
do
@@ -17,6 +18,7 @@ typeParser =
tAttributes <- many $ try typeAttributeParser
return (MakeType tName tSuper tDescription tAttributes)
-- |Parses the super class declaration statement in Rosetta into an Type
superTypeParser :: Parser Type
superTypeParser =
do
@@ -24,6 +26,7 @@ superTypeParser =
name <- pascalNameParser
return $ MakeType name Nothing Nothing []
-- |Parses a declared type attribute in Rosetta into a TypeAttribute
typeAttributeParser :: Parser TypeAttribute
typeAttributeParser =
do
@@ -33,11 +36,13 @@ typeAttributeParser =
desc <- optional descriptionParser
return (MakeTypeAttribute aName (MakeType aType Nothing Nothing []) card desc)
-- |Parses the cardinality of a type attribute in Rosetta into a Cardinality
cardinalityParser :: Parser Cardinality
cardinalityParser =
do
try parseBounded <|> try parseSemiBounded <|> try parseUnbounded
-- |Parses a bounded cardinality statement in Rosetta into a Cardinality
parseBounded :: Parser Cardinality
parseBounded =
do
@@ -48,7 +53,7 @@ parseBounded =
_ <- lexeme $ char ')'
return $ Bounds (read low, read up)
-- |Parses a one bounded cardinality statement in Rosetta into a Cardinality
parseSemiBounded :: Parser Cardinality
parseSemiBounded =
do
@@ -57,13 +62,14 @@ parseSemiBounded =
_ <- lexeme $ string "..*)"
return $ OneBound $ read low
-- |Parses an unbounded cardinality statement in Rosetta into a Cardinality
parseUnbounded :: Parser Cardinality
parseUnbounded =
do
_ <- lexeme $ string "(*..*)"
return NoBounds
-- |Parses the name of a type in Rosetta into a String
typeNameParser :: Parser String
typeNameParser =
do