on master help sign in
Add parsing for record types.
0205998evancz
elm/src/Parse/Expr.hs+1−1
@@ -52,7 +52,7 @@ accessor = do
52 lbl <- try (string "." >> rLabel)
52 lbl <- try (string "." >> rLabel)
53 end <- getPosition
53 end <- getPosition
54 let ctx e = addCtx ("." ++ lbl) (pos start end e)
54 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))
56
56
57
57
58-------- Complex Terms --------
58-------- Complex Terms --------
elm/src/Parse/Types.hs+10−1
@@ -16,6 +16,7 @@ import Guid
16data ParseType = VarPT String
16data ParseType = VarPT String
17 | LambdaPT ParseType ParseType
17 | LambdaPT ParseType ParseType
18 | ADTPT String [ParseType]
18 | ADTPT String [ParseType]
19+ | RecordPT [(String,ParseType)]
19
20
20listPT t = ADTPT "List" [t]
21listPT t = ADTPT "List" [t]
21tuplePT ts = ADTPT ("Tuple" ++ show (length ts)) ts
22tuplePT ts = ADTPT ("Tuple" ++ show (length ts)) ts
@@ -30,8 +31,14 @@ typeTuple :: IParser ParseType
30typeTuple = do ts <- parens (commaSep typeExpr)
31typeTuple = do ts <- parens (commaSep typeExpr)
31 return $ case ts of { [t] -> t ; _ -> tuplePT ts }
32 return $ case ts of { [t] -> t ; _ -> tuplePT ts }
32
33
34+typeRecord :: IParser ParseType
35+typeRecord = fmap RecordPT . brackets . commaSep $ do
36+ lbl <- rLabel
37+ whitespace >> string "::" >> whitespace
38+ (,) lbl <$> typeExpr
39+
33typeUnambiguous :: IParser ParseType
40typeUnambiguous :: IParser ParseType
34−typeUnambiguous = typeList <|> typeTuple
41+typeUnambiguous = typeList <|> typeTuple <|> typeRecord
35
42
36typeSimple :: IParser ParseType
43typeSimple :: IParser ParseType
37typeSimple = dealias <$> var
44typeSimple = dealias <$> var
@@ -80,6 +87,8 @@ toDatatype name args tcs = Datatype name [1..n] <$> mapM toC tcs
80 Just v -> Right v
87 Just v -> Right v
81 Nothing -> Left $ msg x
88 Nothing -> Left $ msg x
82 | otherwise = return $ ADT x []
89 | otherwise = return $ ADT x []
90+ toT (RecordPT fs) = do fs' <- mapM (\(x,pt) -> (,) x <$> toT pt) fs
91+ return (RecordT (recordT fs') EmptyRecord)
83 msg x = "Type variable '" ++ x ++
92 msg x = "Type variable '" ++ x ++
84 "' is unbound in type constructor '" ++ name ++ "'."
93 "' is unbound in type constructor '" ++ name ++ "'."
85
94
elm/src/Types/Constrain.hs−4
@@ -293,7 +293,3 @@ stmtGen (ImportEvent js e@(C txt span base) elm tipe) = do
293getDatatypeInfo (Datatype name args tcs) =
293getDatatypeInfo (Datatype name args tcs) =
294 Just (name, args, tcs)
294 Just (name, args, tcs)
295getDatatypeInfo _ = Nothing
295getDatatypeInfo _ = 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
24 | X :<<: Scheme
24 | X :<<: Scheme
25 deriving (Eq, Ord, Show)
25 deriving (Eq, Ord, Show)
26
26
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
28
30
31+tipe t = ADT t []
29
32
30int = tipe "Int"
33int = tipe "Int"
31float = tipe "Float"
34float = tipe "Float"