diff options
-rw-r--r-- | bootstrap/src/BuildModel.hs | 40 | ||||
-rw-r--r-- | bootstrap/unit_test/ModuleSourceConstructionTest.hs | 37 |
2 files changed, 69 insertions, 8 deletions
diff --git a/bootstrap/src/BuildModel.hs b/bootstrap/src/BuildModel.hs index e5291ac..db44b54 100644 --- a/bootstrap/src/BuildModel.hs +++ b/bootstrap/src/BuildModel.hs @@ -25,18 +25,24 @@ import Text.ParserCombinators.ReadP ( ReadP , string ) -data LineContents = ProgramDeclaration | ModuleUsed String | Other +data LineContents = + ProgramDeclaration + | ModuleDeclaration String + | ModuleUsed String + | Other data RawSource = RawSource { rawSourceFilename :: FilePath , rawSourceContents :: String } -data Source = Program { - programSourceFileName :: FilePath - , programObjectFileName :: FilePath -> FilePath - , programModulesUsed :: [String] -} +data Source = + Program + { programSourceFileName :: FilePath + , programObjectFileName :: FilePath -> FilePath + , programModulesUsed :: [String] + } + | Module {} processRawSource :: RawSource -> Source processRawSource rawSource = @@ -53,7 +59,7 @@ processRawSource rawSource = <.> "o" , programModulesUsed = getModulesUsed parsedContents } - else undefined + else if hasModuleDeclaration parsedContents then Module{} else undefined pathSeparatorsToUnderscores :: FilePath -> FilePath pathSeparatorsToUnderscores fileName = @@ -73,6 +79,15 @@ hasProgramDeclaration parsedContents = case filter f parsedContents of ProgramDeclaration -> True _ -> False +hasModuleDeclaration :: [LineContents] -> Bool +hasModuleDeclaration parsedContents = case filter f parsedContents of + x : _ -> True + _ -> False + where + f lc = case lc of + ModuleDeclaration{} -> True + _ -> False + getModulesUsed :: [LineContents] -> [String] getModulesUsed = mapMaybe contentToMaybeModuleName where @@ -99,7 +114,8 @@ doFortranLineParse :: ReadP LineContents doFortranLineParse = option Other fortranUsefulContents fortranUsefulContents :: ReadP LineContents -fortranUsefulContents = programDeclaration <|> useStatement +fortranUsefulContents = + programDeclaration <|> moduleDeclaration <|> useStatement programDeclaration :: ReadP LineContents programDeclaration = do @@ -109,6 +125,14 @@ programDeclaration = do _ <- validIdentifier return ProgramDeclaration +moduleDeclaration :: ReadP LineContents +moduleDeclaration = do + skipSpaces + _ <- string "module" + skipAtLeastOneWhiteSpace + moduleName <- validIdentifier + return $ ModuleDeclaration moduleName + useStatement :: ReadP LineContents useStatement = do skipSpaces diff --git a/bootstrap/unit_test/ModuleSourceConstructionTest.hs b/bootstrap/unit_test/ModuleSourceConstructionTest.hs new file mode 100644 index 0000000..cc6d079 --- /dev/null +++ b/bootstrap/unit_test/ModuleSourceConstructionTest.hs @@ -0,0 +1,37 @@ +module ModuleSourceConstructionTest + ( test + ) +where + +import BuildModel ( RawSource(..) + , Source(..) + , processRawSource + ) +import Hedge ( Result + , Test + , assertThat + , givenInput + , then' + , whenTransformed + ) +import System.FilePath ( (</>) ) + +test :: IO (Test ()) +test = return $ givenInput + "a module" + exampleModule + [ whenTransformed "processed to a source" + processRawSource + [then' "it is a Module" checkIsModule] + ] + +exampleModule :: RawSource +exampleModule = + RawSource moduleSourceFileName' $ unlines ["module some_module", "end module"] + +moduleSourceFileName' :: String +moduleSourceFileName' = "some" </> "file" </> "somewhere.f90" + +checkIsModule :: Source -> Result +checkIsModule Module{} = assertThat True +checkIsModule _ = assertThat False |