summaryrefslogtreecommitdiff
path: root/compiler/GHC/Parser/Errors.hs
blob: b67bf32bafe0a093869897db18a41a72cded3d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
module GHC.Parser.Errors
   ( Warning(..)
   , TransLayoutReason(..)
   , NumUnderscoreReason(..)
   , Error(..)
   , ErrorDesc(..)
   , LexErr(..)
   , CmmParserError(..)
   , LexErrKind(..)
   , Hint(..)
   , StarIsType (..)
   )
where

import GHC.Prelude
import GHC.Types.SrcLoc
import GHC.Types.Name.Reader (RdrName)
import GHC.Types.Name.Occurrence (OccName)
import GHC.Parser.Types
import GHC.Hs.Extension
import GHC.Hs.Expr
import GHC.Hs.Pat
import GHC.Hs.Type
import GHC.Hs.Lit
import GHC.Hs.Decls
import GHC.Core.Coercion.Axiom (Role)
import GHC.Utils.Outputable (SDoc)
import GHC.Data.FastString
import GHC.Unit.Module.Name

data Warning

     -- | Warn when tabulations are found
   = WarnTab
      { tabFirst :: !SrcSpan -- ^ First occurence of a tab
      , tabCount :: !Word    -- ^ Number of other occurences
      }

   | WarnTransitionalLayout !SrcSpan !TransLayoutReason
      -- ^ Transitional layout warnings

   | WarnUnrecognisedPragma !SrcSpan
      -- ^ Unrecognised pragma

   | WarnHaddockInvalidPos !SrcSpan
      -- ^ Invalid Haddock comment position

   | WarnHaddockIgnoreMulti !SrcSpan
      -- ^ Multiple Haddock comment for the same entity

   | WarnStarBinder !SrcSpan
      -- ^ Found binding occurence of "*" while StarIsType is enabled

   | WarnStarIsType !SrcSpan
      -- ^ Using "*" for "Type" without StarIsType enabled

   | WarnImportPreQualified !SrcSpan
      -- ^ Pre qualified import with 'WarnPrepositiveQualifiedModule' enabled


data TransLayoutReason
   = TransLayout_Where -- ^ "`where' clause at the same depth as implicit layout block"
   | TransLayout_Pipe  -- ^ "`|' at the same depth as implicit layout block")

data Error = Error
   { errDesc  :: !ErrorDesc   -- ^ Error description
   , errHints :: ![Hint]      -- ^ Hints
   , errLoc   :: !SrcSpan     -- ^ Error position
   }

