diff --git a/Nix/Parser.hs b/Nix/Parser.hs index d745198..0adbeea 100644 --- a/Nix/Parser.hs +++ b/Nix/Parser.hs @@ -11,43 +11,11 @@ import qualified Data.Map as Map import Data.Text hiding (concat, concatMap, head, map) import Nix.Types import Nix.Internal +import Nix.Parser.Library import qualified Prelude import Prelude hiding (readFile, concat, concatMap, elem, mapM, 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 = go <$> some (whiteSpace *> nixTerm True) where @@ -174,3 +142,6 @@ setOrArgs = do trace ("args: " ++ show args) $ return () symbolic ':' *> ((Fix .) . NAbs <$> pure args <*> nixApp) <|> pure args + +parseNixFile :: MonadIO m => FilePath -> m (Result NExpr) +parseNixFile = parseFromFileEx nixApp diff --git a/Nix/Parser/Library.hs b/Nix/Parser/Library.hs new file mode 100644 index 0000000..9b25233 --- /dev/null +++ b/Nix/Parser/Library.hs @@ -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 diff --git a/hnix.cabal b/hnix.cabal index fe0c9d3..e8f27ca 100644 --- a/hnix.cabal +++ b/hnix.cabal @@ -25,6 +25,7 @@ Library Nix.Types Other-modules: Nix.Internal + Nix.Parser.Library Default-extensions: DataKinds DeriveDataTypeable