1 -----------------------------------------------------------------------------
4 -- Module : Distribution.Lex
5 -- Copyright : Ben Gamari 2015-2019
7 -- Maintainer : cabal-devel@haskell.org
8 -- Portability : portable
10 -- This module contains a simple lexer supporting quoted strings
11 module Distribution
.Lex
15 import Distribution
.Compat
.DList
16 import Distribution
.Compat
.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
32 -- \^ in quoted region
36 -- \^ string to be parsed
41 |
otherwise = [accum']
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