data ErrorDesc
   = ErrLambdaCase
      -- ^ LambdaCase syntax used without the extension enabled

   | ErrNumUnderscores !NumUnderscoreReason
      -- ^ Underscores in literals without the extension enabled

   | ErrPrimStringInvalidChar
      -- ^ Invalid character in primitive string

   | ErrMissingBlock
      -- ^ Missing block

   | ErrLexer !LexErr !LexErrKind
      -- ^ Lexer error

   | ErrSuffixAT
      -- ^ Suffix occurence of `@`

   | ErrParse !String
      -- ^ Parse errors

   | ErrCmmLexer
      -- ^ Cmm lexer error

   | ErrUnsupportedBoxedSumExpr !(SumOrTuple (HsExpr GhcPs))
      -- ^ Unsupported boxed sum in expression

   | ErrUnsupportedBoxedSumPat !(SumOrTuple (PatBuilder GhcPs))
      -- ^ Unsupported boxed sum in pattern

   | ErrUnexpectedQualifiedConstructor !RdrName
      -- ^ Unexpected qualified constructor

   | ErrTupleSectionInPat
      -- ^ Tuple section in pattern context

   | ErrIllegalBangPattern !(Pat GhcPs)
      -- ^ Bang-pattern without BangPattterns enabled

   | ErrOpFewArgs !StarIsType !RdrName
      -- ^ Operator applied to too few arguments

   | ErrImportQualifiedTwice
      -- ^ Import: multiple occurrences of 'qualified'

   | ErrImportPostQualified
      -- ^ Post qualified import without 'ImportQualifiedPost'

   | ErrIllegalExplicitNamespace
      -- ^ Explicit namespace keyword without 'ExplicitNamespaces'

   | ErrVarForTyCon !RdrName
      -- ^ Expecting a type constructor but found a variable

   | ErrIllegalPatSynExport
      -- ^ Illegal export form allowed by PatternSynonyms

   | ErrMalformedEntityString
      -- ^ Malformed entity string

   | ErrDotsInRecordUpdate
      -- ^ Dots used in record update

   | ErrPrecedenceOutOfRange !Int
      -- ^ Precedence out of range

   | ErrInvalidDataCon !(HsType GhcPs)
      -- ^ Cannot parse data constructor in a data/newtype declaration

   | ErrInvalidInfixDataCon !(HsType GhcPs) !RdrName !(HsType GhcPs)
      -- ^ Cannot parse data constructor in a data/newtype declaration

   | ErrUnpackDataCon
      -- ^ UNPACK applied to a data constructor

   | ErrUnexpectedKindAppInDataCon !DataConBuilder !(HsType GhcPs)
      -- ^ Unexpected kind application in data/newtype declaration

   | ErrInvalidRecordCon !(PatBuilder GhcPs)
      -- ^ Not a record constructor

   | ErrIllegalUnboxedStringInPat !(HsLit GhcPs)
      -- ^ Illegal unboxed string literal in pattern

   | ErrDoNotationInPat
      -- ^ Do-notation in pattern

   | ErrIfTheElseInPat
      -- ^ If-then-else syntax in pattern

   | ErrTypeAppInPat
      -- ^ Type-application in pattern

   | ErrLambdaCaseInPat
      -- ^ Lambda-case in pattern

   | ErrCaseInPat
      -- ^ case..of in pattern

   | ErrLetInPat
      -- ^ let-syntax in pattern

   | ErrLambdaInPat
      -- ^ Lambda-syntax in pattern

   | ErrArrowExprInPat !(HsExpr GhcPs)
      -- ^ Arrow expression-syntax in pattern

   | ErrArrowCmdInPat !(HsCmd GhcPs)
      -- ^ Arrow command-syntax in pattern

   | ErrArrowCmdInExpr !(HsCmd GhcPs)
      -- ^ Arrow command-syntax in expression

   | ErrViewPatInExpr !(LHsExpr GhcPs) !(LHsExpr GhcPs)
      -- ^ View-pattern in expression

   | ErrTypeAppWithoutSpace !RdrName !(LHsExpr GhcPs)
      -- ^ Type-application without space before '@'

   | ErrLazyPatWithoutSpace !(LHsExpr GhcPs)
      -- ^ Lazy-pattern ('~') without space after it

   | ErrBangPatWithoutSpace !(LHsExpr GhcPs)
      -- ^ Bang-pattern ('!') without space after it

   | ErrUnallowedPragma !(HsPragE GhcPs)
      -- ^ Pragma not allowed in this position

   | ErrQualifiedDoInCmd !ModuleName
      -- ^ Qualified do block in command

   | ErrInvalidInfixHole
      -- ^ Invalid infix hole, expected an infix operator

   | ErrSemiColonsInCondExpr
      -- ^ Unexpected semi-colons in conditional expression
         !(HsExpr GhcPs) -- ^ conditional expr
         !Bool           -- ^ "then" semi-colon?
         !(HsExpr GhcPs) -- ^ "then" expr
         !Bool           -- ^ "else" semi-colon?
         !(HsExpr GhcPs) -- ^ "else" expr

   | ErrSemiColonsInCondCmd
      -- ^ Unexpected semi-colons in conditional command
         !(HsExpr GhcPs) -- ^ conditional expr
         !Bool           -- ^ "then" semi-colon?
         !(HsCmd GhcPs)  -- ^ "then" expr
         !Bool           -- ^ "else" semi-colon?
         !(HsCmd GhcPs)  -- ^ "else" expr

   | ErrAtInPatPos
      -- ^ @-operator in a pattern position

   | ErrLambdaCmdInFunAppCmd !(LHsCmd GhcPs)
      -- ^ Unexpected lambda command in function application

   | ErrCaseCmdInFunAppCmd !(LHsCmd GhcPs)
      -- ^ Unexpected case command in function application

   | ErrIfCmdInFunAppCmd !(LHsCmd GhcPs)
      -- ^ Unexpected if command in function application

   | ErrLetCmdInFunAppCmd !(LHsCmd GhcPs)
      -- ^ Unexpected let command in function application

   | ErrDoCmdInFunAppCmd !(LHsCmd GhcPs)
      -- ^ Unexpected do command in function application

   | ErrDoInFunAppExpr !(Maybe ModuleName) !(LHsExpr GhcPs)
      -- ^ Unexpected do block in function application

   | ErrMDoInFunAppExpr !(Maybe ModuleName) !(LHsExpr GhcPs)
      -- ^ Unexpected mdo block in function application

   | ErrLambdaInFunAppExpr !(LHsExpr GhcPs)
      -- ^ Unexpected lambda expression in function application

   | ErrCaseInFunAppExpr !(LHsExpr GhcPs)
      -- ^ Unexpected case expression in function application

   | ErrLambdaCaseInFunAppExpr !(LHsExpr GhcPs)
      -- ^ Unexpected lambda-case expression in function application

   | ErrLetInFunAppExpr !(LHsExpr GhcPs)
      -- ^ Unexpected let expression in function application

   | ErrIfInFunAppExpr !(LHsExpr GhcPs)
      -- ^ Unexpected if expression in function application

   | ErrProcInFunAppExpr !(LHsExpr GhcPs)
      -- ^ Unexpected proc expression in function application

   | ErrMalformedTyOrClDecl !(LHsType GhcPs)
      -- ^ Malformed head of type or class declaration

   | ErrIllegalWhereInDataDecl
      -- ^ Illegal 'where' keyword in data declaration

   | ErrIllegalDataTypeContext !(LHsContext GhcPs)
      -- ^ Illegal datatyp context

   | ErrParseErrorOnInput !OccName
      -- ^ Parse error on input

   | ErrMalformedDecl !SDoc !RdrName
      -- ^ Malformed ... declaration for ...

   | ErrUnexpectedTypeAppInDecl !(LHsType GhcPs) !SDoc !RdrName
      -- ^ Unexpected type application in a declaration

   | ErrNotADataCon !RdrName
      -- ^ Not a data constructor

   | ErrRecordSyntaxInPatSynDecl !(LPat GhcPs)
      -- ^ Record syntax used in pattern synonym declaration

   | ErrEmptyWhereInPatSynDecl !RdrName
      -- ^ Empty 'where' clause in pattern-synonym declaration

   | ErrInvalidWhereBindInPatSynDecl !RdrName !(HsDecl GhcPs)
      -- ^ Invalid binding name in 'where' clause of pattern-synonym declaration

   | ErrNoSingleWhereBindInPatSynDecl !RdrName !(HsDecl GhcPs)
      -- ^ Multiple bindings in 'where' clause of pattern-synonym declaration

   | ErrDeclSpliceNotAtTopLevel !(SpliceDecl GhcPs)
      -- ^ Declaration splice not a top-level

   | ErrInferredTypeVarNotAllowed
      -- ^ Inferred type variables not allowed here

   | ErrMultipleNamesInStandaloneKindSignature [LIdP GhcPs]
      -- ^ Multiple names in standalone kind signatures

   | ErrIllegalImportBundleForm
      -- ^ Illegal import bundle form

   | ErrIllegalRoleName !FastString [Role]
      -- ^ Illegal role name

   | ErrInvalidTypeSignature !(LHsExpr GhcPs)
      -- ^ Invalid type signature

   | ErrUnexpectedTypeInDecl !(LHsType GhcPs) !SDoc !RdrName [LHsTypeArg GhcPs] !SDoc
      -- ^ Unexpected type in declaration

   | ErrExpectedHyphen
      -- ^ Expected a hyphen

   | ErrSpaceInSCC
      -- ^ Found a space in a SCC

   | ErrEmptyDoubleQuotes !Bool-- Is TH on?
      -- ^ Found two single quotes

   | ErrInvalidPackageName !FastString
      -- ^ Invalid package name

   | ErrInvalidRuleActivationMarker
      -- ^ Invalid rule activation marker

   | ErrLinearFunction
      -- ^ Linear function found but LinearTypes not enabled

   | ErrMultiWayIf
      -- ^ Multi-way if-expression found but MultiWayIf not enabled

   | ErrExplicitForall !Bool -- is Unicode forall?
      -- ^ Explicit forall found but no extension allowing it is enabled

   | ErrIllegalQualifiedDo !SDoc
      -- ^ Found qualified-do without QualifiedDo enabled

   | ErrCmmParser !CmmParserError
      -- ^ Cmm parser error

   | ErrIllegalTraditionalRecordSyntax !SDoc
      -- ^ Illegal traditional record syntax
      --
      -- TODO: distinguish errors without using SDoc

   | ErrParseErrorInCmd !SDoc
      -- ^ Parse error in command
      --
      -- TODO: distinguish errors without using SDoc

   | ErrParseErrorInPat !SDoc
      -- ^ Parse error in pattern
      --
      -- TODO: distinguish errors without using SDoc


