Harden wording files parsing against partial read due to unexpected token and add support for comments

This commit is contained in:
Tissevert 2019-03-01 23:03:31 +01:00
parent d35a4c33ef
commit f9a82cb088

View file

@ -19,7 +19,7 @@ import Paths_hablo (getDataFileName)
import Text.ParserCombinators.Parsec (
Parser
, (<|>)
, char, choice, endBy, many, many1, noneOf, parse, string, try
, char, choice, endBy, eof, many, many1, noneOf, optional, parse, string, try
)
import System.Exit (die)
@ -61,11 +61,14 @@ addWording currentWording wordingFile = do
Right newWording -> return $ Map.union currentWording newWording
wordingP :: Parser (Map String Text)
wordingP = Map.map Text.pack . Map.fromList <$> (many eol *> line `endBy` (many1 eol))
wordingP = Map.map Text.pack . Map.fromList <$>
(many skip *> line `endBy` (many1 skip) <* eof)
where
line = (,) <$> (choice (try . string <$> keys) <* equal) <*> many (noneOf "\r\n")
equal = many (char ' ') *> char '=' *> many (char ' ')
restOfLine = many $ noneOf "\r\n"
eol = try (string "\r\n") <|> string "\r" <|> string "\n"
skip = optional (char '#' *> restOfLine) *> eol
line = (,) <$> (choice (try . string <$> keys) <* equal) <*> restOfLine
equal = many (char ' ') *> char '=' *> many (char ' ')
checkTemplateWith :: [Text] -> String -> Map String Text -> IO Template
checkTemplateWith variables key wording =