1
1
{-# LANGUAGE DeriveDataTypeable, FlexibleInstances #-}
2
+ {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
2
3
3
4
module Language.JavaScript.Parser.AST
4
5
( JSExpression (.. )
@@ -45,8 +46,10 @@ module Language.JavaScript.Parser.AST
45
46
, showStripped
46
47
) where
47
48
49
+ import Control.DeepSeq (NFData )
48
50
import Data.Data
49
51
import Data.List
52
+ import GHC.Generics (Generic )
50
53
import Language.JavaScript.Parser.SrcLocation (TokenPosn (.. ))
51
54
import Language.JavaScript.Parser.Token
52
55
@@ -56,7 +59,7 @@ data JSAnnot
56
59
= JSAnnot ! TokenPosn ! [CommentAnnotation ] -- ^ Annotation: position and comment/whitespace information
57
60
| JSAnnotSpace -- ^ A single space character
58
61
| JSNoAnnot -- ^ No annotation
59
- deriving (Data , Eq , Show , Typeable )
62
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
60
63
61
64
62
65
data JSAST
@@ -65,67 +68,67 @@ data JSAST
65
68
| JSAstStatement ! JSStatement ! JSAnnot
66
69
| JSAstExpression ! JSExpression ! JSAnnot
67
70
| JSAstLiteral ! JSExpression ! JSAnnot
68
- deriving (Data , Eq , Show , Typeable )
71
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
69
72
70
73
-- Shift AST
71
74
-- https://github.com/shapesecurity/shift-spec/blob/83498b92c436180cc0e2115b225a68c08f43c53e/spec.idl#L229-L234
72
75
data JSModuleItem
73
76
= JSModuleImportDeclaration ! JSAnnot ! JSImportDeclaration -- ^ import,decl
74
77
| JSModuleExportDeclaration ! JSAnnot ! JSExportDeclaration -- ^ export,decl
75
78
| JSModuleStatementListItem ! JSStatement
76
- deriving (Data , Eq , Show , Typeable )
79
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
77
80
78
81
data JSImportDeclaration
79
82
= JSImportDeclaration ! JSImportClause ! JSFromClause ! JSSemi -- ^ imports, module, semi
80
83
| JSImportDeclarationBare ! JSAnnot ! String ! JSSemi -- ^ module, module, semi
81
- deriving (Data , Eq , Show , Typeable )
84
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
82
85
83
86
data JSImportClause
84
87
= JSImportClauseDefault ! JSIdent -- ^ default
85
88
| JSImportClauseNameSpace ! JSImportNameSpace -- ^ namespace
86
89
| JSImportClauseNamed ! JSImportsNamed -- ^ named imports
87
90
| JSImportClauseDefaultNameSpace ! JSIdent ! JSAnnot ! JSImportNameSpace -- ^ default, comma, namespace
88
91
| JSImportClauseDefaultNamed ! JSIdent ! JSAnnot ! JSImportsNamed -- ^ default, comma, named imports
89
- deriving (Data , Eq , Show , Typeable )
92
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
90
93
91
94
data JSFromClause
92
95
= JSFromClause ! JSAnnot ! JSAnnot ! String -- ^ from, string literal, string literal contents
93
- deriving (Data , Eq , Show , Typeable )
96
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
94
97
95
98
-- | Import namespace, e.g. '* as whatever'
96
99
data JSImportNameSpace
97
100
= JSImportNameSpace ! JSBinOp ! JSAnnot ! JSIdent -- ^ *, as, ident
98
- deriving (Data , Eq , Show , Typeable )
101
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
99
102
100
103
-- | Named imports, e.g. '{ foo, bar, baz as quux }'
101
104
data JSImportsNamed
102
105
= JSImportsNamed ! JSAnnot ! (JSCommaList JSImportSpecifier ) ! JSAnnot -- ^ lb, specifiers, rb
103
- deriving (Data , Eq , Show , Typeable )
106
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
104
107
105
108
-- |
106
109
-- Note that this data type is separate from ExportSpecifier because the
107
110
-- grammar is slightly different (e.g. in handling of reserved words).
108
111
data JSImportSpecifier
109
112
= JSImportSpecifier ! JSIdent -- ^ ident
110
113
| JSImportSpecifierAs ! JSIdent ! JSAnnot ! JSIdent -- ^ ident, as, ident
111
- deriving (Data , Eq , Show , Typeable )
114
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
112
115
113
116
data JSExportDeclaration
114
117
-- = JSExportAllFrom
115
118
= JSExportFrom JSExportClause JSFromClause ! JSSemi -- ^ exports, module, semi
116
119
| JSExportLocals JSExportClause ! JSSemi -- ^ exports, autosemi
117
120
| JSExport ! JSStatement ! JSSemi -- ^ body, autosemi
118
121
-- | JSExportDefault
119
- deriving (Data , Eq , Show , Typeable )
122
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
120
123
121
124
data JSExportClause
122
125
= JSExportClause ! JSAnnot ! (JSCommaList JSExportSpecifier ) ! JSAnnot -- ^ lb, specifiers, rb
123
- deriving (Data , Eq , Show , Typeable )
126
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
124
127
125
128
data JSExportSpecifier
126
129
= JSExportSpecifier ! JSIdent -- ^ ident
127
130
| JSExportSpecifierAs ! JSIdent ! JSAnnot ! JSIdent -- ^ ident1, as, ident2
128
- deriving (Data , Eq , Show , Typeable )
131
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
129
132
130
133
data JSStatement
131
134
= JSStatementBlock ! JSAnnot ! [JSStatement ] ! JSAnnot ! JSSemi -- ^ lbrace, stmts, rbrace, autosemi
@@ -164,7 +167,7 @@ data JSStatement
164
167
| JSVariable ! JSAnnot ! (JSCommaList JSExpression ) ! JSSemi -- ^ var, decl, autosemi
165
168
| JSWhile ! JSAnnot ! JSAnnot ! JSExpression ! JSAnnot ! JSStatement -- ^ while,lb,expr,rb,stmt
166
169
| JSWith ! JSAnnot ! JSAnnot ! JSExpression ! JSAnnot ! JSStatement ! JSSemi -- ^ with,lb,expr,rb,stmt list
167
- deriving (Data , Eq , Show , Typeable )
170
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
168
171
169
172
data JSExpression
170
173
-- | Terminals
@@ -204,7 +207,7 @@ data JSExpression
204
207
| JSVarInitExpression ! JSExpression ! JSVarInitializer -- ^ identifier, initializer
205
208
| JSYieldExpression ! JSAnnot ! (Maybe JSExpression ) -- ^ yield, optional expr
206
209
| JSYieldFromExpression ! JSAnnot ! JSAnnot ! JSExpression -- ^ yield, *, expr
207
- deriving (Data , Eq , Show , Typeable )
210
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
208
211
209
212
data JSConciseBody
210
213
= JSConciseFunctionBody ! JSBlock
@@ -214,7 +217,7 @@ data JSConciseBody
214
217
data JSArrowParameterList
215
218
= JSUnparenthesizedArrowParameter ! JSIdent
216
219
| JSParenthesizedArrowParameterList ! JSAnnot ! (JSCommaList JSExpression ) ! JSAnnot
217
- deriving (Data , Eq , Show , Typeable )
220
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
218
221
219
222
data JSBinOp
220
223
= JSBinOpAnd ! JSAnnot
@@ -241,7 +244,7 @@ data JSBinOp
241
244
| JSBinOpStrictNeq ! JSAnnot
242
245
| JSBinOpTimes ! JSAnnot
243
246
| JSBinOpUrsh ! JSAnnot
244
- deriving (Data , Eq , Show , Typeable )
247
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
245
248
246
249
data JSUnaryOp
247
250
= JSUnaryOpDecr ! JSAnnot
@@ -253,12 +256,12 @@ data JSUnaryOp
253
256
| JSUnaryOpTilde ! JSAnnot
254
257
| JSUnaryOpTypeof ! JSAnnot
255
258
| JSUnaryOpVoid ! JSAnnot
256
- deriving (Data , Eq , Show , Typeable )
259
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
257
260
258
261
data JSSemi
259
262
= JSSemi ! JSAnnot
260
263
| JSSemiAuto
261
- deriving (Data , Eq , Show , Typeable )
264
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
262
265
263
266
data JSAssignOp
264
267
= JSAssign ! JSAnnot
@@ -273,95 +276,95 @@ data JSAssignOp
273
276
| JSBwAndAssign ! JSAnnot
274
277
| JSBwXorAssign ! JSAnnot
275
278
| JSBwOrAssign ! JSAnnot
276
- deriving (Data , Eq , Show , Typeable )
279
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
277
280
278
281
data JSTryCatch
279
282
= JSCatch ! JSAnnot ! JSAnnot ! JSExpression ! JSAnnot ! JSBlock -- ^ catch,lb,ident,rb,block
280
283
| JSCatchIf ! JSAnnot ! JSAnnot ! JSExpression ! JSAnnot ! JSExpression ! JSAnnot ! JSBlock -- ^ catch,lb,ident,if,expr,rb,block
281
- deriving (Data , Eq , Show , Typeable )
284
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
282
285
283
286
data JSTryFinally
284
287
= JSFinally ! JSAnnot ! JSBlock -- ^ finally,block
285
288
| JSNoFinally
286
- deriving (Data , Eq , Show , Typeable )
289
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
287
290
288
291
data JSBlock
289
292
= JSBlock ! JSAnnot ! [JSStatement ] ! JSAnnot -- ^ lbrace, stmts, rbrace
290
- deriving (Data , Eq , Show , Typeable )
293
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
291
294
292
295
data JSSwitchParts
293
296
= JSCase ! JSAnnot ! JSExpression ! JSAnnot ! [JSStatement ] -- ^ expr,colon,stmtlist
294
297
| JSDefault ! JSAnnot ! JSAnnot ! [JSStatement ] -- ^ colon,stmtlist
295
- deriving (Data , Eq , Show , Typeable )
298
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
296
299
297
300
data JSVarInitializer
298
301
= JSVarInit ! JSAnnot ! JSExpression -- ^ assignop, initializer
299
302
| JSVarInitNone
300
- deriving (Data , Eq , Show , Typeable )
303
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
301
304
302
305
data JSObjectProperty
303
306
= JSPropertyNameandValue ! JSPropertyName ! JSAnnot ! [JSExpression ] -- ^ name, colon, value
304
307
| JSPropertyIdentRef ! JSAnnot ! String
305
308
| JSObjectMethod ! JSMethodDefinition
306
309
| JSObjectSpread ! JSAnnot ! JSExpression
307
- deriving (Data , Eq , Show , Typeable )
310
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
308
311
309
312
data JSMethodDefinition
310
313
= JSMethodDefinition ! JSPropertyName ! JSAnnot ! (JSCommaList JSExpression ) ! JSAnnot ! JSBlock -- name, lb, params, rb, block
311
314
| JSGeneratorMethodDefinition ! JSAnnot ! JSPropertyName ! JSAnnot ! (JSCommaList JSExpression ) ! JSAnnot ! JSBlock -- ^ *, name, lb, params, rb, block
312
315
| JSPropertyAccessor ! JSAccessor ! JSPropertyName ! JSAnnot ! (JSCommaList JSExpression ) ! JSAnnot ! JSBlock -- ^ get/set, name, lb, params, rb, block
313
- deriving (Data , Eq , Show , Typeable )
316
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
314
317
315
318
data JSPropertyName
316
319
= JSPropertyIdent ! JSAnnot ! String
317
320
| JSPropertyString ! JSAnnot ! String
318
321
| JSPropertyNumber ! JSAnnot ! String
319
322
| JSPropertyComputed ! JSAnnot ! JSExpression ! JSAnnot -- ^ lb, expr, rb
320
- deriving (Data , Eq , Show , Typeable )
323
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
321
324
322
325
type JSObjectPropertyList = JSCommaTrailingList JSObjectProperty
323
326
324
327
-- | Accessors for JSObjectProperty is either 'get' or 'set'.
325
328
data JSAccessor
326
329
= JSAccessorGet ! JSAnnot
327
330
| JSAccessorSet ! JSAnnot
328
- deriving (Data , Eq , Show , Typeable )
331
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
329
332
330
333
data JSIdent
331
334
= JSIdentName ! JSAnnot ! String
332
335
| JSIdentNone
333
- deriving (Data , Eq , Show , Typeable )
336
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
334
337
335
338
data JSArrayElement
336
339
= JSArrayElement ! JSExpression
337
340
| JSArrayComma ! JSAnnot
338
- deriving (Data , Eq , Show , Typeable )
341
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
339
342
340
343
data JSCommaList a
341
344
= JSLCons ! (JSCommaList a ) ! JSAnnot ! a -- ^ head, comma, a
342
345
| JSLOne ! a -- ^ single element (no comma)
343
346
| JSLNil
344
- deriving (Data , Eq , Show , Typeable )
347
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
345
348
346
349
data JSCommaTrailingList a
347
350
= JSCTLComma ! (JSCommaList a ) ! JSAnnot -- ^ list, trailing comma
348
351
| JSCTLNone ! (JSCommaList a ) -- ^ list
349
- deriving (Data , Eq , Show , Typeable )
352
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
350
353
351
354
data JSTemplatePart
352
355
= JSTemplatePart ! JSExpression ! JSAnnot ! String -- ^ expr, rb, suffix
353
- deriving (Data , Eq , Show , Typeable )
356
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
354
357
355
358
data JSClassHeritage
356
359
= JSExtends ! JSAnnot ! JSExpression
357
360
| JSExtendsNone
358
- deriving (Data , Eq , Show , Typeable )
361
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
359
362
360
363
data JSClassElement
361
364
= JSClassInstanceMethod ! JSMethodDefinition
362
365
| JSClassStaticMethod ! JSAnnot ! JSMethodDefinition
363
366
| JSClassSemi ! JSAnnot
364
- deriving (Data , Eq , Show , Typeable )
367
+ deriving (Data , Eq , Generic , NFData , Show , Typeable )
365
368
366
369
-- -----------------------------------------------------------------------------
367
370
-- | Show the AST elements stripped of their JSAnnot data.
0 commit comments