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 )
6768import Data.Data
6869
6970--------------------------------------------------------------------------------
@@ -75,7 +76,7 @@ class Annotated t where
7576
7677-- | Identifier.
7778data 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
8081type 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--
9495newtype 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
9798type 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
129130type 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
151152type FromItemSpan = FromItem SrcSpan
152153
@@ -160,7 +161,7 @@ instance Annotated FromItem where
160161data 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
165166type 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
182183type 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
338339type StatementSpan = Statement SrcSpan
339340
@@ -347,7 +348,7 @@ instance Annotated Statement where
347348data 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
352353type 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
363364type 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
415416type ParameterSpan = Parameter SrcSpan
416417
@@ -424,7 +425,7 @@ instance Annotated Parameter where
424425data 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
429430type 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
453454type 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
470471type 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
487488type 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.
513526data 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
522535type 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
539552type CompIfSpan = CompIf SrcSpan
540553
@@ -548,7 +561,7 @@ instance Annotated CompIf where
548561data 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
553566type 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
645658type ExprSpan = Expr SrcSpan
646659
@@ -650,7 +663,7 @@ instance Span ExprSpan where
650663data 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
655668type YieldArgSpan = YieldArg SrcSpan
656669
@@ -663,10 +676,13 @@ instance Annotated Expr where
663676
664677data DictMappingPair annot =
665678 DictMappingPair (Expr annot ) (Expr annot )
666- deriving (Eq ,Ord ,Show ,Typeable ,Data )
679+ deriving (Eq ,Ord ,Show ,Typeable ,Data , Functor )
667680
668681type DictMappingPairSpan = DictMappingPair SrcSpan
669682
683+ instance Span DictMappingPairSpan where
684+ getSpan (DictMappingPair e1 e2) = spanning e1 e2
685+
670686-- | Slice compenent.
671687data 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
685701type 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
724740type 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
748764type AssignOpSpan = AssignOp SrcSpan
749765
0 commit comments