Skip to content

Commit e0432af

Browse files
committed
derive Generic and NFData
1 parent 68d7f5a commit e0432af

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

language-javascript.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Cabal-version: >= 1.9.2
3131
Library
3232
Build-depends: base >= 4 && < 5
3333
, array >= 0.3
34+
, deepseq >= 1.4
3435
, mtl >= 1.1
3536
, containers >= 0.2
3637
, blaze-builder >= 0.2

src/Language/JavaScript/Parser/AST.hs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DeriveDataTypeable, FlexibleInstances #-}
2+
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
23

34
module Language.JavaScript.Parser.AST
45
( JSExpression (..)
@@ -45,8 +46,10 @@ module Language.JavaScript.Parser.AST
4546
, showStripped
4647
) where
4748

49+
import Control.DeepSeq (NFData)
4850
import Data.Data
4951
import Data.List
52+
import GHC.Generics (Generic)
5053
import Language.JavaScript.Parser.SrcLocation (TokenPosn (..))
5154
import Language.JavaScript.Parser.Token
5255

@@ -56,7 +59,7 @@ data JSAnnot
5659
= JSAnnot !TokenPosn ![CommentAnnotation] -- ^Annotation: position and comment/whitespace information
5760
| JSAnnotSpace -- ^A single space character
5861
| JSNoAnnot -- ^No annotation
59-
deriving (Data, Eq, Show, Typeable)
62+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
6063

6164

6265
data JSAST
@@ -65,67 +68,67 @@ data JSAST
6568
| JSAstStatement !JSStatement !JSAnnot
6669
| JSAstExpression !JSExpression !JSAnnot
6770
| JSAstLiteral !JSExpression !JSAnnot
68-
deriving (Data, Eq, Show, Typeable)
71+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
6972

7073
-- Shift AST
7174
-- https://github.com/shapesecurity/shift-spec/blob/83498b92c436180cc0e2115b225a68c08f43c53e/spec.idl#L229-L234
7275
data JSModuleItem
7376
= JSModuleImportDeclaration !JSAnnot !JSImportDeclaration -- ^import,decl
7477
| JSModuleExportDeclaration !JSAnnot !JSExportDeclaration -- ^export,decl
7578
| JSModuleStatementListItem !JSStatement
76-
deriving (Data, Eq, Show, Typeable)
79+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
7780

7881
data JSImportDeclaration
7982
= JSImportDeclaration !JSImportClause !JSFromClause !JSSemi -- ^imports, module, semi
8083
| JSImportDeclarationBare !JSAnnot !String !JSSemi -- ^module, module, semi
81-
deriving (Data, Eq, Show, Typeable)
84+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
8285

8386
data JSImportClause
8487
= JSImportClauseDefault !JSIdent -- ^default
8588
| JSImportClauseNameSpace !JSImportNameSpace -- ^namespace
8689
| JSImportClauseNamed !JSImportsNamed -- ^named imports
8790
| JSImportClauseDefaultNameSpace !JSIdent !JSAnnot !JSImportNameSpace -- ^default, comma, namespace
8891
| JSImportClauseDefaultNamed !JSIdent !JSAnnot !JSImportsNamed -- ^default, comma, named imports
89-
deriving (Data, Eq, Show, Typeable)
92+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
9093

9194
data JSFromClause
9295
= JSFromClause !JSAnnot !JSAnnot !String -- ^ from, string literal, string literal contents
93-
deriving (Data, Eq, Show, Typeable)
96+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
9497

9598
-- | Import namespace, e.g. '* as whatever'
9699
data JSImportNameSpace
97100
= JSImportNameSpace !JSBinOp !JSAnnot !JSIdent -- ^ *, as, ident
98-
deriving (Data, Eq, Show, Typeable)
101+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
99102

100103
-- | Named imports, e.g. '{ foo, bar, baz as quux }'
101104
data JSImportsNamed
102105
= JSImportsNamed !JSAnnot !(JSCommaList JSImportSpecifier) !JSAnnot -- ^lb, specifiers, rb
103-
deriving (Data, Eq, Show, Typeable)
106+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
104107

