Skip to content

Commit 2bb838d

Browse files
feat(ast): add Generic and NFData derivations to AST types
Implements PR #122 by adding Generic and NFData instances to all 36 AST data types in Language.JavaScript.Parser.AST module. - Add DeriveGeneric and DeriveAnyClass language extensions - Import Control.DeepSeq (NFData) and GHC.Generics (Generic) - Update all deriving clauses to include Generic and NFData - Enables performance benchmarking and space leak prevention
1 parent a087313 commit 2bb838d

File tree

1 file changed

+39
-37
lines changed
  • src/Language/JavaScript/Parser

1 file changed

+39
-37
lines changed

src/Language/JavaScript/Parser/AST.hs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE DeriveDataTypeable, FlexibleInstances #-}
1+
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, DeriveAnyClass, FlexibleInstances #-}
22

33
module Language.JavaScript.Parser.AST
44
( JSExpression (..)
@@ -45,8 +45,10 @@ module Language.JavaScript.Parser.AST
4545
, showStripped
4646
) where
4747

48+
import Control.DeepSeq (NFData)
4849
import Data.Data
4950
import Data.List
51+
import GHC.Generics (Generic)
5052
import Language.JavaScript.Parser.SrcLocation (TokenPosn (..))
5153
import Language.JavaScript.Parser.Token
5254

@@ -56,7 +58,7 @@ data JSAnnot
5658
= JSAnnot !TokenPosn ![CommentAnnotation] -- ^Annotation: position and comment/whitespace information
5759
| JSAnnotSpace -- ^A single space character
5860
| JSNoAnnot -- ^No annotation
59-
deriving (Data, Eq, Show, Typeable)
61+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
6062

6163

6264
data JSAST
@@ -65,67 +67,67 @@ data JSAST
6567
| JSAstStatement !JSStatement !JSAnnot
6668
| JSAstExpression !JSExpression !JSAnnot
6769
| JSAstLiteral !JSExpression !JSAnnot
68-
deriving (Data, Eq, Show, Typeable)
70+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
6971

7072
-- Shift AST
7173
-- https://github.com/shapesecurity/shift-spec/blob/83498b92c436180cc0e2115b225a68c08f43c53e/spec.idl#L229-L234
7274
data JSModuleItem
7375
= JSModuleImportDeclaration !JSAnnot !JSImportDeclaration -- ^import,decl
7476
| JSModuleExportDeclaration !JSAnnot !JSExportDeclaration -- ^export,decl
7577
| JSModuleStatementListItem !JSStatement
76-
deriving (Data, Eq, Show, Typeable)
78+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
7779

7880
data JSImportDeclaration
7981
= JSImportDeclaration !JSImportClause !JSFromClause !JSSemi -- ^imports, module, semi
8082
| JSImportDeclarationBare !JSAnnot !String !JSSemi -- ^module, module, semi
81-
deriving (Data, Eq, Show, Typeable)
83+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
8284

8385
data JSImportClause
8486
= JSImportClauseDefault !JSIdent -- ^default
8587
| JSImportClauseNameSpace !JSImportNameSpace -- ^namespace
8688
| JSImportClauseNamed !JSImportsNamed -- ^named imports
8789
| JSImportClauseDefaultNameSpace !JSIdent !JSAnnot !JSImportNameSpace -- ^default, comma, namespace
8890
| JSImportClauseDefaultNamed !JSIdent !JSAnnot !JSImportsNamed -- ^default, comma, named imports
89-
deriving (Data, Eq, Show, Typeable)
91+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
9092

9193
data JSFromClause
9294
= JSFromClause !JSAnnot !JSAnnot !String -- ^ from, string literal, string literal contents
93-
deriving (Data, Eq, Show, Typeable)
95+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
9496

9597
-- | Import namespace, e.g. '* as whatever'
9698
data JSImportNameSpace
9799
= JSImportNameSpace !JSBinOp !JSAnnot !JSIdent -- ^ *, as, ident
98-
deriving (Data, Eq, Show, Typeable)
100+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
99101

100102
-- | Named imports, e.g. '{ foo, bar, baz as quux }'
101103
data JSImportsNamed
102104
= JSImportsNamed !JSAnnot !(JSCommaList JSImportSpecifier) !JSAnnot -- ^lb, specifiers, rb
103-
deriving (Data, Eq, Show, Typeable)
105+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
104106

