Merge branch 'shell-parse-stx-etx' of https://github.com/atom-haskell/ghc-mod into release-5.5.0.0

This commit is contained in:
Daniel Gröber 2015-12-30 23:02:52 +01:00
commit 59193c71cd
2 changed files with 35 additions and 48 deletions

View File

@ -16,35 +16,20 @@
module GHCMod.Options.ShellParse (parseCmdLine) where module GHCMod.Options.ShellParse (parseCmdLine) where
import Data.Char import Data.Char
import Data.Maybe import Data.List
isQuote :: Char -> Bool go :: String -> String -> [String] -> Bool -> [String]
isQuote = (==) '"'
isEscapeChar :: Char -> Bool
isEscapeChar = (==) '\\'
isEscapable :: Char -> Bool
isEscapable c = any ($ c) [isSpace, isQuote, isEscapeChar]
go :: String -> String -> [String] -> Maybe Char -> [String]
-- result -- result
go [] curarg accargs _ = reverse $ reverse curarg : accargs go [] curarg accargs _ = reverse $ reverse curarg : accargs
-- escaped character
go (esc:c:cl) curarg accargs quote
| isEscapeChar esc
= if isEscapable c
then go cl (c:curarg) accargs quote
else go (c:cl) (esc:curarg) accargs quote
go (c:cl) curarg accargs quotes go (c:cl) curarg accargs quotes
-- quote character -- opens quotes -- open quotes
| isQuote c, isNothing quotes | c == '\STX', not quotes
= go cl curarg accargs (Just c) = go cl curarg accargs True
-- close quotes -- close quotes
| quotes == Just c | c == '\ETX', quotes
= go cl curarg accargs Nothing = go cl curarg accargs False
-- space separates argumetns outside quotes -- space separates arguments outside quotes
| isSpace c, isNothing quotes | isSpace c, not quotes
= if null curarg = if null curarg
then go cl curarg accargs quotes then go cl curarg accargs quotes
else go cl [] (reverse curarg : accargs) quotes else go cl [] (reverse curarg : accargs) quotes
@ -52,4 +37,8 @@ go (c:cl) curarg accargs quotes
| otherwise = go cl (c:curarg) accargs quotes | otherwise = go cl (c:curarg) accargs quotes
parseCmdLine :: String -> [String] parseCmdLine :: String -> [String]
parseCmdLine comline = go comline [] [] Nothing parseCmdLine comline'
| Just comline <- stripPrefix "ascii-escape " $ dropWhile isSpace comline'
= go (dropWhile isSpace comline) [] [] False
parseCmdLine [] = [""]
parseCmdLine comline = words comline

View File

@ -8,29 +8,27 @@ import Test.Hspec
spec :: Spec spec :: Spec
spec = spec =
describe "parseCmdLine" $ do describe "parseCmdLine" $ do
it "splits arguments" $ it "splits arguments" $ do
parseCmdLine "test command line" `shouldBe` ["test", "command", "line"] parseCmdLine "test command line" `shouldBe` ["test", "command", "line"]
it "honors double quotes" $ parseCmdLine "ascii-escape test command line" `shouldBe` ["test", "command", "line"]
parseCmdLine "test command line \"with double quotes\"" it "honors quoted segments if turned on" $
`shouldBe` ["test", "command", "line", "with double quotes"] parseCmdLine "ascii-escape test command line \STXwith quoted segment\ETX"
it "escapes spaces" $ do `shouldBe` ["test", "command", "line", "with quoted segment"]
parseCmdLine "with\\ spaces" it "doesn't honor quoted segments if turned off" $
`shouldBe` ["with spaces"] parseCmdLine "test command line \STXwith quoted segment\ETX"
parseCmdLine "\"with\\ spaces\"" `shouldBe` words "test command line \STXwith quoted segment\ETX"
`shouldBe` ["with spaces"] it "squashes multiple spaces" $ do
it "escapes '\\'" $ do
parseCmdLine "\\\\"
`shouldBe` ["\\"]
parseCmdLine "\"\\\\\""
`shouldBe` ["\\"]
it "escapes double quotes" $ do
parseCmdLine "\\\""
`shouldBe` ["\""]
parseCmdLine "\"\\\"\""
`shouldBe` ["\""]
it "doesn't escape random characters" $
parseCmdLine "\\a\\b\\c"
`shouldBe` ["\\a\\b\\c"]
it "squashes multiple spaces" $
parseCmdLine "test command" parseCmdLine "test command"
`shouldBe` ["test", "command"] `shouldBe` ["test", "command"]
parseCmdLine "ascii-escape test command"
`shouldBe` ["test", "command"]
it "ingores leading spaces" $ do
parseCmdLine " test command"
`shouldBe` ["test", "command"]
parseCmdLine " ascii-escape test command"
`shouldBe` ["test", "command"]
it "parses empty string as no argument" $ do
parseCmdLine ""
`shouldBe` [""]
parseCmdLine "ascii-escape "
`shouldBe` [""]