-
Notifications
You must be signed in to change notification settings - Fork 11
/
Statement.scala
434 lines (377 loc) · 13.8 KB
/
Statement.scala
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
425
426
427
428
429
430
431
432
433
434
package org.bykn.bosatsu
import Parser.{Combinators, Indy, maybeSpace, keySpace, toEOL}
import cats.data.NonEmptyList
import cats.implicits._
import cats.parse.{Parser0 => P0, Parser => P}
import org.typelevel.paiges.{Doc, Document}
import scala.collection.immutable.SortedSet
import Indy.IndyMethods
import Identifier.{Bindable, Constructor}
sealed abstract class Statement {
/** This describes the region of the current statement, not the entire linked
* list of statements
*/
def region: Region
def replaceRegions(r: Region): Statement = {
import Statement._
this match {
case Bind(BindingStatement(p, v, in)) =>
Bind(BindingStatement(p, v.replaceRegionsNB(r), in))(r)
case Comment(c) =>
Comment(c)(r)
case Def(d) =>
Def(d.copy(result = d.result.map(_.replaceRegions(r))))(r)
case PaddingStatement(p) =>
// this will just be some number of lines
PaddingStatement(p)(r)
case Struct(nm, typeArgs, args) =>
Struct(nm, typeArgs, args)(r)
case Enum(nm, typeArgs, parts) =>
Enum(nm, typeArgs, parts)(r)
case ExternalDef(name, ta, args, res) =>
ExternalDef(name, ta, args, res)(r)
case ExternalStruct(nm, targs) =>
ExternalStruct(nm, targs)(r)
}
}
}
sealed abstract class TypeDefinitionStatement extends Statement {
import Statement.{Struct, Enum, ExternalStruct}
/** This is the name of the type being defined
*/
def name: Constructor
/** here are the names of the constructors for this type
*/
def constructors: List[Constructor] =
this match {
case Struct(nm, _, _) => nm :: Nil
case Enum(_, _, items) =>
items.get.toList.map { case (nm, _) => nm }
case ExternalStruct(_, _) => Nil
}
}
object Statement {
def definitionsOf(
stmts: Iterable[Statement]
): LazyList[TypeDefinitionStatement] =
stmts.iterator
.collect { case tds: TypeDefinitionStatement => tds }
.to(LazyList)
def valuesOf(stmts: Iterable[Statement]): LazyList[ValueStatement] =
stmts.iterator.collect { case vs: ValueStatement => vs }.to(LazyList)
/** These introduce new values into scope
*/
sealed abstract class ValueStatement extends Statement {
/** All the names that are bound by this statement
*/
def names: List[Bindable] =
this match {
case Bind(BindingStatement(bound, _, _)) =>
bound.names // TODO Keep identifiers
case Def(defstatement) => defstatement.name :: Nil
case ExternalDef(name, _, _, _) => name :: Nil
}
/** These are all the free bindable names in the right hand side of this
* binding
*/
def freeVars: SortedSet[Bindable] =
this match {
case Bind(BindingStatement(_, decl, _)) => decl.freeVars
case Def(defstatement) =>
val innerFrees = defstatement.result.get.freeVars
// but the def name and, args shadow
(innerFrees - defstatement.name) -- defstatement.args.toList.flatMap(
_.patternNames
)
case ExternalDef(_, _, _, _) => SortedSet.empty
}
/** These are all the bindings, free or not, in this Statement
*/
def allNames: SortedSet[Bindable] =
this match {
case Bind(BindingStatement(pat, decl, _)) => decl.allNames ++ pat.names
case Def(defstatement) =>
(defstatement.result.get.allNames + defstatement.name) ++ defstatement.args.toList
.flatMap(_.patternNames)
case ExternalDef(name, _, _, _) => SortedSet(name)
}
}
//////
// All the ValueStatements, which set up new bindings in the order they appear in the file
///// .
case class Bind(
bind: BindingStatement[Pattern.Parsed, Declaration.NonBinding, Unit]
)(val region: Region)
extends ValueStatement
case class Def(
defstatement: DefStatement[Pattern.Parsed, OptIndent[Declaration]]
)(val region: Region)
extends ValueStatement
case class ExternalDef(
name: Bindable,
typeArgs: Option[NonEmptyList[(TypeRef.TypeVar, Option[Kind])]],
params: List[(Bindable, TypeRef)],
result: TypeRef
)(val region: Region)
extends ValueStatement
//////
// TypeDefinitionStatement types:
//////
case class Enum(
name: Constructor,
typeArgs: Option[NonEmptyList[(TypeRef.TypeVar, Option[Kind.Arg])]],
items: OptIndent[
NonEmptyList[(Constructor, List[(Bindable, Option[TypeRef])])]
]
)(val region: Region)
extends TypeDefinitionStatement
case class ExternalStruct(
name: Constructor,
typeArgs: List[(TypeRef.TypeVar, Option[Kind.Arg])]
)(val region: Region)
extends TypeDefinitionStatement
case class Struct(
name: Constructor,
typeArgs: Option[NonEmptyList[(TypeRef.TypeVar, Option[Kind.Arg])]],
args: List[(Bindable, Option[TypeRef])]
)(val region: Region)
extends TypeDefinitionStatement
////
// These have no effect on the semantics of the Statement linked list
////
case class PaddingStatement(padding: Padding[Unit])(val region: Region)
extends Statement
case class Comment(comment: CommentStatement[Unit])(val region: Region)
extends Statement
// Parse a single item
final val parser1: P[Statement] = {
import Declaration.NonBinding
val bindingLike: Indy[(Pattern.Parsed, OptIndent[NonBinding])] = {
val pat = Pattern.bindParser
val patPart = pat <* (maybeSpace *> Declaration.eqP *> maybeSpace)
// allow = to be like a block, we can continue on the next line indented
OptIndent.blockLike(
Indy.lift(patPart),
Declaration.nonBindingParser,
P.unit
)
}
val bindingP: P[Statement] =
(bindingLike("") <* toEOL).region
.map { case (region, (pat, value)) =>
Bind(BindingStatement(pat, value.get, ()))(region)
}
val paddingSP: P[Statement] =
Padding.nonEmptyParser.region
.map { case (region, p) => PaddingStatement(p)(region) }
val commentP: P[Statement] =
CommentStatement
.parser(_ => P.unit)
.region
.map { case (region, cs) => Comment(cs)(region) }
.run("")
val defBody = maybeSpace.with1 *> OptIndent.indy(Declaration.parser).run("")
val defP: P[Statement] =
DefStatement
.parser(Pattern.bindParser, defBody <* toEOL)
.region
.map { case (region, DefStatement(nm, ta, args, ret, body)) =>
Def(DefStatement(nm, ta, args, ret, body))(region)
}
val argParser: P[(Bindable, Option[TypeRef])] =
Identifier.bindableParser ~ TypeRef.annotationParser.?
val structKey = keySpace("struct")
val typeParams: P[NonEmptyList[(TypeRef.TypeVar, Option[Kind.Arg])]] = {
val kindAnnot: P[Kind.Arg] =
(maybeSpace.soft.with1 *> (P.char(
':'
) *> maybeSpace *> Kind.paramKindParser))
TypeRef.typeParams(kindAnnot.?)
}
val external = {
val externalStruct =
(structKey *> (Identifier.consParser ~ Parser.nonEmptyListToList(
typeParams
)).region <* toEOL)
.map { case (region, (name, tva)) =>
ExternalStruct(name, tva)(region)
}
val argParser: P[(Bindable, TypeRef)] =
Identifier.bindableParser ~ TypeRef.annotationParser
val externalDef = {
val kindAnnot: P[Kind] =
(maybeSpace.soft.with1 *> (P.char(':') *> maybeSpace *> Kind.parser))
val typeParams = TypeRef.typeParams(kindAnnot.?).?
val args =
P.char('(') *> maybeSpace *> argParser.nonEmptyList <* maybeSpace <* P
.char(')')
val result =
maybeSpace.with1 *> P.string("->") *> maybeSpace *> TypeRef.parser
(((keySpace(
"def"
) *> Identifier.bindableParser ~ typeParams ~ args ~ result).region) <* toEOL)
.map { case (region, (((name, tps), args), resType)) =>
ExternalDef(name, tps, args.toList, resType)(region)
}
}
val externalVal =
(argParser <* toEOL).region
.map { case (region, (name, resType)) =>
ExternalDef(name, None, Nil, resType)(region)
}
keySpace("external") *> P.oneOf(
externalStruct :: externalDef :: externalVal :: Nil
)
}
val struct =
((structKey *> Identifier.consParser ~ typeParams.? ~ Parser
.nonEmptyListToList(argParser.parensLines1Cut)).region <* toEOL)
.map { case (region, ((name, typeArgs), argsList)) =>
Struct(name, typeArgs, argsList)(region)
}
val enumP = {
val constructorP =
(Identifier.consParser ~ argParser.parensLines1Cut.?)
.map {
case (n, None) => (n, Nil)
case (n, Some(args)) => (n, args.toList)
}
val sep = (Indy
.lift(P.char(',') <* maybeSpace))
.combineK(Indy.toEOLIndent)
.void
val variants = Indy.lift(constructorP <* maybeSpace).nonEmptyList(sep)
val nameVars =
OptIndent
.block(
Indy.lift(
keySpace("enum") *> Identifier.consParser ~ (typeParams.?)
),
variants
)
.run("")
.region
(nameVars <* toEOL)
.map { case (region, ((ename, typeArgs), vars)) =>
Enum(ename, typeArgs, vars)(region)
}
}
// bindingP should come last so there is no ambiguity about identifiers
P.oneOf(
commentP :: paddingSP :: defP :: struct :: enumP :: external :: bindingP :: Nil
)
}
/** This parses the *rest* of the string (it must end with End)
*/
val parser: P0[List[Statement]] =
parser1.rep0 <* Parser.maybeSpacesAndLines <* P.end
private def constructor(
name: Constructor,
taDoc: Doc,
args: List[(Bindable, Option[TypeRef])]
): Doc =
Document[Identifier].document(name) + taDoc +
(if (args.nonEmpty) {
Doc.char('(') + Doc.intercalate(
Doc.text(", "),
args.toList.map(TypeRef.argDoc[Bindable] _)
) + Doc.char(')')
} else Doc.empty)
private val colonSpace = Doc.text(": ")
implicit private val dunit: Document[Unit] =
Document.instance[Unit](_ => Doc.empty)
private val optKindArgs: Document[Option[Kind.Arg]] =
Document {
case None => Doc.empty
case Some(ka) => colonSpace + Kind.argDoc(ka)
}
implicit lazy val document: Document[Statement] = {
val db =
Document[BindingStatement[Pattern.Parsed, Declaration.NonBinding, Unit]]
val dc = Document[CommentStatement[Unit]]
implicit val pair: Document[OptIndent[Declaration]] =
Document.instance[OptIndent[Declaration]] { body =>
body.sepDoc +
OptIndent.document(Declaration.document).document(body)
}
val dd = DefStatement.document[Pattern.Parsed, OptIndent[Declaration]]
implicit val consDoc =
Document.instance[(Constructor, List[(Bindable, Option[TypeRef])])] {
case (nm, parts) => constructor(nm, Doc.empty, parts)
}
Document.instance[Statement] {
case Bind(bs) =>
db.document(bs) + Doc.line
case Comment(cm) =>
// Comments already end with newline
dc.document(cm)
case Def(d) =>
dd.document(d) + Doc.line
case PaddingStatement(p) =>
// this will just be some number of lines
Padding.document[Unit].document(p)
case Struct(nm, typeArgs, args) =>
val taDoc = typeArgs match {
case None => Doc.empty
case Some(ta) => TypeRef.docTypeArgs(ta.toList)(optKindArgs.document)
}
Doc.text("struct ") + constructor(nm, taDoc, args) + Doc.line
case Enum(nm, typeArgs, parts) =>
val (colonSep, itemSep) = parts match {
case OptIndent.SameLine(_) => (Doc.space, Doc.text(", "))
case OptIndent.NotSameLine(_) => (Doc.empty, Doc.line)
}
implicit def neDoc[T: Document]: Document[NonEmptyList[T]] =
Document.instance { ne =>
Doc.intercalate(itemSep, ne.toList.map(Document[T].document _))
}
val indentedCons = OptIndent.document(neDoc(consDoc)).document(parts)
val taDoc = typeArgs match {
case None => Doc.empty
case Some(ta) => TypeRef.docTypeArgs(ta.toList)(optKindArgs.document)
}
Doc.text("enum ") + Document[Constructor].document(nm) + taDoc + Doc
.char(':') +
colonSep +
indentedCons + Doc.line
case ExternalDef(name, None, Nil, res) =>
Doc.text("external ") + Document[Bindable].document(name) + Doc.text(
": "
) + res.toDoc + Doc.line
case ExternalDef(name, tps, args, res) =>
val taDoc = tps match {
case None => Doc.empty
case Some(ta) =>
TypeRef.docTypeArgs(ta.toList) {
case None => Doc.empty
case Some(k) => colonSpace + Kind.toDoc(k)
}
}
val argDoc = {
val da = Doc.intercalate(
Doc.text(", "),
args.map { case (n, tr) =>
Document[Bindable].document(n) + Doc.text(": ") + tr.toDoc
}
)
Doc.char('(') + da + Doc.char(')')
}
Doc.text("external def ") + Document[Bindable].document(
name
) + taDoc + argDoc + Doc.text(" -> ") + res.toDoc + Doc.line
case ExternalStruct(nm, typeArgs) =>
val taDoc =
TypeRef.docTypeArgs(typeArgs.toList) {
case None => Doc.empty
case Some(ka) => Doc.text(": ") + Kind.argDoc(ka)
}
Doc.text("external struct ") + Document[Constructor].document(
nm
) + taDoc + Doc.line
}
}
implicit lazy val documentList: Document[List[Statement]] =
Document.instance[List[Statement]] { stmts =>
Doc.intercalate(Doc.empty, stmts.toList.map(document.document(_)))
}
}