105107
-- |
106108
-- Note that this data type is separate from ExportSpecifier because the
107109
-- grammar is slightly different (e.g. in handling of reserved words).
108110
data JSImportSpecifier
109111
= JSImportSpecifier !JSIdent -- ^ident
110112
| JSImportSpecifierAs !JSIdent !JSAnnot !JSIdent -- ^ident, as, ident
111-
deriving (Data, Eq, Show, Typeable)
113+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
112114

113115
data JSExportDeclaration
114116
-- = JSExportAllFrom
115117
= JSExportFrom JSExportClause JSFromClause !JSSemi -- ^exports, module, semi
116118
| JSExportLocals JSExportClause !JSSemi -- ^exports, autosemi
117119
| JSExport !JSStatement !JSSemi -- ^body, autosemi
118120
-- | JSExportDefault
119-
deriving (Data, Eq, Show, Typeable)
121+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
120122

121123
data JSExportClause
122124
= JSExportClause !JSAnnot !(JSCommaList JSExportSpecifier) !JSAnnot -- ^lb, specifiers, rb
123-
deriving (Data, Eq, Show, Typeable)
125+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
124126

125127
data JSExportSpecifier
126128
= JSExportSpecifier !JSIdent -- ^ident
127129
| JSExportSpecifierAs !JSIdent !JSAnnot !JSIdent -- ^ident1, as, ident2
128-
deriving (Data, Eq, Show, Typeable)
130+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
129131

130132
data JSStatement
131133
= JSStatementBlock !JSAnnot ![JSStatement] !JSAnnot !JSSemi -- ^lbrace, stmts, rbrace, autosemi
@@ -164,7 +166,7 @@ data JSStatement
164166
| JSVariable !JSAnnot !(JSCommaList JSExpression) !JSSemi -- ^var, decl, autosemi
165167
| JSWhile !JSAnnot !JSAnnot !JSExpression !JSAnnot !JSStatement -- ^while,lb,expr,rb,stmt
166168
| 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)
168170

169171
data JSExpression
170172
-- | Terminals
@@ -208,17 +210,17 @@ data JSExpression
208210
| JSVarInitExpression !JSExpression !JSVarInitializer -- ^identifier, initializer
209211
| JSYieldExpression !JSAnnot !(Maybe JSExpression) -- ^yield, optional expr
210212
| JSYieldFromExpression !JSAnnot !JSAnnot !JSExpression -- ^yield, *, expr
211-
deriving (Data, Eq, Show, Typeable)
213+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
212214

213215
data JSArrowParameterList
214216
= JSUnparenthesizedArrowParameter !JSIdent
215217
| JSParenthesizedArrowParameterList !JSAnnot !(JSCommaList JSExpression) !JSAnnot
216-
deriving (Data, Eq, Show, Typeable)
218+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
217219

218220
data JSConciseBody
219221
= JSConciseFunctionBody !JSBlock
220222
| JSConciseExpressionBody !JSExpression
221-
deriving (Data, Eq, Show, Typeable)
223+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
222224

223225
data JSBinOp
224226
= JSBinOpAnd !JSAnnot
@@ -246,7 +248,7 @@ data JSBinOp
246248
| JSBinOpStrictNeq !JSAnnot
247249
| JSBinOpTimes !JSAnnot
248250
| JSBinOpUrsh !JSAnnot
249-
deriving (Data, Eq, Show, Typeable)
251+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
250252

251253
data JSUnaryOp
252254
= JSUnaryOpDecr !JSAnnot
@@ -258,12 +260,12 @@ data JSUnaryOp
258260
| JSUnaryOpTilde !JSAnnot
259261
| JSUnaryOpTypeof !JSAnnot
260262
| JSUnaryOpVoid !JSAnnot
261-
deriving (Data, Eq, Show, Typeable)
263+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
262264

263265
data JSSemi
264266
= JSSemi !JSAnnot
265267
| JSSemiAuto
266-
deriving (Data, Eq, Show, Typeable)
268+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
267269

268270
data JSAssignOp
269271
= JSAssign !JSAnnot
@@ -278,95 +280,95 @@ data JSAssignOp
278280
| JSBwAndAssign !JSAnnot
279281
| JSBwXorAssign !JSAnnot
280282
| JSBwOrAssign !JSAnnot
281-
deriving (Data, Eq, Show, Typeable)
283+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
282284

283285
data JSTryCatch
284286
= JSCatch !JSAnnot !JSAnnot !JSExpression !JSAnnot !JSBlock -- ^catch,lb,ident,rb,block
285287
| 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)
287289

