Clean up parser, implement operators
We now parse down to line 45 in all-packages.nix: let.
This commit is contained in:
parent
d2df092907
commit
569ebf3f9a
|
@ -5,22 +5,19 @@ module Nix.Parser (parseNixFile, Result(..)) where
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Control.Monad.IO.Class
|
import Control.Monad.IO.Class
|
||||||
import Data.Char
|
|
||||||
import Data.Foldable
|
import Data.Foldable
|
||||||
import Data.List (foldl1')
|
import Data.List (foldl1')
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Text hiding (head, map, foldl1')
|
import Data.Text hiding (head, map, foldl1')
|
||||||
import qualified Data.Text as T
|
|
||||||
import Nix.Types
|
import Nix.Types
|
||||||
import Nix.Internal
|
import Nix.Internal
|
||||||
import Nix.Parser.Library
|
import Nix.Parser.Library
|
||||||
import Prelude hiding (elem)
|
import Prelude hiding (elem)
|
||||||
import qualified Prelude
|
|
||||||
|
|
||||||
nixApp :: Parser NExpr
|
nixApp :: Parser NExpr
|
||||||
nixApp = go <$>
|
nixApp = go <$>
|
||||||
someTill (whiteSpace *> nixExpr True)
|
someTill (whiteSpace *> nixExpr True)
|
||||||
(try (lookAhead (() <$ oneOf "=,;])}" <|> eof)))
|
(try (lookAhead (() <$ oneOf "=,;])}" <|> eof)))
|
||||||
where
|
where
|
||||||
go [] = error "some has failed us"
|
go [] = error "some has failed us"
|
||||||
go [x] = x
|
go [x] = x
|
||||||
|
@ -31,11 +28,19 @@ nixExpr = buildExpressionParser table . nixTerm
|
||||||
where
|
where
|
||||||
table =
|
table =
|
||||||
[ [ prefix "-" NNeg ]
|
[ [ prefix "-" NNeg ]
|
||||||
|
, [ prefix "~" NNeg ]
|
||||||
|
, [ prefix "?" NNeg ]
|
||||||
, [ binary "++" NConcat AssocRight ]
|
, [ binary "++" NConcat AssocRight ]
|
||||||
, [ binary "*" NMult AssocLeft,
|
, [ binary "*" NMult AssocLeft, binary "/" NDiv AssocLeft ]
|
||||||
binary "/" NDiv AssocLeft ]
|
, [ binary "+" NPlus AssocLeft, binary "-" NMinus AssocLeft ]
|
||||||
, [ binary "+" NPlus AssocLeft,
|
, [ prefix "!" NNot ]
|
||||||
binary "-" NMinus AssocLeft ]
|
, [ binary "//" NUpdate AssocRight ]
|
||||||
|
, [ binary "<" NLt AssocLeft, binary ">" NGt AssocLeft
|
||||||
|
, binary "<=" NLte AssocLeft, binary ">=" NGte AssocLeft ]
|
||||||
|
, [ binary "==" NEq AssocNone, binary "!=" NNEq AssocNone ]
|
||||||
|
, [ binary "&&" NAnd AssocLeft ]
|
||||||
|
, [ binary "||" NOr AssocLeft ]
|
||||||
|
, [ binary "->" NImpl AssocNone ]
|
||||||
]
|
]
|
||||||
|
|
||||||
binary name fun = Infix ((\x y -> Fix (NOper (fun x y))) <$ symbol name)
|
binary name fun = Infix ((\x y -> Fix (NOper (fun x y))) <$ symbol name)
|
||||||
|
|
28
Nix/Types.hs
28
Nix/Types.hs
|
@ -67,14 +67,28 @@ data NOperF r
|
||||||
| NConcat r r
|
| NConcat r r
|
||||||
deriving (Eq, Ord, Generic, Typeable, Data, Functor)
|
deriving (Eq, Ord, Generic, Typeable, Data, Functor)
|
||||||
|
|
||||||
-- show (NConcat l) = go l
|
instance Show f => Show (NOperF f) where
|
||||||
-- where
|
show (NNot r) = "! " ++ show r
|
||||||
-- go [] = ""
|
show (NNeg r) = "-" ++ show r
|
||||||
-- go [x] = show x
|
|
||||||
-- go (x:xs) = show x ++ " ++ " ++ go xs
|
|
||||||
|
|
||||||
|
show (NEq r1 r2) = show r1 ++ " == " ++ show r2
|
||||||
|
show (NNEq r1 r2) = show r1 ++ " != " ++ show r2
|
||||||
|
show (NLt r1 r2) = show r1 ++ " < " ++ show r2
|
||||||
|
show (NLte r1 r2) = show r1 ++ " <= " ++ show r2
|
||||||
|
show (NGt r1 r2) = show r1 ++ " > " ++ show r2
|
||||||
|
show (NGte r1 r2) = show r1 ++ " >= " ++ show r2
|
||||||
|
show (NAnd r1 r2) = show r1 ++ " && " ++ show r2
|
||||||
|
show (NOr r1 r2) = show r1 ++ " || " ++ show r2
|
||||||
|
show (NImpl r1 r2) = show r1 ++ " -> " ++ show r2
|
||||||
|
show (NUpdate r1 r2) = show r1 ++ " // " ++ show r2
|
||||||
|
show (NHasAttr r1 r2) = show r1 ++ " ? " ++ show r2
|
||||||
|
|
||||||
-- phi (NConcat l) = "NConcat " ++ show l
|
show (NPlus r1 r2) = show r1 ++ " + " ++ show r2
|
||||||
|
show (NMinus r1 r2) = show r1 ++ " - " ++ show r2
|
||||||
|
show (NMult r1 r2) = show r1 ++ " * " ++ show r2
|
||||||
|
show (NDiv r1 r2) = show r1 ++ " / " ++ show r2
|
||||||
|
|
||||||
|
show (NConcat r1 r2) = show r1 ++ " ++ " ++ show r2
|
||||||
|
|
||||||
data NExprF r
|
data NExprF r
|
||||||
= NConstant NAtom
|
= NConstant NAtom
|
||||||
|
@ -119,7 +133,7 @@ instance Ord (Fix NExprF) where compare (Fix x) (Fix y) = compare x y
|
||||||
|
|
||||||
instance Show f => Show (NExprF f) where
|
instance Show f => Show (NExprF f) where
|
||||||
show (NConstant x) = show x
|
show (NConstant x) = show x
|
||||||
-- show (NOper x) = show x
|
show (NOper x) = show x
|
||||||
|
|
||||||
show (NList l) = "[ " ++ go l ++ " ]"
|
show (NList l) = "[ " ++ go l ++ " ]"
|
||||||
where
|
where
|
||||||
|
|
Loading…
Reference in New Issue