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,25 +909,32 @@ 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
key <- many1 letter
char '=' <|> fail "Expected '=' after directive key"
annotations <- case key of
"disable" -> readCode `sepBy` char ','
where where
readCode = do readCode = do
optional $ string "SC" optional $ string "SC"
int <- many1 digit int <- many1 digit
return $ DisableComment (read int) return $ DisableComment (read int)
readSourceOverride = forKey "source" $ do "source" -> do
filename <- many1 $ noneOf " \n" filename <- many1 $ noneOf " \n"
return [SourceOverride filename] return [SourceOverride filename]
readShellOverride = forKey "shell" $ do "shell" -> do
pos <- getPosition pos <- getPosition
shell <- many1 $ noneOf " \n" shell <- many1 $ noneOf " \n"
when (isNothing $ shellForExecutable shell) $ when (isNothing $ shellForExecutable shell) $
@ -935,21 +942,14 @@ readAnnotation = called "shellcheck directive" $ do
"This shell type is unknown. Use e.g. sh or bash." "This shell type is unknown. Use e.g. sh or bash."
return [ShellOverride shell] return [ShellOverride shell]
forKey s p = do _ -> do
try $ string s parseNoteAt keyPos WarningC 1107 "This directive is unknown. It will be ignored."
char '=' <|> fail "Expected '=' after directive key"
value <- p
many linewhitespace
return value
anyKey = do
pos <- getPosition
noneOf "#\r\n"
anyChar `reluctantlyTill` whitespace anyChar `reluctantlyTill` whitespace
many linewhitespace
parseNoteAt pos WarningC 1107 "This directive is unknown. It will be ignored."
return [] return []
many linewhitespace
return annotations
readAnnotations = do readAnnotations = do
annotations <- many (readAnnotation `thenSkip` allspacing) annotations <- many (readAnnotation `thenSkip` allspacing)
return $ concat annotations return $ concat annotations