on master help sign in
Add parsing for record types.
commit02059984925ab1e53298533f68422cf9c1a1ff23 authorevancz <info@elm-lang.org> dateThu 3 Jan 2013, 00:30 (14 years ago) parent9ecb6a7
4 files changed+15−7
elm/src/Parse/Expr.hs+1−1 elm/src/Parse/Types.hs+10−1 elm/src/Types/Constrain.hs−4 elm/src/Types/Types.hs+4−1
elm/src/Parse/Expr.hs+1−1
@@ -52,7 +52,7 @@ accessor = do
5252 lbl <- try (string "." >> rLabel)
5353 end <- getPosition
5454 let ctx e = addCtx ("." ++ lbl) (pos start end e)
55− return (Lambda "r" (ctx $ Access (ctx $ Var "r") lbl))
55+ return (Lambda "_" (ctx $ Access (ctx $ Var "_") lbl))
5656
5757
5858-------- Complex Terms --------
elm/src/Parse/Types.hs+10−1
@@ -16,6 +16,7 @@ import Guid
1616data ParseType = VarPT String
1717 | LambdaPT ParseType ParseType
1818 | ADTPT String [ParseType]
19+ | RecordPT [(String,ParseType)]
1920
2021listPT t = ADTPT "List" [t]
2122tuplePT ts = ADTPT ("Tuple" ++ show (length ts)) ts
@@ -30,8 +31,14 @@ typeTuple :: IParser ParseType
3031typeTuple = do ts <- parens (commaSep typeExpr)
3132 return $ case ts of { [t] -> t ; _ -> tuplePT ts }
3233
34+typeRecord :: IParser ParseType
35+typeRecord = fmap RecordPT . brackets . commaSep $ do
36+ lbl <- rLabel
37+ whitespace >> string "::" >> whitespace
38+ (,) lbl <$> typeExpr
39+
3340typeUnambiguous :: IParser ParseType
34−typeUnambiguous = typeList <|> typeTuple
41+typeUnambiguous = typeList <|> typeTuple <|> typeRecord
3542
3643typeSimple :: IParser ParseType
3744typeSimple = dealias <$> var
@@ -80,6 +87,8 @@ toDatatype name args tcs = Datatype name [1..n] <$> mapM toC tcs
8087 Just v -> Right v
8188 Nothing -> Left $ msg x
8289 | otherwise = return $ ADT x []
90+ toT (RecordPT fs) = do fs' <- mapM (\(x,pt) -> (,) x <$> toT pt) fs
91+ return (RecordT (recordT fs') EmptyRecord)
8392 msg x = "Type variable '" ++ x ++
8493 "' is unbound in type constructor '" ++ name ++ "'."
8594
elm/src/Types/Constrain.hs−4
@@ -293,7 +293,3 @@ stmtGen (ImportEvent js e@(C txt span base) elm tipe) = do
293293getDatatypeInfo (Datatype name args tcs) =
294294 Just (name, args, tcs)
295295getDatatypeInfo _ = Nothing
296−
297−recordT :: [(String,Type)] -> Map.Map String [Type]
298−recordT fields =
299− foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
elm/src/Types/Types.hs+4−1
@@ -24,8 +24,11 @@ data Constraint = Type :=: Type
2424 | X :<<: Scheme
2525 deriving (Eq, Ord, Show)
2626
27−tipe t = ADT t []
27+recordT :: [(String,Type)] -> Map.Map String [Type]
28+recordT fields =
29+ foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
2830
31+tipe t = ADT t []
2932
3033int = tipe "Int"
3134float = tipe "Float"