aontu 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/lang.ts CHANGED
@@ -21,6 +21,10 @@ import {
21
21
  Op,
22
22
  } from '@jsonic/expr'
23
23
 
24
+ import {
25
+ Path
26
+ } from '@jsonic/path'
27
+
24
28
 
25
29
 
26
30
  import {
@@ -32,6 +36,7 @@ import {
32
36
  Nil,
33
37
  TOP,
34
38
  MapVal,
39
+ ListVal,
35
40
  ScalarTypeVal,
36
41
  Integer,
37
42
  StringVal,
@@ -45,7 +50,6 @@ import {
45
50
  } from './val'
46
51
 
47
52
 
48
-
49
53
  class Site {
50
54
  row: number = -1
51
55
  col: number = -1
@@ -67,53 +71,68 @@ class Site {
67
71
 
68
72
  let AontuJsonic: Plugin = function aontu(jsonic: Jsonic) {
69
73
 
74
+ jsonic.use(Path)
75
+
76
+ // TODO: refactor Val constructor
77
+ let addpath = (v: Val, p: string[]) => (v.path = [...(p || [])], v)
78
+
79
+
70
80
  jsonic.options({
71
81
  value: {
72
- // JSONIC-UPDATE: map: { val: ... }
73
82
  map: {
74
83
  // NOTE: specify with functions as jsonic/deep will
75
84
  // remove class prototype as options are assumed plain
76
85
  // (except for functions).
77
86
  // TODO: jsonic should be able to pass context into these
78
- 'string': { val: () => new ScalarTypeVal(String) },
79
- 'number': { val: () => new ScalarTypeVal(Number) },
80
- 'integer': { val: () => new ScalarTypeVal(Integer) },
81
- 'boolean': { val: () => new ScalarTypeVal(Boolean) },
82
- 'nil': { val: () => new Nil('literal') },
87
+ 'string': {
88
+ val: (r: Rule) =>
89
+ addpath(new ScalarTypeVal(String), r.keep.path)
90
+ },
91
+ 'number': {
92
+ val: (r: Rule) =>
93
+ addpath(new ScalarTypeVal(Number), r.keep.path)
94
+ },
95
+ 'integer': {
96
+ val: (r: Rule) =>
97
+ addpath(new ScalarTypeVal(Integer), r.keep.path)
98
+ },
99
+ 'boolean': {
100
+ val: (r: Rule) =>
101
+ addpath(new ScalarTypeVal(Boolean), r.keep.path)
102
+ },
103
+ 'nil': {
104
+ val: (r: Rule) =>
105
+ addpath(new Nil('literal'), r.keep.path)
106
+ },
107
+
108
+ // TODO: FIX: need a TOP instance to hold path
83
109
  'top': { val: () => TOP },
84
110
  }
85
111
  },
86
112
 
87
- // // JSONIC-UPDATE: fixed: { token }
88
- // fixed: {
89
- // token: {
90
- // '#A&': '&',
91
- // '#A|': '|',
92
- // '#A/': '/',
93
- // '#A*': '*', // TODO: REVIEW char as * is a bit overloaded
94
- // '#A=': '=',
95
- // }
96
- // },
97
-
98
- // JSONIC-UPDATE: check impl
99
113
  map: {
100
114
  merge: (prev: any, curr: any) => {
101
115
  let pval = (prev as Val)
102
116
  let cval = (curr as Val)
103
- return new ConjunctVal([pval, cval])
117
+ return addpath(new ConjunctVal([pval, cval]), prev.path)
104
118
  }
105
119
  }
106
120
  })
107
121
 
108
122
 
109
123
  let opmap: any = {
110
- 'conjunct-infix': (_op: Op, terms: any) => new ConjunctVal(terms),
111
- 'disjunct-infix': (_op: Op, terms: any) => new DisjunctVal(terms),
112
-
113
- 'dot-prefix': (_op: Op, terms: any) => new RefVal(terms, true),
114
- 'dot-infix': (_op: Op, terms: any) => new RefVal(terms),
115
-
116
- 'star-prefix': (_op: Op, terms: any) => new PrefVal(terms[0]),
124
+ 'conjunct-infix': (r: Rule, _op: Op, terms: any) =>
125
+ addpath(new ConjunctVal(terms), r.keep.path),
126
+ 'disjunct-infix': (r: Rule, _op: Op, terms: any) =>
127
+ addpath(new DisjunctVal(terms), r.keep.path),
128
+
129
+ 'dot-prefix': (r: Rule, _op: Op, terms: any) =>
130
+ addpath(new RefVal(terms, true), r.keep.path),
131
+ 'dot-infix': (r: Rule, _op: Op, terms: any) =>
132
+ addpath(new RefVal(terms), r.keep.path),
133
+
134
+ 'star-prefix': (r: Rule, _op: Op, terms: any) =>
135
+ addpath(new PrefVal(terms[0]), r.keep.path),
117
136
  }
118
137
 
119
138
 
@@ -147,307 +166,127 @@ let AontuJsonic: Plugin = function aontu(jsonic: Jsonic) {
147
166
  right: 14_000_000,
148
167
  },
149
168
  },
150
- evaluate: (op: Op, terms: any) => {
151
- // console.log('LANG EVAL', op, terms)
152
- return opmap[op.name](op, terms)
169
+ evaluate: (r: Rule, op: Op, terms: any) => {
170
+ // console.log('LANG EVAL', r.keep, op, terms)
171
+ let val: Val = opmap[op.name](r, op, terms)
172
+ // console.dir(val, { depth: null })
173
+
174
+ return val
153
175
  }
154
176
  })
155
177
 
156
- // console.log(jsonic.token)
178
+
157
179
  let CJ = jsonic.token['#E&']
158
180
  let CL = jsonic.token.CL
159
181
 
160
- // let NR = jsonic.token.NR
161
- // let TX = jsonic.token.TX
162
- // let ST = jsonic.token.ST
163
- // let VL = jsonic.token.VL
164
- // let OB = jsonic.token.OB
165
- // let OS = jsonic.token.OS
166
-
167
-
168
- // let DJ = jsonic.token['#A|']
169
- // let FS = jsonic.token['#A/']
170
- // let AK = jsonic.token['#A*']
171
- // let EQ = jsonic.token['#A=']
172
-
173
-
174
- // JSONIC-UPDATE: rule.open[], rule.close[] - replace with rule.oN|cN
175
-
176
- // jsonic.rule('expr', (rs: RuleSpec) => {
177
- // rs
178
- // .open([
179
- // { s: [[CJ, DJ, AK]], p: 'disjunct', b: 1, n: { expr: 1 } },
180
- // ])
181
-
182
- // .close([
183
- // { s: [] }
184
- // ])
185
-
186
- // // NOTE: expr node are meta structures, not Vals
187
- // // t=most recent term on the left, o=Val
188
- // .bo((r: Rule) => r.node = { t: r.node })
189
-
190
- // .ac((r: Rule) => {
191
- // let cn = r.child.node.o
192
-
193
- // if (cn instanceof PrefVal) {
194
- // return r.o0.bad('single-pref')
195
- // }
196
-
197
- // // replace first val with expr val
198
- // r.node = cn
199
-
200
- // if ('val' === r.parent?.name) {
201
- // r.parent.node = r.node
202
- // }
203
- // })
204
-
205
- // })
206
-
207
-
208
- // jsonic.rule('disjunct', (rs: RuleSpec) => {
209
- // rs
210
- // .open([
211
- // {
212
- // s: [CJ], p: 'conjunct', b: 1
213
- // },
214
- // {
215
- // s: [AK], p: 'pref', b: 1
216
- // },
217
- // {
218
- // s: [DJ, AK], p: 'pref', b: 1,
219
- // a: (r: Rule) => {
220
- // // Append to existing or start new
221
- // r.node.o = r.node.o instanceof DisjunctVal ?
222
- // r.node.o : new DisjunctVal([r.node.t])
223
- // }
224
- // },
225
- // {
226
- // s: [DJ, [NR, TX, ST, VL, OB, OS]], b: 1,
227
- // p: 'val',
228
- // a: (r: Rule) => {
229
- // // Append to existing or start new
230
- // r.node.o = r.node.o instanceof DisjunctVal ?
231
- // r.node.o : new DisjunctVal([r.node.t])
232
- // }
233
- // },
234
- // ])
235
-
236
- // .close([
237
- // {
238
- // s: [DJ], r: 'disjunct', b: 1, a: (r: Rule) => {
239
- // // higher precedence term (e.g &) was on the left
240
- // let cn = r.child.node?.o || r.child.node
241
- // r.node.t = cn
242
- // }
243
- // },
244
- // {
245
- // s: [CJ], r: 'disjunct', b: 1, a: (r: Rule) => {
246
- // // & with higher precedence to the right
247
- // let cn = r.child.node?.o || r.child.node
248
- // r.node.t = cn
249
- // r.child.node = null
250
- // }
251
- // },
252
- // {}
253
- // ])
254
- // .ac((r: Rule) => {
255
- // // child values may be normal or expr metas
256
- // let cn = r.child.node?.o || r.child.node
257
- // if (cn) {
258
- // if (r.node.o instanceof DisjunctVal) {
259
- // r.node.o.append(cn)
260
- // }
261
- // else {
262
- // // this rule was just a pass-through
263
- // r.node.o = cn
264
- // }
265
- // }
266
- // })
267
- // })
268
-
269
-
270
-
271
- // jsonic.rule('conjunct', (rs: RuleSpec) => {
272
- // rs
273
- // .open([
274
- // {
275
- // s: [CJ, [NR, TX, ST, VL, OB, OS, FS]], b: 1,
276
- // p: 'val',
277
- // a: (r: Rule) => {
278
- // r.node = {
279
- // o: r.node.o instanceof ConjunctVal ?
280
- // r.node.o : new ConjunctVal([r.node.t])
281
- // }
282
- // }
283
- // },
284
- // ])
285
- // .close([
286
- // {
287
- // s: [CJ], r: 'conjunct', b: 1
288
- // },
289
- // {}
290
- // ])
291
- // .ac((r: Rule) => {
292
- // let cn = r.child.node?.o || r.child.node
293
- // if (cn) {
294
- // if (r.node.o instanceof ConjunctVal) {
295
- // r.node.o.append(cn)
296
- // }
297
- // else {
298
- // r.node.o = cn
299
- // }
300
- // }
301
- // })
302
- // })
303
-
304
-
305
- // jsonic.rule('path', (rs: RuleSpec) => {
306
- // rs
307
- // .open([
308
- // { s: [FS, [TX, ST, NR, VL]], p: 'part', b: 2 }
309
- // ])
310
- // .bo((r: Rule) => r.node = new RefVal('/'))
311
- // })
312
-
313
-
314
- // jsonic.rule('part', (rs: RuleSpec) => {
315
- // rs.
316
- // open([
317
- // {
318
- // s: [FS, [TX, ST, NR, VL]], r: 'part', a: (r: Rule) => {
319
- // r.node.append(r.o1.src)
320
- // }
321
- // },
322
- // {}, // no more parts
323
- // ])
324
- // })
325
-
326
-
327
-
328
- // // rs.def.open.unshift({
329
- // // s: [[CJ, DJ], EQ], p: 'val', u: { spread: true }
330
- // // })
331
-
332
- // // // TODO: make before/after function[]
333
- // // let orig_bc: any = rs.def.bc
334
- // // rs.def.bc = function(rule: Rule, ctx: Context) {
335
- // // let out = orig_bc.call(this, rule, ctx)
336
-
337
- // // if (rule.use.spread) {
338
- // // rule.node[MapVal.SPREAD] =
339
- // // (rule.node[MapVal.SPREAD] || { o: rule.o0.src, v: [] })
340
- // // rule.node[MapVal.SPREAD].v.push(rule.child.node)
341
- // // }
342
-
343
- // // return out
344
- // // }
345
-
346
- // return rs
347
- // })
348
-
349
-
350
- // jsonic.rule('pref', (rs: RuleSpec) => {
351
- // rs
352
- // .open([
353
- // {
354
- // s: [AK, [NR, TX, ST, VL, OB, OS, FS]], b: 1,
355
- // p: 'val',
356
- // },
357
- // ])
358
- // .close([
359
- // // Can't be in a conjunct
360
- // { s: [CJ], e: (r: Rule) => r.o1 },
361
- // {}
362
- // ])
363
- // .ac((r: Rule) => {
364
- // r.node = new PrefVal(r.child.node)
365
- // })
366
- // })
367
-
368
-
369
182
 
370
183
  jsonic.rule('val', (rs: RuleSpec) => {
371
- // rs.def.open.unshift(
372
- // // Prefs are always within an expression
373
- // { s: [AK, [NR, TX, ST, VL, OB, OS, FS]], p: 'expr', b: 2 },
374
- // { s: [FS, [TX, ST, NR, VL]], p: 'path', b: 2 },
375
- // )
376
- // rs.def.close.unshift(
377
- // {
378
- // s: [[CJ, DJ]], p: 'expr', b: 1, c: (r: Rule) => {
379
- // return null == r.n.expr || 0 === r.n.expr
380
- // }
381
- // },
382
- // )
383
-
384
184
 
385
185
  // TODO: wrap utility needed for jsonic to do this?
386
- let orig_bc: any = rs.def.bc
387
- rs.def.bc = function(rule: Rule, ctx: Context) {
388
- let out = orig_bc.call(this, rule, ctx)
186
+ // let orig_bc: any = rs.def.bc
187
+ // rs.def.bc = function(rule: Rule, ctx: Context) {
188
+ // let out = orig_bc.call(this, rule, ctx)
189
+
190
+ rs.bc(false, (r: Rule, ctx: Context) => {
389
191
 
390
- let valnode: Val = rule.node
192
+ let valnode: Val = r.node
391
193
  let valtype = typeof valnode
392
194
 
195
+ // console.log('VAL RULE', rule.use, rule.node)
196
+
393
197
  if ('string' === valtype) {
394
- valnode = new StringVal(rule.node)
198
+ valnode = addpath(new StringVal(r.node), r.keep.path)
395
199
  }
396
200
  else if ('number' === valtype) {
397
- if (Number.isInteger(rule.node)) {
398
- valnode = new IntegerVal(rule.node)
201
+ if (Number.isInteger(r.node)) {
202
+ valnode = addpath(new IntegerVal(r.node), r.keep.path)
399
203
  }
400
204
  else {
401
- valnode = new NumberVal(rule.node)
205
+ valnode = addpath(new NumberVal(r.node), r.keep.path)
402
206
  }
403
207
  }
404
208
  else if ('boolean' === valtype) {
405
- valnode = new BooleanVal(rule.node)
209
+ valnode = addpath(new BooleanVal(r.node), r.keep.path)
406
210
  }
407
211
 
408
- let st = rule.o0
212
+ let st = r.o0
409
213
  valnode.row = st.rI
410
214
  valnode.col = st.cI
411
215
 
412
216
  // JSONIC-UPDATE: still valid? check multisource
413
217
  valnode.url = ctx.meta.multisource && ctx.meta.multisource.path
414
218
 
415
- rule.node = valnode
219
+ r.node = valnode
220
+
221
+ // return out
222
+ return undefined
223
+ })
416
224
 
417
- return out
418
- }
419
225
  return rs
420
226
  })
421
227
 
422
228
 
423
229
 
424
230
  jsonic.rule('map', (rs: RuleSpec) => {
425
- let orig_bc = rs.def.bc
426
- rs.def.bc = function(rule: Rule, ctx: Context) {
427
- let out = orig_bc ? orig_bc.call(this, rule, ctx) : undefined
231
+ // let orig_bc = rs.def.bc
232
+ // rs.def.bc = function(rule: Rule, ctx: Context) {
233
+ // let out = orig_bc ? orig_bc.call(this, rule, ctx) : undefined
428
234
 
429
- rule.node = new MapVal(rule.node)
235
+ rs.bc(false, (r: Rule) => {
236
+
237
+ // console.log('MAP RULE', rule.use, rule.node)
238
+ r.node = addpath(new MapVal(r.node), r.keep.path)
239
+
240
+ // return out
241
+ return undefined
242
+ })
243
+
244
+ return rs
245
+ })
246
+
247
+
248
+ jsonic.rule('list', (rs: RuleSpec) => {
249
+ // let orig_bc = rs.def.bc
250
+ // rs.def.bc = function(rule: Rule, ctx: Context) {
251
+ // let out = orig_bc ? orig_bc.call(this, rule, ctx) : undefined
252
+
253
+ rs.bc(false, (r: Rule) => {
254
+ r.node = addpath(new ListVal(r.node), r.keep.path)
255
+
256
+ // return out
257
+ return undefined
258
+ })
430
259
 
431
- return out
432
- }
433
260
  return rs
434
261
  })
435
262
 
436
263
 
264
+
437
265
  jsonic.rule('pair', (rs: RuleSpec) => {
438
- let orig_bc: any = rs.def.bc
266
+ // let orig_bc: any = rs.def.bc
439
267
  rs
440
268
  .open([{ s: [CJ, CL], p: 'val', u: { spread: true }, g: 'spread' }])
441
- .bc((...rest: any) => {
442
- orig_bc(...rest)
443
269
 
444
- let rule = rest[0]
270
+ // .bc((...rest: any) => {
271
+ // orig_bc(...rest)
272
+
273
+
274
+ .bc(false, (rule: Rule) => {
275
+ // let rule = rest[0]
276
+ // console.log('PAIR RULE', rule.use, rule.node,
277
+ // rule.parent.name, rule.parent.use)
278
+
279
+ // TRAVERSE PARENTS TO GET PATH
280
+
445
281
  if (rule.use.spread) {
446
282
  rule.node[MapVal.SPREAD] =
447
283
  (rule.node[MapVal.SPREAD] || { o: rule.o0.src, v: [] })
448
284
  rule.node[MapVal.SPREAD].v.push(rule.child.node)
449
285
  }
286
+
287
+ return undefined
450
288
  })
289
+
451
290
  return rs
452
291
  })
453
292
 
@@ -491,6 +330,8 @@ class Lang {
491
330
  jm.log = opts.log
492
331
  }
493
332
 
333
+ // jm.log = -1
334
+
494
335
  let val = this.jsonic(src, jm)
495
336
 
496
337
  return val
package/lib/op/unite.ts CHANGED
@@ -64,7 +64,7 @@ const unite: Operation = (ctx: Context, a?: Val, b?: Val, whence?: string) => {
64
64
  }
65
65
  }
66
66
 
67
- if (!out) {
67
+ if (!out || !out.unify) {
68
68
  out = Nil.make(ctx, 'unite', a, b)
69
69
  }
70
70
 
package/lib/unify.ts CHANGED
@@ -66,6 +66,7 @@ class Context {
66
66
 
67
67
 
68
68
  find(ref: RefVal) {
69
+ // console.log('FIND', ref.path, ref.peg)
69
70
 
70
71
  // TODO: relative paths
71
72
  if (this.root instanceof MapVal && ref.absolute) {