Fix header level for foreign-libraries section
[cabal.git] / Cabal / Distribution / Lex.hs
blob4dbab5932fe1f9fc16dc7a0b0e070e46d9e5b356
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Distribution.Lex
4 -- Copyright : Ben Gamari 2015-2019
5 --
6 -- Maintainer : cabal-devel@haskell.org
7 -- Portability : portable
8 --
9 -- This module contains a simple lexer supporting quoted strings
11 module Distribution.Lex (
12 tokenizeQuotedWords
13 ) where
15 import Prelude ()
16 import Distribution.Compat.Prelude
17 import Distribution.Compat.DList
19 tokenizeQuotedWords :: String -> [String]
20 tokenizeQuotedWords = filter (not . null) . go False mempty
21 where
22 go :: Bool -- ^ in quoted region
23 -> DList Char -- ^ accumulator
24 -> String -- ^ string to be parsed
25 -> [String] -- ^ parse result
26 go _ accum []
27 | [] <- accum' = []
28 | otherwise = [accum']
29 where accum' = runDList accum
31 go False accum (c:cs)
32 | isSpace c = runDList accum : go False mempty cs
33 | c == '"' = go True accum cs
35 go True accum (c:cs)
36 | c == '"' = go False accum cs
38 go quoted accum (c:cs)
39 = go quoted (accum `mappend` singleton c) cs