Skip to content

Commit ebfbb40

Browse files
committed
Add Functor instances to data types in AST.hs
1 parent ae8b81e commit ebfbb40

5 files changed

Lines changed: 65 additions & 45 deletions

File tree

src/Language/Python/Common/AST.hs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, CPP, DeriveDataTypeable #-}
1+
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, CPP, DeriveDataTypeable, DeriveFunctor, StandaloneDeriving #-}
22
-----------------------------------------------------------------------------
33
-- |
44
-- Module : Language.Python.Version2.Syntax.AST
@@ -57,13 +57,14 @@ module Language.Python.Common.AST (
5757
, RaiseExpr (..), RaiseExprSpan
5858
-- * Comprehensions
5959
, Comprehension (..), ComprehensionSpan
60+
, ComprehensionExpr (..), ComprehensionExprSpan
6061
, CompFor (..), CompForSpan
6162
, CompIf (..), CompIfSpan
6263
, CompIter (..), CompIterSpan
6364
)
6465
where
6566

66-
import Language.Python.Common.SrcLocation ( Span (getSpan), SrcSpan (..) )
67+
import Language.Python.Common.SrcLocation ( Span (getSpan), SrcSpan (..), spanning )
6768
import Data.Data
6869

6970
--------------------------------------------------------------------------------
@@ -75,7 +76,7 @@ class Annotated t where
7576

7677
-- | Identifier.
7778
data Ident annot = Ident { ident_string :: !String, ident_annot :: annot }
78-
deriving (Eq,Ord,Show,Typeable,Data)
79+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
7980

8081
type IdentSpan = Ident SrcSpan
8182

@@ -92,7 +93,7 @@ instance Annotated Ident where
9293
-- * Version 3.1 <http://www.python.org/doc/3.1/reference/toplevel_components.html>
9394
--
9495
newtype Module annot = Module [Statement annot] -- ^ A module is just a sequence of top-level statements.
95-
deriving (Eq,Ord,Show,Typeable,Data)
96+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
9697

9798
type ModuleSpan = Module SrcSpan
9899

@@ -124,7 +125,7 @@ data ImportItem annot =
124125
, import_as_name :: Maybe (Ident annot) -- ^ An optional name to refer to the entity (the \'as\' name).
125126
, import_item_annot :: annot
126127
}
127-
deriving (Eq,Ord,Show,Typeable,Data)
128+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
128129

129130
type ImportItemSpan = ImportItem SrcSpan
130131

@@ -146,7 +147,7 @@ data FromItem annot =
146147
, from_as_name :: Maybe (Ident annot) -- ^ An optional name to refer to the entity (the \'as\' name).
147148
, from_item_annot :: annot
148149
}
149-
deriving (Eq,Ord,Show,Typeable,Data)
150+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
150151

151152
type FromItemSpan = FromItem SrcSpan
152153

@@ -160,7 +161,7 @@ instance Annotated FromItem where
160161
data FromItems annot
161162
= ImportEverything { from_items_annot :: annot } -- ^ Import everything exported from the module.
162163
| FromItems { from_items_items :: [FromItem annot], from_items_annot :: annot } -- ^ Import a specific list of items from the module.
163-
deriving (Eq,Ord,Show,Typeable,Data)
164+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
164165

165166
type FromItemsSpan = FromItems SrcSpan
166167

@@ -177,7 +178,7 @@ data ImportRelative annot
177178
, import_relative_module :: Maybe (DottedName annot)
178179
, import_relative_annot :: annot
179180
}
180-
deriving (Eq,Ord,Show,Typeable,Data)
181+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
181182

182183
type ImportRelativeSpan = ImportRelative SrcSpan
183184

@@ -333,7 +334,7 @@ data Statement annot
333334
, exec_globals_locals :: Maybe (Expr annot, Maybe (Expr annot)) -- ^ Global and local environments to evaluate the expression within.
334335
, stmt_annot :: annot
335336
}
336-
deriving (Eq,Ord,Show,Typeable,Data)
337+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
337338

338339
type StatementSpan = Statement SrcSpan
339340

@@ -347,7 +348,7 @@ instance Annotated Statement where
347348
data RaiseExpr annot
348349
= RaiseV3 (Maybe (Expr annot, Maybe (Expr annot))) -- ^ Optional expression to evaluate, and optional \'from\' clause. /Version 3 only/.
349350
| RaiseV2 (Maybe (Expr annot, (Maybe (Expr annot, Maybe (Expr annot))))) -- ^ /Version 2 only/.
350-
deriving (Eq,Ord,Show,Typeable,Data)
351+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
351352

352353
type RaiseExprSpan = RaiseExpr SrcSpan
353354

@@ -358,7 +359,7 @@ data Decorator annot =
358359
, decorator_args :: [Argument annot] -- ^ Decorator arguments.
359360
, decorator_annot :: annot
360361
}
361-
deriving (Eq,Ord,Show,Typeable,Data)
362+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
362363

