Clean up parser, implement operators

We now parse down to line 45 in all-packages.nix: let.
This commit is contained in:
John Wiegley 2014-07-04 01:52:50 -05:00
parent d2df092907
commit 569ebf3f9a
2 changed files with 34 additions and 15 deletions

View File

@ -5,22 +5,19 @@ module Nix.Parser (parseNixFile, Result(..)) where
import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import Data.Char
import Data.Foldable
import Data.List (foldl1')
import qualified Data.Map as Map
import Data.Text hiding (head, map, foldl1')
import qualified Data.Text as T
import Nix.Types
import Nix.Internal
import Nix.Parser.Library
import Prelude hiding (elem)
import qualified Prelude
nixApp :: Parser NExpr
nixApp = go <$>
someTill (whiteSpace *> nixExpr True)
(try (lookAhead (() <$ oneOf "=,;])}" <|> eof)))
(try (lookAhead (() <$ oneOf "=,;])}" <|> eof)))
where
go [] = error "some has failed us"
go [x] = x
@ -31,11 +28,19 @@ nixExpr = buildExpressionParser table . nixTerm
where
table =
[ [ prefix "-" NNeg ]
, [ prefix "~" NNeg ]
, [ prefix "?" NNeg ]
, [ binary "++" NConcat AssocRight ]
, [ binary "*" NMult AssocLeft,
binary "/" NDiv AssocLeft ]
, [ binary "+" NPlus AssocLeft,
binary "-" NMinus AssocLeft ]
, [ binary "*" NMult AssocLeft, binary "/" NDiv AssocLeft ]
, [ binary "+" NPlus AssocLeft, binary "-" NMinus AssocLeft ]
, [ prefix "!" NNot ]
, [ 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)

View File

@ -67,14 +67,28 @@ data NOperF r
| NConcat r r
deriving (Eq, Ord, Generic, Typeable, Data, Functor)
-- show (NConcat l) = go l
-- where
-- go [] = ""
-- go [x] = show x
-- go (x:xs) = show x ++ " ++ " ++ go xs
instance Show f => Show (NOperF f) where
show (NNot r) = "! " ++ show r
show (NNeg r) = "-" ++ show r
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
= 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
show (NConstant x) = show x
-- show (NOper x) = show x
show (NOper x) = show x
show (NList l) = "[ " ++ go l ++ " ]"
where