Make `check` recognise `main-is` in conditional branches (#9768)
[cabal.git] / Cabal / src / Distribution / Lex.hs
blobaec376678323e1525c515d136cc6628084544dd0
1 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Distribution.Lex
5 -- Copyright : Ben Gamari 2015-2019
6 --
7 -- Maintainer : cabal-devel@haskell.org
8 -- Portability : portable
9 --
10 -- This module contains a simple lexer supporting quoted strings
11 module Distribution.Lex
12 ( tokenizeQuotedWords
13 ) where
15 import Distribution.Compat.DList
16 import Distribution.Compat.Prelude
17 import Prelude ()
19 -- | A simple parser supporting quoted strings.
21 -- Please be aware that this will only split strings when seeing whitespace
22 -- outside of quotation marks, i.e, @"foo\"bar baz\"qux quux"@ will be
23 -- converted to @["foobar bazqux", "quux"]@.
25 -- This behavior can be useful when parsing text like
26 -- @"ghc-options: -Wl,\"some option with spaces\""@, for instance.
27 tokenizeQuotedWords :: String -> [String]
28 tokenizeQuotedWords = filter (not . null) . go False mempty
29 where
31 :: Bool
32 -- \^ in quoted region
33 -> DList Char
34 -- \^ accumulator
35 -> String
36 -- \^ string to be parsed
37 -> [String]
38 -- \^ parse result
39 go _ accum []
40 | [] <- accum' = []
41 | otherwise = [accum']
42 where
43 accum' = runDList accum
44 go False accum (c : cs)
45 | isSpace c = runDList accum : go False mempty cs
46 | c == '"' = go True accum cs
47 go True accum (c : cs)
48 | c == '"' = go False accum cs
49 go quoted accum (c : cs) =
50 go quoted (accum `mappend` singleton c) cs