never executed always true always false
1 {-# LANGUAGE DeriveAnyClass #-}
2
3 -- |
4 --
5 -- Module : Aihc.Parser.Syntax
6 -- Description : Abstract Syntax Tree
7 -- License : Unlicense
8 --
9 -- Abstract Syntax Tree (AST) covering Haskell2010 plus all language extensions.
10 module Aihc.Parser.Syntax
11 ( ArithSeq (..),
12 BangType (..),
13 BinderName,
14 CallConv (..),
15 CaseAlt (..),
16 ClassDecl (..),
17 ClassDeclItem (..),
18 CompStmt (..),
19 Constraint (..),
20 DataConDecl (..),
21 DataDecl (..),
22 Decl (..),
23 DerivingClause (..),
24 DerivingStrategy (..),
25 DoStmt (..),
26 Expr (..),
27 Extension (..),
28 ExtensionSetting (..),
29 ExportSpec (..),
30 FieldDecl (..),
31 FixityAssoc (..),
32 ForeignDecl (..),
33 ForeignDirection (..),
34 ForeignEntitySpec (..),
35 ForeignSafety (..),
36 GadtBody (..),
37 GuardQualifier (..),
38 GuardedRhs (..),
39 ImportDecl (..),
40 ImportLevel (..),
41 ImportItem (..),
42 ImportSpec (..),
43 InstanceDecl (..),
44 InstanceDeclItem (..),
45 Literal (..),
46 Match (..),
47 Module (..),
48 ModuleHead (..),
49 WarningText (..),
50 NewtypeDecl (..),
51 OperatorName,
52 Pattern (..),
53 Rhs (..),
54 HasSourceSpan (..),
55 SourceSpan (..),
56 StandaloneDerivingDecl (..),
57 Type (..),
58 TypeLiteral (..),
59 TypePromotion (..),
60 TyVarBinder (..),
61 TypeSynDecl (..),
62 ValueDecl (..),
63 declValueBinderNames,
64 allKnownExtensions,
65 extensionName,
66 extensionSettingName,
67 gadtBodyResultType,
68 mergeSourceSpans,
69 noSourceSpan,
70 parseExtensionName,
71 parseExtensionSettingName,
72 sourceSpanEnd,
73 valueDeclBinderName,
74 moduleName,
75 moduleWarningText,
76 moduleExports,
77 )
78 where
79
80 import Control.Applicative ((<|>))
81 import Control.DeepSeq (NFData)
82 import Data.Data (Data)
83 import Data.Text (Text)
84 import Data.Text qualified as T
85 import GHC.Generics (Generic)
86 import Text.Read (readMaybe)
87
88 data Extension
89 = AllowAmbiguousTypes
90 | AlternativeLayoutRule
91 | AlternativeLayoutRuleTransitional
92 | ApplicativeDo
93 | Arrows
94 | AutoDeriveTypeable
95 | BangPatterns
96 | BinaryLiterals
97 | BlockArguments
98 | CApiFFI
99 | ConstrainedClassMethods
100 | ConstraintKinds
101 | CPP
102 | CUSKs
103 | DataKinds
104 | DatatypeContexts
105 | DeepSubsumption
106 | DefaultSignatures
107 | DeriveAnyClass
108 | DeriveDataTypeable
109 | DeriveFoldable
110 | DeriveFunctor
111 | DeriveGeneric
112 | DeriveLift
113 | DeriveTraversable
114 | DerivingStrategies
115 | DerivingVia
116 | DisambiguateRecordFields
117 | DoAndIfThenElse
118 | DoRec
119 | DuplicateRecordFields
120 | EmptyCase
121 | EmptyDataDecls
122 | EmptyDataDeriving
123 | ExistentialQuantification
124 | ExplicitForAll
125 | ExplicitLevelImports
126 | ExplicitNamespaces
127 | ExtensibleRecords
128 | ExtendedDefaultRules
129 | ExtendedLiterals
130 | FieldSelectors
131 | FlexibleContexts
132 | FlexibleInstances
133 | ForeignFunctionInterface
134 | FunctionalDependencies
135 | GADTs
136 | GADTSyntax
137 | Generics
138 | GeneralizedNewtypeDeriving
139 | GHC2021
140 | GHC2024
141 | GHCForeignImportPrim
142 | Haskell2010
143 | Haskell98
144 | HereDocuments
145 | HexFloatLiterals
146 | ImplicitParams
147 | ImplicitPrelude
148 | ImplicitStagePersistence
149 | ImportQualifiedPost
150 | ImpredicativeTypes
151 | IncoherentInstances
152 | InstanceSigs
153 | InterruptibleFFI
154 | JavaScriptFFI
155 | KindSignatures
156 | LambdaCase
157 | LexicalNegation
158 | LiberalTypeSynonyms
159 | LinearTypes
160 | ListTuplePuns
161 | MagicHash
162 | MonadComprehensions
163 | MonadFailDesugaring
164 | MonoLocalBinds
165 | MonoPatBinds
166 | MonomorphismRestriction
167 | MultilineStrings
168 | MultiParamTypeClasses
169 | MultiWayIf
170 | NamedDefaults
171 | NamedFieldPuns
172 | NamedWildCards
173 | NewQualifiedOperators
174 | NegativeLiterals
175 | NondecreasingIndentation
176 | NPlusKPatterns
177 | NullaryTypeClasses
178 | NumDecimals
179 | NumericUnderscores
180 | OrPatterns
181 | OverlappingInstances
182 | OverloadedLabels
183 | OverloadedLists
184 | OverloadedRecordDot
185 | OverloadedRecordUpdate
186 | OverloadedStrings
187 | PackageImports
188 | ParallelArrays
189 | ParallelListComp
190 | PartialTypeSignatures
191 | PatternSignatures
192 | PatternGuards
193 | PatternSynonyms
194 | PolymorphicComponents
195 | PolyKinds
196 | PostfixOperators
197 | QualifiedDo
198 | QualifiedStrings
199 | QuantifiedConstraints
200 | QuasiQuotes
201 | Rank2Types
202 | RankNTypes
203 | RebindableSyntax
204 | RecordPuns
205 | RecordWildCards
206 | RecursiveDo
207 | RegularPatterns
208 | RelaxedLayout
209 | RelaxedPolyRec
210 | RestrictedTypeSynonyms
211 | RequiredTypeArguments
212 | RoleAnnotations
213 | SafeImports
214 | SafeHaskell
215 | ScopedTypeVariables
216 | StandaloneDeriving
217 | StandaloneKindSignatures
218 | StarIsType
219 | StaticPointers
220 | Strict
221 | StrictData
222 | TemplateHaskell
223 | TemplateHaskellQuotes
224 | TraditionalRecordSyntax
225 | TransformListComp
226 | Trustworthy
227 | TupleSections
228 | TypeAbstractions
229 | TypeApplications
230 | TypeData
231 | TypeFamilies
232 | TypeFamilyDependencies
233 | TypeInType
234 | TypeOperators
235 | TypeSynonymInstances
236 | UnboxedSums
237 | UnboxedTuples
238 | UndecidableInstances
239 | UndecidableSuperClasses
240 | UnicodeSyntax
241 | UnliftedDatatypes
242 | UnliftedFFITypes
243 | UnliftedNewtypes
244 | UnsafeHaskell
245 | ViewPatterns
246 | XmlSyntax
247 deriving (Eq, Ord, Show, Read, Enum, Bounded, Generic, NFData)
248
249 data ExtensionSetting
250 = EnableExtension Extension
251 | DisableExtension Extension
252 deriving (Eq, Ord, Show, Read, Generic, NFData)
253
254 allKnownExtensions :: [Extension]
255 allKnownExtensions = [minBound .. maxBound]
256
257 extensionName :: Extension -> Text
258 extensionName ext =
259 case ext of
260 SafeHaskell -> T.pack "Safe"
261 UnsafeHaskell -> T.pack "Unsafe"
262 _ -> T.pack (show ext)
263
264 extensionSettingName :: ExtensionSetting -> Text
265 extensionSettingName setting =
266 case setting of
267 EnableExtension ext -> extensionName ext
268 DisableExtension ext -> T.pack "No" <> extensionName ext
269
270 parseExtensionName :: Text -> Maybe Extension
271 parseExtensionName raw =
272 readMaybe (T.unpack trimmed) <|> lookup (T.unpack trimmed) aliases
273 where
274 trimmed = T.strip raw
275 aliases =
276 [ ("Cpp", CPP),
277 ("GeneralisedNewtypeDeriving", GeneralizedNewtypeDeriving),
278 ("Safe", SafeHaskell),
279 ("Unsafe", UnsafeHaskell)
280 ]
281
282 parseExtensionSettingName :: Text -> Maybe ExtensionSetting
283 parseExtensionSettingName raw =
284 case T.stripPrefix (T.pack "No") trimmed of
285 Just rest
286 | not (T.null rest) ->
287 case parseExtensionName rest of
288 Just ext -> Just (DisableExtension ext)
289 Nothing -> EnableExtension <$> parseExtensionName trimmed
290 _ -> EnableExtension <$> parseExtensionName trimmed
291 where
292 trimmed = T.strip raw
293
294 data SourceSpan
295 = NoSourceSpan
296 | SourceSpan
297 { sourceSpanStartLine :: !Int,
298 sourceSpanStartCol :: !Int,
299 sourceSpanEndLine :: !Int,
300 sourceSpanEndCol :: !Int
301 }
302 deriving (Data, Eq, Ord, Show, Generic, NFData)
303
304 noSourceSpan :: SourceSpan
305 noSourceSpan = NoSourceSpan
306
307 class HasSourceSpan a where
308 getSourceSpan :: a -> SourceSpan
309
310 mergeSourceSpans :: SourceSpan -> SourceSpan -> SourceSpan
311 mergeSourceSpans left right =
312 case (left, right) of
313 (SourceSpan l1 c1 _ _, SourceSpan _ _ l2 c2) -> SourceSpan l1 c1 l2 c2
314 (NoSourceSpan, span') -> span'
315 (span', NoSourceSpan) -> span'
316
317 sourceSpanEnd :: (HasSourceSpan a) => [a] -> SourceSpan
318 sourceSpanEnd xs =
319 case reverse xs of
320 [] -> NoSourceSpan
321 x : _ -> getSourceSpan x
322
323 type BinderName = Text
324
325 type OperatorName = Text
326
327 data WarningText
328 = DeprText SourceSpan Text
329 | WarnText SourceSpan Text
330 deriving (Eq, Show, Generic, NFData)
331
332 instance HasSourceSpan WarningText where
333 getSourceSpan warningText =
334 case warningText of
335 DeprText span' _ -> span'
336 WarnText span' _ -> span'
337
338 data Module = Module
339 { moduleSpan :: SourceSpan,
340 moduleHead :: Maybe ModuleHead,
341 moduleLanguagePragmas :: [ExtensionSetting],
342 moduleImports :: [ImportDecl],
343 moduleDecls :: [Decl]
344 }
345 deriving (Eq, Show, Generic, NFData)
346
347 instance HasSourceSpan Module where
348 getSourceSpan = moduleSpan
349
350 data ModuleHead = ModuleHead
351 { moduleHeadSpan :: SourceSpan,
352 moduleHeadName :: Text,
353 moduleHeadWarningText :: Maybe WarningText,
354 moduleHeadExports :: Maybe [ExportSpec]
355 }
356 deriving (Eq, Show, Generic, NFData)
357
358 instance HasSourceSpan ModuleHead where
359 getSourceSpan = moduleHeadSpan
360
361 moduleName :: Module -> Maybe Text
362 moduleName modu = moduleHeadName <$> moduleHead modu
363
364 moduleWarningText :: Module -> Maybe WarningText
365 moduleWarningText modu = moduleHeadWarningText =<< moduleHead modu
366
367 moduleExports :: Module -> Maybe [ExportSpec]
368 moduleExports modu = moduleHeadExports =<< moduleHead modu
369
370 data ExportSpec
371 = ExportModule SourceSpan Text
372 | ExportVar SourceSpan (Maybe Text) Text
373 | ExportAbs SourceSpan (Maybe Text) Text
374 | ExportAll SourceSpan (Maybe Text) Text
375 | ExportWith SourceSpan (Maybe Text) Text [Text]
376 deriving (Eq, Show, Generic, NFData)
377
378 data ImportDecl = ImportDecl
379 { importDeclSpan :: SourceSpan,
380 importDeclLevel :: Maybe ImportLevel,
381 importDeclPackage :: Maybe Text,
382 importDeclQualified :: Bool,
383 importDeclQualifiedPost :: Bool,
384 importDeclModule :: Text,
385 importDeclAs :: Maybe Text,
386 importDeclSpec :: Maybe ImportSpec
387 }
388 deriving (Eq, Show, Generic, NFData)
389
390 instance HasSourceSpan ImportDecl where
391 getSourceSpan = importDeclSpan
392
393 data ImportLevel
394 = ImportLevelQuote
395 | ImportLevelSplice
396 deriving (Eq, Show, Generic, NFData)
397
398 data ImportSpec = ImportSpec
399 { importSpecSpan :: SourceSpan,
400 importSpecHiding :: Bool,
401 importSpecItems :: [ImportItem]
402 }
403 deriving (Eq, Show, Generic, NFData)
404
405 instance HasSourceSpan ImportSpec where
406 getSourceSpan = importSpecSpan
407
408 data ImportItem
409 = ImportItemVar SourceSpan (Maybe Text) Text
410 | ImportItemAbs SourceSpan (Maybe Text) Text
411 | ImportItemAll SourceSpan (Maybe Text) Text
412 | ImportItemWith SourceSpan (Maybe Text) Text [Text]
413 deriving (Eq, Show, Generic, NFData)
414
415 data Decl
416 = DeclValue SourceSpan ValueDecl
417 | DeclTypeSig SourceSpan [BinderName] Type
418 | DeclStandaloneKindSig SourceSpan BinderName Type
419 | DeclFixity SourceSpan FixityAssoc (Maybe Int) [OperatorName]
420 | DeclTypeSyn SourceSpan TypeSynDecl
421 | DeclData SourceSpan DataDecl
422 | DeclNewtype SourceSpan NewtypeDecl
423 | DeclClass SourceSpan ClassDecl
424 | DeclInstance SourceSpan InstanceDecl
425 | DeclStandaloneDeriving SourceSpan StandaloneDerivingDecl
426 | DeclDefault SourceSpan [Type]
427 | DeclForeign SourceSpan ForeignDecl
428 deriving (Data, Eq, Show, Generic, NFData)
429
430 instance HasSourceSpan Decl where
431 getSourceSpan decl =
432 case decl of
433 DeclValue span' _ -> span'
434 DeclTypeSig span' _ _ -> span'
435 DeclStandaloneKindSig span' _ _ -> span'
436 DeclFixity span' _ _ _ -> span'
437 DeclTypeSyn span' _ -> span'
438 DeclData span' _ -> span'
439 DeclNewtype span' _ -> span'
440 DeclClass span' _ -> span'
441 DeclInstance span' _ -> span'
442 DeclStandaloneDeriving span' _ -> span'
443 DeclDefault span' _ -> span'
444 DeclForeign span' _ -> span'
445
446 data ValueDecl
447 = FunctionBind SourceSpan BinderName [Match]
448 | PatternBind SourceSpan Pattern Rhs
449 deriving (Data, Eq, Show, Generic, NFData)
450
451 instance HasSourceSpan ValueDecl where
452 getSourceSpan valueDecl =
453 case valueDecl of
454 FunctionBind span' _ _ -> span'
455 PatternBind span' _ _ -> span'
456
457 data Match = Match
458 { matchSpan :: SourceSpan,
459 matchPats :: [Pattern],
460 matchRhs :: Rhs
461 }
462 deriving (Data, Eq, Show, Generic, NFData)
463
464 instance HasSourceSpan Match where
465 getSourceSpan = matchSpan
466
467 data Rhs
468 = UnguardedRhs SourceSpan Expr
469 | GuardedRhss SourceSpan [GuardedRhs]
470 deriving (Data, Eq, Show, Generic, NFData)
471
472 instance HasSourceSpan Rhs where
473 getSourceSpan rhs =
474 case rhs of
475 UnguardedRhs span' _ -> span'
476 GuardedRhss span' _ -> span'
477
478 data GuardedRhs = GuardedRhs
479 { guardedRhsSpan :: SourceSpan,
480 guardedRhsGuards :: [GuardQualifier],
481 guardedRhsBody :: Expr
482 }
483 deriving (Data, Eq, Show, Generic, NFData)
484
485 instance HasSourceSpan GuardedRhs where
486 getSourceSpan = guardedRhsSpan
487
488 data GuardQualifier
489 = GuardExpr SourceSpan Expr
490 | GuardPat SourceSpan Pattern Expr
491 | GuardLet SourceSpan [Decl]
492 deriving (Data, Eq, Show, Generic, NFData)
493
494 instance HasSourceSpan GuardQualifier where
495 getSourceSpan qualifier =
496 case qualifier of
497 GuardExpr span' _ -> span'
498 GuardPat span' _ _ -> span'
499 GuardLet span' _ -> span'
500
501 data Literal
502 = LitInt SourceSpan Integer Text
503 | LitIntBase SourceSpan Integer Text
504 | LitFloat SourceSpan Double Text
505 | LitChar SourceSpan Char Text
506 | LitString SourceSpan Text Text
507 deriving (Data, Eq, Show, Generic, NFData)
508
509 instance HasSourceSpan Literal where
510 getSourceSpan literal =
511 case literal of
512 LitInt span' _ _ -> span'
513 LitIntBase span' _ _ -> span'
514 LitFloat span' _ _ -> span'
515 LitChar span' _ _ -> span'
516 LitString span' _ _ -> span'
517
518 data Pattern
519 = PVar SourceSpan Text
520 | PWildcard SourceSpan
521 | PLit SourceSpan Literal
522 | PQuasiQuote SourceSpan Text Text
523 | PTuple SourceSpan [Pattern]
524 | PList SourceSpan [Pattern]
525 | PCon SourceSpan Text [Pattern]
526 | PInfix SourceSpan Pattern Text Pattern
527 | PView SourceSpan Expr Pattern
528 | PAs SourceSpan Text Pattern
529 | PStrict SourceSpan Pattern
530 | PIrrefutable SourceSpan Pattern
531 | PNegLit SourceSpan Literal
532 | PParen SourceSpan Pattern
533 | PRecord SourceSpan Text [(Text, Pattern)]
534 deriving (Data, Eq, Show, Generic, NFData)
535
536 instance HasSourceSpan Pattern where
537 getSourceSpan pat =
538 case pat of
539 PVar span' _ -> span'
540 PWildcard span' -> span'
541 PLit span' _ -> span'
542 PQuasiQuote span' _ _ -> span'
543 PTuple span' _ -> span'
544 PList span' _ -> span'
545 PCon span' _ _ -> span'
546 PInfix span' _ _ _ -> span'
547 PView span' _ _ -> span'
548 PAs span' _ _ -> span'
549 PStrict span' _ -> span'
550 PIrrefutable span' _ -> span'
551 PNegLit span' _ -> span'
552 PParen span' _ -> span'
553 PRecord span' _ _ -> span'
554
555 data Type
556 = TVar SourceSpan Text
557 | TCon SourceSpan Text TypePromotion
558 | TTypeLit SourceSpan TypeLiteral
559 | TStar SourceSpan
560 | TQuasiQuote SourceSpan Text Text
561 | TForall SourceSpan [Text] Type
562 | TApp SourceSpan Type Type
563 | TFun SourceSpan Type Type
564 | TTuple SourceSpan TypePromotion [Type]
565 | TList SourceSpan TypePromotion Type
566 | TParen SourceSpan Type
567 | TContext SourceSpan [Constraint] Type
568 deriving (Data, Eq, Show, Generic, NFData)
569
570 instance HasSourceSpan Type where
571 getSourceSpan ty =
572 case ty of
573 TVar span' _ -> span'
574 TCon span' _ _ -> span'
575 TTypeLit span' _ -> span'
576 TStar span' -> span'
577 TQuasiQuote span' _ _ -> span'
578 TForall span' _ _ -> span'
579 TApp span' _ _ -> span'
580 TFun span' _ _ -> span'
581 TTuple span' _ _ -> span'
582 TList span' _ _ -> span'
583 TParen span' _ -> span'
584 TContext span' _ _ -> span'
585
586 data TypeLiteral
587 = TypeLitInteger Integer Text
588 | TypeLitSymbol Text Text
589 | TypeLitChar Char Text
590 deriving (Data, Eq, Show, Generic, NFData)
591
592 data TypePromotion
593 = Unpromoted
594 | Promoted
595 deriving (Data, Eq, Show, Generic, NFData)
596
597 data Constraint = Constraint
598 { constraintSpan :: SourceSpan,
599 constraintClass :: Text,
600 constraintArgs :: [Type],
601 constraintParen :: Bool
602 }
603 deriving (Data, Eq, Show, Generic, NFData)
604
605 instance HasSourceSpan Constraint where
606 getSourceSpan = constraintSpan
607
608 data TyVarBinder = TyVarBinder
609 { tyVarBinderSpan :: SourceSpan,
610 tyVarBinderName :: Text,
611 tyVarBinderKind :: Maybe Type
612 }
613 deriving (Data, Eq, Show, Generic, NFData)
614
615 instance HasSourceSpan TyVarBinder where
616 getSourceSpan = tyVarBinderSpan
617
618 data TypeSynDecl = TypeSynDecl
619 { typeSynSpan :: SourceSpan,
620 typeSynName :: Text,
621 typeSynParams :: [TyVarBinder],
622 typeSynBody :: Type
623 }
624 deriving (Data, Eq, Show, Generic, NFData)
625
626 instance HasSourceSpan TypeSynDecl where
627 getSourceSpan = typeSynSpan
628
629 data DataDecl = DataDecl
630 { dataDeclSpan :: SourceSpan,
631 dataDeclContext :: [Constraint],
632 dataDeclName :: Text,
633 dataDeclParams :: [TyVarBinder],
634 dataDeclConstructors :: [DataConDecl],
635 dataDeclDeriving :: [DerivingClause]
636 }
637 deriving (Data, Eq, Show, Generic, NFData)
638
639 instance HasSourceSpan DataDecl where
640 getSourceSpan = dataDeclSpan
641
642 data NewtypeDecl = NewtypeDecl
643 { newtypeDeclSpan :: SourceSpan,
644 newtypeDeclContext :: [Constraint],
645 newtypeDeclName :: Text,
646 newtypeDeclParams :: [TyVarBinder],
647 newtypeDeclConstructor :: Maybe DataConDecl,
648 newtypeDeclDeriving :: [DerivingClause]
649 }
650 deriving (Data, Eq, Show, Generic, NFData)
651
652 instance HasSourceSpan NewtypeDecl where
653 getSourceSpan = newtypeDeclSpan
654
655 data DataConDecl
656 = PrefixCon SourceSpan [Text] [Constraint] Text [BangType]
657 | InfixCon SourceSpan [Text] [Constraint] BangType Text BangType
658 | RecordCon SourceSpan [Text] [Constraint] Text [FieldDecl]
659 | -- | GADT-style constructor: @Con :: forall a. Ctx => Type@
660 -- The list of names supports multiple constructors: @T1, T2 :: Type@
661 GadtCon SourceSpan [TyVarBinder] [Constraint] [Text] GadtBody
662 deriving (Data, Eq, Show, Generic, NFData)
663
664 -- | Body of a GADT constructor after the @::@ and optional forall/context
665 data GadtBody
666 = -- | Prefix body: @a -> b -> T a@
667 GadtPrefixBody [BangType] Type
668 | -- | Record body: @{ field :: Type } -> T a@
669 GadtRecordBody [FieldDecl] Type
670 deriving (Data, Eq, Show, Generic, NFData)
671
672 -- | Get the result type from a GADT body
673 gadtBodyResultType :: GadtBody -> Type
674 gadtBodyResultType body =
675 case body of
676 GadtPrefixBody _ ty -> ty
677 GadtRecordBody _ ty -> ty
678
679 instance HasSourceSpan DataConDecl where
680 getSourceSpan dataConDecl =
681 case dataConDecl of
682 PrefixCon span' _ _ _ _ -> span'
683 InfixCon span' _ _ _ _ _ -> span'
684 RecordCon span' _ _ _ _ -> span'
685 GadtCon span' _ _ _ _ -> span'
686
687 data BangType = BangType
688 { bangSpan :: SourceSpan,
689 bangStrict :: Bool,
690 bangType :: Type
691 }
692 deriving (Data, Eq, Show, Generic, NFData)
693
694 instance HasSourceSpan BangType where
695 getSourceSpan = bangSpan
696
697 data FieldDecl = FieldDecl
698 { fieldSpan :: SourceSpan,
699 fieldNames :: [Text],
700 fieldType :: BangType
701 }
702 deriving (Data, Eq, Show, Generic, NFData)
703
704 instance HasSourceSpan FieldDecl where
705 getSourceSpan = fieldSpan
706
707 data DerivingClause = DerivingClause
708 { derivingStrategy :: Maybe DerivingStrategy,
709 derivingClasses :: [Text]
710 }
711 deriving (Data, Eq, Show, Generic, NFData)
712
713 data DerivingStrategy
714 = DerivingStock
715 | DerivingNewtype
716 | DerivingAnyclass
717 deriving (Data, Eq, Show, Generic, NFData)
718
719 data StandaloneDerivingDecl = StandaloneDerivingDecl
720 { standaloneDerivingSpan :: SourceSpan,
721 standaloneDerivingStrategy :: Maybe DerivingStrategy,
722 standaloneDerivingContext :: [Constraint],
723 standaloneDerivingClassName :: Text,
724 standaloneDerivingTypes :: [Type]
725 }
726 deriving (Data, Eq, Show, Generic, NFData)
727
728 instance HasSourceSpan StandaloneDerivingDecl where
729 getSourceSpan = standaloneDerivingSpan
730
731 data ClassDecl = ClassDecl
732 { classDeclSpan :: SourceSpan,
733 classDeclContext :: Maybe [Constraint],
734 classDeclName :: Text,
735 classDeclParams :: [TyVarBinder],
736 classDeclItems :: [ClassDeclItem]
737 }
738 deriving (Data, Eq, Show, Generic, NFData)
739
740 instance HasSourceSpan ClassDecl where
741 getSourceSpan = classDeclSpan
742
743 data ClassDeclItem
744 = ClassItemTypeSig SourceSpan [BinderName] Type
745 | ClassItemFixity SourceSpan FixityAssoc (Maybe Int) [OperatorName]
746 | ClassItemDefault SourceSpan ValueDecl
747 deriving (Data, Eq, Show, Generic, NFData)
748
749 instance HasSourceSpan ClassDeclItem where
750 getSourceSpan classDeclItem =
751 case classDeclItem of
752 ClassItemTypeSig span' _ _ -> span'
753 ClassItemFixity span' _ _ _ -> span'
754 ClassItemDefault span' _ -> span'
755
756 data InstanceDecl = InstanceDecl
757 { instanceDeclSpan :: SourceSpan,
758 instanceDeclContext :: [Constraint],
759 instanceDeclClassName :: Text,
760 instanceDeclTypes :: [Type],
761 instanceDeclItems :: [InstanceDeclItem]
762 }
763 deriving (Data, Eq, Show, Generic, NFData)
764
765 instance HasSourceSpan InstanceDecl where
766 getSourceSpan = instanceDeclSpan
767
768 data InstanceDeclItem
769 = InstanceItemBind SourceSpan ValueDecl
770 | InstanceItemTypeSig SourceSpan [BinderName] Type
771 | InstanceItemFixity SourceSpan FixityAssoc (Maybe Int) [OperatorName]
772 deriving (Data, Eq, Show, Generic, NFData)
773
774 instance HasSourceSpan InstanceDeclItem where
775 getSourceSpan instanceDeclItem =
776 case instanceDeclItem of
777 InstanceItemBind span' _ -> span'
778 InstanceItemTypeSig span' _ _ -> span'
779 InstanceItemFixity span' _ _ _ -> span'
780
781 data FixityAssoc
782 = Infix
783 | InfixL
784 | InfixR
785 deriving (Data, Eq, Show, Generic, NFData)
786
787 data ForeignDecl = ForeignDecl
788 { foreignDeclSpan :: SourceSpan,
789 foreignDirection :: ForeignDirection,
790 foreignCallConv :: CallConv,
791 foreignSafety :: Maybe ForeignSafety,
792 foreignEntity :: ForeignEntitySpec,
793 foreignName :: Text,
794 foreignType :: Type
795 }
796 deriving (Data, Eq, Show, Generic, NFData)
797
798 instance HasSourceSpan ForeignDecl where
799 getSourceSpan = foreignDeclSpan
800
801 data ForeignEntitySpec
802 = ForeignEntityDynamic
803 | ForeignEntityWrapper
804 | ForeignEntityStatic (Maybe Text)
805 | ForeignEntityAddress (Maybe Text)
806 | ForeignEntityNamed Text
807 | ForeignEntityOmitted
808 deriving (Data, Eq, Show, Generic, NFData)
809
810 data ForeignDirection
811 = ForeignImport
812 | ForeignExport
813 deriving (Data, Eq, Show, Generic, NFData)
814
815 data CallConv
816 = CCall
817 | StdCall
818 deriving (Data, Eq, Show, Generic, NFData)
819
820 data ForeignSafety
821 = Safe
822 | Unsafe
823 deriving (Data, Eq, Show, Generic, NFData)
824
825 data Expr
826 = EVar SourceSpan Text
827 | EInt SourceSpan Integer Text
828 | EIntBase SourceSpan Integer Text
829 | EFloat SourceSpan Double Text
830 | EChar SourceSpan Char Text
831 | EString SourceSpan Text Text
832 | EQuasiQuote SourceSpan Text Text
833 | EIf SourceSpan Expr Expr Expr
834 | ELambdaPats SourceSpan [Pattern] Expr
835 | ELambdaCase SourceSpan [CaseAlt]
836 | EInfix SourceSpan Expr Text Expr
837 | ENegate SourceSpan Expr
838 | ESectionL SourceSpan Expr Text
839 | ESectionR SourceSpan Text Expr
840 | ELetDecls SourceSpan [Decl] Expr
841 | ECase SourceSpan Expr [CaseAlt]
842 | EDo SourceSpan [DoStmt]
843 | EListComp SourceSpan Expr [CompStmt]
844 | EListCompParallel SourceSpan Expr [[CompStmt]]
845 | EArithSeq SourceSpan ArithSeq
846 | ERecordCon SourceSpan Text [(Text, Expr)]
847 | ERecordUpd SourceSpan Expr [(Text, Expr)]
848 | ETypeSig SourceSpan Expr Type
849 | EParen SourceSpan Expr
850 | EWhereDecls SourceSpan Expr [Decl]
851 | EList SourceSpan [Expr]
852 | ETuple SourceSpan [Expr]
853 | ETupleSection SourceSpan [Maybe Expr]
854 | ETupleCon SourceSpan Int
855 | ETypeApp SourceSpan Expr Type
856 | EApp SourceSpan Expr Expr
857 deriving (Data, Eq, Show, Generic, NFData)
858
859 instance HasSourceSpan Expr where
860 getSourceSpan expr =
861 case expr of
862 EVar span' _ -> span'
863 EInt span' _ _ -> span'
864 EIntBase span' _ _ -> span'
865 EFloat span' _ _ -> span'
866 EChar span' _ _ -> span'
867 EString span' _ _ -> span'
868 EQuasiQuote span' _ _ -> span'
869 EIf span' _ _ _ -> span'
870 ELambdaPats span' _ _ -> span'
871 ELambdaCase span' _ -> span'
872 EInfix span' _ _ _ -> span'
873 ENegate span' _ -> span'
874 ESectionL span' _ _ -> span'
875 ESectionR span' _ _ -> span'
876 ELetDecls span' _ _ -> span'
877 ECase span' _ _ -> span'
878 EDo span' _ -> span'
879 EListComp span' _ _ -> span'
880 EListCompParallel span' _ _ -> span'
881 EArithSeq span' _ -> span'
882 ERecordCon span' _ _ -> span'
883 ERecordUpd span' _ _ -> span'
884 ETypeSig span' _ _ -> span'
885 EParen span' _ -> span'
886 EWhereDecls span' _ _ -> span'
887 EList span' _ -> span'
888 ETuple span' _ -> span'
889 ETupleSection span' _ -> span'
890 ETupleCon span' _ -> span'
891 ETypeApp span' _ _ -> span'
892 EApp span' _ _ -> span'
893
894 data CaseAlt = CaseAlt
895 { caseAltSpan :: SourceSpan,
896 caseAltPattern :: Pattern,
897 caseAltRhs :: Rhs
898 }
899 deriving (Data, Eq, Show, Generic, NFData)
900
901 instance HasSourceSpan CaseAlt where
902 getSourceSpan = caseAltSpan
903
904 data DoStmt
905 = DoBind SourceSpan Pattern Expr
906 | DoLet SourceSpan [(Text, Expr)]
907 | DoLetDecls SourceSpan [Decl]
908 | DoExpr SourceSpan Expr
909 deriving (Data, Eq, Show, Generic, NFData)
910
911 instance HasSourceSpan DoStmt where
912 getSourceSpan doStmt =
913 case doStmt of
914 DoBind span' _ _ -> span'
915 DoLet span' _ -> span'
916 DoLetDecls span' _ -> span'
917 DoExpr span' _ -> span'
918
919 data CompStmt
920 = CompGen SourceSpan Pattern Expr
921 | CompGuard SourceSpan Expr
922 | CompLet SourceSpan [(Text, Expr)]
923 | CompLetDecls SourceSpan [Decl]
924 deriving (Data, Eq, Show, Generic, NFData)
925
926 instance HasSourceSpan CompStmt where
927 getSourceSpan compStmt =
928 case compStmt of
929 CompGen span' _ _ -> span'
930 CompGuard span' _ -> span'
931 CompLet span' _ -> span'
932 CompLetDecls span' _ -> span'
933
934 data ArithSeq
935 = ArithSeqFrom SourceSpan Expr
936 | ArithSeqFromThen SourceSpan Expr Expr
937 | ArithSeqFromTo SourceSpan Expr Expr
938 | ArithSeqFromThenTo SourceSpan Expr Expr Expr
939 deriving (Data, Eq, Show, Generic, NFData)
940
941 instance HasSourceSpan ArithSeq where
942 getSourceSpan arithSeq =
943 case arithSeq of
944 ArithSeqFrom span' _ -> span'
945 ArithSeqFromThen span' _ _ -> span'
946 ArithSeqFromTo span' _ _ -> span'
947 ArithSeqFromThenTo span' _ _ _ -> span'
948
949 valueDeclBinderName :: ValueDecl -> Maybe Text
950 valueDeclBinderName vdecl =
951 case vdecl of
952 FunctionBind _ name _ -> Just name
953 PatternBind _ pat _ ->
954 case pat of
955 PVar _ name -> Just name
956 _ -> Nothing
957
958 declValueBinderNames :: Decl -> [Text]
959 declValueBinderNames decl =
960 case decl of
961 DeclValue _ vdecl ->
962 case valueDeclBinderName vdecl of
963 Just name -> [name]
964 Nothing -> []
965 _ -> []