Improve directive parsing

This commit is contained in:
Vidar Holen 2017-10-29 17:29:27 -07:00
parent 0c1e2bbd4d
commit acead72c93

View file

@ -909,46 +909,46 @@ prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellc
readAnnotation = called "shellcheck directive" $ do readAnnotation = called "shellcheck directive" $ do
try readAnnotationPrefix try readAnnotationPrefix
many1 linewhitespace many1 linewhitespace
values <- many1 (readDisable <|> readSourceOverride <|> readShellOverride <|> anyKey) values <- many1 readKey
optional readAnyComment optional readAnyComment
linefeed void linefeed <|> do
parseNote ErrorC 1125 "Invalid key=value pair? Ignoring the rest of this directive starting here."
many (noneOf "\n")
void linefeed <|> eof
many linewhitespace many linewhitespace
return $ concat values return $ concat values
where where
readDisable = forKey "disable" $ readKey = do
readCode `sepBy` char ',' keyPos <- getPosition
where key <- many1 letter
readCode = do
optional $ string "SC"
int <- many1 digit
return $ DisableComment (read int)
readSourceOverride = forKey "source" $ do
filename <- many1 $ noneOf " \n"
return [SourceOverride filename]
readShellOverride = forKey "shell" $ do
pos <- getPosition
shell <- many1 $ noneOf " \n"
when (isNothing $ shellForExecutable shell) $
parseNoteAt pos ErrorC 1103
"This shell type is unknown. Use e.g. sh or bash."
return [ShellOverride shell]
forKey s p = do
try $ string s
char '=' <|> fail "Expected '=' after directive key" char '=' <|> fail "Expected '=' after directive key"
value <- p annotations <- case key of
many linewhitespace "disable" -> readCode `sepBy` char ','
return value where
readCode = do
optional $ string "SC"
int <- many1 digit
return $ DisableComment (read int)
"source" -> do
filename <- many1 $ noneOf " \n"
return [SourceOverride filename]
"shell" -> do
pos <- getPosition
shell <- many1 $ noneOf " \n"
when (isNothing $ shellForExecutable shell) $
parseNoteAt pos ErrorC 1103
"This shell type is unknown. Use e.g. sh or bash."
return [ShellOverride shell]
_ -> do
parseNoteAt keyPos WarningC 1107 "This directive is unknown. It will be ignored."
anyChar `reluctantlyTill` whitespace
return []
anyKey = do
pos <- getPosition
noneOf "#\r\n"
anyChar `reluctantlyTill` whitespace
many linewhitespace many linewhitespace
parseNoteAt pos WarningC 1107 "This directive is unknown. It will be ignored." return annotations
return []
readAnnotations = do readAnnotations = do
annotations <- many (readAnnotation `thenSkip` allspacing) annotations <- many (readAnnotation `thenSkip` allspacing)