Skip to content

Commit

Permalink
feat(pretty-printing): Improve colourisation of pretty-printer output
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed May 31, 2024
1 parent 8e3d367 commit 28492c5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/Elara/AST/Generic/Instances/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Elara.AST.Name
import Elara.AST.Pretty
import Elara.AST.StripLocation
import Elara.Data.Pretty
import Elara.Data.Pretty.Styles
import Prelude hiding (group)

deriving instance Pretty (ASTLocate ast (BinaryOperator' ast)) => Pretty (BinaryOperator ast)
Expand Down Expand Up @@ -195,13 +196,13 @@ prettyExpr' ::
) =>
Expr' ast ->
Doc AnsiStyle
prettyExpr' (Int i) = pretty i
prettyExpr' (Float f) = pretty f
prettyExpr' (String s) = pretty '\"' <> pretty s <> pretty '\"'
prettyExpr' (Char c) = "'" <> escapeChar c <> "'"
prettyExpr' Unit = "()"
prettyExpr' (Var v) = pretty v
prettyExpr' (Constructor c) = pretty c
prettyExpr' (Int i) = scalar (pretty i)
prettyExpr' (Float f) = scalar (pretty f)
prettyExpr' (String s) = scalar (pretty '\"' <> pretty s <> pretty '\"')
prettyExpr' (Char c) = scalar ("'" <> escapeChar c <> "'")
prettyExpr' Unit = scalar "()"
prettyExpr' (Var v) = varName (pretty v)
prettyExpr' (Constructor c) = typeName (pretty c)
prettyExpr' (Lambda ps e) = prettyLambdaExpr (fieldToList @(ASTLocate ast (Select "LambdaPattern" ast)) ps :: [lambdaPatterns]) (prettyExpr e)
prettyExpr' (FunctionCall e1 e2) = prettyFunctionCallExpr e1 e2 False
prettyExpr' (TypeApplication e1 e2) = prettyFunctionCall e1 ("@" <> parens (pretty e2))
Expand Down
14 changes: 7 additions & 7 deletions src/Elara/AST/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ prettyFunctionCallExpr e1 e2 tyApp = prettyFunctionCall e1' e2'
tyApp' = if tyApp then "@" else ""

prettyIfExpr :: (Pretty a, Pretty b, Pretty c) => a -> b -> c -> Doc AnsiStyle
prettyIfExpr e1 e2 e3 = parens ("if" <+> pretty e1 <+> "then" <+> pretty e2 <+> "else" <+> pretty e3)
prettyIfExpr e1 e2 e3 = parens (keyword "if" <+> pretty e1 <+> keyword "then" <+> pretty e2 <+> keyword "else" <+> pretty e3)

prettyBinaryOperatorExpr :: (Pretty b, Pretty (Expr ast), RUnlocate ast) => Expr ast -> b -> Expr ast -> Doc AnsiStyle
prettyBinaryOperatorExpr e1 o e2 =
Expand All @@ -76,10 +76,10 @@ prettyTupleExpr :: Pretty a => NonEmpty a -> Doc AnsiStyle
prettyTupleExpr l = parens (hsep (punctuate "," (pretty <$> toList l)))

prettyMatchExpr :: (Pretty a1, Pretty a2, Foldable t, ?contextFree :: Bool) => a1 -> t a2 -> Doc AnsiStyle
prettyMatchExpr e m = parens ("match" <+> pretty e <+> "with" <+> prettyBlockExpr m)
prettyMatchExpr e m = parens (keyword "match" <+> pretty e <+> keyword "with" <+> prettyBlockExpr m)

prettyMatchBranch :: (Pretty a1, Pretty a2) => (a1, a2) -> Doc AnsiStyle
prettyMatchBranch (p, e) = pretty p <+> "->" <+> pretty e
prettyMatchBranch (p, e) = pretty p <+> punctuation "->" <+> pretty e

prettyLetInExpr ::
(Pretty a1, Pretty a2, ?contextFree :: Bool, RUnlocate ast, Pretty (Expr ast)) =>
Expand All @@ -89,12 +89,12 @@ prettyLetInExpr ::
Expr ast ->
Doc AnsiStyle
prettyLetInExpr v ps e1 e2 =
"let"
keyword "let"
<+> pretty v
<+> hsep (pretty <$> ps)
<+> "="
<+> blockParensIf (?contextFree && shouldBrace e1) (pretty e1)
<+> "in"
<+> keyword "in"
<+> blockParensIf (?contextFree && shouldBrace e2) (pretty e2)

shouldBrace :: forall astK (ast :: astK). RUnlocate ast => Expr ast -> Bool
Expand All @@ -112,10 +112,10 @@ shouldParen x = case (x ^. _Unwrapped % _1 % to (rUnlocate @astK @ast)) :: Expr'

prettyLetExpr :: (Pretty a1, Pretty a2, RUnlocate ast, ?contextFree :: Bool, Pretty (Expr ast)) => a1 -> [a2] -> Expr ast -> Doc AnsiStyle
prettyLetExpr v ps e =
"let"
keyword "let"
<+> pretty v
<+> hsep (pretty <$> ps)
<+> "="
<+> punctuation "="
<+> blockParensIf (?contextFree && shouldBrace e) (pretty e)

prettyBlockExpr :: (Pretty a, Foldable t, ?contextFree :: Bool) => t a -> Doc AnsiStyle
Expand Down
3 changes: 3 additions & 0 deletions src/Elara/Data/Pretty/Styles.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ punctuation = annotate (bold <> colorDull Green)
label :: Doc AnsiStyle -> Doc AnsiStyle
label = annotate mempty

scalar :: Doc AnsiStyle -> Doc AnsiStyle
scalar = annotate (colorDull Cyan)

builtin :: Doc AnsiStyle -> Doc AnsiStyle
builtin = annotate underlined

Expand Down
2 changes: 1 addition & 1 deletion src/Elara/TypeInfer/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ prettyPrimitiveType UnsolvedType{..} =
prettyPrimitiveType Record{..} =
prettyRecordType fields
prettyPrimitiveType Scalar{..} =
pretty scalar
Elara.Data.Pretty.Styles.scalar (pretty scalar)
prettyPrimitiveType Custom{..} =
if null typeArguments
then label (pretty conName)
Expand Down

0 comments on commit 28492c5

Please sign in to comment.