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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
{- |
    Module      :  AST.SpanAST
    Description :  Extension of Abstract Syntax Tree

    This module contains the description of the AST which has been
    extended by span information (i.e. start and end positions) (SpanAST).
    The inline comments document for which tokens span information was added.
-}







module AST.SpanAST where

import AST.AST   (KnownExtension (..), Tool (..))
import AST.Ident (ModuleIdent, Ident, QualIdent, SymIdent, SymQualIdent)
import AST.Span  (Span)

-- ---------------------------------------------------------------------------
-- Modules
-- ---------------------------------------------------------------------------

-- |Curry module
--  1. (Maybe Span) -  KW_module
--  2. (Maybe Span) -  KW_where
data Module = Module [ModulePragma] (Maybe Span) ModuleIdent (Maybe Span)
                     (Maybe ExportSpec) [ImportDecl] [Decl]
 deriving Show

-- |Module pragma
data ModulePragma
  -- 1. Span   -  PragmaLanguage
  -- 2. [Span] -  Commas
  -- 3. Span   -  PragmaEnd
  = LanguagePragma Span [Extension] [Span] Span
  -- 1. Span   - PragmaOptions
  -- 2. Span   - PragmaEnd
  | OptionsPragma  Span (Maybe String) String Span
 deriving Show

-- |Specified language extensions, either known or unknown.
data Extension
  = KnownExtension   Span KnownExtension   -- 1. Span  -  Id "KnownExtension"
  | UnknownExtension Span String           -- 1. Span  -  Id "Name"
 deriving Show

-- |Export specification
-- 1. Span   -  LeftParen
-- 2. [Span] -  Commas
-- 3. Span   -  RightParen
data ExportSpec = Exporting Span [Export] [Span] Span
 deriving Show

-- |Single exported entity
data Export
  = Export         SymQualIdent
  -- 1. Span   -  LeftParen
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightParen
  | ExportTypeWith QualIdent Span [Ident] [Span] Span
  -- 1. Span   -  LeftParen
  -- 2. Span   -  DotDot
  -- 3. Span   -  RightParen
  | ExportTypeAll  QualIdent Span Span Span
  -- 1. Span   -  KW_module
  | ExportModule   Span ModuleIdent
 deriving Show

-- |Import declaration
-- 1. Span         -  KW_import
-- 2. (Maybe Span) -  Id_qualified
-- 3. (Maybe Span) -  Id_as
data ImportDecl = ImportDecl Span (Maybe Span) ModuleIdent (Maybe Span) Qualified
                             (Maybe ModuleIdent) (Maybe ImportSpec)
 deriving Show

-- |Flag to signal qualified import
type Qualified = Bool

-- |Import specification
data ImportSpec
  -- 1. Span   -  LeftParen
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightParen
  = Importing Span [Import] [Span] Span
  -- 1. Span   -  Id_hiding
  -- 2. Span   -  LeftParen
  -- 3. [Span] -  Commas
  -- 4. Span   -  RightParen
  | Hiding    Span Span [Import] [Span] Span
 deriving Show

-- |Single imported entity
data Import
  = Import         SymIdent
  -- 1. Span   -  LeftParen
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightParen
  | ImportTypeWith Ident Span [Ident] [Span] Span
  -- 1. Span   -  LeftParen
  -- 2. Span   -  DotDot
  -- 3. Span   -  RightParen
  | ImportTypeAll  Ident Span Span Span
 deriving Show

-- ---------------------------------------------------------------------------
-- Declarations (local or top-level)
-- ---------------------------------------------------------------------------

