aboutsummaryrefslogtreecommitdiff
path: root/bootstrap
diff options
context:
space:
mode:
authorBrad Richardson <everythingfunctional@protonmail.com>2020-10-15 10:47:40 -0500
committerBrad Richardson <everythingfunctional@protonmail.com>2020-10-15 10:47:40 -0500
commit311c695aa30f63fc1be0ef8b8c56ca372e01a31e (patch)
treefb50d1452fccbdad137b53478e8addc8358194e9 /bootstrap
parent134713a6c3620bf5b71ceaa2b6bed3a228d1c297 (diff)
downloadfpm-311c695aa30f63fc1be0ef8b8c56ca372e01a31e.tar.gz
fpm-311c695aa30f63fc1be0ef8b8c56ca372e01a31e.zip
Add test for a module's name
Diffstat (limited to 'bootstrap')
-rw-r--r--bootstrap/src/BuildModel.hs18
-rw-r--r--bootstrap/unit_test/ModuleSourceConstructionTest.hs5
2 files changed, 23 insertions, 0 deletions
diff --git a/bootstrap/src/BuildModel.hs b/bootstrap/src/BuildModel.hs
index 1610784..baefda9 100644
--- a/bootstrap/src/BuildModel.hs
+++ b/bootstrap/src/BuildModel.hs
@@ -46,6 +46,7 @@ data Source =
{ moduleSourceFileName :: FilePath
, moduleObjectFileName :: FilePath -> FilePath
, moduleModulesUsed :: [String]
+ , moduleName :: String
}
processRawSource :: RawSource -> Source
@@ -64,6 +65,7 @@ processRawSource rawSource =
then Module { moduleSourceFileName = sourceFileName
, moduleObjectFileName = objectFileName
, moduleModulesUsed = modulesUsed
+ , moduleName = getModuleName parsedContents
}
else undefined
@@ -101,6 +103,13 @@ getModulesUsed = mapMaybe contentToMaybeModuleName
ModuleUsed moduleName -> Just moduleName
_ -> Nothing
+getModuleName :: [LineContents] -> String
+getModuleName pc = head $ mapMaybe contentToMaybeModuleName pc
+ where
+ contentToMaybeModuleName content = case content of
+ ModuleDeclaration moduleName -> Just moduleName
+ _ -> Nothing
+
readFileLinesIO :: FilePath -> IO [String]
readFileLinesIO file = do
contents <- readFile file
@@ -137,6 +146,7 @@ moduleDeclaration = do
_ <- string "module"
skipAtLeastOneWhiteSpace
moduleName <- validIdentifier
+ skipSpaceCommentOrEnd
return $ ModuleDeclaration moduleName
useStatement :: ReadP LineContents
@@ -159,11 +169,19 @@ skipSpaceOrEnd = eof <|> skipAtLeastOneWhiteSpace
skipSpaceCommaOrEnd :: ReadP ()
skipSpaceCommaOrEnd = eof <|> skipComma <|> skipAtLeastOneWhiteSpace
+skipSpaceCommentOrEnd :: ReadP ()
+skipSpaceCommentOrEnd = eof <|> skipComment <|> skipAtLeastOneWhiteSpace
+
skipComma :: ReadP ()
skipComma = do
_ <- char ','
return ()
+skipComment :: ReadP ()
+skipComment = do
+ _ <- char '!'
+ return ()
+
whiteSpace :: ReadP Char
whiteSpace = satisfy (`elem` " \t")
diff --git a/bootstrap/unit_test/ModuleSourceConstructionTest.hs b/bootstrap/unit_test/ModuleSourceConstructionTest.hs
index 20bc011..26f08b2 100644
--- a/bootstrap/unit_test/ModuleSourceConstructionTest.hs
+++ b/bootstrap/unit_test/ModuleSourceConstructionTest.hs
@@ -32,6 +32,7 @@ test = return $ givenInput
"its object file name is the 'flattened' path of the source file with '.o' appeneded"
checkModuleObjectFileName
, then' "it knows what modules it uses directly" checkModuleModulesUsed
+ , then' "it knows its name" checkModuleName
]
]
@@ -61,3 +62,7 @@ checkModuleModulesUsed :: Source -> Result
checkModuleModulesUsed m@(Module{}) =
assertEquals ["module1", "module2"] $ moduleModulesUsed m
checkModuleModulesUsed _ = fail' "wasn't a Module"
+
+checkModuleName :: Source -> Result
+checkModuleName m@(Module{}) = assertEquals "some_module" $ moduleName m
+checkModuleName _ = fail' "wasn't a Module"