363364
type DecoratorSpan = Decorator SrcSpan
364365

@@ -410,7 +411,7 @@ data Parameter annot
410411
, param_default :: Maybe (Expr annot) -- ^ Optional default value.
411412
, param_annot :: annot
412413
}
413-
deriving (Eq,Ord,Show,Typeable,Data)
414+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
414415

415416
type ParameterSpan = Parameter SrcSpan
416417

@@ -424,7 +425,7 @@ instance Annotated Parameter where
424425
data ParamTuple annot
425426
= ParamTupleName { param_tuple_name :: Ident annot, param_tuple_annot :: annot } -- ^ A variable name.
426427
| ParamTuple { param_tuple :: [ParamTuple annot], param_tuple_annot :: annot } -- ^ A (possibly nested) tuple parameter.
427-
deriving (Eq,Ord,Show,Typeable,Data)
428+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
428429

429430
type ParamTupleSpan = ParamTuple SrcSpan
430431

@@ -448,7 +449,7 @@ data Argument annot
448449
, arg_expr :: Expr annot -- ^ Argument expression.
449450
, arg_annot :: annot
450451
}
451-
deriving (Eq,Ord,Show,Typeable,Data)
452+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
452453

453454
type ArgumentSpan = Argument SrcSpan
454455

@@ -465,7 +466,7 @@ data Handler annot
465466
, handler_suite :: Suite annot
466467
, handler_annot :: annot
467468
}
468-
deriving (Eq,Ord,Show,Typeable,Data)
469+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
469470

470471
type HandlerSpan = Handler SrcSpan
471472

@@ -482,7 +483,7 @@ data ExceptClause annot
482483
{ except_clause :: Maybe (Expr annot, Maybe (Expr annot))
483484
, except_clause_annot :: annot
484485
}
485-
deriving (Eq,Ord,Show,Typeable,Data)
486+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
486487

487488
type ExceptClauseSpan = ExceptClause SrcSpan
488489

@@ -493,22 +494,34 @@ instance Annotated ExceptClause where
493494
annot = except_clause_annot
494495

495496
-- | Comprehension. In version 3.x this can be used for lists, sets, dictionaries and generators.
496-
data Comprehension e annot
497+
-- data Comprehension e annot
498+
data Comprehension annot
497499
= Comprehension
498-
{ comprehension_expr :: e
500+
{ comprehension_expr :: ComprehensionExpr annot
499501
, comprehension_for :: CompFor annot
500502
, comprehension_annot :: annot
501503
}
502-
deriving (Eq,Ord,Show,Typeable,Data)
504+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
503505

504-
type ComprehensionSpan e = Comprehension e SrcSpan
506+
type ComprehensionSpan = Comprehension SrcSpan
505507

506-
instance Span (ComprehensionSpan e) where
508+
instance Span ComprehensionSpan where
507509
getSpan = annot
508510

509-
instance Annotated (Comprehension e) where
511+
instance Annotated Comprehension where
510512
annot = comprehension_annot
511513

514+
data ComprehensionExpr annot
515+
= ComprehensionExpr (Expr annot)
516+
| ComprehensionDict (DictMappingPair annot)
517+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
518+
519+
type ComprehensionExprSpan = ComprehensionExpr SrcSpan
520+
521+
instance Span ComprehensionExprSpan where
522+
getSpan (ComprehensionExpr e) = getSpan e
523+
getSpan (ComprehensionDict d) = getSpan d
524+
512525
-- | Comprehension \'for\' component.
513526
data CompFor annot =
514527
CompFor
@@ -517,7 +530,7 @@ data CompFor annot =
517530
, comp_for_iter :: Maybe (CompIter annot)
518531
, comp_for_annot :: annot
519532
}
520-
deriving (Eq,Ord,Show,Typeable,Data)
533+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
521534

522535
type CompForSpan = CompFor SrcSpan
523536

@@ -534,7 +547,7 @@ data CompIf annot =
534547
, comp_if_iter :: Maybe (CompIter annot)
535548
, comp_if_annot :: annot
536549
}
537-
deriving (Eq,Ord,Show,Typeable,Data)
550+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
538551

539552
type CompIfSpan = CompIf SrcSpan
540553

@@ -548,7 +561,7 @@ instance Annotated CompIf where
548561
data CompIter annot
549562
= IterFor { comp_iter_for :: CompFor annot, comp_iter_annot :: annot }
550563
| IterIf { comp_iter_if :: CompIf annot, comp_iter_annot :: annot }
551-
deriving (Eq,Ord,Show,Typeable,Data)
564+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
552565

553566
type CompIterSpan = CompIter SrcSpan
554567