105108
-- |
106109
-- Note that this data type is separate from ExportSpecifier because the
107110
-- grammar is slightly different (e.g. in handling of reserved words).
108111
data JSImportSpecifier
109112
= JSImportSpecifier !JSIdent -- ^ident
110113
| JSImportSpecifierAs !JSIdent !JSAnnot !JSIdent -- ^ident, as, ident
111-
deriving (Data, Eq, Show, Typeable)
114+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
112115

113116
data JSExportDeclaration
114117
-- = JSExportAllFrom
115118
= JSExportFrom JSExportClause JSFromClause !JSSemi -- ^exports, module, semi
116119
| JSExportLocals JSExportClause !JSSemi -- ^exports, autosemi
117120
| JSExport !JSStatement !JSSemi -- ^body, autosemi
118121
-- | JSExportDefault
119-
deriving (Data, Eq, Show, Typeable)
122+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
120123

121124
data JSExportClause
122125
= JSExportClause !JSAnnot !(JSCommaList JSExportSpecifier) !JSAnnot -- ^lb, specifiers, rb
123-
deriving (Data, Eq, Show, Typeable)
126+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
124127

125128
data JSExportSpecifier
126129
= JSExportSpecifier !JSIdent -- ^ident
127130
| JSExportSpecifierAs !JSIdent !JSAnnot !JSIdent -- ^ident1, as, ident2
128-
deriving (Data, Eq, Show, Typeable)
131+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
129132

130133
data JSStatement
131134
= JSStatementBlock !JSAnnot ![JSStatement] !JSAnnot !JSSemi -- ^lbrace, stmts, rbrace, autosemi
@@ -164,7 +167,7 @@ data JSStatement
164167
| JSVariable !JSAnnot !(JSCommaList JSExpression) !JSSemi -- ^var, decl, autosemi
165168
| JSWhile !JSAnnot !JSAnnot !JSExpression !JSAnnot !JSStatement -- ^while,lb,expr,rb,stmt
166169
| 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)
168171

169172
data JSExpression
170173
-- | Terminals
@@ -204,7 +207,7 @@ data JSExpression
204207
| JSVarInitExpression !JSExpression !JSVarInitializer -- ^identifier, initializer
205208
| JSYieldExpression !JSAnnot !(Maybe JSExpression) -- ^yield, optional expr
206209
| JSYieldFromExpression !JSAnnot !JSAnnot !JSExpression -- ^yield, *, expr
207-
deriving (Data, Eq, Show, Typeable)
210+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
208211

209212
data JSConciseBody
210213
= JSConciseFunctionBody !JSBlock
@@ -214,7 +217,7 @@ data JSConciseBody
214217
data JSArrowParameterList
215218
= JSUnparenthesizedArrowParameter !JSIdent
216219
| JSParenthesizedArrowParameterList !JSAnnot !(JSCommaList JSExpression) !JSAnnot
217-
deriving (Data, Eq, Show, Typeable)
220+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
218221

219222
data JSBinOp
220223
= JSBinOpAnd !JSAnnot
@@ -241,7 +244,7 @@ data JSBinOp
241244
| JSBinOpStrictNeq !JSAnnot
242245
| JSBinOpTimes !JSAnnot
243246
| JSBinOpUrsh !JSAnnot
244-
deriving (Data, Eq, Show, Typeable)
247+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
245248

246249
data JSUnaryOp
247250
= JSUnaryOpDecr !JSAnnot
@@ -253,12 +256,12 @@ data JSUnaryOp
253256
| JSUnaryOpTilde !JSAnnot
254257
| JSUnaryOpTypeof !JSAnnot
255258
| JSUnaryOpVoid !JSAnnot
256-
deriving (Data, Eq, Show, Typeable)
259+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
257260

258261
data JSSemi
259262
= JSSemi !JSAnnot
260263
| JSSemiAuto
261-
deriving (Data, Eq, Show, Typeable)
264+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
262265

263266
data JSAssignOp
264267
= JSAssign !JSAnnot
@@ -273,95 +276,95 @@ data JSAssignOp
273276
| JSBwAndAssign !JSAnnot
274277
| JSBwXorAssign !JSAnnot
275278
| JSBwOrAssign !JSAnnot
276-
deriving (Data, Eq, Show, Typeable)
279+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
277280