-- |Declaration in a module
data Decl
  -- 1. [Span] -  Backquotes
  = InfixDecl    Infix (Maybe Precedence) [SymIdent] [Span]
  -- 1. Span         -  KW_data
  -- 2. (Maybe Span) -  Equals
  -- 3. [Span]       -  Bars
  -- 4. (Maybe Span) -  KW_deriving
  -- 5. (Maybe Span) -  LeftParen
  -- 6. [Span]       -  Commas
  -- 7. (Maybe Span) -  RightParen
  | DataDecl     Span Ident [Ident] (Maybe Span) [ConstrDecl] [Span] (Maybe Span) (Maybe Span) [QualIdent] [Span] (Maybe Span)
  -- 1. Span         -  KW_newtype
  -- 2. Span         -  Equals
  -- 3. (Maybe Span) -  KW_deriving
  -- 4. (Maybe Span) -  LeftParen
  -- 5. [Span]       -  Commas
  -- 6. (Maybe Span) -  RightParen
  | NewtypeDecl  Span Ident [Ident] Span NewConstrDecl (Maybe Span) (Maybe Span) [QualIdent] [Span] (Maybe Span)
  -- 1. Span   -  KW_type
  -- 2. Span   -  Equals
  | TypeDecl     Span Ident [Ident] Span TypeExpr
  -- 1. [Span] - Commas
  -- 2. Span   - DotDot
  | TypeSig      [SymIdent] [Span] Span QualTypeExpr
  | FunctionDecl Ident [Equation]
  -- 1. Span                   -  KW_foreign
  -- 2. (Maybe (Span, String)) -  Id "name.h"
  -- 3. Span                   -  DotDot
  | ForeignDecl  Span CallConv (Maybe (Span, String)) SymIdent Span TypeExpr
  -- 1. [Span] -  Commas
  -- 2. Span   -  KW_external
  | ExternalDecl [SymIdent] [Span] Span
  | PatternDecl  Pattern Rhs
  -- 1. [Span] -  Commas
  -- 2. Span   -  KW_free
  | FreeDecl     [Ident] [Span] Span
  -- 1. Span   -  KW_default
  -- 2. Span   -  LeftParen
  -- 3. [Span] -  Commas
  -- 4. Span   -  RightParen
  | DefaultDecl  Span Span [TypeExpr] [Span] Span
  -- 1. Span         -  KW_class
  -- 2. (Maybe Span) -  DoubleArrow
  -- 3. (Maybe Span) -  KW_where
  | ClassDecl    Span Context (Maybe Span) Ident Ident (Maybe Span) [Decl]
  -- 1. Span         -  KW_instance
  -- 2. (Maybe Span) -  DoubleArrow
  -- 3. (Maybe Span) -  KW_where
  | InstanceDecl Span Context (Maybe Span) QualIdent InstanceType (Maybe Span) [Decl]
 deriving Show

-- |Operator precedence
type Precedence = (Span, Int)

-- |Fixity of operators
data Infix
  = InfixL Span   -- 1. Span -  KW_InfixL
  | InfixR Span   -- 1. Span -  KW_InfixR
  | Infix  Span   -- 1. Span -  KW_Infix
 deriving Show

-- |Constructor declaration for algebraic data types
data ConstrDecl
  -- 1. (Maybe Span) -  Id_forall
  -- 2. (Maybe Span) -  Dot
  -- 3. (Maybe Span) -  DoubleArrow
  = ConstrDecl (Maybe Span) [Ident] (Maybe Span)  Context (Maybe Span) Ident [TypeExpr]
  -- 1. (Maybe Span) -  Id_forall
  -- 2. (Maybe Span) -  Dot
  -- 3. (Maybe Span) -  DoubleArrow
  | ConOpDecl  (Maybe Span) [Ident] (Maybe Span) Context (Maybe Span) TypeExpr Ident TypeExpr
  -- 1. (Maybe Span) -  Id_forall
  -- 2. (Maybe Span) -  Dot
  -- 3. (Maybe Span) -  DoubleArrow
  -- 4. Span         -  LeftBrace
  -- 5. [Span]       -  Commas
  -- 6. Span         -  RightBrace
  | RecordDecl (Maybe Span) [Ident] (Maybe Span) Context (Maybe Span) Ident Span [FieldDecl] [Span] Span
 deriving Show

