|
| 1 | +{-# LANGUAGE PackageImports #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +module Language.PowerQuery.PrettyPrinter where |
| 4 | + |
| 5 | +import "base" Data.Monoid ((<>)) |
| 6 | +import "text" Data.Text (Text, pack, intercalate) |
| 7 | +import "language-powerquery-ast" Language.PowerQuery.AST |
| 8 | + |
| 9 | +class PrettyPrint a where |
| 10 | + pprint :: a -> Text |
| 11 | + |
| 12 | +instance (PrettyPrint a) => PrettyPrint (Maybe a) where |
| 13 | + pprint Nothing = "" |
| 14 | + pprint (Just x) = pprint x |
| 15 | + |
| 16 | + |
| 17 | +-- 12.1.3 Tokens |
| 18 | +instance PrettyPrint Token where |
| 19 | + pprint TComment = "" |
| 20 | + pprint (TLiteral l) = pprint l |
| 21 | + pprint (TIdentifier i) = pprint i |
| 22 | + pprint (TKeyword k) = pprint k |
| 23 | + pprint (TOperator o) = pprint o |
| 24 | + pprint TEOF = "" |
| 25 | + |
| 26 | +-- 12.1.5 Literals |
| 27 | +instance PrettyPrint Literal where |
| 28 | + pprint (Logical' True) = "true" |
| 29 | + pprint (Logical' False) = "false" |
| 30 | + pprint (Integer' i) = pack . show $ i |
| 31 | + pprint (Float' f) = pack . show $ f |
| 32 | + pprint (String' t) = t |
| 33 | + pprint Null = "null" |
| 34 | + |
| 35 | +-- 12.1.6 Identifiers |
| 36 | +instance PrettyPrint Identifier where |
| 37 | + pprint (RegularIdentifier t) = t |
| 38 | + pprint (QuotedIdentifier t) = t -- "#\"" <> t <> "\"" |
| 39 | + |
| 40 | +-- 12.1.7 Keywords and predefined identifiers |
| 41 | +instance PrettyPrint Keyword where |
| 42 | + pprint And = "and" |
| 43 | + pprint As = "as" |
| 44 | + pprint Each = "each" |
| 45 | + pprint Else = "else" |
| 46 | + pprint Error = "error" |
| 47 | + pprint False' = "false" |
| 48 | + pprint If = "if" |
| 49 | + pprint In = "in" |
| 50 | + pprint Is = "is" |
| 51 | + pprint Let = "let" |
| 52 | + pprint Meta = "meta" |
| 53 | + pprint Not = "not" |
| 54 | + pprint Otherwise = "otherwise" |
| 55 | + pprint Or = "or" |
| 56 | + pprint Section = "section" |
| 57 | + pprint Shared = "shared" |
| 58 | + pprint Then = "then" |
| 59 | + pprint True' = "true" |
| 60 | + pprint Try = "try" |
| 61 | + pprint Type' = "type" |
| 62 | + pprint H_Binary = "#binary" |
| 63 | + pprint H_Date = "#date" |
| 64 | + pprint H_DateTime = "#datetime" |
| 65 | + pprint H_DateTimezone = "#datetimezone" |
| 66 | + pprint H_Duration = "#duration" |
| 67 | + pprint H_Infinity = "#infinity" |
| 68 | + pprint H_Nan = "#nan" |
| 69 | + pprint H_Sections = "#sections" |
| 70 | + pprint H_Shared = "#shared" |
| 71 | + pprint H_Table = "#table" |
| 72 | + pprint H_Time = "#time" |
| 73 | + |
| 74 | +-- 12.1.8 Operators and punctuators |
| 75 | +instance PrettyPrint Operator where |
| 76 | + pprint Comma = "," |
| 77 | + pprint SemiColon = ";" |
| 78 | + pprint Equal = "=" |
| 79 | + pprint LT' = "<" |
| 80 | + pprint LEQ = "<=" |
| 81 | + pprint GT' = ">" |
| 82 | + pprint GEQ = ">=" |
| 83 | + pprint NEQ = "<>" |
| 84 | + pprint Plus = "+" |
| 85 | + pprint Minus = "-" |
| 86 | + pprint Mult = "*" |
| 87 | + pprint Div = "/" |
| 88 | + pprint Ampersand = "&" |
| 89 | + pprint LeftParen = "(" |
| 90 | + pprint RightParen = ")" |
| 91 | + pprint LeftBracket = "[" |
| 92 | + pprint RightBracket = "]" |
| 93 | + pprint LeftCurly = "{" |
| 94 | + pprint RightCurly = "}" |
| 95 | + pprint At = "@" |
| 96 | + pprint QMark = "?" |
| 97 | + pprint Arrow = "=>" |
| 98 | + pprint TwoDots = ".." |
| 99 | + pprint ThreeDots = "..." |
| 100 | + |
| 101 | +-- 12.2.1 Documents |
| 102 | +instance (PrettyPrint a) => PrettyPrint (Document a) where |
| 103 | + pprint (SectionDocument section') = pprint section' |
| 104 | + pprint (ExpressionDocument expression') = pprint expression' |
| 105 | + |
| 106 | +-- 12.2.2 Section Documents |
| 107 | +instance (PrettyPrint a) => PrettyPrint (Section a) where |
| 108 | + pprint (Section' mAttrs mName members _) |
| 109 | + = pprint mAttrs <> " section " <> pprint mName <> " ; " <> members' |
| 110 | + where |
| 111 | + members' = mconcat . map pprint $ members |
| 112 | + |
| 113 | +instance (PrettyPrint a) => PrettyPrint (SectionMember a) where |
| 114 | + pprint (SectionMember mAttrs shared name expression _) |
| 115 | + = pprint mAttrs <> " " <> isShared shared <> " " <> pprint name <> " = " <> pprint expression <> " ; " |
| 116 | + where |
| 117 | + isShared True = "shared" |
| 118 | + isShared False = "" |
| 119 | + |
| 120 | +-- 12.2.3.1 Expressions |
| 121 | +instance (PrettyPrint a) => PrettyPrint (Expression a) where |
| 122 | + pprint _ = undefined |
| 123 | + |
| 124 | +-- 12.2.4 Literal Attributes |
| 125 | +instance (PrettyPrint a) => PrettyPrint (RecordLiteral a) where |
| 126 | + pprint (RecordLiteral fields) |
| 127 | + = "[" <> fields' <> "]" |
| 128 | + where |
| 129 | + fields' = intercalate "," . map pprint $ fields |
| 130 | + |
| 131 | +instance (PrettyPrint a) => PrettyPrint (LiteralField a) where |
| 132 | + pprint (LiteralField name literal) |
| 133 | + = pprint name <> " = " <> pprint literal |
| 134 | + |
| 135 | +instance (PrettyPrint a) => PrettyPrint (ListLiteral a) where |
| 136 | + pprint (ListLiteral items) |
| 137 | + = "{" <> items' <> "}" |
| 138 | + where |
| 139 | + items' = intercalate "," . map pprint $ items |
| 140 | + |
| 141 | +instance (PrettyPrint a) => PrettyPrint (AnyLiteral a) where |
| 142 | + pprint (Record' r) = pprint r |
| 143 | + pprint (List' ls) = pprint ls |
| 144 | + pprint (Literal' l) = pprint l |
0 commit comments