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
10
11module Distribution.Lex (
12        tokenizeQuotedWords
13 ) where
14
15import Prelude ()
16import Distribution.Compat.Prelude
17import Distribution.Compat.DList
18
19tokenizeQuotedWords :: String -> [String]
20tokenizeQuotedWords = 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
30
31    go False  accum (c:cs)
32      | isSpace c = runDList accum : go False mempty cs
33      | c == '"'  = go True accum cs
34
35    go True   accum (c:cs)
36      | c == '"'  = go False accum cs
37
38    go quoted accum (c:cs)
39                  = go quoted (accum `mappend` singleton c) cs
40
41