git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add Parens constructor back, ensure new operator sections are parenthesized
a4510eePhil Freeman
examples/passing/ExplicitOperatorSections.purs+2−2
@@ -3,9 +3,9 @@ module Main where
3import Prelude
3import Prelude
4
4
5subtractOne :: Int -> Int
5subtractOne :: Int -> Int
6−subtractOne = _ - 1
6+subtractOne = (_ - 1)
7
7
8addOne :: Int -> Int
8addOne :: Int -> Int
9−addOne = 1 + _
9+addOne = (1 + _)
10
10
11main = Control.Monad.Eff.Console.log "Done"
11main = Control.Monad.Eff.Console.log "Done"
src/Language/PureScript/AST/Declarations.hs+8
@@ -334,6 +334,14 @@ data Expr
334 --
334 --
335 | BinaryNoParens Expr Expr Expr
335 | BinaryNoParens Expr Expr Expr
336 -- |
336 -- |
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+ -- |
337 -- Operator section. This will be removed during desugaring and replaced with a partially applied
345 -- Operator section. This will be removed during desugaring and replaced with a partially applied
338 -- operator or lambda to flip the arguments.
346 -- operator or lambda to flip the arguments.
339 --
347 --
src/Language/PureScript/AST/Traversals.hs+7
@@ -49,6 +49,7 @@ everywhereOnValues f g h = (f', g', h')
49 g' :: Expr -> Expr
49 g' :: Expr -> Expr
50 g' (UnaryMinus v) = g (UnaryMinus (g' v))
50 g' (UnaryMinus v) = g (UnaryMinus (g' v))
51 g' (BinaryNoParens op v1 v2) = g (BinaryNoParens (g' op) (g' v1) (g' v2))
51 g' (BinaryNoParens op v1 v2) = g (BinaryNoParens (g' op) (g' v1) (g' v2))
52+ g' (Parens v) = g (Parens (g' v))
52 g' (OperatorSection op (Left v)) = g (OperatorSection (g' op) (Left $ g' v))
53 g' (OperatorSection op (Left v)) = g (OperatorSection (g' op) (Left $ g' v))
53 g' (OperatorSection op (Right v)) = g (OperatorSection (g' op) (Right $ g' v))
54 g' (OperatorSection op (Right v)) = g (OperatorSection (g' op) (Right $ g' v))
54 g' (ArrayLiteral vs) = g (ArrayLiteral (map g' vs))
55 g' (ArrayLiteral vs) = g (ArrayLiteral (map g' vs))
@@ -104,6 +105,7 @@ everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
104
105
105 g' (UnaryMinus v) = UnaryMinus <$> (g v >>= g')
106 g' (UnaryMinus v) = UnaryMinus <$> (g v >>= g')
106 g' (BinaryNoParens op v1 v2) = BinaryNoParens <$> (g op >>= g') <*> (g v1 >>= g') <*> (g v2 >>= g')
107 g' (BinaryNoParens op v1 v2) = BinaryNoParens <$> (g op >>= g') <*> (g v1 >>= g') <*> (g v2 >>= g')
108+ g' (Parens v) = Parens <$> (g v >>= g')
107 g' (OperatorSection op (Left v)) = OperatorSection <$> (g op >>= g') <*> (Left <$> (g v >>= g'))
109 g' (OperatorSection op (Left v)) = OperatorSection <$> (g op >>= g') <*> (Left <$> (g v >>= g'))
108 g' (OperatorSection op (Right v)) = OperatorSection <$> (g op >>= g') <*> (Right <$> (g v >>= g'))
110 g' (OperatorSection op (Right v)) = OperatorSection <$> (g op >>= g') <*> (Right <$> (g v >>= g'))
109 g' (ArrayLiteral vs) = ArrayLiteral <$> traverse (g' <=< g) vs
111 g' (ArrayLiteral vs) = ArrayLiteral <$> traverse (g' <=< g) vs
@@ -154,6 +156,7 @@ everywhereOnValuesM f g h = (f', g', h')
154
156
155 g' (UnaryMinus v) = (UnaryMinus <$> g' v) >>= g
157 g' (UnaryMinus v) = (UnaryMinus <$> g' v) >>= g
156 g' (BinaryNoParens op v1 v2) = (BinaryNoParens <$> g' op <*> g' v1 <*> g' v2) >>= g
158 g' (BinaryNoParens op v1 v2) = (BinaryNoParens <$> g' op <*> g' v1 <*> g' v2) >>= g
159+ g' (Parens v) = (Parens <$> g' v) >>= g
157 g' (OperatorSection op (Left v)) = (OperatorSection <$> g' op <*> (Left <$> g' v)) >>= g
160 g' (OperatorSection op (Left v)) = (OperatorSection <$> g' op <*> (Left <$> g' v)) >>= g
158 g' (OperatorSection op (Right v)) = (OperatorSection <$> g' op <*> (Right <$> g' v)) >>= g
161 g' (OperatorSection op (Right v)) = (OperatorSection <$> g' op <*> (Right <$> g' v)) >>= g
159 g' (ArrayLiteral vs) = (ArrayLiteral <$> traverse g' vs) >>= g
162 g' (ArrayLiteral vs) = (ArrayLiteral <$> traverse g' vs) >>= g
@@ -207,6 +210,7 @@ everythingOnValues (<>) f g h i j = (f', g', h', i', j')
207
210
208 g' v@(UnaryMinus v1) = g v <> g' v1
211 g' v@(UnaryMinus v1) = g v <> g' v1
209 g' v@(BinaryNoParens op v1 v2) = g v <> g' op <> g' v1 <> g' v2
212 g' v@(BinaryNoParens op v1 v2) = g v <> g' op <> g' v1 <> g' v2
213+ g' v@(Parens v1) = g v <> g' v1
210 g' v@(OperatorSection op (Left v1)) = g v <> g' op <> g' v1
214 g' v@(OperatorSection op (Left v1)) = g v <> g' op <> g' v1
211 g' v@(OperatorSection op (Right v1)) = g v <> g' op <> g' v1
215 g' v@(OperatorSection op (Right v1)) = g v <> g' op <> g' v1
212 g' v@(ArrayLiteral vs) = foldl (<>) (g v) (map g' vs)
216 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'
271
275
272 g' s (UnaryMinus v1) = g'' s v1
276 g' s (UnaryMinus v1) = g'' s v1
273 g' s (BinaryNoParens op v1 v2) = g'' s op <> g'' s v1 <> g'' s v2
277 g' s (BinaryNoParens op v1 v2) = g'' s op <> g'' s v1 <> g'' s v2
278+ g' s (Parens v1) = g'' s v1
274 g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
279 g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
275 g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
280 g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
276 g' s (ArrayLiteral vs) = foldl (<>) r0 (map (g'' s) vs)
281 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
338
343
339 g' s (UnaryMinus v) = UnaryMinus <$> g'' s v
344 g' s (UnaryMinus v) = UnaryMinus <$> g'' s v
340 g' s (BinaryNoParens op v1 v2) = BinaryNoParens <$> g'' s op <*> g'' s v1 <*> g'' s v2
345 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
341 g' s (OperatorSection op (Left v)) = OperatorSection <$> g'' s op <*> (Left <$> g'' s v)
347 g' s (OperatorSection op (Left v)) = OperatorSection <$> g'' s op <*> (Left <$> g'' s v)
342 g' s (OperatorSection op (Right v)) = OperatorSection <$> g'' s op <*> (Right <$> g'' s v)
348 g' s (OperatorSection op (Right v)) = OperatorSection <$> g'' s op <*> (Right <$> g'' s v)
343 g' s (ArrayLiteral vs) = ArrayLiteral <$> traverse (g'' s) vs
349 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)
417
423
418 g' s (UnaryMinus v1) = g'' s v1
424 g' s (UnaryMinus v1) = g'' s v1
419 g' s (BinaryNoParens op v1 v2) = g' s op <> g' s v1 <> g' s v2
425 g' s (BinaryNoParens op v1 v2) = g' s op <> g' s v1 <> g' s v2
426+ g' s (Parens v1) = g'' s v1
420 g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
427 g' s (OperatorSection op (Left v)) = g'' s op <> g'' s v
421 g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
428 g' s (OperatorSection op (Right v)) = g'' s op <> g'' s v
422 g' s (ArrayLiteral vs) = foldMap (g'' s) vs
429 g' s (ArrayLiteral vs) = foldMap (g'' s) vs
src/Language/PureScript/Parser/Declarations.hs+1−1
@@ -386,7 +386,7 @@ parseValueAtom = P.choice
386 , parseIfThenElse
386 , parseIfThenElse
387 , parseDo
387 , parseDo
388 , parseLet
388 , parseLet
389− , P.try $ parens parseValue
389+ , P.try $ Parens <$> parens parseValue
390 , parseOperatorSection
390 , parseOperatorSection
391 ]
391 ]
392
392
src/Language/PureScript/Sugar/ObjectWildcards.hs+12−2
@@ -1,4 +1,5 @@
1{-# LANGUAGE FlexibleContexts #-}
1{-# LANGUAGE FlexibleContexts #-}
2+{-# LANGUAGE PatternGuards #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
3
4
4module Language.PureScript.Sugar.ObjectWildcards (
5module Language.PureScript.Sugar.ObjectWildcards (
@@ -30,8 +31,13 @@ desugarObjectConstructors (Module ss coms mn ds exts) = Module ss coms mn <$> ma
30
31
31 desugarExpr :: Expr -> m Expr
32 desugarExpr :: Expr -> m Expr
32 desugarExpr AnonymousArgument = throwError . errorMessage $ IncorrectAnonymousArgument
33 desugarExpr AnonymousArgument = throwError . errorMessage $ IncorrectAnonymousArgument
34+ desugarExpr (Parens b)
35+ | b' <- stripPositionInfo b
36+ , BinaryNoParens op val u <- b'
33− desugarExpr (BinaryNoParens op val u) | isAnonymousArgument u = return $ OperatorSection op (Left val)
37+ , isAnonymousArgument u = return $ OperatorSection op (Left val)
38+ | b' <- stripPositionInfo b
39+ , BinaryNoParens op u val <- b'
34− desugarExpr (BinaryNoParens op u val) | isAnonymousArgument u = return $ OperatorSection op (Right val)
40+ , isAnonymousArgument u = return $ OperatorSection op (Right val)
35 desugarExpr (ObjectLiteral ps) = wrapLambda ObjectLiteral ps
41 desugarExpr (ObjectLiteral ps) = wrapLambda ObjectLiteral ps
36 desugarExpr (ObjectUpdate u ps) | isAnonymousArgument u = do
42 desugarExpr (ObjectUpdate u ps) | isAnonymousArgument u = do
37 obj <- freshIdent'
43 obj <- freshIdent'
@@ -51,6 +57,10 @@ desugarObjectConstructors (Module ss coms mn ds exts) = Module ss coms mn <$> ma
51 (args', ps') <- unzip <$> mapM mkProp ps
57 (args', ps') <- unzip <$> mapM mkProp ps
52 return $ foldr (Abs . Left) (mkVal ps') (catMaybes args')
58 return $ foldr (Abs . Left) (mkVal ps') (catMaybes args')
53
59
60+ stripPositionInfo :: Expr -> Expr
61+ stripPositionInfo (PositionedValue _ _ e) = stripPositionInfo e
62+ stripPositionInfo e = e
63+
54 isAnonymousArgument :: Expr -> Bool
64 isAnonymousArgument :: Expr -> Bool
55 isAnonymousArgument AnonymousArgument = True
65 isAnonymousArgument AnonymousArgument = True
56 isAnonymousArgument (PositionedValue _ _ e) = isAnonymousArgument e
66 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)
77rebracketModule :: (Applicative m, MonadError MultipleErrors m) => [[(Qualified Ident, Expr -> Expr -> Expr, Associativity)]] -> Module -> m Module
77rebracketModule :: (Applicative m, MonadError MultipleErrors m) => [[(Qualified Ident, Expr -> Expr -> Expr, Associativity)]] -> Module -> m Module
78rebracketModule opTable (Module ss coms mn ds exts) =
78rebracketModule opTable (Module ss coms mn ds exts) =
79 let (f, _, _) = everywhereOnValuesTopDownM return (matchOperators opTable) return
79 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
81
89
82externsFixities :: ExternsFile -> [(Qualified Ident, SourceSpan, Fixity, Maybe (Qualified Ident))]
90externsFixities :: ExternsFile -> [(Qualified Ident, SourceSpan, Fixity, Maybe (Qualified Ident))]
83externsFixities ExternsFile{..} =
91externsFixities ExternsFile{..} =