Browse files

Fixes #5; ternary operator without the second argument

Tested w/ dump-ast-id on:

<?php
echo (2 ? : 4);
  • Loading branch information...
1 parent 32c0f0f commit 61a1b8b071898274ee2481996b56044b6c132f6e @dancor dancor committed Sep 12, 2012
Showing with 12 additions and 10 deletions.
  1. +9 −7 src/Common.hs
  2. +2 −2 src/Lang/Php/Ast/ExprParse.hs
  3. +1 −1 src/Lang/Php/Ast/ExprTypes.hs
View
16 src/Common.hs
@@ -1,4 +1,5 @@
-{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE TypeSynonymInstances #-}
module Common (
module Text.ParserCombinators.Parsec,
@@ -9,8 +10,7 @@ import Control.Monad
import Text.ParserCombinators.Parsec hiding (State, parse, choice)
import Text.ParserCombinators.Parsec.Expr
--- parsec 2 vs 3 stuff
-
+-- | Used w/ buildExpressionParser.
type Oper a = Operator Char () a
class Parse a where
@@ -22,13 +22,12 @@ class Unparse a where
instance (Parse a) => Parse [a] where
parse = many parse
+instance (Parse a) => Parse (Maybe a) where
+ parse = Just <$> parse <|> return Nothing
+
instance (Parse a, Parse b) => Parse (Either a b) where
parse = Left <$> parse <|> Right <$> parse
-instance (Unparse a, Unparse b) => Unparse (Either a b) where
- unparse (Left a) = unparse a
- unparse (Right a) = unparse a
-
instance (Unparse a, Unparse b) => Unparse (a, b) where
unparse (a, b) = unparse a ++ unparse b
@@ -38,3 +37,6 @@ instance (Unparse a) => Unparse [a] where
instance (Unparse a) => Unparse (Maybe a) where
unparse = maybe "" unparse
+instance (Unparse a, Unparse b) => Unparse (Either a b) where
+ unparse (Left a) = unparse a
+ unparse (Right a) = unparse a
View
4 src/Lang/Php/Ast/ExprParse.hs
@@ -489,7 +489,7 @@ preRep, postRep :: Parser (a -> a) -> Parser (a -> a)
preRep p = (p >>= \ f -> (f .) <$> preRep p) <|> return id
postRep p = (p >>= \ f -> (. f) <$> postRep p) <|> return id
-ial :: [Parser (a -> a -> a)] -> [Oper a]
+ial, ian :: [Parser (a -> a -> a)] -> [Oper a]
ial = map $ flip Infix AssocLeft
ian = map $ flip Infix AssocNone
@@ -557,7 +557,7 @@ eptOr = binOp BOr tokOrP
eptTernaryIf :: Parser ((Expr, WS) -> (Expr, WS))
eptTernaryIf = do
w2 <- tokQMarkP >> parse
- (e2, w3) <- parse
+ (e2, w3) <- maybe (Nothing, []) (first Just) <$> parse
w4 <- tokColonP >> parse
(e3, w5) <- parse
return $ \ (e1, w1) ->
View
2 src/Lang/Php/Ast/ExprTypes.hs
@@ -142,7 +142,7 @@ data OnceOrNot = Once | NotOnce
data TernaryIf = TernaryIf {
ternaryIfCond :: Expr,
ternaryIfWS1 :: WS2,
- ternaryIfThen :: Expr,
+ ternaryIfThen :: Maybe Expr,
ternaryIfWS2 :: WS2,
ternaryIfElse :: Expr}
deriving (Data, Eq, Generic, Show, Typeable)

0 comments on commit 61a1b8b

Please sign in to comment.