forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS.hs
More file actions
303 lines (279 loc) · 11 KB
/
JS.hs
File metadata and controls
303 lines (279 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
-- |
-- Pretty printer for the Javascript AST
--
module Language.PureScript.Pretty.JS
( prettyPrintJS
, prettyPrintJSWithSourceMaps
) where
import Prelude.Compat
import Control.Arrow ((<+>))
import Control.Monad (forM, mzero)
import Control.Monad.State (StateT, evalStateT)
import Control.PatternArrows
import qualified Control.Arrow as A
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text as T
import Language.PureScript.AST (SourceSpan(..))
import Language.PureScript.CodeGen.JS.AST
import Language.PureScript.CodeGen.JS.Common
import Language.PureScript.Comments
import Language.PureScript.Crash
import Language.PureScript.Pretty.Common
import Language.PureScript.PSString (PSString, decodeString, prettyPrintStringJS)
-- TODO (Christoph): Get rid of T.unpack / pack
literals :: (Emit gen) => Pattern PrinterState JS gen
literals = mkPattern' match'
where
match' :: (Emit gen) => JS -> StateT PrinterState Maybe gen
match' js = (addMapping' (getSourceSpan js) <>) <$> match js
match :: (Emit gen) => JS -> StateT PrinterState Maybe gen
match (JSNumericLiteral _ n) = return $ emit $ T.pack $ either show show n
match (JSStringLiteral _ s) = return $ emit $ prettyPrintStringJS s
match (JSBooleanLiteral _ True) = return $ emit "true"
match (JSBooleanLiteral _ False) = return $ emit "false"
match (JSArrayLiteral _ xs) = mconcat <$> sequence
[ return $ emit "[ "
, intercalate (emit ", ") <$> forM xs prettyPrintJS'
, return $ emit " ]"
]
match (JSObjectLiteral _ []) = return $ emit "{}"
match (JSObjectLiteral _ ps) = mconcat <$> sequence
[ return $ emit "{\n"
, withIndent $ do
jss <- forM ps $ \(key, value) -> fmap ((objectPropertyToString key <> emit ": ") <>) . prettyPrintJS' $ value
indentString <- currentIndent
return $ intercalate (emit ", \n") $ map (indentString <>) jss
, return $ emit "\n"
, currentIndent
, return $ emit "}"
]
where
objectPropertyToString :: (Emit gen) => PSString -> gen
objectPropertyToString s =
emit $ case decodeString s of
Just s' | not (identNeedsEscaping s') ->
s'
_ ->
prettyPrintStringJS s
match (JSBlock _ sts) = mconcat <$> sequence
[ return $ emit "{\n"
, withIndent $ prettyStatements sts
, return $ emit "\n"
, currentIndent
, return $ emit "}"
]
match (JSVar _ ident) = return $ emit ident
match (JSVariableIntroduction _ ident value) = mconcat <$> sequence
[ return $ emit $ "var " <> ident
, maybe (return mempty) (fmap (emit " = " <>) . prettyPrintJS') value
]
match (JSAssignment _ target value) = mconcat <$> sequence
[ prettyPrintJS' target
, return $ emit " = "
, prettyPrintJS' value
]
match (JSWhile _ cond sts) = mconcat <$> sequence
[ return $ emit "while ("
, prettyPrintJS' cond
, return $ emit ") "
, prettyPrintJS' sts
]
match (JSFor _ ident start end sts) = mconcat <$> sequence
[ return $ emit $ "for (var " <> ident <> " = "
, prettyPrintJS' start
, return $ emit $ "; " <> ident <> " < "
, prettyPrintJS' end
, return $ emit $ "; " <> ident <> "++) "
, prettyPrintJS' sts
]
match (JSForIn _ ident obj sts) = mconcat <$> sequence
[ return $ emit $ "for (var " <> ident <> " in "
, prettyPrintJS' obj
, return $ emit ") "
, prettyPrintJS' sts
]
match (JSIfElse _ cond thens elses) = mconcat <$> sequence
[ return $ emit "if ("
, prettyPrintJS' cond
, return $ emit ") "
, prettyPrintJS' thens
, maybe (return mempty) (fmap (emit " else " <>) . prettyPrintJS') elses
]
match (JSReturn _ value) = mconcat <$> sequence
[ return $ emit "return "
, prettyPrintJS' value
]
match (JSThrow _ value) = mconcat <$> sequence
[ return $ emit "throw "
, prettyPrintJS' value
]
match (JSBreak _ lbl) = return $ emit $ "break " <> lbl
match (JSContinue _ lbl) = return $ emit $ "continue " <> lbl
match (JSLabel _ lbl js) = mconcat <$> sequence
[ return $ emit $ lbl <> ": "
, prettyPrintJS' js
]
match (JSComment _ com js) = mconcat <$> sequence
[ mconcat <$> forM com comment
, prettyPrintJS' js
]
match (JSRaw _ js) = return $ emit js
match _ = mzero
comment :: (Emit gen) => Comment -> StateT PrinterState Maybe gen
comment (LineComment com) = fmap mconcat $ sequence $
[ return $ emit "\n"
, currentIndent
, return $ emit "//" <> emit com <> emit "\n"
]
comment (BlockComment com) = fmap mconcat $ sequence $
[ return $ emit "\n"
, currentIndent
, return $ emit "/**\n"
] ++
map asLine (T.lines com) ++
[ currentIndent
, return $ emit " */\n"
, currentIndent
]
where
asLine :: (Emit gen) => Text -> StateT PrinterState Maybe gen
asLine s = do
i <- currentIndent
return $ i <> emit " * " <> (emit . removeComments) s <> emit "\n"
removeComments :: Text -> Text
removeComments t =
case T.stripPrefix "*/" t of
Just rest -> removeComments rest
Nothing -> case T.uncons t of
Just (x, xs) -> x `T.cons` removeComments xs
Nothing -> ""
conditional :: Pattern PrinterState JS ((Maybe SourceSpan, JS, JS), JS)
conditional = mkPattern match
where
match (JSConditional ss cond th el) = Just ((ss, th, el), cond)
match _ = Nothing
accessor :: Pattern PrinterState JS (Text, JS)
accessor = mkPattern match
where
match (JSIndexer _ (JSStringLiteral _ prop) val) =
case decodeString prop of
Just s | not (identNeedsEscaping s) -> Just (s, val)
_ -> Nothing
match _ = Nothing
indexer :: (Emit gen) => Pattern PrinterState JS (gen, JS)
indexer = mkPattern' match
where
match (JSIndexer _ index val) = (,) <$> prettyPrintJS' index <*> pure val
match _ = mzero
lam :: Pattern PrinterState JS ((Maybe Text, [Text], Maybe SourceSpan), JS)
lam = mkPattern match
where
match (JSFunction ss name args ret) = Just ((name, args, ss), ret)
match _ = Nothing
app :: (Emit gen) => Pattern PrinterState JS (gen, JS)
app = mkPattern' match
where
match (JSApp _ val args) = do
jss <- traverse prettyPrintJS' args
return (intercalate (emit ", ") jss, val)
match _ = mzero
typeOf :: Pattern PrinterState JS ((), JS)
typeOf = mkPattern match
where
match (JSTypeOf _ val) = Just ((), val)
match _ = Nothing
instanceOf :: Pattern PrinterState JS (JS, JS)
instanceOf = mkPattern match
where
match (JSInstanceOf _ val ty) = Just (val, ty)
match _ = Nothing
unary' :: (Emit gen) => UnaryOperator -> (JS -> Text) -> Operator PrinterState JS gen
unary' op mkStr = Wrap match (<>)
where
match :: (Emit gen) => Pattern PrinterState JS (gen, JS)
match = mkPattern match'
where
match' (JSUnary _ op' val) | op' == op = Just (emit $ mkStr val, val)
match' _ = Nothing
unary :: (Emit gen) => UnaryOperator -> Text -> Operator PrinterState JS gen
unary op str = unary' op (const str)
negateOperator :: (Emit gen) => Operator PrinterState JS gen
negateOperator = unary' Negate (\v -> if isNegate v then "- " else "-")
where
isNegate (JSUnary _ Negate _) = True
isNegate _ = False
binary :: (Emit gen) => BinaryOperator -> Text -> Operator PrinterState JS gen
binary op str = AssocL match (\v1 v2 -> v1 <> emit (" " <> str <> " ") <> v2)
where
match :: Pattern PrinterState JS (JS, JS)
match = mkPattern match'
where
match' (JSBinary _ op' v1 v2) | op' == op = Just (v1, v2)
match' _ = Nothing
prettyStatements :: (Emit gen) => [JS] -> StateT PrinterState Maybe gen
prettyStatements sts = do
jss <- forM sts prettyPrintJS'
indentString <- currentIndent
return $ intercalate (emit "\n") $ map ((<> emit ";") . (indentString <>)) jss
-- |
-- Generate a pretty-printed string representing a Javascript expression
--
prettyPrintJS1 :: (Emit gen) => JS -> gen
prettyPrintJS1 = fromMaybe (internalError "Incomplete pattern") . flip evalStateT (PrinterState 0) . prettyPrintJS'
-- |
-- Generate a pretty-printed string representing a collection of Javascript expressions at the same indentation level
--
prettyPrintJSWithSourceMaps :: [JS] -> (Text, [SMap])
prettyPrintJSWithSourceMaps js =
let StrPos (_, s, mp) = (fromMaybe (internalError "Incomplete pattern") . flip evalStateT (PrinterState 0) . prettyStatements) js
in (s, mp)
prettyPrintJS :: [JS] -> Text
prettyPrintJS = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyStatements
-- |
-- Generate an indented, pretty-printed string representing a Javascript expression
--
prettyPrintJS' :: (Emit gen) => JS -> StateT PrinterState Maybe gen
prettyPrintJS' = A.runKleisli $ runPattern matchValue
where
matchValue :: (Emit gen) => Pattern PrinterState JS gen
matchValue = buildPrettyPrinter operators (literals <+> fmap parensPos matchValue)
operators :: (Emit gen) => OperatorTable PrinterState JS gen
operators =
OperatorTable [ [ Wrap indexer $ \index val -> val <> emit "[" <> index <> emit "]" ]
, [ Wrap accessor $ \prop val -> val <> emit "." <> emit prop ]
, [ Wrap app $ \args val -> val <> emit "(" <> args <> emit ")" ]
, [ unary JSNew "new " ]
, [ Wrap lam $ \(name, args, ss) ret -> addMapping' ss <>
emit ("function "
<> fromMaybe "" name
<> "(" <> intercalate ", " args <> ") ")
<> ret ]
, [ Wrap typeOf $ \_ s -> emit "typeof " <> s ]
, [ unary Not "!"
, unary BitwiseNot "~"
, unary Positive "+"
, negateOperator ]
, [ binary Multiply "*"
, binary Divide "/"
, binary Modulus "%" ]
, [ binary Add "+"
, binary Subtract "-" ]
, [ binary ShiftLeft "<<"
, binary ShiftRight ">>"
, binary ZeroFillShiftRight ">>>" ]
, [ binary LessThan "<"
, binary LessThanOrEqualTo "<="
, binary GreaterThan ">"
, binary GreaterThanOrEqualTo ">="
, AssocR instanceOf $ \v1 v2 -> v1 <> emit " instanceof " <> v2 ]
, [ binary EqualTo "==="
, binary NotEqualTo "!==" ]
, [ binary BitwiseAnd "&" ]
, [ binary BitwiseXor "^" ]
, [ binary BitwiseOr "|" ]
, [ binary And "&&" ]
, [ binary Or "||" ]
, [ Wrap conditional $ \(ss, th, el) cond -> cond <> addMapping' ss <> emit " ? " <> prettyPrintJS1 th <> addMapping' ss <> emit " : " <> prettyPrintJS1 el ]
]