-- |Constructor declaration for renaming types (newtypes)
data NewConstrDecl
  = NewConstrDecl Ident TypeExpr
  -- 1. Span -  LeftBrace
  -- 2. Span -  DotDot
  -- 3. Span -  RightBrace
  | NewRecordDecl Ident Span (Ident, Span, TypeExpr) Span
 deriving Show

-- |Declaration for labelled fields
-- 1. [Span] -  Commas
-- 2. Span   -  DotDot
data FieldDecl = FieldDecl [Ident] [Span] Span TypeExpr
 deriving Show

-- |Calling convention for C code
data CallConv
  = CallConvPrimitive Span    -- 1. Span -  Id_primitive
  | CallConvCCall     Span    -- 1. Span -  Id_ccall
 deriving Show

-- |Type expressions
data TypeExpr
  = ConstructorType QualIdent
  | ApplyType       TypeExpr TypeExpr
  | VariableType    Ident
  -- 1. Span   -  LeftParen
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightParen
  | TupleType       Span [TypeExpr] [Span] Span
  -- 1. Span   -  LeftBracket
  -- 2. Span   -  RightBracket
  | ListType        Span TypeExpr Span
  -- 1. Span   - RightArrow
  | ArrowType       TypeExpr Span TypeExpr
  -- 1. Span   - LeftParen
  -- 2. Span   - RightParen
  | ParenType       Span TypeExpr Span
 deriving Show

-- |Qualified type expressions
-- 1. (Maybe Span) -  DoubleArrow
data QualTypeExpr = QualTypeExpr Context (Maybe Span) TypeExpr
 deriving Show

-- ---------------------------------------------------------------------------
-- Type classes
-- ---------------------------------------------------------------------------

-- 1. (Maybe Span) -  LeftParen
-- 2. [Span]       -  Commas
-- 3. (Maybe Span) -  RightParen
data Context = Context (Maybe Span) [Constraint] [Span] (Maybe Span)
 deriving Show

data Constraint = Constraint QualIdent TypeExpr
 deriving Show

type InstanceType = TypeExpr

-- ---------------------------------------------------------------------------
-- Functions
-- ---------------------------------------------------------------------------

-- |Function defining equation
data Equation = Equation Lhs Rhs
 deriving Show

-- |Left-hand-side of an 'Equation' (function identifier and patterns)
data Lhs
  = FunLhs SymIdent [Pattern]
  | OpLhs  Pattern SymIdent Pattern
  | ApLhs  Lhs [Pattern]
 deriving Show

-- |Right-hand-side of an 'Equation'
data Rhs
  -- 1. Span         -  Equals or RightArrow
  -- 2. (Maybe Span) -  KW_where
  = SimpleRhs  Span Expression (Maybe Span) [Decl]
  -- 1. Span         -  First Bar
  -- 2. [Span]       -  more Bars
  -- 3. (Maybe Span) -  KW_where
  | GuardedRhs Span [CondExpr] [Span] (Maybe Span) [Decl]
 deriving Show

-- |Conditional expression (expression conditioned by a guard)
data CondExpr = CondExpr Expression Span Expression  -- 1. Span -  Equals
 deriving Show

-- |Literal
data Literal
  = Char   Span Char       -- 1. Span -  CharTok
  | Int    Span Int        -- 1. Span -  IntTok
  | Float  Span Float      -- 1. Span -  FloatTok
  | String Span String     -- 1. Span -  StringTok
 deriving Show

-- |Constructor term (used for patterns)
data Pattern
  = LiteralPattern     Literal
  -- 1. Span -  Minus
  | NegativePattern    Span Literal
  | VariablePattern    Ident
  | ConstructorPattern QualIdent [Pattern]
  | InfixPattern       Pattern QualIdent Pattern
  -- 1. Span   -  LeftPattern
  -- 2. Span   -  RightPattern
  | ParenPattern       Span Pattern Span
  -- 1. Span   -  LeftBrace
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightBrace
  | RecordPattern      QualIdent Span [Field Pattern] [Span] Span
  -- 1. Span   -  LeftParen
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightParen
  | TuplePattern       Span [Pattern] [Span] Span
  -- 1. Span   -  LeftBracket
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightBracket
  | ListPattern        Span [Pattern] [Span] Span
  -- 1. Span   -  At
  | AsPattern          Ident Span Pattern
  -- 1. Span   -  Tilde
  | LazyPattern        Span Pattern
  | FunctionPattern    QualIdent [Pattern]
  | InfixFuncPattern   Pattern QualIdent Pattern
 deriving Show