278281
data JSTryCatch
279282
= JSCatch !JSAnnot !JSAnnot !JSExpression !JSAnnot !JSBlock -- ^catch,lb,ident,rb,block
280283
| 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)
282285

283286
data JSTryFinally
284287
= JSFinally !JSAnnot !JSBlock -- ^finally,block
285288
| JSNoFinally
286-
deriving (Data, Eq, Show, Typeable)
289+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
287290

288291
data JSBlock
289292
= JSBlock !JSAnnot ![JSStatement] !JSAnnot -- ^lbrace, stmts, rbrace
290-
deriving (Data, Eq, Show, Typeable)
293+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
291294

292295
data JSSwitchParts
293296
= JSCase !JSAnnot !JSExpression !JSAnnot ![JSStatement] -- ^expr,colon,stmtlist
294297
| JSDefault !JSAnnot !JSAnnot ![JSStatement] -- ^colon,stmtlist
295-
deriving (Data, Eq, Show, Typeable)
298+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
296299

297300
data JSVarInitializer
298301
= JSVarInit !JSAnnot !JSExpression -- ^ assignop, initializer
299302
| JSVarInitNone
300-
deriving (Data, Eq, Show, Typeable)
303+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
301304

302305
data JSObjectProperty
303306
= JSPropertyNameandValue !JSPropertyName !JSAnnot ![JSExpression] -- ^name, colon, value
304307
| JSPropertyIdentRef !JSAnnot !String
305308
| JSObjectMethod !JSMethodDefinition
306309
| JSObjectSpread !JSAnnot !JSExpression
307-
deriving (Data, Eq, Show, Typeable)
310+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
308311

309312
data JSMethodDefinition
310313
= JSMethodDefinition !JSPropertyName !JSAnnot !(JSCommaList JSExpression) !JSAnnot !JSBlock -- name, lb, params, rb, block
311314
| JSGeneratorMethodDefinition !JSAnnot !JSPropertyName !JSAnnot !(JSCommaList JSExpression) !JSAnnot !JSBlock -- ^*, name, lb, params, rb, block
312315
| 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)
314317

315318
data JSPropertyName
316319
= JSPropertyIdent !JSAnnot !String
317320
| JSPropertyString !JSAnnot !String
318321
| JSPropertyNumber !JSAnnot !String
319322
| JSPropertyComputed !JSAnnot !JSExpression !JSAnnot -- ^lb, expr, rb
320-
deriving (Data, Eq, Show, Typeable)
323+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
321324

322325
type JSObjectPropertyList = JSCommaTrailingList JSObjectProperty
323326

324327
-- | Accessors for JSObjectProperty is either 'get' or 'set'.
325328
data JSAccessor
326329
= JSAccessorGet !JSAnnot
327330
| JSAccessorSet !JSAnnot
328-
deriving (Data, Eq, Show, Typeable)
331+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
329332

330333
data JSIdent
331334
= JSIdentName !JSAnnot !String
332335
| JSIdentNone
333-
deriving (Data, Eq, Show, Typeable)
336+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
334337

335338
data JSArrayElement
336339
= JSArrayElement !JSExpression
337340
| JSArrayComma !JSAnnot
338-
deriving (Data, Eq, Show, Typeable)
341+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
339342

340343
data JSCommaList a
341344
= JSLCons !(JSCommaList a) !JSAnnot !a -- ^head, comma, a
342345
| JSLOne !a -- ^ single element (no comma)
343346
| JSLNil
344-
deriving (Data, Eq, Show, Typeable)
347+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
345348

346349
data JSCommaTrailingList a
347350
= JSCTLComma !(JSCommaList a) !JSAnnot -- ^list, trailing comma
348351
| JSCTLNone !(JSCommaList a) -- ^list
349-
deriving (Data, Eq, Show, Typeable)
352+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
350353

351354
data JSTemplatePart
352355
= JSTemplatePart !JSExpression !JSAnnot !String -- ^expr, rb, suffix
353-
deriving (Data, Eq, Show, Typeable)
356+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
354357

355358
data JSClassHeritage
356359
= JSExtends !JSAnnot !JSExpression
357360
| JSExtendsNone
358-
deriving (Data, Eq, Show, Typeable)
361+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
359362

