Factor out the abstract of which parsing library to use
This commit is contained in:
parent
9b12ebf081
commit
8158557bb9
|
@ -11,43 +11,11 @@ import qualified Data.Map as Map
|
||||||
import Data.Text hiding (concat, concatMap, head, map)
|
import Data.Text hiding (concat, concatMap, head, map)
|
||||||
import Nix.Types
|
import Nix.Types
|
||||||
import Nix.Internal
|
import Nix.Internal
|
||||||
|
import Nix.Parser.Library
|
||||||
import qualified Prelude
|
import qualified Prelude
|
||||||
import Prelude hiding (readFile, concat, concatMap, elem, mapM,
|
import Prelude hiding (readFile, concat, concatMap, elem, mapM,
|
||||||
sequence)
|
sequence)
|
||||||
|
|
||||||
#if USE_PARSEC
|
|
||||||
import Data.Text.IO
|
|
||||||
import Text.Parsec hiding ((<|>), many, optional)
|
|
||||||
import Text.Parsec.Text
|
|
||||||
import Text.PrettyPrint.ANSI.Leijen (Doc, text)
|
|
||||||
|
|
||||||
symbol :: String -> Parser String
|
|
||||||
symbol str = string str <* whiteSpace
|
|
||||||
|
|
||||||
symbolic :: Char -> Parser Char
|
|
||||||
symbolic c = char c <* whiteSpace
|
|
||||||
|
|
||||||
decimal :: Parser Integer
|
|
||||||
decimal = read <$> some digit
|
|
||||||
|
|
||||||
whiteSpace :: Parser ()
|
|
||||||
whiteSpace = spaces
|
|
||||||
|
|
||||||
data Result a = Success a
|
|
||||||
| Failure Doc
|
|
||||||
|
|
||||||
parseFromFileEx :: MonadIO m => Parser a -> FilePath -> m (Result a)
|
|
||||||
parseFromFileEx p path =
|
|
||||||
(either (Failure . text . show) Success . parse p path)
|
|
||||||
`liftM` liftIO (readFile path)
|
|
||||||
#else
|
|
||||||
import Text.Trifecta
|
|
||||||
import Text.Parser.LookAhead
|
|
||||||
#endif
|
|
||||||
|
|
||||||
parseNixFile :: MonadIO m => FilePath -> m (Result NExpr)
|
|
||||||
parseNixFile = parseFromFileEx nixApp
|
|
||||||
|
|
||||||
nixApp :: Parser NExpr
|
nixApp :: Parser NExpr
|
||||||
nixApp = go <$> some (whiteSpace *> nixTerm True)
|
nixApp = go <$> some (whiteSpace *> nixTerm True)
|
||||||
where
|
where
|
||||||
|
@ -174,3 +142,6 @@ setOrArgs = do
|
||||||
trace ("args: " ++ show args) $ return ()
|
trace ("args: " ++ show args) $ return ()
|
||||||
symbolic ':' *> ((Fix .) . NAbs <$> pure args <*> nixApp)
|
symbolic ':' *> ((Fix .) . NAbs <$> pure args <*> nixApp)
|
||||||
<|> pure args
|
<|> pure args
|
||||||
|
|
||||||
|
parseNixFile :: MonadIO m => FilePath -> m (Result NExpr)
|
||||||
|
parseNixFile = parseFromFileEx nixApp
|
||||||
|
|
48
Nix/Parser/Library.hs
Normal file
48
Nix/Parser/Library.hs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
{-# LANGUAGE CPP #-}
|
||||||
|
|
||||||
|
module Nix.Parser.Library
|
||||||
|
(
|
||||||
|
#if USE_PARSEC
|
||||||
|
module Text.Parsec
|
||||||
|
, module Text.Parsec.Text
|
||||||
|
#else
|
||||||
|
module Text.Trifecta
|
||||||
|
, module Text.Parser.LookAhead
|
||||||
|
#endif
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
|
#if USE_PARSEC
|
||||||
|
|
||||||
|
import Control.Applicative
|
||||||
|
import Data.Text.IO
|
||||||
|
import Text.Parsec hiding ((<|>), many, optional)
|
||||||
|
import Text.Parsec.Text
|
||||||
|
import Text.PrettyPrint.ANSI.Leijen (Doc, text)
|
||||||
|
|
||||||
|
symbol :: String -> Parser String
|
||||||
|
symbol str = string str <* whiteSpace
|
||||||
|
|
||||||
|
symbolic :: Char -> Parser Char
|
||||||
|
symbolic c = char c <* whiteSpace
|
||||||
|
|
||||||
|
decimal :: Parser Integer
|
||||||
|
decimal = read <$> some digit
|
||||||
|
|
||||||
|
whiteSpace :: Parser ()
|
||||||
|
whiteSpace = spaces
|
||||||
|
|
||||||
|
data Result a = Success a
|
||||||
|
| Failure Doc
|
||||||
|
|
||||||
|
parseFromFileEx :: MonadIO m => Parser a -> FilePath -> m (Result a)
|
||||||
|
parseFromFileEx p path =
|
||||||
|
(either (Failure . text . show) Success . parse p path)
|
||||||
|
`liftM` liftIO (readFile path)
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
import Text.Parser.LookAhead
|
||||||
|
import Text.Trifecta
|
||||||
|
|
||||||
|
#endif
|
|
@ -25,6 +25,7 @@ Library
|
||||||
Nix.Types
|
Nix.Types
|
||||||
Other-modules:
|
Other-modules:
|
||||||
Nix.Internal
|
Nix.Internal
|
||||||
|
Nix.Parser.Library
|
||||||
Default-extensions:
|
Default-extensions:
|
||||||
DataKinds
|
DataKinds
|
||||||
DeriveDataTypeable
|
DeriveDataTypeable
|
||||||
|
|
Loading…
Reference in a new issue