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