git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix precedence of -> and type application #144
d698508Phil Freeman
examples/passing/Applicative.pursnew file+16
@@ -0,0 +1,16 @@
1+module Applicative where
2+
3+class Applicative f where
4+ pure :: forall a. a -> f a
5+ (<*>) :: forall a b. f (a -> b) -> f a -> f b
6+
7+data Maybe a = Nothing | Just a
8+
9+instance Applicative Maybe where
10+ pure = Just
11+ (<*>) (Just f) (Just a) = Just (f a)
12+ (<*>) _ _ = Nothing
13+
14+module Main where
15+
16+main = Trace.trace "Done"
src/Language/PureScript/Parser/Types.hs+9−14
@@ -24,7 +24,7 @@ import Language.PureScript.Parser.Common
24import Control.Applicative
24import Control.Applicative
25import qualified Text.Parsec as P
25import qualified Text.Parsec as P
26import qualified Text.Parsec.Expr as P
26import qualified Text.Parsec.Expr as P
27−import Control.Monad (unless)
27+import Control.Monad (when, unless)
28
28
29parseNumber :: P.Parsec String ParseState Type
29parseNumber :: P.Parsec String ParseState Type
30parseNumber = const Number <$> reserved "Number"
30parseNumber = const Number <$> reserved "Number"
@@ -42,7 +42,7 @@ parseArrayOf :: P.Parsec String ParseState Type
42parseArrayOf = squares $ TypeApp Array <$> parseType
42parseArrayOf = squares $ TypeApp Array <$> parseType
43
43
44parseObject :: P.Parsec String ParseState Type
44parseObject :: P.Parsec String ParseState Type
45−parseObject = braces $ Object <$> parseRow
45+parseObject = braces $ Object <$> parseRow False
46
46
47parseFunction :: P.Parsec String ParseState Type
47parseFunction :: P.Parsec String ParseState Type
48parseFunction = do
48parseFunction = do
@@ -69,11 +69,10 @@ parseTypeAtom = indented *> P.choice (map P.try
69 , parseArray
69 , parseArray
70 , parseArrayOf
70 , parseArrayOf
71 , parseObject
71 , parseObject
72− , parseFunction
73 , parseTypeVariable
72 , parseTypeVariable
74 , parseTypeConstructor
73 , parseTypeConstructor
75 , parseForAll
74 , parseForAll
76− , parens parseRow
75+ , parens (parseRow True)
77 , parens parseType ])
76 , parens parseType ])
78
77
79parseConstrainedType :: P.Parsec String ParseState Type
78parseConstrainedType :: P.Parsec String ParseState Type
@@ -91,11 +90,10 @@ parseConstrainedType = do
91 return $ maybe ty (flip ConstrainedType ty) constraints
90 return $ maybe ty (flip ConstrainedType ty) constraints
92
91
93parseAnyType :: P.Parsec String ParseState Type
92parseAnyType :: P.Parsec String ParseState Type
94−parseAnyType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
93+parseAnyType = (P.buildExpressionParser operators $ parseTypeAtom) <|> parseFunction P.<?> "type"
95 where
94 where
96− postfixTable :: [Type -> P.Parsec String ParseState Type]
95+ operators = [ [ P.Infix (return TypeApp) P.AssocLeft ]
97− postfixTable = [ \x -> TypeApp x <$> P.try (indented *> parseTypeAtom) ]
98− operators = [ [ P.Infix (lexeme (P.try (P.string "->")) >> return (\t1 t2 -> Function [t1] t2)) P.AssocRight ] ]
96+ , [ P.Infix (P.try (lexeme (P.string "->")) >> return (\t1 t2 -> Function [t1] t2)) P.AssocRight ] ]
99
97
100-- |
98-- |
101-- Parse a monotype
99-- Parse a monotype
@@ -121,9 +119,6 @@ parseNameAndType p = (,) <$> (indented *> identifier <* indented <* lexeme (P.st
121parseRowEnding :: P.Parsec String ParseState Type
119parseRowEnding :: P.Parsec String ParseState Type
122parseRowEnding = P.option REmpty (TypeVar <$> (lexeme (indented *> P.char '|') *> indented *> identifier))
120parseRowEnding = P.option REmpty (TypeVar <$> (lexeme (indented *> P.char '|') *> indented *> identifier))
123
121
124−parseRow :: P.Parsec String ParseState Type
122+parseRow :: Bool -> P.Parsec String ParseState Type
125−parseRow = (fromList <$> (commaSep $ parseNameAndType parsePolyType) <*> parseRowEnding) P.<?> "row"
123+parseRow nonEmpty = (curry rowFromList <$> (many $ parseNameAndType parsePolyType) <*> parseRowEnding) P.<?> "row"
126− where
124+ where many = if nonEmpty then commaSep1 else commaSep
127− fromList :: [(String, Type)] -> Type -> Type
128− fromList [] r = r
129− fromList ((name, t):ts) r = RCons name t (fromList ts r)