git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add Parens constructor back, ensure new operator sections are parenthesized
commita4510ee873656d564a02a55d843c23100addf3bc authorPhil Freeman <paf31@cantab.net> dateThu 4 Feb 2016, 20:45 (11 years ago) parent22c9dfd
6 files changed+39−6
examples/passing/ExplicitOperatorSections.purs+2−2 src/Language/PureScript/AST/Declarations.hs+8 src/Language/PureScript/AST/Traversals.hs+7 src/Language/PureScript/Parser/Declarations.hs+1−1 src/Language/PureScript/Sugar/ObjectWildcards.hs+12−2 src/Language/PureScript/Sugar/Operators.hs+9−1
examples/passing/ExplicitOperatorSections.purs+2−2
@@ -3,9 +3,9 @@ module Main where
33import Prelude
44
55subtractOne :: Int -> Int
6−subtractOne = _ - 1
6+subtractOne = (_ - 1)
77
88addOne :: Int -> Int
9−addOne = 1 + _
9+addOne = (1 + _)
1010
1111main = Control.Monad.Eff.Console.log "Done"
src/Language/PureScript/AST/Declarations.hs+8
@@ -334,6 +334,14 @@ data Expr
334334 --
335335 | BinaryNoParens Expr Expr Expr
336336 -- |
337+ -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
338+ -- will be removed.
339+ --
340+ -- Note: although it seems this constructor is not used, it _is_ useful, since it prevents
341+ -- certain traversals from matching.
342+ --
343+ | Parens Expr
344+ -- |
337345 -- Operator section. This will be removed during desugaring and replaced with a partially applied
338346 -- operator or lambda to flip the arguments.
339347 --
src/Language/PureScript/AST/Traversals.hs+7
@@ -49,6 +49,7 @@ everywhereOnValues f g h = (f', g', h')
4949 g' :: Expr -> Expr
5050 g' (UnaryMinus v) = g (UnaryMinus (g' v))
5151 g' (BinaryNoParens op v1 v2) = g (BinaryNoParens (g' op) (g' v1) (g' v2))
52+ g' (Parens v) = g (Parens (g' v))
5253 g' (OperatorSection op (Left v)) = g (OperatorSection (g' op) (Left $ g' v))
5354 g' (OperatorSection op (Right v)) = g (OperatorSection (g' op) (Right $ g' v))
5455 g' (ArrayLiteral vs) = g (ArrayLiteral (map g' vs))
@@ -104,6 +105,7 @@ everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
104105
105106 g' (UnaryMinus v) = UnaryMinus <$> (g v >>= g')
106107 g' (BinaryNoParens op v1 v2) = BinaryNoParens <$> (g op >>= g') <*> (g v1 >>= g') <*> (g v2 >>= g')
108+ g' (Parens v) = Parens <$> (g v >>= g')
107109 g' (OperatorSection op (Left v)) = OperatorSection <$> (g op >>= g') <*> (Left <$> (g v >>= g'))
108110 g' (OperatorSection op (Right v)) = OperatorSection <$> (g op >>= g') <*> (Right <$> (g v >>= g'))
109111 g' (ArrayLiteral vs) = ArrayLiteral <$> traverse (g' <=< g) vs
@@ -154,6 +156,7 @@ everywhereOnValuesM f g h = (f', g', h')
154156
155157 g' (UnaryMinus v) = (UnaryMinus <$> g' v) >>= g
156158 g' (BinaryNoParens op v1 v2) = (BinaryNoParens <$> g' op <*> g' v1 <*> g' v2) >>= g
159+ g' (Parens v) = (Parens <$> g' v) >>= g
157160 g' (OperatorSection op (Left v)) = (OperatorSection <$> g' op <*> (Left <$> g' v)) >>= g
158161 g' (OperatorSection op (Right v)) = (OperatorSection <$> g' op <*> (Right <$> g' v)) >>= g
159162 g' (ArrayLiteral vs) = (ArrayLiteral <$> traverse g' vs) >>= g
@@ -207,6 +210,7 @@ everythingOnValues (<>) f g h i j = (f', g', h', i', j')
207210
208211 g' v@(UnaryMinus v1) = g v <> g' v1
209212 g' v@(BinaryNoParens op v1 v2) = g v <> g' op <> g' v1 <> g' v2
213+ g' v@(Parens v1) = g v <> g' v1
210214 g' v@(OperatorSection op (Left v1)) = g v <> g' op <> g' v1
211215 g' v@(OperatorSection op (Right v1)) = g v <> g' op <> g' v1
212216 g' v@(ArrayLiteral vs) = foldl (<>) (g v) (map g' vs)
@@ -271,6 +275,7 @@ everythingWithContextOnValues s0 r0 (<>) f g h i j = (f'' s0, g'' s0, h'' s0, i'
271275
272276 g' s (UnaryMinus v1) = g'' s v1
273277 g' s (BinaryNoParens op v1 v2) = g'' s op <> g'' s v1 <> g'' s v2
278+ g' s (Parens v1) = g'' s v1
274279 g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
275280 g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
276281 g' s (ArrayLiteral vs) = foldl (<>) r0 (map (g'' s) vs)
@@ -338,6 +343,7 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
338343
339344 g' s (UnaryMinus v) = UnaryMinus <$> g'' s v
340345 g' s (BinaryNoParens op v1 v2) = BinaryNoParens <$> g'' s op <*> g'' s v1 <*> g'' s v2
346+ g' s (Parens v) = Parens <$> g'' s v
341347 g' s (OperatorSection op (Left v)) = OperatorSection <$> g'' s op <*> (Left <$> g'' s v)
342348 g' s (OperatorSection op (Right v)) = OperatorSection <$> g'' s op <*> (Right <$> g'' s v)
343349 g' s (ArrayLiteral vs) = ArrayLiteral <$> traverse (g'' s) vs
@@ -417,6 +423,7 @@ everythingWithScope f g h i j = (f'', g'', h'', i'', \s -> snd . j'' s)
417423
418424 g' s (UnaryMinus v1) = g'' s v1
419425 g' s (BinaryNoParens op v1 v2) = g' s op <> g' s v1 <> g' s v2
426+ g' s (Parens v1) = g'' s v1
420427 g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
421428 g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
422429 g' s (ArrayLiteral vs) = foldMap (g'' s) vs
src/Language/PureScript/Parser/Declarations.hs+1−1
@@ -386,7 +386,7 @@ parseValueAtom = P.choice
386386 , parseIfThenElse
387387 , parseDo
388388 , parseLet
389− , P.try $ parens parseValue
389+ , P.try $ Parens <$> parens parseValue
390390 , parseOperatorSection
391391 ]
392392
src/Language/PureScript/Sugar/ObjectWildcards.hs+12−2
@@ -1,4 +1,5 @@
11{-# LANGUAGE FlexibleContexts #-}
2+{-# LANGUAGE PatternGuards #-}
23{-# LANGUAGE ScopedTypeVariables #-}
34
45module Language.PureScript.Sugar.ObjectWildcards (
@@ -30,8 +31,13 @@ desugarObjectConstructors (Module ss coms mn ds exts) = Module ss coms mn <$> ma
3031
3132 desugarExpr :: Expr -> m Expr
3233 desugarExpr AnonymousArgument = throwError . errorMessage $ IncorrectAnonymousArgument
33− desugarExpr (BinaryNoParens op val u) | isAnonymousArgument u = return $ OperatorSection op (Left val)
34− desugarExpr (BinaryNoParens op u val) | isAnonymousArgument u = return $ OperatorSection op (Right val)
34+ desugarExpr (Parens b)
35+ | b' <- stripPositionInfo b
36+ , BinaryNoParens op val u <- b'
37+ , isAnonymousArgument u = return $ OperatorSection op (Left val)
38+ | b' <- stripPositionInfo b
39+ , BinaryNoParens op u val <- b'
40+ , isAnonymousArgument u = return $ OperatorSection op (Right val)
3541 desugarExpr (ObjectLiteral ps) = wrapLambda ObjectLiteral ps
3642 desugarExpr (ObjectUpdate u ps) | isAnonymousArgument u = do
3743 obj <- freshIdent'
@@ -51,6 +57,10 @@ desugarObjectConstructors (Module ss coms mn ds exts) = Module ss coms mn <$> ma
5157 (args', ps') <- unzip <$> mapM mkProp ps
5258 return $ foldr (Abs . Left) (mkVal ps') (catMaybes args')
5359
60+ stripPositionInfo :: Expr -> Expr
61+ stripPositionInfo (PositionedValue _ _ e) = stripPositionInfo e
62+ stripPositionInfo e = e
63+
5464 isAnonymousArgument :: Expr -> Bool
5565 isAnonymousArgument AnonymousArgument = True
5666 isAnonymousArgument (PositionedValue _ _ e) = isAnonymousArgument e
src/Language/PureScript/Sugar/Operators.hs+9−1
@@ -77,7 +77,15 @@ removeSignedLiterals (Module ss coms mn ds exts) = Module ss coms mn (map f' ds)
7777rebracketModule :: (Applicative m, MonadError MultipleErrors m) => [[(Qualified Ident, Expr -> Expr -> Expr, Associativity)]] -> Module -> m Module
7878rebracketModule opTable (Module ss coms mn ds exts) =
7979 let (f, _, _) = everywhereOnValuesTopDownM return (matchOperators opTable) return
80− in Module ss coms mn <$> parU ds f <*> pure exts
80+ in Module ss coms mn <$> (map removeParens <$> parU ds f) <*> pure exts
81+
82+removeParens :: Declaration -> Declaration
83+removeParens =
84+ let (f, _, _) = everywhereOnValues id go id
85+ in f
86+ where
87+ go (Parens val) = val
88+ go val = val
8189
8290externsFixities :: ExternsFile -> [(Qualified Ident, SourceSpan, Fixity, Maybe (Qualified Ident))]
8391externsFixities ExternsFile{..} =