-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.go
517 lines (488 loc) · 14.4 KB
/
collections.go
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*******************************************************************************
* Copyright (c) 2009-2023 Crater Dog Technologies™. All Rights Reserved. *
*******************************************************************************
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. *
* *
* This code is free software; you can redistribute it and/or modify it under *
* the terms of The MIT License (MIT), as published by the Open Source *
* Initiative. (See http://opensource.org/licenses/MIT) *
*******************************************************************************/
package bali
import (
fmt "fmt"
abs "github.com/bali-nebula/go-component-framework/abstractions"
col "github.com/bali-nebula/go-component-framework/collections"
cox "github.com/craterdog/go-collection-framework/v2"
)
// TYPE DEFINITIONS
type (
Components []any
Associations [][2]any
)
// UNIVERSAL CONSTRUCTORS
// This function parses a catalog from a source string.
func Catalog(source string) abs.CatalogLike {
return ParseEntity(source).(abs.CatalogLike)
}
// This constructor returns a new catalog component initialized with the
// specified associations and parameterized with the specified context.
func CatalogWithContext(associations Associations, context abs.ContextLike) abs.ComponentLike {
var catalog = col.Catalog()
for _, association := range associations {
var key = Component(association[0]).GetEntity()
var value = Component(association[1])
catalog.SetValue(key, value)
}
return ComponentWithContext(catalog, context)
}
// This function parses a catalog from a source string.
func List(source string) abs.ListLike {
return ParseEntity(source).(abs.ListLike)
}
// This constructor returns a new list component initialized with the
// specified associations and parameterized with the specified context.
func ListWithContext(components Components, context abs.ContextLike) abs.ComponentLike {
var component abs.ComponentLike
var list = col.List()
for _, value := range components {
component = Component(value)
list.AddValue(component)
}
return ComponentWithContext(list, context)
}
// This function parses a catalog from a source string.
func Queue(source string) abs.QueueLike {
return ParseEntity(source).(abs.QueueLike)
}
// This constructor returns a new queue component with the specified capacity
// and parameterized with the specified context.
func QueueWithContext(capacity int, context abs.ContextLike) abs.ComponentLike {
var queue = col.QueueWithCapacity(capacity)
return ComponentWithContext(queue, context)
}
// This function parses a catalog from a source string.
func Set(source string) abs.SetLike {
return ParseEntity(source).(abs.SetLike)
}
// This constructor returns a new set component initialized with the
// specified associations and parameterized with the specified context.
func SetWithContext(components Components, context abs.ContextLike) abs.ComponentLike {
var component abs.ComponentLike
var set = col.Set()
for _, value := range components {
component = Component(value)
set.AddValue(component)
}
return ComponentWithContext(set, context)
}
// This function parses a catalog from a source string.
func Stack(source string) abs.StackLike {
return ParseEntity(source).(abs.StackLike)
}
// This constructor returns a new stack component with the specified capacity
// and parameterized with the specified context.
func StackWithContext(capacity int, context abs.ContextLike) abs.ComponentLike {
var stack = col.StackWithCapacity(capacity)
return ComponentWithContext(stack, context)
}
// PRIVATE METHODS
// This method attempts to parse an association between a key and value. It
// returns the association and whether or not the association was successfully
// parsed.
func (v *parser) parseAssociation() (abs.AssociationLike, *Token, bool) {
var ok bool
var token *Token
var key abs.Primitive
var value abs.ComponentLike
key, token, ok = v.parsePrimitive()
if !ok {
// This is not an association.
return nil, token, false
}
_, token, ok = v.parseDelimiter(":")
if !ok {
// This is not an association.
v.backupOne() // Put back the primitive key token.
return nil, token, false
}
// This must be an association.
value, token, ok = v.parseComponent()
if !ok {
var message = v.formatError(token)
message += generateGrammar("$component",
"$association",
"$key",
"$value")
panic(message)
}
var association = col.Association(key, value)
return association, token, true
}
// This method adds the canonical format for the specified association to the
// state of the formatter.
func (v *formatter) formatAssociation(association abs.AssociationLike) {
var key = association.GetKey()
v.formatPrimitive(key)
v.AppendString(": ")
var value = association.GetValue()
v.formatComponent(value)
}
// This method attempts to parse a collection of components. It returns the
// collection and whether or not the collection was successfully parsed.
func (v *parser) parseCollection() (abs.Collection, *Token, bool) {
var ok bool
var token *Token
var collection abs.Collection
collection, token, ok = v.parseMapping()
if !ok {
// The series must be attempted last since it may start with a component
// which cannot be put back as a single token.
collection, token, ok = v.parseSeries()
}
return collection, token, ok
}
// It returns the mapping collection and whether or not the mapping collection
// was successfully parsed.
func (v *parser) parseInlineMapping() (abs.MappingLike, *Token, bool) {
var ok bool
var token *Token
var association abs.AssociationLike
var mapping = cox.List[abs.AssociationLike]()
_, token, ok = v.parseDelimiter(":")
if ok {
// This is an empty mapping.
return mapping, token, true
}
_, token, ok = v.parseDelimiter("]")
if ok {
// This is an empty series.
v.backupOne() // Put back the ']' character.
return mapping, token, false
}
association, token, ok = v.parseAssociation()
if !ok {
// A non-empty mapping must have at least one association.
return mapping, token, false
}
for {
mapping.AddValue(association)
// Every subsequent association must be preceded by a ','.
_, token, ok = v.parseDelimiter(",")
if !ok {
// No more associations.
break
}
association, token, ok = v.parseAssociation()
if !ok {
var message = v.formatError(token)
message += generateGrammar("$association",
"$mapping",
"$associations",
"$association",
"$key",
"$value")
panic(message)
}
}
return mapping, token, true
}
// This method attempts to parse a series collection with inline components. It
// returns the series collection and whether or not the series collection was
// successfully parsed.
func (v *parser) parseInlineSeries() (abs.SeriesLike, *Token, bool) {
var ok bool
var token *Token
var value abs.ComponentLike
var series = col.List()
_, token, ok = v.parseDelimiter("]")
if ok {
// This is an empty series.
v.backupOne() // Put back the ']' token.
return series, token, true
}
value, token, ok = v.parseComponent()
if !ok {
// A non-empty series must have at least one component value.
var message = v.formatError(token)
message += generateGrammar("$component",
"$series",
"$components",
"$component")
panic(message)
}
for {
series.AddValue(value)
// Every subsequent value must be preceded by a ','.
_, token, ok = v.parseDelimiter(",")
if !ok {
// No more components.
break
}
value, token, ok = v.parseComponent()
if !ok {
var message = v.formatError(token)
message += generateGrammar("$component",
"$series",
"$components",
"$component")
panic(message)
}
}
return series, token, true
}
// This method attempts to parse a mapping collection. It returns the
// mapping collection and whether or not the mapping collection was
// successfully parsed.
func (v *parser) parseMapping() (abs.MappingLike, *Token, bool) {
var ok bool
var token *Token
var mapping abs.MappingLike
_, token, ok = v.parseDelimiter("[")
if !ok {
return mapping, token, false
}
_, token, ok = v.parseEOL()
if !ok {
mapping, token, ok = v.parseInlineMapping()
if !ok {
v.backupOne() // Put back the '[' character.
return mapping, token, false
}
} else {
mapping, token, ok = v.parseMultilineMapping()
if !ok {
v.backupOne() // Put back the EOL character.
v.backupOne() // Put back the '[' character.
return mapping, token, false
}
}
_, token, ok = v.parseDelimiter("]")
if !ok {
var message = v.formatError(token)
message += generateGrammar("]",
"$mapping",
"$associations",
"$association",
"$key",
"$value")
panic(message)
}
return mapping, token, true
}
// This method adds the canonical format for the specified collection to the
// state of the formatter.
func (v *formatter) formatMapping(mapping abs.MappingLike) {
v.AppendString("[")
var iterator = col.AssociationIterator(mapping)
switch mapping.GetSize() {
case 0:
v.AppendString(":")
case 1:
var association = iterator.GetNext()
v.formatAssociation(association)
default:
v.depth++
for iterator.HasNext() {
v.AppendNewline()
var association = iterator.GetNext()
v.formatAssociation(association)
}
v.depth--
v.AppendNewline()
}
v.AppendString("]")
}
// This method attempts to parse a mapping collection with multiline associations.
// It returns the mapping collection and whether or not the mapping collection
// was successfully parsed.
func (v *parser) parseMultilineMapping() (abs.MappingLike, *Token, bool) {
var ok bool
var token *Token
var association abs.AssociationLike
var associations = cox.List[abs.AssociationLike]()
association, token, ok = v.parseAssociation()
if !ok {
// A non-empty mapping must have at least one association.
return associations, token, false
}
for {
associations.AddValue(association)
// Every association must be followed by an EOL.
_, token, ok = v.parseEOL()
if !ok {
var message = v.formatError(token)
message += generateGrammar("EOL",
"$mapping",
"$associations",
"$association",
"$key",
"$value")
panic(message)
}
association, token, ok = v.parseAssociation()
if !ok {
// No more associations.
break
}
}
return associations, token, true
}
// This method attempts to parse a series collection with multiline components.
// It returns the series collection and whether or not the series collection was
// successfully parsed.
func (v *parser) parseMultilineSeries() (abs.SeriesLike, *Token, bool) {
var ok bool
var token *Token
var value abs.ComponentLike
var series = col.List()
value, token, ok = v.parseComponent()
if !ok {
// A non-empty series must have at least one value.
var message = v.formatError(token)
message += generateGrammar("$component",
"$series",
"$components",
"$component")
panic(message)
}
for {
series.AddValue(value)
// Every value must be followed by an EOL.
_, token, ok = v.parseEOL()
if !ok {
var message = v.formatError(token)
message += generateGrammar("EOL",
"$series",
"$components",
"$component")
panic(message)
}
value, token, ok = v.parseComponent()
if !ok {
// No more components.
break
}
}
return series, token, true
}
// This method attempts to parse a primitive. It returns the primitive and
// whether or not the primitive was successfully parsed.
func (v *parser) parsePrimitive() (abs.Primitive, *Token, bool) {
var ok bool
var token *Token
var primitive abs.Primitive
primitive, token, ok = v.parseElement()
if !ok {
primitive, token, ok = v.parseString()
}
if !ok {
// This must be explicitly set to nil since it is of type any.
primitive = nil
}
return primitive, token, ok
}
// This method adds the canonical format for the specified primitive to the
// state of the formatter.
func (v *formatter) formatPrimitive(primitive abs.Primitive) {
switch value := primitive.(type) {
// The order of these cases is very important since Go only compares the
// set of methods supported by each interface. An interface that is a subset
// of another interface must be checked AFTER that interface.
case abs.BinaryLike:
v.formatBinary(value)
case abs.BytecodeLike:
v.formatBytecode(value)
case abs.MonikerLike:
v.formatMoniker(value)
case abs.NarrativeLike:
v.formatNarrative(value)
case abs.QuoteLike:
v.formatQuote(value)
case abs.VersionLike:
v.formatVersion(value)
case abs.DurationLike:
v.formatDuration(value)
case abs.MomentLike:
v.formatMoment(value)
case abs.NumberLike:
v.formatNumber(value)
case abs.PercentageLike:
v.formatPercentage(value)
case abs.ProbabilityLike:
v.formatProbability(value)
case abs.AngleLike:
v.formatAngle(value)
case abs.BooleanLike:
v.formatBoolean(value)
case abs.PatternLike:
v.formatPattern(value)
case abs.ResourceLike:
v.formatResource(value)
case abs.TagLike:
v.formatTag(value)
case abs.SymbolLike:
v.formatSymbol(value)
default:
panic(fmt.Sprintf("An invalid primitive (of type %T) was passed to the formatter: %v", value, value))
}
}
// This method attempts to parse a series of components. It returns the
// series of components and whether or not the series of components was
// successfully parsed.
func (v *parser) parseSeries() (abs.SeriesLike, *Token, bool) {
var ok bool
var token *Token
var series abs.SeriesLike
_, token, ok = v.parseDelimiter("[")
if !ok {
return series, token, false
}
_, token, ok = v.parseEOL()
if !ok {
series, token, ok = v.parseInlineSeries()
if !ok {
v.backupOne() // Put back the '[' character.
return series, token, false
}
} else {
series, token, ok = v.parseMultilineSeries()
if !ok {
v.backupOne() // Put back the EOL character.
v.backupOne() // Put back the '[' character.
return series, token, false
}
}
_, token, ok = v.parseDelimiter("]")
if !ok {
var message = v.formatError(token)
message += generateGrammar("]",
"$series",
"$components",
"$component")
panic(message)
}
return series, token, ok
}
// This method adds the canonical format for the specified collection to the
// state of the formatter.
func (v *formatter) formatSeries(series abs.SeriesLike) {
v.AppendString("[")
var iterator = col.ComponentIterator(series)
switch series.GetSize() {
case 0:
v.AppendString(" ")
case 1:
var value = iterator.GetNext()
v.formatComponent(value)
default:
v.depth++
for iterator.HasNext() {
v.AppendNewline()
var value = iterator.GetNext()
v.formatComponent(value)
}
v.depth--
v.AppendNewline()
}
v.AppendString("]")
}