@@ -621,26 +634,26 @@ data Expr annot
621634
, expr_annot :: annot
622635
}
623636
-- | Generator.
624-
| Generator { gen_comprehension :: Comprehension (Expr annot) annot, expr_annot :: annot }
637+
| Generator { gen_comprehension :: Comprehension annot, expr_annot :: annot }
625638
-- | List comprehension.
626-
| ListComp { list_comprehension :: Comprehension (Expr annot) annot, expr_annot :: annot }
639+
| ListComp { list_comprehension :: Comprehension annot, expr_annot :: annot }
627640
-- | List.
628641
| List { list_exprs :: [Expr annot], expr_annot :: annot }
629642
-- | Dictionary.
630643
| Dictionary { dict_mappings :: [DictMappingPair annot], expr_annot :: annot }
631644
-- | Dictionary comprehension. /Version 3 only/.
632-
| DictComp { dict_comprehension :: Comprehension (DictMappingPair annot) annot, expr_annot :: annot }
645+
| DictComp { dict_comprehension :: Comprehension annot, expr_annot :: annot }
633646
-- | Set.
634647
| Set { set_exprs :: [Expr annot], expr_annot :: annot }
635648
-- | Set comprehension. /Version 3 only/.
636-
| SetComp { set_comprehension :: Comprehension (Expr annot) annot, expr_annot :: annot }
649+
| SetComp { set_comprehension :: Comprehension annot, expr_annot :: annot }
637650
-- | Starred expression. /Version 3 only/.
638651
| Starred { starred_expr :: Expr annot, expr_annot :: annot }
639652
-- | Parenthesised expression.
640653
| Paren { paren_expr :: Expr annot, expr_annot :: annot }
641654
-- | String conversion (backquoted expression). Version 2 only.
642655
| StringConversion { backquoted_expr :: Expr annot, expr_anot :: annot }
643-
deriving (Eq,Ord,Show,Typeable,Data)
656+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
644657

645658
type ExprSpan = Expr SrcSpan
646659

@@ -650,7 +663,7 @@ instance Span ExprSpan where
650663
data YieldArg annot
651664
= YieldFrom (Expr annot) annot -- ^ Yield from a generator (Version 3 only)
652665
| YieldExpr (Expr annot) -- ^ Yield value of an expression
653-
deriving (Eq,Ord,Show,Typeable,Data)
666+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
654667

655668
type YieldArgSpan = YieldArg SrcSpan
656669

@@ -663,10 +676,13 @@ instance Annotated Expr where
663676

664677
data DictMappingPair annot =
665678
DictMappingPair (Expr annot) (Expr annot)
666-
deriving (Eq,Ord,Show,Typeable,Data)
679+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
667680

668681
type DictMappingPairSpan = DictMappingPair SrcSpan
669682

683+
instance Span DictMappingPairSpan where
684+
getSpan (DictMappingPair e1 e2) = spanning e1 e2
685+
670686
-- | Slice compenent.
671687
data Slice annot
672688
= SliceProper
@@ -680,7 +696,7 @@ data Slice annot
680696
, slice_annot :: annot
681697
}
682698
| SliceEllipsis { slice_annot :: annot }
683-
deriving (Eq,Ord,Show,Typeable,Data)
699+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
684700

685701
type SliceSpan = Slice SrcSpan
686702

@@ -719,7 +735,7 @@ data Op annot
719735
| FloorDivide { op_annot :: annot } -- ^ \'\/\/\'
720736
| Invert { op_annot :: annot } -- ^ \'~\' (bitwise inversion of its integer argument)
721737
| Modulo { op_annot :: annot } -- ^ \'%\'
722-
deriving (Eq,Ord,Show,Typeable,Data)
738+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
723739

724740
type OpSpan = Op SrcSpan
725741

@@ -743,7 +759,7 @@ data AssignOp annot
743759
| LeftShiftAssign { assignOp_annot :: annot } -- ^ \'<<=\'
744760
| RightShiftAssign { assignOp_annot :: annot } -- ^ \'>>=\'
745761
| FloorDivAssign { assignOp_annot :: annot } -- ^ \'\/\/=\'
746-
deriving (Eq,Ord,Show,Typeable,Data)
762+
deriving (Eq,Ord,Show,Typeable,Data,Functor)
747763

748764
type AssignOpSpan = AssignOp SrcSpan
749765

src/Language/Python/Common/ParserUtils.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,21 @@ makeTupleParam p@(ParamTupleName {}) optDefault =
145145
makeTupleParam p@(ParamTuple { param_tuple_annot = span }) optDefault =
146146
UnPackTuple p optDefault span
147147

148-
makeComprehension :: ExprSpan -> CompForSpan -> ComprehensionSpan ExprSpan
149-
makeComprehension e for = Comprehension e for (spanning e for)
148+
makeComprehension :: ExprSpan -> CompForSpan -> ComprehensionSpan
149+
makeComprehension e for = Comprehension (ComprehensionExpr e) for (spanning e for)
150150