newtype StarIsType = StarIsType Bool

data NumUnderscoreReason
   = NumUnderscore_Integral
   | NumUnderscore_Float
   deriving (Show,Eq,Ord)

data Hint
   = SuggestTH
   | SuggestRecursiveDo
   | SuggestDo
   | SuggestMissingDo
   | SuggestLetInDo
   | SuggestPatternSynonyms
   | SuggestInfixBindMaybeAtPat !RdrName

data LexErrKind
   = LexErrKind_EOF        -- ^ End of input
   | LexErrKind_UTF8       -- ^ UTF-8 decoding error
   | LexErrKind_Char !Char -- ^ Error at given character
   deriving (Show,Eq,Ord)

data LexErr
   = LexError               -- ^ Lexical error
   | LexUnknownPragma       -- ^ Unknown pragma
   | LexErrorInPragma       -- ^ Lexical error in pragma
   | LexNumEscapeRange      -- ^ Numeric escape sequence out of range
   | LexStringCharLit       -- ^ Llexical error in string/character literal
   | LexStringCharLitEOF    -- ^ Unexpected end-of-file in string/character literal
   | LexUnterminatedComment -- ^ Unterminated `{-'
   | LexUnterminatedOptions -- ^ Unterminated OPTIONS pragma
   | LexUnterminatedQQ      -- ^ Unterminated quasiquotation

-- | Errors from the Cmm parser
data CmmParserError
   = CmmUnknownPrimitive    !FastString -- ^ Unknown Cmm primitive
   | CmmUnknownMacro        !FastString -- ^ Unknown macro
   | CmmUnknownCConv        !String     -- ^ Unknown calling convention
   | CmmUnrecognisedSafety  !String     -- ^ Unrecognised safety
   | CmmUnrecognisedHint    !String     -- ^ Unrecognised hint