Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Fixes #5; ternary operator without the second argument
Browse files Browse the repository at this point in the history
Tested w/ dump-ast-id on:

<?php
echo (2 ? : 4);
  • Loading branch information
dancor committed Sep 13, 2012
1 parent 32c0f0f commit 61a1b8b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/Common.hs
@@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}

module Common (
module Text.ParserCombinators.Parsec,
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
4 changes: 2 additions & 2 deletions src/Lang/Php/Ast/ExprParse.hs
Expand Up @@ -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

Expand Down Expand Up @@ -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) ->
Expand Down
2 changes: 1 addition & 1 deletion src/Lang/Php/Ast/ExprTypes.hs
Expand Up @@ -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)
Expand Down

0 comments on commit 61a1b8b

Please sign in to comment.