Skip to content

Commit 7bdba61

Browse files
committed
pretty printer initial
1 parent 9324a24 commit 7bdba61

File tree

5 files changed

+165
-65
lines changed

5 files changed

+165
-65
lines changed

language-powerquery-ast/language-powerquery-ast.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cabal-version: 2.4
2-
-- Initial package description 'language-powerquery.cabal' generated by
2+
-- Initial package description 'language-powerquery-ast.cabal' generated by
33
-- 'cabal init'. For further documentation, see
44
-- http://haskell.org/cabal/users-guide/
55

@@ -18,7 +18,8 @@ category: Language
1818
extra-source-files: CHANGELOG.md
1919

2020
library
21-
exposed-modules: Language.PowerQuery.AST.Token
21+
exposed-modules: Language.PowerQuery.AST
22+
, Language.PowerQuery.AST.Token
2223
, Language.PowerQuery.AST.AST
2324
, Language.PowerQuery.AST.Lens
2425
, Language.PowerQuery.AST.JSON
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# OPTIONS_GHC -fno-warn-dodgy-exports #-}
2+
module Language.PowerQuery.AST ( module Language.PowerQuery.AST.Annotation
3+
, module Language.PowerQuery.AST.Token
4+
, module Language.PowerQuery.AST.AST
5+
, module Language.PowerQuery.AST.Lens
6+
, module Language.PowerQuery.AST.JSON
7+
, module Language.PowerQuery.AST.Arbitrary
8+
) where
9+
import Language.PowerQuery.AST.Annotation
10+
import Language.PowerQuery.AST.Token
11+
import Language.PowerQuery.AST.AST
12+
import Language.PowerQuery.AST.Lens ()
13+
import Language.PowerQuery.AST.JSON ()
14+
import Language.PowerQuery.AST.Arbitrary ()

language-powerquery-ast/src/Language/PowerQuery/AST/Token.hs

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ data Token
1717
| TKeyword Keyword
1818
| TOperator Operator
1919
| TEOF
20-
deriving (Show, Eq, Data, Typeable, Generic)
20+
deriving (Show, Read, Eq, Data, Typeable, Generic)
2121

2222
-- 12.1.5 Literals
2323
data Literal
@@ -67,44 +67,11 @@ data Keyword
6767
| H_Shared
6868
| H_Table
6969
| H_Time
70-
deriving (Eq, Ord, Enum, Bounded, Data, Typeable, Generic)
70+
deriving (Show, Read, Eq, Ord, Enum, Bounded, Data, Typeable, Generic)
7171

7272
keywords :: [Keyword]
7373
keywords = [toEnum 0 .. ]
7474

75-
instance Show Keyword where
76-
show And = "and"
77-
show As = "as"
78-
show Each = "each"
79-
show Else = "else"
80-
show Error = "error"
81-
show False' = "false"
82-
show If = "if"
83-
show In = "in"
84-
show Is = "is"
85-
show Let = "let"
86-
show Meta = "meta"
87-
show Not = "not"
88-
show Otherwise = "otherwise"
89-
show Or = "or"
90-
show Section = "section"
91-
show Shared = "shared"
92-
show Then = "then"
93-
show True' = "true"
94-
show Try = "try"
95-
show Type' = "type"
96-
show H_Binary = "#binary"
97-
show H_Date = "#date"
98-
show H_DateTime = "#datetime"
99-
show H_DateTimezone = "#datetimezone"
100-
show H_Duration = "#duration"
101-
show H_Infinity = "#infinity"
102-
show H_Nan = "#nan"
103-
show H_Sections = "#sections"
104-
show H_Shared = "#shared"
105-
show H_Table = "#table"
106-
show H_Time = "#time"
107-
10875

10976
-- 12.1.8 Operators and punctuators
11077
data Operator
@@ -132,34 +99,7 @@ data Operator
13299
| Arrow
133100
| TwoDots
134101
| ThreeDots
135-
deriving (Eq, Ord, Enum, Bounded, Data, Typeable, Generic)
102+
deriving (Show, Read, Eq, Ord, Enum, Bounded, Data, Typeable, Generic)
136103

137104
operators :: [Operator]
138105
operators = [toEnum 0 .. ]
139-
140-
instance Show Operator where
141-
show Comma = ","
142-
show SemiColon = ";"
143-
show Equal = "="
144-
show LT' = "<"
145-
show LEQ = "<="
146-
show GT' = ">"
147-
show GEQ = ">="
148-
show NEQ = "<>"
149-
show Plus = "+"
150-
show Minus = "-"
151-
show Mult = "*"
152-
show Div = "/"
153-
show Ampersand = "&"
154-
show LeftParen = "("
155-
show RightParen = ")"
156-
show LeftBracket = "["
157-
show RightBracket = "]"
158-
show LeftCurly = "{"
159-
show RightCurly = "}"
160-
show At = "@"
161-
show QMark = "?"
162-
show Arrow = "=>"
163-
show TwoDots = ".."
164-
show ThreeDots = "..."
165-

language-powerquery/language-powerquery.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extra-source-files: CHANGELOG.md
2020
library
2121
exposed-modules: Language.PowerQuery.Lexer
2222
, Language.PowerQuery.Parser
23+
, Language.PowerQuery.PrettyPrinter
2324
, Language.PowerQuery.Main
2425
-- other-modules:
2526
-- other-extensions:
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)