on master help sign in
style: update to modern style guide
96fa70cEvan Czaplicki
src/Parse/Type.hs+40−25
@@ -9,22 +9,28 @@ import qualified AST.Type as T
9import qualified AST.Variable as Var
9import qualified AST.Variable as Var
10import Parse.Helpers
10import Parse.Helpers
11
11
12+
12tvar :: IParser T.RawType
13tvar :: IParser T.RawType
14+tvar =
13−tvar = T.Var <$> lowVar <?> "type variable"
15+ T.Var <$> lowVar <?> "type variable"
14
16
15
17
16tuple :: IParser T.RawType
18tuple :: IParser T.RawType
19+tuple =
17−tuple = do ts <- parens (commaSep expr)
20+ do ts <- parens (commaSep expr)
18− return $ case ts of
21+ case ts of
19− [t] -> t
22+ [t] -> return t
20− _ -> T.tupleOf ts
23+ _ -> return (T.tupleOf ts)
24+
21
25
22record :: IParser T.RawType
26record :: IParser T.RawType
23record =
27record =
28+ do char '{'
24− do char '{' ; whitespace
29+ whitespace
25− rcrd <- extended <|> normal
30+ rcrd <- extended <|> normal
26− dumbWhitespace ; char '}'
31+ dumbWhitespace
32+ char '}'
27− return rcrd
33+ return rcrd
28 where
34 where
29 normal = flip T.Record Nothing <$> commaSep field
35 normal = flip T.Record Nothing <$> commaSep field
30
36
@@ -39,38 +45,47 @@ record =
39 whitespace >> hasType >> whitespace
45 whitespace >> hasType >> whitespace
40 (,) lbl <$> expr
46 (,) lbl <$> expr
41
47
48+
42capTypeVar :: IParser String
49capTypeVar :: IParser String
50+capTypeVar =
43−capTypeVar = intercalate "." <$> dotSep1 capVar
51+ intercalate "." <$> dotSep1 capVar
52+
44
53
45constructor0 :: IParser T.RawType
54constructor0 :: IParser T.RawType
46constructor0 =
55constructor0 =
47− do name <- capTypeVar
56+ do name <- capTypeVar
48− return (T.Type (Var.Raw name))
57+ return (T.Type (Var.Raw name))
58+
49
59
50term :: IParser T.RawType
60term :: IParser T.RawType
61+term =
51−term = tuple <|> record <|> tvar <|> constructor0
62+ tuple <|> record <|> tvar <|> constructor0
63+
52
64
53app :: IParser T.RawType
65app :: IParser T.RawType
54app =
66app =
55− do f <- constructor0 <|> try tupleCtor <?> "type constructor"
67+ do f <- constructor0 <|> try tupleCtor <?> "type constructor"
56− args <- spacePrefix term
68+ args <- spacePrefix term
57− return $ case args of
69+ case args of
58− [] -> f
70+ [] -> return f
59− _ -> T.App f args
71+ _ -> return (T.App f args)
60 where
72 where
61 tupleCtor = do
73 tupleCtor = do
62 n <- length <$> parens (many (char ','))
74 n <- length <$> parens (many (char ','))
63 let ctor = "_Tuple" ++ show (if n == 0 then 0 else n+1)
75 let ctor = "_Tuple" ++ show (if n == 0 then 0 else n+1)
64 return (T.Type (Var.Raw ctor))
76 return (T.Type (Var.Raw ctor))
65
77
78+
66expr :: IParser T.RawType
79expr :: IParser T.RawType
67expr =
80expr =
68− do t1 <- app <|> term
81+ do t1 <- app <|> term
69− arr <- optionMaybe $ try (whitespace >> arrow)
82+ arr <- optionMaybe $ try (whitespace >> arrow)
70− case arr of
83+ case arr of
71− Just _ -> T.Lambda t1 <$> (whitespace >> expr)
84+ Just _ -> T.Lambda t1 <$> (whitespace >> expr)
72− Nothing -> return t1
85+ Nothing -> return t1
86+
73
87
74constructor :: IParser (String, [T.RawType])
88constructor :: IParser (String, [T.RawType])
89+constructor =
75−constructor = (,) <$> (capTypeVar <?> "another type constructor")
90+ (,) <$> (capTypeVar <?> "another type constructor")
76− <*> spacePrefix term
91+ <*> spacePrefix term