]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Add type signatures to most parsing declarations.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Thu, 20 Sep 2012 14:46:21 +0000 (15:46 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Thu, 20 Sep 2012 14:46:21 +0000 (15:46 +0100)
ofc.cabal
src/Parser.hs

index c2fa95d79ad49d96f96d5a20711c27179364319c..fb95212e6df6d84ad23665ed419122a24d0cc361 100644 (file)
--- a/ofc.cabal
+++ b/ofc.cabal
@@ -10,4 +10,4 @@ Executable ofc
   Main-is:           Main.hs
   Hs-Source-Dirs:    src
   GHC-Options:       -Wall -fno-warn-missing-signatures
-  Build-Depends:     base, containers, parsec >= 3, pretty
+  Build-Depends:     base, containers, parsec >= 3, pretty, transformers
index 4e0c58aa3355fa3b2eb82c3948bc9e8e9c06aa3e..472e0b7def528638051e540d7cf4114ed356fdb7 100644 (file)
@@ -2,6 +2,7 @@ module Parser (runOFLParser) where
 import TopLevel
 import TargetMapping
 import Data.List (foldl1')
+import Data.Functor.Identity (Identity)
 import Text.Parsec
 import Text.Parsec.Expr
 import Text.Parsec.Token
@@ -12,6 +13,7 @@ oflKeywords = map show [minBound::IndexType ..] ++
   map show [minBound::BaseType ..] ++ 
   ["^", "+", "-", "*", "/", "=", "laplacian", "inner", "sum", "derivative", "r", "target"]
 
+oflDef :: LanguageDef st
 oflDef = emptyDef { 
   commentStart = "",
   commentEnd = "",
@@ -36,6 +38,7 @@ TokenParser {
   stringLiteral = lStringLiteral
 } = makeTokenParser oflDef
 
+oflOperatorTable :: OperatorTable String st Identity Expression
 oflOperatorTable = [[Infix  (do _ <- lSymbol "^"; return Power) AssocLeft],
                     [Prefix (do _ <- lSymbol "-"; return Negate)],
                     [Infix  (do _ <- lSymbol "*"; return Multiply) AssocLeft
@@ -44,13 +47,18 @@ oflOperatorTable = [[Infix  (do _ <- lSymbol "^"; return Power) AssocLeft],
                     ,Infix  (do _ <- lSymbol "-"; return Sub) AssocLeft]
                    ]
 
+type OFLParser a = Parsec String OFL a
+
+parseType :: OFLParser BaseType
 parseType = foldl1' (<|>) [do lReserved $ toOFLString t; return t | t <- [minBound::BaseType ..]]
+
+parseIndex :: OFLParser IndexType
 parseIndex = foldl1' (<|>) [do lReserved $ toOFLString t; return t | t <- [minBound::IndexType ..]]
 
-parseDeclaration :: Parsec String OFL ()
+parseDeclaration :: OFLParser ()
 parseDeclaration = parseValueDeclaration <|> parseIndexDeclaration <?> "declaration"
 
-parseValueDeclaration :: Parsec String OFL ()
+parseValueDeclaration :: OFLParser ()
 parseValueDeclaration = do
   valueType <- parseType
   indices <- option [] (lBrackets $ lCommaSep parseIndex)
@@ -58,25 +66,28 @@ parseValueDeclaration = do
   sequence_ [modifyState(\x -> addValueDeclaration x name valueType indices) | name <- names]
   <?> "value declaration"
 
-parseIndexDeclaration :: Parsec String OFL ()
+parseIndexDeclaration :: OFLParser ()
 parseIndexDeclaration = do
   indexType <- parseIndex
   names <- lCommaSep1 parseIdentifier
   sequence_ [modifyState(\x -> addIndexDeclaration x name indexType) | name <- names]
   <?> "index declaration"
 
+parseIdentifier :: OFLParser String
 parseIdentifier = lIdentifier <?> "identifier"
 
-parseAssignment :: Parsec String OFL ()
+parseAssignment :: OFLParser ()
 parseAssignment = do
   lhs <- parseExpression
   _ <- lSymbol "="
   rhs <- parseExpression
   modifyState(\x -> addAssignment x lhs rhs)
   <?> "assignment"
+
+parseExpression :: OFLParser Expression
 parseExpression = buildExpressionParser oflOperatorTable parseTerm <?> "expression"
  
+parseTerm :: OFLParser Expression
 parseTerm = 
   parseLaplacian
   <|> do lReserved "inner"; res <- lParens parseInner; return res
@@ -91,6 +102,7 @@ parseTerm =
   <|> parseIdentifierAccess 
   <?> "term"
 
+parseLaplacian :: OFLParser Expression
 parseLaplacian = do
   lReserved "laplacian"; 
   operand <- lParens parseExpression; 
@@ -103,6 +115,7 @@ parseLaplacian = do
       putState ofl';
       return $ Sum secondDerivative indexName
  
+parseInner :: OFLParser Expression
 parseInner = do 
   e1 <- parseExpression
   _ <- lSymbol ","
@@ -110,6 +123,7 @@ parseInner = do
   return $ Integrate $ Multiply  e1 e2
   <?> "inner product"
  
+parseSum :: OFLParser Expression
 parseSum = do 
   e1 <- parseExpression
   _ <- lSymbol ","
@@ -117,6 +131,7 @@ parseSum = do
   return $ Sum e1 index
   <?> "sum"
 
+parseDerivative :: OFLParser Expression
 parseDerivative = do 
   e1 <- parseExpression
   _ <- lSymbol ","
@@ -124,17 +139,19 @@ parseDerivative = do
   return $ SpatialDerivative e1 index 1
   <?> "derivative"
 
+parseIdentifierAccess :: OFLParser Expression
 parseIdentifierAccess = do
   ident <- parseIdentifier
   indices <- option [] (lBrackets $ lCommaSep parseIdentifier)
   return $ IndexedIdentifier ident indices
   <?> "indexed identifier"
 
+parseTarget :: OFLParser String
 parseTarget = do
   lReserved "target"
   lSymbol "ONETEP"
 
-parseOFL :: Parsec String OFL OFL
+parseOFL :: OFLParser OFL
 parseOFL = do 
   lWhiteSpace 
   _ <- many1 parseDeclaration
@@ -145,6 +162,7 @@ parseOFL = do
   eof
   getState
 
+parseOutputFunction :: OFLParser ()
 parseOutputFunction = do
   _ <- lSymbol "OutputFunction"
   ident <- parseIdentifier
@@ -155,6 +173,7 @@ parseOutputFunction = do
   modifyState(\x -> setOutputFunction x $ FortranFunction ident properties)
   <?> "output function specification"
 
+parseFortranFunctionProperty :: OFLParser FortranFunctionProperty
 parseFortranFunctionProperty = do
   parseFortranFunctionNameProperty <|> parseFortranFunctionParamsProperty
   where
@@ -167,6 +186,7 @@ parseFortranFunctionProperty = do
     idents <- lParens $ lCommaSep lIdentifier
     return $ FortranFunctionParams idents
 
+parseBinding :: OFLParser ()
 parseBinding = do
   _ <- lSymbol "Variable" <|> lSymbol "Parameter"
   ident <- parseIdentifier
@@ -174,12 +194,14 @@ parseBinding = do
   targetType <- (parseFortranParam <|> parsePPDFunctionSet)
   modifyState(\x -> addTargetMapping x ident targetType)
 
+parseFortranParam :: OFLParser TargetType
 parseFortranParam = do
   _ <- lSymbol "fortran_param"
   _ <- lSymbol "with"
   properties <- lCommaSep parseFortranParamProperty
   return $ FortranParameter properties
 
+parseFortranParamProperty :: OFLParser FortranParameterProperty
 parseFortranParamProperty =
   parseNameProperty <|> parseTypeProperty <|> parseSpaceProperty <|> parseIndexedProperty <?> "Fortran parameter property" where
   parseNameProperty = do _ <- lSymbol "name"; name <- lParens lStringLiteral; return $ ParamName name
@@ -190,6 +212,7 @@ parseFortranParamProperty =
     <|> do _ <- lSymbol "psinc_coarse_grid"; indices <- lParens $ lCommaSep1 lIdentifier; return $ CoarsePsinc indices
   parseIndexedProperty = do _ <- lSymbol "indexed"; indices <- lParens $ lCommaSep1 lIdentifier; return $ Indexed indices
 
+parseFortranType :: OFLParser FortranType
 parseFortranType = do
   baseType <- parseBaseType;
   maybeIndices <- optionMaybe parseArrayIndices
@@ -209,12 +232,14 @@ parseFortranType = do
      <|> do _ <- lSymbol "double"; return Double
      <|> do _ <- lSymbol "func_basis"; return FunctionBasis
 
+parsePPDFunctionSet :: OFLParser TargetType
 parsePPDFunctionSet = do
   _ <- lSymbol "ppd_function_set"
   _ <- lSymbol "with"
   properties <- lCommaSep parsePPDFunctionSetProperty
   return $ PPDFunctionSet properties
 
+parsePPDFunctionSetProperty :: OFLParser PPDFunctionSetProperty
 parsePPDFunctionSetProperty =
   parseBasisProperty <|> parseDataProperty <?> "PPD function set property" where
   parseBasisProperty = do _ <- lSymbol "basis"; ident <- lParens lIdentifier; return $ PPDBasis ident