288290
data JSTryFinally
289291
= JSFinally !JSAnnot !JSBlock -- ^finally,block
290292
| JSNoFinally
291-
deriving (Data, Eq, Show, Typeable)
293+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
292294

293295
data JSBlock
294296
= JSBlock !JSAnnot ![JSStatement] !JSAnnot -- ^lbrace, stmts, rbrace
295-
deriving (Data, Eq, Show, Typeable)
297+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
296298

297299
data JSSwitchParts
298300
= JSCase !JSAnnot !JSExpression !JSAnnot ![JSStatement] -- ^expr,colon,stmtlist
299301
| JSDefault !JSAnnot !JSAnnot ![JSStatement] -- ^colon,stmtlist
300-
deriving (Data, Eq, Show, Typeable)
302+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
301303

302304
data JSVarInitializer
303305
= JSVarInit !JSAnnot !JSExpression -- ^ assignop, initializer
304306
| JSVarInitNone
305-
deriving (Data, Eq, Show, Typeable)
307+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
306308

307309
data JSObjectProperty
308310
= JSPropertyNameandValue !JSPropertyName !JSAnnot ![JSExpression] -- ^name, colon, value
309311
| JSPropertyIdentRef !JSAnnot !String
310312
| JSObjectMethod !JSMethodDefinition
311313
| JSObjectSpread !JSAnnot !JSExpression -- ^..., expression
312-
deriving (Data, Eq, Show, Typeable)
314+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
313315

314316
data JSMethodDefinition
315317
= JSMethodDefinition !JSPropertyName !JSAnnot !(JSCommaList JSExpression) !JSAnnot !JSBlock -- name, lb, params, rb, block
316318
| JSGeneratorMethodDefinition !JSAnnot !JSPropertyName !JSAnnot !(JSCommaList JSExpression) !JSAnnot !JSBlock -- ^*, name, lb, params, rb, block
317319
| 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)
319321

320322
data JSPropertyName
321323
= JSPropertyIdent !JSAnnot !String
322324
| JSPropertyString !JSAnnot !String
323325
| JSPropertyNumber !JSAnnot !String
324326
| JSPropertyComputed !JSAnnot !JSExpression !JSAnnot -- ^lb, expr, rb
325-
deriving (Data, Eq, Show, Typeable)
327+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
326328

327329
type JSObjectPropertyList = JSCommaTrailingList JSObjectProperty
328330

329331
-- | Accessors for JSObjectProperty is either 'get' or 'set'.
330332
data JSAccessor
331333
= JSAccessorGet !JSAnnot
332334
| JSAccessorSet !JSAnnot
333-
deriving (Data, Eq, Show, Typeable)
335+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
334336

335337
data JSIdent
336338
= JSIdentName !JSAnnot !String
337339
| JSIdentNone
338-
deriving (Data, Eq, Show, Typeable)
340+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
339341

340342
data JSArrayElement
341343
= JSArrayElement !JSExpression
342344
| JSArrayComma !JSAnnot
343-
deriving (Data, Eq, Show, Typeable)
345+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
344346

345347
data JSCommaList a
346348
= JSLCons !(JSCommaList a) !JSAnnot !a -- ^head, comma, a
347349
| JSLOne !a -- ^ single element (no comma)
348350
| JSLNil
349-
deriving (Data, Eq, Show, Typeable)
351+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
350352

351353
data JSCommaTrailingList a
352354
= JSCTLComma !(JSCommaList a) !JSAnnot -- ^list, trailing comma
353355
| JSCTLNone !(JSCommaList a) -- ^list
354-
deriving (Data, Eq, Show, Typeable)
356+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
355357

356358
data JSTemplatePart
357359
= JSTemplatePart !JSExpression !JSAnnot !String -- ^expr, rb, suffix
358-
deriving (Data, Eq, Show, Typeable)
360+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
359361

360362
data JSClassHeritage
361363
= JSExtends !JSAnnot !JSExpression
362364
| JSExtendsNone
363-
deriving (Data, Eq, Show, Typeable)
365+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
364366

365367
data JSClassElement
366368
= JSClassInstanceMethod !JSMethodDefinition
367369
| JSClassStaticMethod !JSAnnot !JSMethodDefinition
368370
| JSClassSemi !JSAnnot
369-
deriving (Data, Eq, Show, Typeable)
371+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
370372

371373
-- -----------------------------------------------------------------------------
372374
-- | Show the AST elements stripped of their JSAnnot data.

0 commit comments

Comments
 (0)