on master help sign in
Use Reporting.Render.Type in Elm.Compiler.Type

Should make the output of all these functions more uniform.

commit117100ef408a925bd81a7ffd58910eee5219b9b5 authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 18 Apr 2018, 14:31 (9 years ago) parentcb14fe8
1 file changed+39−74
compiler/src/Elm/Compiler/Type.hs+39−74
compiler/src/Elm/Compiler/Type.hs+39−74
@@ -2,7 +2,7 @@
22{-# LANGUAGE OverloadedStrings #-}
33module Elm.Compiler.Type
44 ( Type(..)
5− , Context(..)
5+ , RT.Context(..)
66 , toDoc
77 , DebugMetadata(..)
88 , Alias(..)
@@ -23,7 +23,9 @@ import qualified Parse.Primitives as Parse
2323import qualified Parse.Type as Type
2424import qualified Reporting.Annotation as A
2525import 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+
2729import qualified Json.Decode as Decode
2830import qualified Json.Encode as Encode
2931import Json.Encode ((==>))
@@ -34,12 +36,12 @@ import Json.Encode ((==>))
3436
3537
3638data Type
37− = Lambda Type Type
38− | Var N.Name
39− | Type N.Name [Type]
40− | Record [(N.Name, Type)] (Maybe N.Name)
41− | Unit
42− | Tuple Type Type [Type]
39+ = Lambda Type Type
40+ | Var N.Name
41+ | Type N.Name [Type]
42+ | Record [(N.Name, Type)] (Maybe N.Name)
43+ | Unit
44+ | Tuple Type Type [Type]
4345
4446
4547data DebugMetadata =
@@ -58,90 +60,53 @@ data Union = Union N.Name [N.Name] [(N.Name, [Type])]
5860-- TO DOC
5961
6062
61−data Context = None | InType | InFunction
62−
63−
64−toDoc :: Context -> Type -> D.Doc
65−toDoc context tipe =
63+toDoc :: L.Localizer -> RT.Context -> Type -> D.Doc
64+toDoc localizer context tipe =
6665 case tipe of
6766 Lambda _ _ ->
68− let t:ts =
69− map (toDoc InFunction) (collectLambdas tipe)
70−
71− lambda =
72− D.sep [ t, D.sep (map ("->" <+>) ts) ]
73− in
74− case context of
75− None -> lambda
76− _ -> "(" <> lambda <> ")"
67+ let
68+ a:b:cs =
69+ map (toDoc localizer RT.Func) (collectLambdas tipe)
70+ in
71+ RT.lambda context a b cs
7772
7873 Var name ->
79− D.fromName name
74+ D.fromName name
8075
8176 Unit ->
82− "()"
77+ "()"
8378
8479 Tuple a b cs ->
85− D.sep
86− [ D.cat $
87− [ "(" <+> toDoc None a
88− , "," <+> toDoc None b
89− ]
90− ++ map ("," <+>) (map (toDoc None) cs)
91− , ")"
92− ]
80+ RT.tuple
81+ (toDoc localizer RT.None a)
82+ (toDoc localizer RT.None b)
83+ (map (toDoc localizer RT.None) cs)
9384
9485 Type name args ->
95− case args of
96− [] ->
97− D.fromName name
98−
99− _ ->
100− let
101− docName =
102− D.fromName name
103−
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− "{}"
86+ RT.apply
87+ context
88+ (D.fromName name)
89+ (map (toDoc localizer RT.App) args)
11690
11791 Record fields ext ->
118− case ext of
119− Nothing ->
120− D.sep
121− [ D.cat (zipWith (<+>) ("{" : repeat ",") (map entryToDoc fields))
122− , "}"
123− ]
92+ RT.record
93+ (map (entryToDoc localizer) fields)
94+ (fmap D.fromName ext)
12495
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− ]
13396
134−
135−entryToDoc :: (N.Name, Type) -> D.Doc
136−entryToDoc (field, fieldType) =
137− D.fromName field <+> ":" <+> toDoc None fieldType
97+entryToDoc :: L.Localizer -> (N.Name, Type) -> (D.Doc, D.Doc)
98+entryToDoc localizer (field, fieldType) =
99+ ( D.fromName field, toDoc localizer RT.None fieldType )
138100
139101
140102collectLambdas :: Type -> [Type]
141103collectLambdas tipe =
142104 case tipe of
143− Lambda arg body -> arg : collectLambdas body
144− _ -> [tipe]
105+ Lambda arg body ->
106+ arg : collectLambdas body
107+
108+ _ ->
109+ [tipe]
145110
146111
147112
@@ -150,7 +115,7 @@ collectLambdas tipe =
150115
151116encode :: Type -> Encode.Value
152117encode tipe =
153− Encode.text $ Text.pack $ D.toLine (toDoc None tipe)
118+ Encode.text $ Text.pack $ D.toLine (toDoc L.empty RT.None tipe)
154119
155120
156121decoder :: Decode.Decoder () Type