360363
data JSClassElement
361364
= JSClassInstanceMethod !JSMethodDefinition
362365
| JSClassStaticMethod !JSAnnot !JSMethodDefinition
363366
| JSClassSemi !JSAnnot
364-
deriving (Data, Eq, Show, Typeable)
367+
deriving (Data, Eq, Generic, NFData, Show, Typeable)
365368

366369
-- -----------------------------------------------------------------------------
367370
-- | Show the AST elements stripped of their JSAnnot data.

src/Language/JavaScript/Parser/ParseError.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
12
-----------------------------------------------------------------------------
23
-- |
34
-- Module : Language.JavaScript.ParseError
@@ -17,6 +18,8 @@ module Language.JavaScript.Parser.ParseError
1718

1819
--import Language.JavaScript.Parser.Pretty
1920
-- import Control.Monad.Error.Class -- Control.Monad.Trans.Except
21+
import Control.DeepSeq (NFData)
22+
import GHC.Generics (Generic)
2023
import Language.JavaScript.Parser.Lexer
2124
import Language.JavaScript.Parser.SrcLocation (TokenPosn)
2225
-- import Language.JavaScript.Parser.Token (Token)
@@ -29,7 +32,7 @@ data ParseError
2932
-- ^ An error from the lexer. Character found where it should not be.
3033
| StrError String
3134
-- ^ A generic error containing a string message. No source location.
32-
deriving (Eq, {- Ord,-} Show)
35+
deriving (Eq, Generic, NFData, {- Ord,-} Show)
3336

3437
class Error a where
3538
-- | Creates an exception without a message.

src/Language/JavaScript/Parser/SrcLocation.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{-# LANGUAGE DeriveDataTypeable #-}
2+
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
3+
24
module Language.JavaScript.Parser.SrcLocation (
35
TokenPosn(..)
46
, tokenPosnEmpty
57
) where
68

9+
import Control.DeepSeq (NFData)
710
import Data.Data
11+
import GHC.Generics (Generic)
812

913
-- | `TokenPosn' records the location of a token in the input text. It has three
1014
-- fields: the address (number of characters preceding the token), line number
@@ -14,7 +18,7 @@ import Data.Data
1418
data TokenPosn = TokenPn !Int -- address (number of characters preceding the token)
1519
!Int -- line number
1620
!Int -- column
17-
deriving (Eq,Show, Read, Data, Typeable)
21+
deriving (Data, Eq, Generic, NFData, Read, Show, Typeable)
1822

1923
tokenPosnEmpty :: TokenPosn
2024
tokenPosnEmpty = TokenPn 0 0 0

src/Language/JavaScript/Parser/Token.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP, DeriveDataTypeable #-}
2+
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
23
-----------------------------------------------------------------------------
34
-- |
45
-- Module : Language.Python.Common.Token
@@ -23,14 +24,16 @@ module Language.JavaScript.Parser.Token
2324
-- TokenClass (..),
2425
) where
2526

27+
import Control.DeepSeq (NFData)
2628
import Data.Data
29+
import GHC.Generics (Generic)
2730
import Language.JavaScript.Parser.SrcLocation
2831

2932
data CommentAnnotation
3033
= CommentA TokenPosn String
3134
| WhiteSpace TokenPosn String
3235
| NoComment
33-
deriving (Eq, Show, Typeable, Data, Read)
36+
deriving (Data, Eq, Generic, NFData, Read, Show, Typeable)
3437

3538
-- | Lexical tokens.
3639
-- Each may be annotated with any comment occurring between the prior token and this one
@@ -170,7 +173,7 @@ data Token
170173
| AsToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
171174
| TailToken { tokenSpan :: !TokenPosn, tokenComment :: ![CommentAnnotation] } -- ^ Stuff between last JS and EOF
172175
| EOFToken { tokenSpan :: !TokenPosn, tokenComment :: ![CommentAnnotation] } -- ^ End of file
173-
deriving (Eq, Show, Typeable)
176+
deriving (Eq, Generic, NFData, Show, Typeable)
174177

175178

176179
-- | Produce a string from a token containing detailed information. Mainly intended for debugging.

0 commit comments

Comments
 (0)