-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathipld-schema.pegjs
More file actions
532 lines (455 loc) · 17.3 KB
/
ipld-schema.pegjs
File metadata and controls
532 lines (455 loc) · 17.3 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/** IPLD Schema PEG.js grammar **/
// Utility functions
{
function defaultStructRepresentation () {
return { map: {} }
}
function extend (o1, o2) {
// we only use a 2-argument form and this also lets us supply `extend` as an argument
// to Array#reduce and not worry about the additional reducer arguments
return Object.assign(o1, o2)
}
// some values need coercion into proper forms, `default` being one of them
function coerceValue (value) {
if (value === 'true') {
return true
}
if (value === 'false') {
return false
}
// this isn't needed yet, just trying the concept
if (parseInt(value, 10) == value) {
return parseInt(value, 10)
}
return value
}
function flattenArray (a) {
if (!Array.isArray(a)) {
return a
}
return a.reduce((p, c) => p.concat(flattenArray(c)), [])
}
function processComments (precomments, linecomment) {
let pcl = precomments.split('\n')
// trim trailing empty lines
while (pcl.length && !pcl[pcl.length - 1].trim()) {
pcl.pop()
}
// trim leading empty lines
while (pcl.length && !pcl[0].trim()) {
pcl.shift()
}
// Check if there's a blank line followed by comments
// If so, those comments should be linecomments for the previous field
let firstCommentAfterBlank = -1
let hasBlankLine = false
for (let i = 0; i < pcl.length; i++) {
if (/^\s*$/.test(pcl[i])) {
hasBlankLine = true
} else if (hasBlankLine && /^\s*#/.test(pcl[i])) {
firstCommentAfterBlank = i
break
}
}
if (firstCommentAfterBlank !== -1 && !linecomment) {
// Move comments after blank line to linecomment
const commentLines = pcl.slice(firstCommentAfterBlank)
linecomment = commentLines.join('\n')
pcl = pcl.slice(0, firstCommentAfterBlank)
// Remove any trailing empty lines from pcl
while (pcl.length && !pcl[pcl.length - 1].trim()) {
pcl.pop()
}
} else {
// Original behavior: only keep comments after the last empty line
let lastempty = pcl.findLastIndex((l) => /^\s*$/.test(l))
if (lastempty !== -1) {
pcl = pcl.slice(lastempty + 1)
}
}
// trim leading space and # on each line
pcl = pcl.map((l) => l.replace(/^[ \t]*#[ \t]?/gm, ''))
if (linecomment && typeof linecomment !== 'string') {
linecomment = flattenArray(linecomment).join('')
}
linecomment = linecomment ? linecomment.replace(/^[ \t]*#[ \t]?/gm, '') : null
const comments = (pcl.length || linecomment) ? {} : null
if (pcl.length) {
comments.precomments = pcl.join('\n')
}
if (linecomment) {
comments.linecomment = linecomment
}
return comments
}
}
Root = roots:RootConstructs+ {
// merge 'type' and 'advanced' structures into one {types:{}, advanced:{}}
return roots.reduce((o1, o2) => {
if (o2.types) {
if (!o1.types) {
o1.types = {}
}
Object.assign(o1.types, o2.types)
} else if (o2.advanced) {
if (!o1.advanced) {
o1.advanced = {}
}
Object.assign(o1.advanced, o2.advanced)
}
return o1
}, {})
}
RootConstructs
= types:TypeDef { return { types } }
/ advanced:AdvancedDef { return { advanced } }
TypeDef =
ws*
precomments:capturedcomment
annotations:Annotation*
'type'
ws+
name:TypeName
ws+
definition:Definition
ws*
newline* {
if (Object.keys(definition).length !== 1) {
throw new Error('Unexpected definition for type: ' + JSON.stringify(definition))
}
const typ = Object.keys(definition)[0]
const comments = processComments(precomments)
if (options.includeComments && comments) {
definition[typ].comments = extend(definition[typ].comments || {}, { type: comments })
}
if (options.includeComments && annotations && annotations.length) {
definition[typ].annotations = extend(definition[typ].annotations || {}, { type: annotations })
}
return { [name]: definition }
}
AdvancedDef = _ 'advanced' _ name:TypeName _ {
return { [name]: { advanced: {} } }
}
AdvancedRepresentation = name:TypeName {
return { advanced: name }
}
Definition
= descriptor:MapDescriptor { return descriptor } // "map" assumed if goes straight to a {}
/ descriptor:ListDescriptor { return descriptor } // "list" assumed if goes straight to a []
/ descriptor:LinkDescriptor { return descriptor } // "link" assumed if goes straight to a &
/ descriptor:CopyDescriptor { return descriptor } // "="
/ EnumKind wsnl descriptor:EnumDescriptor { return descriptor }
/ UnionKind wsnl descriptor:UnionDescriptor { return descriptor }
/ StructKind wsnl descriptor:StructDescriptor { return descriptor }
/ BytesKind wsnl descriptor:BytesDescriptor { return descriptor }
/ kind:SimpleKind { return { [kind]: {} } }
MapKind = "map"
ListKind = "list"
EnumKind = "enum"
UnionKind = "union"
StructKind = "struct"
BytesKind = "bytes"
SimpleKind = kind:BaseType { return kind }
ListDescriptor = "[" _ fields:TypeDescriptor _ "]" wsnl representation:ListRepresentation? {
return { list: Object.assign({}, fields, representation ? { representation } : null) }
}
// TODO: generalise this TypeName / MapDescriptor / ListDescriptor / LinkDescriptor combo, it's used elsewhere
TypeDescriptor = options:TypeOption* _ valueType:(TypeName / MapDescriptor / ListDescriptor / LinkDescriptor) {
return options.reduce(extend, { valueType })
}
LinkDescriptor = "&" expectedType:TypeName {
return { link: { expectedType } }
}
CopyDescriptor = "=" wsnl fromType:TypeName {
return { copy: { fromType } }
}
EnumDescriptor = "{" members:EnumMember+ "}" wsnl representation:EnumRepresentation? wsnl {
if (!representation || !(representation.string || representation.int)) {
representation = { string: {} }
}
const repr = members.filter((m) => Object.values(m)[0]).reduce(extend, {})
members = Object.keys(members.reduce(extend, {}))
if (representation.string) {
representation.string = repr
} else if (representation.int) {
representation.int = repr
Object.entries(repr).forEach(([k, v]) => {
const i = parseInt(v, 10)
if (i != v) {
throw new Error('int representations only support integer representation values')
}
repr[k] = i
})
}
return { enum: { members, representation } }
}
EnumMember = wsnl "|" wsnl name:EnumValue _ representationOptions:EnumFieldRepresentationOptions? wsnl {
return { [name]: representationOptions }
}
EnumFieldRepresentationOptions = "(" ws* value:QuotedString ws* ")" { return value }
UnionDescriptor = "{" values:UnionValue+ "}" wsnl representation:UnionRepresentation wsnl {
let fields = values.reduce(extend, {})
if (representation.keyed) {
representation.keyed = fields
} else if (representation.kinded) {
representation.kinded = fields
} else if (representation.stringprefix) {
representation.stringprefix = { prefixes: fields }
} else if (representation.bytesprefix) {
representation.bytesprefix = { prefixes: fields }
} else if (representation.inline) {
representation.inline.discriminantTable = fields
} else if (representation.envelope) {
representation.envelope.discriminantTable = fields
} else {
throw new Error('Unsupported union type') // we shouldn't get here if we're coded right
}
return { union: { members: Object.values(fields), representation } }
}
// TODO: tighten these up, kinded doesn't get quoted kinds, keyed and envelope does, this allows a kinded
// union to pass through with quoted strings, it's currently just a messy duplication
UnionValue = wsnl "|" ws* type:(TypeName / LinkDescriptor) ws* name:(QuotedString / BaseType) _ {
return { [name]: type }
}
MapDescriptor = "{" _ keyType:TypeName _ ":" _ valueType:TypeDescriptor _ "}" wsnl representation:MapRepresentation? {
let representationType = (representation && representation.type)
if (representationType) {
representation = { [representationType]: representation || {} }
delete representation[representationType].type
}
return { map: Object.assign({ keyType }, valueType, representation ? { representation } : null) }
}
StructDescriptor = "{" values:StructValues "}" ws* representation:StructRepresentation? {
let fields = values.reduce(extend, {})
// Field representation options can come in from parens following field definitions,
// annotations comments prior to field definitions, and any additional precomments or line
// comments need to be captured from around the field. These all need to be lifted out of the
// entry and packaged separately.
const [representationFields, annotationsFields, commentsFields] = Object.entries(fields).reduce((p, fieldEntry) => {
if (fieldEntry[1].representationOptions) {
p[0][fieldEntry[0]] = fieldEntry[1].representationOptions
delete fieldEntry[1].representationOptions
}
if (fieldEntry[1].annotations) {
p[1][fieldEntry[0]] = fieldEntry[1].annotations
delete fieldEntry[1].annotations
}
if (fieldEntry[1].comments) {
p[2][fieldEntry[0]] = fieldEntry[1].comments
delete fieldEntry[1].comments
}
return p
}, [{}, {}, {}])
let representationType = (representation && representation.type)
if (representationType) {
// restructure from { type: 'foo', bar: 'baz' } to { foo: { bar: 'baz' } }
representation = { [representationType]: representation || {} }
delete representation[representationType].type
/* auto-fill fieldOrder? if (representationType === 'tuple' && !representation.tuple.fieldOrder) {
representation.tuple.fieldOrder = Object.keys(fields)
} */
}
// handle inline field representation data
if (Object.keys(representationFields).length) {
if (!representation) {
representation = defaultStructRepresentation()
}
if (!representation.map) {
throw new Error('field modifiers only valid for struct map representation')
}
representation.map.fields = representationFields
}
return { struct:
extend(
extend(
extend({ fields }, { representation: representation || defaultStructRepresentation() }),
Object.keys(annotationsFields).length > 0 ? { annotations: { fields: annotationsFields } } : null
), Object.keys(commentsFields).length > 0 ? { comments: { fields: commentsFields} } : null
)
}
}
StructValues
= values:StructValue+ {
return values
}
/ _ { return [] }
StructValue =
ws*
precomments:capturedcomment
annotations:Annotation*
key:StringName
ws+
options:(options:StructFieldOption ws+ { return options })*
type:StructType
ws*
representationOptions:StructFieldRepresentationOptions?
ws*
linecomment:comment?
ws*
newline* {
const comments = processComments(precomments, linecomment)
return { [key]: options.reduce(extend,
extend(
extend(
extend({ type }, comments ? { comments } : null),
representationOptions ? { representationOptions } : null),
annotations.length ? { annotations } : null
)
)
}
}
StructFieldOption
= "optional" { return { optional: true } }
/ "nullable" { return { nullable: true } }
TypeOption
= "optional" { return { optional: true } }
/ "nullable" { return { valueNullable: true } }
StructType
= type:StringName { return type }
/ MapDescriptor
/ ListDescriptor
/ LinkDescriptor
StructFieldRepresentationOptions = "(" ws* options:StructFieldRepresentationOption* ws* ")" {
return options.reduce(extend, {})
}
StructFieldRepresentationOption
= "implicit" ws* implicit:ImplicitOption { return { implicit } }
/ "rename" ws* rename:QuotedString ws* { return { rename } }
ImplicitOption
= implicit:QuotedString ws* { return implicit }
/ implicit:Integer ws* { return parseInt(implicit, 10) }
/ "true" { return true }
/ "false" { return false }
// TODO: floats and bytes
UnionRepresentation = "representation" wsnl representation:UnionRepresentationType {
return representation
}
MapRepresentation = "representation" wsnl representation:MapRepresentationType {
return representation
}
ListRepresentation = "representation" wsnl representation:ListRepresentationType {
return representation
}
StructRepresentation = "representation" wsnl representation:StructRepresentationType {
return representation
}
EnumRepresentation = "representation" wsnl representation:EnumRepresentationType {
return representation
}
UnionRepresentationType
= "keyed" { return { keyed: {} } }
/ "kinded" { return { kinded: {} } }
/ "stringprefix" { return { stringprefix: {} } } // TODO: check kind reprs for union types are all strings
/ "bytesprefix" { return { bytesprefix: {} } } // TODO: check kind reprs for union types are all bytes
/ "inline" wsnl descriptor:UnionInlineKeyDefinition { return descriptor }
/ "envelope" wsnl descriptor:UnionEnvelopeKeyDefinition { return descriptor }
UnionInlineKeyDefinition = "{" wsnl ("discriminantKey" / "discriminantKey") wsnl discriminantKey:QuotedString _ "}" {
return { inline: { discriminantKey } }
}
// TODO: break these by newline || "}" (non-greedy match)
UnionEnvelopeKeyDefinition = "{" wsnl ("discriminantKey" / "discriminantKey") wsnl discriminantKey:QuotedString wsnl "contentKey" wsnl contentKey:QuotedString _ "}" {
return { envelope: { discriminantKey, contentKey } }
}
MapRepresentationType
= "map" { return { type: 'map' } }
/ "listpairs" { return { type: 'listpairs' } }
/ "stringpairs" wsnl representation:MapStringpairsRepresentation { return representation }
/ "advanced" wsnl representation:AdvancedRepresentation { return representation }
// TODO: break these by newline || "}" (non-greedy match)
MapStringpairsRepresentation = "{" wsnl options:MapStringpairsRepresentationOptions* wsnl "}" {
let representation = extend({ type: 'stringpairs' }, options.reduce(extend, {}))
if (!representation.innerDelim || !representation.entryDelim) {
throw new Error('"stringpairs" representation requires both "innerDelim" and "entryDelim" options')
}
return representation
}
MapStringpairsRepresentationOptions
= wsnl "innerDelim" wsnl innerDelim:QuotedString { return { innerDelim } }
/ wsnl "entryDelim" wsnl entryDelim:QuotedString { return { entryDelim } }
ListRepresentationType = "advanced" wsnl representation:AdvancedRepresentation { return representation }
StructRepresentationType
= "map" { return { type: 'map' } }
/ "tuple" wsnl fieldOrder:StructTupleRepresentationFields? { return extend({ type: 'tuple' }, fieldOrder ? { fieldOrder } : null) }
/ "stringjoin" wsnl fields:StructStringjoinRepresentationFields { return extend({ type: 'stringjoin' }, fields ) }
/ "stringpairs" wsnl representation:MapStringpairsRepresentation { return representation }
/ "listpairs" { return { type: 'listpairs' } }
// TODO: break these by newline || "}" (non-greedy match)
StructMapRepresentationFields = "{" _ "}"
StructMapRepresentationField = "field" ws* field:StringName ws* isImplicit:"implicit" ws* implicitValue:QuotedString _ {
return { [field]: extend({}, isImplicit ? { implicit: coerceValue(implicitValue) } : null) }
}
StructTupleRepresentationFields = "{" _ fieldOrder:StructTupleRepresentationFieldOrder? _ "}" {
return fieldOrder
}
StructTupleRepresentationFieldOrder = "fieldOrder" _ fieldOrder:QuotedStringArray {
return fieldOrder
}
// TODO: break these by newline || "}" (non-greedy match)
StructStringjoinRepresentationFields = "{" fields:StructStringjoinRepresentationField+ "}" {
fields = fields.reduce(extend, {})
if (!fields.join) {
throw new Error('stringjoin representation needs a "join" specifier')
}
return fields
}
StructStringjoinRepresentationField
= StructStringjoinRepresentationField_Join
/ StructStringjoinRepresentationField_FieldOrder
StructStringjoinRepresentationField_Join = _ "join" _ join:QuotedString _ {
return { join }
}
StructStringjoinRepresentationField_FieldOrder = _ "fieldOrder" _ fieldOrder:QuotedStringArray _ {
return { fieldOrder }
}
EnumRepresentationType
= "string" { return { string: {} } }
/ "int" { return { int: {} } }
BytesDescriptor = "representation" wsnl "advanced" wsnl representation:AdvancedRepresentation { return { bytes: { representation } } }
QuotedStringArray = "[" _ firstElement:QuotedString? subsequentElements:(_ "," _ s:QuotedString _ { return s })* _ "]" {
if (!firstElement) {
return []
}
if (!subsequentElements) {
return [ firstElement ]
}
return [ firstElement ].concat(subsequentElements)
}
TypeName = StringName
EnumValue = StringName
QuotedString = "\"" chars:[^"]+ "\"" { return chars.join('') }
StringName = first:[a-zA-Z] remainder:[a-zA-Z0-9_]* { return first + remainder.join('') }
Integer = chars:[0-9]+ { return parseInt(chars.join(''), 10) }
BaseType
= "bool"
/ "string"
/ "bytes"
/ "int"
/ "float"
/ "map"
/ "list"
/ "link"
/ "null"
/ "any"
// Annotation is '# @name(value)' or '# @name', swallowing any trailing comments
Annotation = [ \t]* "#" [ \t]* "@" name:StringName value:("(" value:[^)]* ")" { return value })? [^\r\n]* newline ws* {
return { [name]: value ? value.join('') : '' }
}
capturedcomment = comments:_capturedcomment* {
return flattenArray(comments).join('')
}
_capturedcomment
= ws+
/ newline+
/ comment+
__
= ws+ { return }
/ newline+ { return }
/ comment+ { return }
_ = __*
_wsnl
= ws+ { return }
/ newline+ { return }
wsnl = _wsnl*
comment = "#" !([ \t]* "@" StringName) [^\r\n]*
ws = [ \t]
newline = "\r"? "\n"