1
1
module Language.PS.CST.Printers where
2
2
3
+ import Prelude
4
+
3
5
import Language.PS.CST.Printers.PrintImports (printImports )
4
6
import Language.PS.CST.Printers.PrintModuleModuleNameAndExports (printModuleModuleNameAndExports )
5
7
import Language.PS.CST.Printers.TypeLevel (PrintType_Style (..), printConstraint , printDataCtor , printDataHead , printFixity , printFundep , printKind , printQualifiedName_AnyOpNameType , printQualifiedName_AnyProperNameType , printQualifiedName_Ident , printType , printTypeVarBinding )
6
- import Language.PS.CST.Printers.Utils (emptyColumn , emptyRow , ifelse , lines , maybeWrapInParentheses , printAndConditionallyAddNewlinesBetween , textFromNewtype , twoSpaceIdentation , wrapInParentheses )
8
+ import Language.PS.CST.Printers.Utils (emptyColumn , emptyRow , ifelse , lines , maybeWrapInParentheses , printAndConditionallyAddNewlinesBetween , twoSpaceIdentation , wrapInParentheses )
7
9
import Language.PS.CST.Types (Binder (..), Comments (..), DeclDeriveType (..), Declaration (..), Expr (..), FixityOp (..), Foreign (..), Guarded (..), Instance , InstanceBinding (..), LetBinding (..), Module (..), RecordLabeled (..), RecordUpdate (..), Type (..), ValueBindingFields )
8
- import Prelude ( flip , map , show , (#), ($), (<#>), (<<<), (==) )
10
+ import Language.PS.CST.ReservedNames ( appendUnderscoreIfReserved , quoteIfReserved )
9
11
12
+ import Data.Newtype (unwrap )
10
13
import Data.Either (Either (..))
11
14
import Data.Foldable (any , null )
12
15
import Data.FunctorWithIndex (mapWithIndex )
@@ -92,22 +95,22 @@ printDeclaration (DeclNewtype { comments, head, name, type_ }) =
92
95
printedType :: Box
93
96
printedType = maybeWrapInParentheses (doWrap type_) $ printType PrintType_Multiline $ type_
94
97
in
95
- printMaybeComments comments // (printDataHead (text " newtype" ) head <<+>> text " =" <<+>> (textFromNewtype name <<+>> printedType))
98
+ printMaybeComments comments // (printDataHead (text " newtype" ) head <<+>> text " =" <<+>> ((text <<< appendUnderscoreIfReserved <<< unwrap) name <<+>> printedType))
96
99
printDeclaration (DeclFixity { comments, fixityFields: { keyword, precedence, operator } }) =
97
100
let
98
101
printFixityOp :: FixityOp -> Box
99
- printFixityOp (FixityValue (Left qualifiedIdent) opName) = printQualifiedName_Ident qualifiedIdent <<+>> text " as" <<+>> textFromNewtype opName
100
- printFixityOp (FixityValue (Right qualifiedPropName) opName) = printQualifiedName_AnyProperNameType qualifiedPropName <<+>> text " as" <<+>> textFromNewtype opName
101
- printFixityOp (FixityType qualifiedPropName opName) = text " type" <<+>> printQualifiedName_AnyProperNameType qualifiedPropName <<+>> text " as" <<+>> textFromNewtype opName
102
+ printFixityOp (FixityValue (Left qualifiedIdent) opName) = printQualifiedName_Ident qualifiedIdent <<+>> text " as" <<+>> (text <<< appendUnderscoreIfReserved <<< unwrap) opName
103
+ printFixityOp (FixityValue (Right qualifiedPropName) opName) = printQualifiedName_AnyProperNameType qualifiedPropName <<+>> text " as" <<+>> (text <<< appendUnderscoreIfReserved <<< unwrap) opName
104
+ printFixityOp (FixityType qualifiedPropName opName) = text " type" <<+>> printQualifiedName_AnyProperNameType qualifiedPropName <<+>> text " as" <<+>> (text <<< appendUnderscoreIfReserved <<< unwrap) opName
102
105
in
103
106
printMaybeComments comments // (printFixity keyword <<+>> text (show precedence) <<+>> printFixityOp operator)
104
107
printDeclaration (DeclForeign { comments, foreign_ }) =
105
108
printMaybeComments comments //
106
109
( text " foreign" <<+>> text " import" <<+>>
107
110
case foreign_ of
108
- (ForeignValue { ident, type_ }) -> textFromNewtype ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_
109
- (ForeignData { name, kind_ }) -> text " data" <<+>> textFromNewtype name <<+>> text " ::" <<+>> printKind kind_
110
- (ForeignKind { name }) -> text " kind" <<+>> textFromNewtype name
111
+ (ForeignValue { ident, type_ }) -> (text <<< appendUnderscoreIfReserved <<< unwrap) ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_
112
+ (ForeignData { name, kind_ }) -> text " data" <<+>> (text <<< appendUnderscoreIfReserved <<< unwrap) name <<+>> text " ::" <<+>> printKind kind_
113
+ (ForeignKind { name }) -> text " kind" <<+>> (text <<< appendUnderscoreIfReserved <<< unwrap) name
111
114
)
112
115
printDeclaration (DeclDerive { comments, deriveType, head: { instName, instConstraints, instClass, instTypes } }) =
113
116
let
@@ -132,7 +135,7 @@ printDeclaration (DeclDerive { comments, deriveType, head: { instName, instConst
132
135
133
136
types' = punctuateH left (emptyColumn) $ map (\type_ -> maybeWrapInParentheses (doWrap type_) $ printType PrintType_OneLine type_) instTypes
134
137
in
135
- printMaybeComments comments // (text " derive" <<>> deriveType' <<+>> text " instance" <<+>> textFromNewtype instName <<+>> text " ::" <<>> constraints' <<+>> printQualifiedName_AnyProperNameType instClass <<+>> types')
138
+ printMaybeComments comments // (text " derive" <<>> deriveType' <<+>> text " instance" <<+>> (text <<< appendUnderscoreIfReserved <<< unwrap) instName <<+>> text " ::" <<>> constraints' <<+>> printQualifiedName_AnyProperNameType instClass <<+>> types')
136
139
printDeclaration (DeclClass { comments, head: { name, vars, super, fundeps }, methods }) =
137
140
let
138
141
printedVars =
@@ -151,15 +154,15 @@ printDeclaration (DeclClass { comments, head: { name, vars, super, fundeps }, me
151
154
then nullBox
152
155
else emptyColumn <<>> text " |" <<+>> (fundeps # map printFundep # punctuateH left (text " , " ))
153
156
154
- printedHeader = text " class" <<+>> printedSuper <<>> textFromNewtype name <<>> printedVars <<>> printedFundeps
157
+ printedHeader = text " class" <<+>> printedSuper <<>> (text <<< appendUnderscoreIfReserved <<< unwrap) name <<>> printedVars <<>> printedFundeps
155
158
in
156
159
if null methods
157
160
then printMaybeComments comments // printedHeader
158
161
else
159
162
printMaybeComments comments //
160
163
( printedHeader <<+>> (text " where" )
161
164
// (methods
162
- <#> (\({ ident, type_ }) -> textFromNewtype ident <<+>> text " ::" <<+>> (printType PrintType_Multiline type_))
165
+ <#> (\({ ident, type_ }) -> (text <<< appendUnderscoreIfReserved <<< unwrap) ident <<+>> text " ::" <<+>> (printType PrintType_Multiline type_))
163
166
<#> (twoSpaceIdentation <<>> _)
164
167
# vcat left
165
168
)
@@ -169,7 +172,7 @@ printDeclaration (DeclInstanceChain { comments, instances }) =
169
172
printInstance :: Instance -> Box
170
173
printInstance { head: { instName, instConstraints, instClass, instTypes }, body } =
171
174
let
172
- head = text " instance" <<+>> textFromNewtype instName <<+>> text " ::" <<+>> printQualifiedName_AnyProperNameType instClass
175
+ head = text " instance" <<+>> (text <<< appendUnderscoreIfReserved <<< unwrap) instName <<+>> text " ::" <<+>> printQualifiedName_AnyProperNameType instClass
173
176
174
177
doWrap :: Type -> Boolean
175
178
doWrap (TypeApp _ _) = true
@@ -196,11 +199,11 @@ printDeclaration (DeclInstanceChain { comments, instances }) =
196
199
// (firstRow <<+>> text " where" )
197
200
// (twoSpaceIdentation <<>> printedBody)
198
201
in instances <#> printInstance # punctuateV left (nullBox /+/ text " else" /+/ nullBox)
199
- printDeclaration (DeclSignature { comments, ident, type_ }) = printMaybeComments comments // (textFromNewtype ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_)
202
+ printDeclaration (DeclSignature { comments, ident, type_ }) = printMaybeComments comments // ((text <<< appendUnderscoreIfReserved <<< unwrap) ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_)
200
203
printDeclaration (DeclValue { comments, valueBindingFields }) = printMaybeComments comments // (printValueBindingFields valueBindingFields)
201
204
202
205
printInstanceBinding :: InstanceBinding -> Box
203
- printInstanceBinding (InstanceBindingSignature { ident, type_ }) = textFromNewtype ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_
206
+ printInstanceBinding (InstanceBindingSignature { ident, type_ }) = (text <<< appendUnderscoreIfReserved <<< unwrap) ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_
204
207
printInstanceBinding (InstanceBindingName valueBindingFields) = printValueBindingFields valueBindingFields
205
208
206
209
printValueBindingFields :: ValueBindingFields -> Box
@@ -211,7 +214,7 @@ printValueBindingFields { name, binders, guarded } =
211
214
then nullBox
212
215
else (punctuateH left emptyColumn $ map printBinder binders) <<>> emptyColumn
213
216
214
- printedHead = textFromNewtype name <<+>> printedBinders <<>> text " ="
217
+ printedHead = (text <<< appendUnderscoreIfReserved <<< unwrap) name <<+>> printedBinders <<>> text " ="
215
218
in printGuarded printedHead guarded
216
219
217
220
printGuarded :: Box -> Guarded -> Box
@@ -240,8 +243,8 @@ exprShouldBeOnNextLine _ = false
240
243
241
244
printBinder :: Binder -> Box
242
245
printBinder BinderWildcard = text " _"
243
- printBinder (BinderVar ident) = textFromNewtype ident
244
- printBinder (BinderNamed { ident, binder }) = textFromNewtype ident <<>> text " @" <<>> (wrapInParentheses $ printBinder binder)
246
+ printBinder (BinderVar ident) = (text <<< appendUnderscoreIfReserved <<< unwrap) ident
247
+ printBinder (BinderNamed { ident, binder }) = (text <<< appendUnderscoreIfReserved <<< unwrap) ident <<>> text " @" <<>> (wrapInParentheses $ printBinder binder)
245
248
printBinder (BinderConstructor { name, args: [] }) = printQualifiedName_AnyProperNameType name
246
249
printBinder (BinderConstructor { name, args }) = printQualifiedName_AnyProperNameType name <<+>> (punctuateH left emptyColumn $ map printBinder args)
247
250
printBinder (BinderBoolean boolean) = text $ show boolean
@@ -255,11 +258,11 @@ printBinder (BinderTyped binder type_) = printBinder binder <<+>> text "::" <<+>
255
258
printBinder (BinderOp binderLeft operator binderRight) = printBinder binderLeft <<+>> printQualifiedName_AnyOpNameType operator <<+>> printBinder binderRight
256
259
257
260
printRecordLabeled :: ∀ a . (a -> Box ) -> RecordLabeled a -> Box
258
- printRecordLabeled _ (RecordPun ident) = textFromNewtype ident
259
- printRecordLabeled print (RecordField label a) = textFromNewtype label <<>> text " :" <<>> print a
261
+ printRecordLabeled _ (RecordPun ident) = (text <<< quoteIfReserved <<< unwrap) ident
262
+ printRecordLabeled print (RecordField label a) = (text <<< quoteIfReserved <<< unwrap) label <<>> text " :" <<+ >> print a
260
263
261
264
printExpr :: Expr -> Box
262
- printExpr (ExprHole hole) = text " ?" <<>> textFromNewtype hole
265
+ printExpr (ExprHole hole) = text " ?" <<>> (text <<< appendUnderscoreIfReserved <<< unwrap) hole
263
266
printExpr ExprSection = text " _"
264
267
printExpr (ExprIdent qualifiedIdent) = printQualifiedName_Ident qualifiedIdent
265
268
printExpr (ExprConstructor qualifiedPropName) = printQualifiedName_AnyProperNameType qualifiedPropName
@@ -269,13 +272,13 @@ printExpr (ExprString string) = text $ show string
269
272
printExpr (ExprNumber (Left int)) = text $ show int
270
273
printExpr (ExprNumber (Right num)) = text $ show num
271
274
printExpr (ExprArray array) = text " [" <<>> (punctuateH left (text " , " ) $ map printExpr array) <<>> text " ]"
272
- printExpr (ExprRecord arrayRecordLabeled) = punctuateH left (text " , " ) $ map (printRecordLabeled printExpr) arrayRecordLabeled
275
+ printExpr (ExprRecord arrayRecordLabeled) = text " { " <<+>> ( punctuateH left (text " , " ) $ map (printRecordLabeled printExpr) arrayRecordLabeled) <<+>> text " } "
273
276
printExpr (ExprTyped expr type_) = printExpr expr <<+>> text " ::" <<+>> printType PrintType_OneLine type_
274
277
printExpr (ExprInfix exprLeft operator exprRight) = printExpr exprLeft <<+>> printExpr operator <<+>> printExpr exprRight
275
278
printExpr (ExprOp exprLeft operator exprRight) = printExpr exprLeft <<+>> printQualifiedName_AnyOpNameType operator <<+>> printExpr exprRight
276
279
printExpr (ExprOpName opName) = printQualifiedName_AnyOpNameType opName
277
280
printExpr (ExprNegate expr) = text " -" <<>> printExpr expr
278
- printExpr (ExprRecordAccessor { recExpr, recPath }) = printExpr recExpr <<>> text " ." <<>> (punctuateH left (text " ." ) $ map textFromNewtype recPath)
281
+ printExpr (ExprRecordAccessor { recExpr, recPath }) = printExpr recExpr <<>> text " ." <<>> (punctuateH left (text " ." ) $ map (text <<< appendUnderscoreIfReserved <<< unwrap) recPath)
279
282
printExpr (ExprRecordUpdate expr recordUpdates) = wrapInParentheses $ printExpr expr <<+>> printRecordUpdates recordUpdates
280
283
printExpr (ExprApp exprLeft exprRight) =
281
284
let
@@ -365,13 +368,13 @@ printExpr (ExprDo doStatements) = nullBox -- TODO
365
368
printExpr (ExprAdo { statements, result }) = nullBox -- TODO
366
369
367
370
printLetBinding :: LetBinding -> Box
368
- printLetBinding (LetBindingSignature { ident, type_ }) = textFromNewtype ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_
371
+ printLetBinding (LetBindingSignature { ident, type_ }) = (text <<< appendUnderscoreIfReserved <<< unwrap) ident <<+>> text " ::" <<+>> printType PrintType_Multiline type_
369
372
printLetBinding (LetBindingName valueBindingFields) = printValueBindingFields valueBindingFields
370
373
printLetBinding (LetBindingPattern { binder, where_: { expr, whereBindings } }) = printBinder binder /+/ printExpr expr // text " where" // (vsep 1 left $ map printLetBinding whereBindings)
371
374
372
375
printRecordUpdates :: NonEmptyArray RecordUpdate -> Box
373
376
printRecordUpdates recordUpdates = text " {" <<+>> (punctuateH left (text " ," ) $ map printRecordUpdate recordUpdates) <<+>> text " }"
374
377
375
378
printRecordUpdate :: RecordUpdate -> Box
376
- printRecordUpdate (RecordUpdateLeaf label expr) = textFromNewtype label <<+>> text " =" <<+>> printExpr expr
377
- printRecordUpdate (RecordUpdateBranch label recordUpdates) = textFromNewtype label <<+>> text " =" <<+>> printRecordUpdates recordUpdates
379
+ printRecordUpdate (RecordUpdateLeaf label expr) = (text <<< appendUnderscoreIfReserved <<< unwrap) label <<+>> text " =" <<+>> printExpr expr
380
+ printRecordUpdate (RecordUpdateBranch label recordUpdates) = (text <<< appendUnderscoreIfReserved <<< unwrap) label <<+>> text " =" <<+>> printRecordUpdates recordUpdates
0 commit comments