{-# OPTIONS_GHC -Wwarn #-}

-- | GHC language options parser
module Ide.Plugin.Eval.Parse.Option (
    langOptions,
) where

import Control.Monad.Combinators (many)
import Ide.Plugin.Eval.Parse.Parser (
    Parser,
    letterChar,
    runParser,
    space,
    string,
 )

{- |
>>> langOptions ":set   -XBinaryLiterals  -XOverloadedStrings "
Right ["BinaryLiterals","OverloadedStrings"]

>>> langOptions ":set"
Right []

>>> langOptions ""
Left "No match"
-}
langOptions :: [Char] -> Either String [[Char]]
langOptions :: [Char] -> Either [Char] [[Char]]
langOptions = Parser Char [[Char]] -> [Char] -> Either [Char] [[Char]]
forall t a. Show t => Parser t a -> [t] -> Either [Char] a
runParser (Parser Char Char -> Parser Char [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many Parser Char Char
space Parser Char [Char] -> Parser Char [[Char]] -> Parser Char [[Char]]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Char [[Char]]
languageOpts Parser Char [[Char]] -> Parser Char [Char] -> Parser Char [[Char]]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Char Char -> Parser Char [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many Parser Char Char
space)

-- >>> runParser languageOpts ":set -XBinaryLiterals -XOverloadedStrings"
-- Right ["BinaryLiterals","OverloadedStrings"]
languageOpts :: Parser Char [[Char]]
languageOpts :: Parser Char [[Char]]
languageOpts = [Char] -> Parser Char [Char]
string [Char]
":set" Parser Char [Char] -> Parser Char [[Char]] -> Parser Char [[Char]]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Char [Char] -> Parser Char [[Char]]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many (Parser Char Char -> Parser Char [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many Parser Char Char
space Parser Char [Char] -> Parser Char [Char] -> Parser Char [Char]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [Char] -> Parser Char [Char]
string [Char]
"-X" Parser Char [Char] -> Parser Char [Char] -> Parser Char [Char]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Parser Char Char -> Parser Char [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many Parser Char Char
letterChar))