on master help sign in
Use Reporting.Render.Type in Elm.Compiler.Type
117100eEvan Czaplicki
compiler/src/Elm/Compiler/Type.hs+39−74
@@ -2,7 +2,7 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
3module Elm.Compiler.Type
4 ( Type(..)
4 ( Type(..)
5− , Context(..)
5+ , RT.Context(..)
6 , toDoc
6 , toDoc
7 , DebugMetadata(..)
7 , DebugMetadata(..)
8 , Alias(..)
8 , Alias(..)
@@ -23,7 +23,9 @@ import qualified Parse.Primitives as Parse
23import qualified Parse.Type as Type
23import qualified Parse.Type as Type
24import qualified Reporting.Annotation as A
24import qualified Reporting.Annotation as A
25import qualified Reporting.Doc as D
25import qualified Reporting.Doc as D
26−import Reporting.Doc ((<>), (<+>))
26+import qualified Reporting.Render.Type as RT
27+import qualified Reporting.Render.Type.Localizer as L
28+
27import qualified Json.Decode as Decode
29import qualified Json.Decode as Decode
28import qualified Json.Encode as Encode
30import qualified Json.Encode as Encode
29import Json.Encode ((==>))
31import Json.Encode ((==>))
@@ -34,12 +36,12 @@ import Json.Encode ((==>))
34
36
35
37
36data Type
38data Type
37− = Lambda Type Type
39+ = Lambda Type Type
38− | Var N.Name
40+ | Var N.Name
39− | Type N.Name [Type]
41+ | Type N.Name [Type]
40− | Record [(N.Name, Type)] (Maybe N.Name)
42+ | Record [(N.Name, Type)] (Maybe N.Name)
41− | Unit
43+ | Unit
42− | Tuple Type Type [Type]
44+ | Tuple Type Type [Type]
43
45
44
46
45data DebugMetadata =
47data DebugMetadata =
@@ -58,90 +60,53 @@ data Union = Union N.Name [N.Name] [(N.Name, [Type])]
58-- TO DOC
60-- TO DOC
59
61
60
62
61−data Context = None | InType | InFunction
62−
63−
64−toDoc :: Context -> Type -> D.Doc
63+toDoc :: L.Localizer -> RT.Context -> Type -> D.Doc
65−toDoc context tipe =
64+toDoc localizer context tipe =
66 case tipe of
65 case tipe of
67 Lambda _ _ ->
66 Lambda _ _ ->
68− let t:ts =
67+ let
68+ a:b:cs =
69− map (toDoc InFunction) (collectLambdas tipe)
69+ map (toDoc localizer RT.Func) (collectLambdas tipe)
70−
71− lambda =
72− D.sep [ t, D.sep (map ("->" <+>) ts) ]
73− in
70+ in
74− case context of
71+ RT.lambda context a b cs
75− None -> lambda
76− _ -> "(" <> lambda <> ")"
77
72
78 Var name ->
73 Var name ->
79− D.fromName name
74+ D.fromName name
80
75
81 Unit ->
76 Unit ->
82− "()"
77+ "()"
83
78
84 Tuple a b cs ->
79 Tuple a b cs ->
85− D.sep
80+ RT.tuple
86− [ D.cat $
81+ (toDoc localizer RT.None a)
87− [ "(" <+> toDoc None a
82+ (toDoc localizer RT.None b)
88− , "," <+> toDoc None b
89− ]
90− ++ map ("," <+>) (map (toDoc None) cs)
83+ (map (toDoc localizer RT.None) cs)
91− , ")"
92− ]
93
84
94 Type name args ->
85 Type name args ->
95− case args of
86+ RT.apply
96− [] ->
87+ context
97− D.fromName name
98−
99− _ ->
100− let
101− docName =
102− D.fromName name
88+ (D.fromName name)
103−
89+ (map (toDoc localizer RT.App) args)
104− application =
105− D.hang 2 $ D.sep (docName : map (toDoc InType) args)
106− in
107− case context of
108− InType ->
109− "(" <> application <> ")"
110−
111− _ ->
112− application
113−
114− Record [] Nothing ->
115− "{}"
116
90
117 Record fields ext ->
91 Record fields ext ->
118− case ext of
92+ RT.record
119− Nothing ->
120− D.sep
121− [ D.cat (zipWith (<+>) ("{" : repeat ",") (map entryToDoc fields))
93+ (map (entryToDoc localizer) fields)
122− , "}"
94+ (fmap D.fromName ext)
123− ]
124
95
125− Just x ->
126− D.sep
127− [ D.hang 4 $ D.sep $
128− [ "{" <+> D.fromName x
129− , D.cat (zipWith (<+>) ("|" : repeat ",") (map entryToDoc fields))
130− ]
131− , "}"
132− ]
133
96
134−
135−entryToDoc :: (N.Name, Type) -> D.Doc
97+entryToDoc :: L.Localizer -> (N.Name, Type) -> (D.Doc, D.Doc)
136−entryToDoc (field, fieldType) =
98+entryToDoc localizer (field, fieldType) =
137− D.fromName field <+> ":" <+> toDoc None fieldType
99+ ( D.fromName field, toDoc localizer RT.None fieldType )
138
100
139
101
140collectLambdas :: Type -> [Type]
102collectLambdas :: Type -> [Type]
141collectLambdas tipe =
103collectLambdas tipe =
142 case tipe of
104 case tipe of
105+ Lambda arg body ->
143− Lambda arg body -> arg : collectLambdas body
106+ arg : collectLambdas body
107+
108+ _ ->
144− _ -> [tipe]
109+ [tipe]
145
110
146
111
147
112
@@ -150,7 +115,7 @@ collectLambdas tipe =
150
115
151encode :: Type -> Encode.Value
116encode :: Type -> Encode.Value
152encode tipe =
117encode tipe =
153− Encode.text $ Text.pack $ D.toLine (toDoc None tipe)
118+ Encode.text $ Text.pack $ D.toLine (toDoc L.empty RT.None tipe)
154
119
155
120
156decoder :: Decode.Decoder () Type
121decoder :: Decode.Decoder () Type