151-
makeListForm :: SrcSpan -> Either ExprSpan (ComprehensionSpan ExprSpan) -> ExprSpan
151+
makeListForm :: SrcSpan -> Either ExprSpan ComprehensionSpan -> ExprSpan
152152
makeListForm span (Left tuple@(Tuple {})) = List (tuple_exprs tuple) span
153153
makeListForm span (Left other) = List [other] span
154154
makeListForm span (Right comprehension) = ListComp comprehension span
155155

156156
makeSet :: ExprSpan -> Either CompForSpan [ExprSpan] -> SrcSpan -> ExprSpan
157-
makeSet e (Left compFor) = SetComp (Comprehension e compFor (spanning e compFor))
157+
makeSet e (Left compFor) = SetComp (Comprehension (ComprehensionExpr e) compFor (spanning e compFor))
158158
makeSet e (Right es) = Set (e:es)
159159

160160
makeDictionary :: (ExprSpan, ExprSpan) -> Either CompForSpan [(ExprSpan,ExprSpan)] -> SrcSpan -> ExprSpan
161161
makeDictionary mapping@(key, val) (Left compFor) =
162-
DictComp (Comprehension (DictMappingPair key val) compFor (spanning mapping compFor))
162+
DictComp (Comprehension (ComprehensionDict (DictMappingPair key val)) compFor (spanning mapping compFor))
163163
makeDictionary (key, val) (Right es) =
164164
Dictionary (DictMappingPair key val: map (\(e1, e2) -> DictMappingPair e1 e2) es)
165165

@@ -184,7 +184,7 @@ makeReturn :: Token -> Maybe ExprSpan -> StatementSpan
184184
makeReturn t1 Nothing = AST.Return Nothing (getSpan t1)
185185
makeReturn t1 expr@(Just e) = AST.Return expr (spanning t1 e)
186186

187-
makeParenOrGenerator :: Either ExprSpan (ComprehensionSpan ExprSpan) -> SrcSpan -> ExprSpan
187+
makeParenOrGenerator :: Either ExprSpan ComprehensionSpan -> SrcSpan -> ExprSpan
188188
makeParenOrGenerator (Left e) span = Paren e span
189189
makeParenOrGenerator (Right comp) span = Generator comp span
190190

src/Language/Python/Common/PrettyAST.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,14 @@ instance Pretty (Argument a) where
195195
pretty (ArgKeyword { arg_keyword = ident, arg_expr = e })
196196
= pretty ident <> equals <> pretty e
197197

198-
instance Pretty t => Pretty (Comprehension t a) where
198+
instance Pretty (Comprehension a) where
199199
pretty (Comprehension { comprehension_expr = e, comprehension_for = for })
200200
= pretty e <+> pretty for
201201

202+
instance Pretty (ComprehensionExpr a) where
203+
pretty (ComprehensionExpr e) = pretty e
204+
pretty (ComprehensionDict d) = pretty d
205+
202206
instance Pretty (CompFor a) where
203207
pretty (CompFor { comp_for_exprs = es, comp_in_expr = e, comp_for_iter = iter })
204208
= text "for" <+> commaList es <+> text "in" <+> pretty e <+> pretty iter

src/Language/Python/Version2/Parser/Parser.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ dict_or_set_atom
781781
: '{' '}' { Dictionary [] (spanning $1 $2) }
782782
| '{' dictorsetmaker '}' { $2 (spanning $1 $3) }
783783

784-
testlistfor :: { Either ExprSpan (ComprehensionSpan ExprSpan) }
784+
testlistfor :: { Either ExprSpan ComprehensionSpan }
785785
testlistfor
786786
: testlist { Left $1 }
787787
| test list_for { Right (makeComprehension $1 $2) }
@@ -794,7 +794,7 @@ yield_or_testlist_gexp
794794

795795
-- testlist_gexp: test ( gen_for | (',' test)* [','] )
796796

797-
testlist_gexp :: { Either ExprSpan (ComprehensionSpan ExprSpan) }
797+
testlist_gexp :: { Either ExprSpan ComprehensionSpan }
798798
testlist_gexp
799799
: testlist { Left $1 }
800800
| test gen_for { Right (makeComprehension $1 $2) }

src/Language/Python/Version3/Parser/Parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ yield_or_testlist_comp
804804
805805
-- testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
806806
807-
testlist_comp :: { Either ExprSpan (ComprehensionSpan ExprSpan) }
807+
testlist_comp :: { Either ExprSpan ComprehensionSpan }
808808
testlist_comp
809809
: testlist_star_expr { Left $1 }
810810
| or(test,star_expr) comp_for { Right (makeComprehension $1 $2) }

0 commit comments

Comments
 (0)