git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix precedence of -> and type application #144
commitd6985082d130fd133ea94614947b67919dec0a1e authorPhil Freeman <paf31@cantab.net> dateTue 28 Jan 2014, 21:23 (13 years ago) parent1cb455b
2 files changed+25−14
examples/passing/Applicative.purs+16 src/Language/PureScript/Parser/Types.hs+9−14
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
2424import Control.Applicative
2525import qualified Text.Parsec as P
2626import qualified Text.Parsec.Expr as P
27−import Control.Monad (unless)
27+import Control.Monad (when, unless)
2828
2929parseNumber :: P.Parsec String ParseState Type
3030parseNumber = const Number <$> reserved "Number"
@@ -42,7 +42,7 @@ parseArrayOf :: P.Parsec String ParseState Type
4242parseArrayOf = squares $ TypeApp Array <$> parseType
4343
4444parseObject :: P.Parsec String ParseState Type
45−parseObject = braces $ Object <$> parseRow
45+parseObject = braces $ Object <$> parseRow False
4646
4747parseFunction :: P.Parsec String ParseState Type
4848parseFunction = do
@@ -69,11 +69,10 @@ parseTypeAtom = indented *> P.choice (map P.try
6969 , parseArray
7070 , parseArrayOf
7171 , parseObject
72− , parseFunction
7372 , parseTypeVariable
7473 , parseTypeConstructor
7574 , parseForAll
76− , parens parseRow
75+ , parens (parseRow True)
7776 , parens parseType ])
7877
7978parseConstrainedType :: P.Parsec String ParseState Type
@@ -91,11 +90,10 @@ parseConstrainedType = do
9190 return $ maybe ty (flip ConstrainedType ty) constraints
9291
9392parseAnyType :: P.Parsec String ParseState Type
94−parseAnyType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
93+parseAnyType = (P.buildExpressionParser operators $ parseTypeAtom) <|> parseFunction P.<?> "type"
9594 where
96− postfixTable :: [Type -> P.Parsec String ParseState Type]
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 ] ]
95+ operators = [ [ P.Infix (return TypeApp) P.AssocLeft ]
96+ , [ P.Infix (P.try (lexeme (P.string "->")) >> return (\t1 t2 -> Function [t1] t2)) P.AssocRight ] ]
9997
10098-- |
10199-- Parse a monotype
@@ -121,9 +119,6 @@ parseNameAndType p = (,) <$> (indented *> identifier <* indented <* lexeme (P.st
121119parseRowEnding :: P.Parsec String ParseState Type
122120parseRowEnding = P.option REmpty (TypeVar <$> (lexeme (indented *> P.char '|') *> indented *> identifier))
123121
124−parseRow :: P.Parsec String ParseState Type
125−parseRow = (fromList <$> (commaSep $ parseNameAndType parsePolyType) <*> parseRowEnding) P.<?> "row"
126− where
127− fromList :: [(String, Type)] -> Type -> Type
128− fromList [] r = r
129− fromList ((name, t):ts) r = RCons name t (fromList ts r)
122+parseRow :: Bool -> P.Parsec String ParseState Type
123+parseRow nonEmpty = (curry rowFromList <$> (many $ parseNameAndType parsePolyType) <*> parseRowEnding) P.<?> "row"
124+ where many = if nonEmpty then commaSep1 else commaSep