More work on the megaparsec parser
This commit is contained in:
parent
70f21d30ae
commit
4f9dec43cd
|
@ -113,7 +113,7 @@ builtinsList = sequence [
|
|||
, add TopLevel "throw" throw_
|
||||
, add2 TopLevel "scopedImport" scopedImport
|
||||
, add TopLevel "derivationStrict" derivationStrict_
|
||||
-- jww (2018-04-09): NYI
|
||||
-- jww (2018-04-10): TODO
|
||||
-- , add0 TopLevel "derivation" $(do
|
||||
-- let f = "data/nix/corepkgs/derivation.nix"
|
||||
-- addDependentFile f
|
||||
|
|
|
@ -67,7 +67,7 @@ nixSelector = annotateLocation $ keyName `sepBy1` selDot
|
|||
{-
|
||||
-- | A self-contained unit.
|
||||
nixTerm :: Parser NExprLoc
|
||||
nixTerm = choice
|
||||
nixTerm = nixSelect $ choice
|
||||
[ dbg "Path" nixPath
|
||||
, dbg "SPath" nixSPath
|
||||
, dbg "Float" nixFloat
|
||||
|
@ -87,7 +87,7 @@ nixToplevelForm = choice
|
|||
, dbg "Let" nixLet
|
||||
, dbg "If" nixIf
|
||||
, dbg "Assert" nixAssert
|
||||
, dbg "Lambda" nixWith ]
|
||||
, dbg "With" nixWith ]
|
||||
-}
|
||||
|
||||
nixTerm :: Parser NExprLoc
|
||||
|
@ -154,11 +154,11 @@ nixSPath = annotateLocation1
|
|||
<?> "spath")
|
||||
|
||||
pathStr :: Parser FilePath
|
||||
pathStr = liftM2 (++) (many pathChar)
|
||||
pathStr = lexeme $ liftM2 (++) (many pathChar)
|
||||
(Prelude.concat <$> some (liftM2 (:) slash (some pathChar)))
|
||||
|
||||
nixPath :: Parser NExprLoc
|
||||
nixPath = annotateLocation1 $ try $ mkPathF False <$> pathStr
|
||||
nixPath = annotateLocation1 (try (mkPathF False <$> pathStr) <?> "path")
|
||||
|
||||
nixLet :: Parser NExprLoc
|
||||
nixLet = annotateLocation1 (reserved "let"
|
||||
|
@ -194,8 +194,7 @@ nixWith = annotateLocation1 (NWith
|
|||
<?> "with")
|
||||
|
||||
nixLambda :: Parser NExprLoc
|
||||
nixLambda = (nAbs <$> annotateLocation (try argExpr <?> "lambda arguments")
|
||||
<*> nixExprLoc) <?> "lambda"
|
||||
nixLambda = nAbs <$> annotateLocation (try argExpr) <*> nixExprLoc
|
||||
|
||||
nixStringExpr :: Parser NExprLoc
|
||||
nixStringExpr = nStr <$> annotateLocation nixString
|
||||
|
@ -205,9 +204,10 @@ uriAfterColonC = alphaNumChar <|>
|
|||
satisfy (\x -> x `elem` ("%/?:@&=+$,-_.!~*'" :: String))
|
||||
|
||||
nixUri :: Parser NExprLoc
|
||||
nixUri = annotateLocation1 $ fmap (mkUriF . pack) $ (++)
|
||||
nixUri = annotateLocation1 (fmap (mkUriF . pack) ((++)
|
||||
<$> try ((++) <$> (scheme <* char ':') <*> fmap (\x -> [':',x]) uriAfterColonC)
|
||||
<*> many uriAfterColonC
|
||||
<?> "uri"))
|
||||
where
|
||||
scheme = (:) <$> letterChar
|
||||
<*> many (alphaNumChar <|> satisfy (\x -> x `elem` ("+-." :: String)))
|
||||
|
|
|
@ -11,7 +11,7 @@ module Nix.Parser.Library
|
|||
import Control.Applicative hiding (many)
|
||||
import Control.Monad
|
||||
import Control.Monad.IO.Class
|
||||
import Data.Char (isAlpha, isDigit)
|
||||
import Data.Char (isAlpha, isDigit, isSpace)
|
||||
import Data.Functor.Identity
|
||||
import Data.HashSet (HashSet)
|
||||
import qualified Data.HashSet as HashSet
|
||||
|
@ -33,9 +33,13 @@ lexeme :: Parser a -> Parser a
|
|||
lexeme p = p <* whiteSpace
|
||||
{-# INLINEABLE lexeme #-}
|
||||
|
||||
symbol = lexeme . string
|
||||
reservedOp = symbol
|
||||
reserved = symbol
|
||||
symbol = lexeme . string
|
||||
|
||||
reserved :: Text -> Parser ()
|
||||
reserved n = lexeme $ do
|
||||
_ <- string n <*
|
||||
lookAhead (satisfy (\x -> isSpace x || x == '{' || x == '(' || x == ';'))
|
||||
return ()
|
||||
|
||||
opStart :: Parser Char
|
||||
opStart = satisfy $ \x ->
|
||||
|
@ -91,10 +95,12 @@ type Parser = ParsecT Void Text Identity
|
|||
data Result a = Success a | Failure Doc deriving Show
|
||||
|
||||
parseFromFileEx :: MonadIO m => Parser a -> FilePath -> m (Result a)
|
||||
parseFromFileEx p path =
|
||||
(either (Failure . text . parseErrorPretty) Success . parse p path)
|
||||
`liftM` liftIO (T.readFile path)
|
||||
parseFromFileEx p path = do
|
||||
txt <- liftIO (T.readFile path)
|
||||
return $ either (Failure . text . parseErrorPretty' txt) Success
|
||||
$ parse p path txt
|
||||
|
||||
parseFromText :: Parser a -> Text -> Result a
|
||||
parseFromText p =
|
||||
either (Failure . text . parseErrorPretty) Success . parse p "<string>"
|
||||
parseFromText p txt =
|
||||
either (Failure . text . parseErrorPretty' txt) Success $
|
||||
parse p "<string>" txt
|
||||
|
|
|
@ -12,7 +12,7 @@ import Control.DeepSeq
|
|||
import Data.Data (Data(..))
|
||||
import Data.Foldable (concat)
|
||||
import qualified Data.Map as Map
|
||||
import Data.Text (Text)
|
||||
import Data.Text (Text, unpack)
|
||||
import Data.Typeable (Typeable)
|
||||
import GHC.Generics hiding (Prefix)
|
||||
import Nix.Expr
|
||||
|
@ -48,7 +48,7 @@ operator n = symbol n
|
|||
|
||||
opWithLoc :: Text -> o -> (Ann SrcSpan o -> a) -> Parser a
|
||||
opWithLoc name op f = do
|
||||
Ann ann _ <- annotateLocation $ operator name
|
||||
Ann ann _ <- annotateLocation $ {- dbg (unpack name) $ -} operator name
|
||||
return $ f (Ann ann op)
|
||||
|
||||
binaryN name op = (NBinaryDef name op NAssocNone,
|
||||
|
@ -125,7 +125,8 @@ getBinaryOperator = (m Map.!) where
|
|||
_ -> []
|
||||
|
||||
getSpecialOperator :: NSpecialOp -> OperatorInfo
|
||||
getSpecialOperator = (m Map.!) where
|
||||
getSpecialOperator NSelectOp = OperatorInfo 1 NAssocLeft "."
|
||||
getSpecialOperator o = m Map.! o where
|
||||
m = Map.fromList $ concat $ zipWith buildEntry [1..]
|
||||
(nixOperators (error "unused"))
|
||||
buildEntry i = concatMap $ \case
|
||||
|
|
Loading…
Reference in a new issue