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
{- |
    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]

-- |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

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

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

-- |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

-- |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)

-- |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

-- |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

-- ---------------------------------------------------------------------------
-- 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
  | DataDecl     Span Ident [Ident] (Maybe Span) [ConstrDecl] [Span]
  -- 1. Span   -  KW_newtype
  -- 2. Span   -  Equals
  | NewtypeDecl  Span Ident [Ident] Span NewConstrDecl
  -- 1. Span   -  KW_type
  -- 2. Span   -  Equals
  | TypeDecl     Span Ident [Ident] Span TypeExpr
  -- 1. [Span] - Commas
  -- 2. Span   - DotDot
  | TypeSig      [SymIdent] [Span] Span TypeExpr
  | 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

-- |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

-- |Constructor declaration for algebraic data types
data ConstrDecl
  = ConstrDecl [Ident] Ident [TypeExpr]
  | ConOpDecl  [Ident] TypeExpr Ident TypeExpr
  -- 1. Span   -  LeftBrace
  -- 2. [Span] -  Commas
  -- 3. Span   -  RightBrace
  | RecordDecl [Ident] Ident Span [FieldDecl] [Span] Span

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

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

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

-- |Type expressions
data TypeExpr
  -- 1. (Maybe Span) - LeftParen
  -- 2. (Maybe Span) - RightParen
  = ConstructorType (Maybe Span) QualIdent [TypeExpr] (Maybe Span)
  | 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

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

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

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

-- |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]

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

-- |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

-- |Constructor term (used for patterns)
data Pattern
  = LiteralPattern     Literal
  | NegativePattern    Ident 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

-- |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 TypeExpr
  -- 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
  | UnaryMinus        Ident 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]

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

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

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

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

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