-- |Expression
data Expression
  = Literal           Literal
  | Variable          SymQualIdent
  | Constructor       SymQualIdent
  -- 1. Span   -  LeftParen
  -- 2. Span   -  RightParen
  | Paren             Span Expression Span
  -- 1. Span   -  DotDot
  | Typed             Expression Span QualTypeExpr
  -- 1. Span   -  LeftBrace
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightBrace
  | Record            QualIdent Span [Field Expression] [Span] Span
  -- 1. Span   -  LeftBrace
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightBrace
  | RecordUpdate      Expression Span [Field Expression] [Span] Span
  -- 1. Span   -  LeftParen
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightParen
  | Tuple             Span [Expression] [Span] Span
  -- 1. Span   -  LeftBracket
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightBracket
  | List              Span [Expression] [Span] Span
  -- 1. Span   -  LeftBracket
  -- 2. Span   -  Bar
  -- 3. [Span] -  Commas
  -- 4. Span   -  RightBracket
  | ListCompr         Span Expression Span [Statement] [Span] Span
  -- 1. Span   -  LeftBracket
  -- 2. Span   -  DotDot
  -- 3. Span   -  RightBracket
  | EnumFrom          Span Expression Span Span
  -- 1. Span   -  LeftBracket
  -- 2. Span   -  Comma
  -- 3. Span   -  DotDot
  -- 4. Span   -  RightBracket
  | EnumFromThen      Span Expression Span Expression Span Span
  -- 1. Span   -  LeftBracket
  -- 2. Span   -  DotDot
  -- 3. Span   -  RightBracket
  | EnumFromTo        Span Expression Span Expression Span
  -- 1. Span   -  LeftBracket
  -- 2. Span   -  Comma
  -- 3. Span   -  DotDot
  -- 4. Span   -  RightBracket
  | EnumFromThenTo    Span Expression Span Expression Span Expression Span
  -- 1. Span -  Minus
  | UnaryMinus        Span Expression
  | Apply             Expression Expression
  | InfixApply        Expression InfixOp Expression
  -- 1. Span   - LeftParen
  -- 2. Span   - RightParen
  | LeftSection       Span Expression InfixOp Span
  -- 1. Span   - LeftParen
  -- 2. Span   - RightParen
  | RightSection      Span InfixOp Expression Span
  -- 1. Span   - Backslash
  -- 2. Span   - RightArrow
  | Lambda            Span [Pattern] Span Expression
  -- 1. Span   - KW_let
  -- 2. Span   - KW_in
  | Let               Span [Decl] Span Expression
  -- 1. Span   - KW_do
  | Do                Span [Statement] Expression
  -- 1. Span   - KW_if
  -- 2. Span   - KW_then
  -- 3. Span   - KW_else
  | IfThenElse        Span Expression Span Expression Span Expression
  -- 1. Span   - KW_case
  -- 2. Span   - KW_of
  | Case              CaseType Span Expression Span [Alt]
 deriving Show

-- |Infix operation
data InfixOp
  = InfixOp     SymQualIdent
  | InfixConstr SymQualIdent
 deriving Show

-- |Statement
data Statement
  = StmtExpr Expression
  | StmtDecl Span [Decl]                -- 1. Span -  KW_let
  | StmtBind Span Pattern Expression    -- 1. Span -  LeftArrow
 deriving Show

-- |Type of case expressions
data CaseType
  = Rigid
  | Flex
 deriving Show

-- |Single case alternative
data Alt = Alt Pattern Rhs
 deriving Show

-- |Record field
data Field a = Field QualIdent Span a    -- 1. Span -  Equals
 deriving Show