-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
202 lines (175 loc) · 5.45 KB
/
index.js
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
const debug = require('debug')(__filename.split('/').slice(-1).join())
const intoStream = require('into-stream')
const parseTemplate = require('./parse-template')
const parseData = require('./parse-data')
const { ReferenceTable } = require('./reftable')
const { Matcher } = require('./manual-match')
function isClass (x) {
return (x.name &&
x.prototype &&
x.prototype.constructor &&
x.prototype.constructor.name === x.name)
}
class Mapper {
constructor (options = {}) {
this.templates = []
this.varMap = {}
this.genid = options.genid
Object.assign(this, options)
}
// try to give nice do-what-I-mean arguments to addPair
//
// it's completely ridiculous, I know. tests in test-dwim.js
//
add (a, b) {
let local, text
if (isClass(a)) {
const AppClass = a
a = {
class: AppClass,
input: x => new AppClass(x),
output: x => x instanceof AppClass
}
}
local = a
text = b
if (typeof a === 'string') {
local = null // it's okay to have no .local on [subject] template. check?
text = a
}
const cls = local && local.class
// console.error({a,b,local,text,cls})
if (text === undefined && cls) {
if (cls.definitions) {
for (const def of cls.definitions) this.addPair(local, def)
return
}
text = cls.definition
}
if (!text) throw new RangeError('no definition text supplied to add()')
this.addPair(local, text)
}
addPair (local, text) {
const parsed = [...parseTemplate.parseTemplate(text)]
const index = this.templates.length // gets coded into regexp
const re = parseData.makeRE(parsed, index, this.varMap)
const template = { local, text, parsed, re, index }
this.templates.push(template)
delete this.mergedRE
// return template
}
//
// Parsing
//
* parsedItems (text, reftable) {
if (!this.mergedRE) {
if (this.trace) {
debug('tracing')
this.mergedRE = new Matcher(this)
} else {
const re = parseData.mergeTemplates(this.templates)
this.mergedRE = new RegExp(re, 'imgy') // u prevents \-space ?!
}
}
yield * parseData.parse(this, text, reftable)
}
parse (text) {
const reftable = new ReferenceTable({ genid: this.genid })
const result = [...this.parsedItems(text, reftable)]
reftable.complete()
return result
}
parser () { // would return a transformStream from strings to objects
// will need its own copy of mergedRE, since that holds state
throw Error('not implemented')
// the main trick here would be remembering where the junk
// starts at the end of a parse, then keeping that stuff around to
// prepend to the next chunk. re.lastIndex makes that fairly easy.
}
//
// Serializing
//
streamify (items) {
return intoStream(this.stringChunks(items))
}
stringify (items) {
return [...this.stringChunks(items)].join('')
}
* stringChunks (items) {
const reftable = new ReferenceTable({ genid: this.genid })
for (const item of items) {
yield * this.stringChunk1(item, reftable)
}
}
* stringChunk1 (item, reftable) {
// console.log('stringify %O', item)
const t = this.findTemplate(item)
// console.log('.. using template %O', t)
if (!t) throw Error('cant stringify object')
const needs = new Set()
yield * this.fill(t, item, needs, reftable)
yield ('\n\n')
for (const prereq of needs) {
yield * this.stringChunk1(prereq, reftable)
}
}
findTemplate (item) {
// console.log('ft', item)
for (const t of this.templates) {
if (t.local.output(item)) return t
}
return undefined
}
/*
Given a template and an object of data, yield parts of string with the
template filled in, using the fields of that object.
- needs to be able to queue up things to be sent / first?
- needs to flag which properties it used, so we can use other
templates for some others.
*/
* fill (t, item, needs, reftable) {
for (const [index, part] of t.parsed.entries()) {
// console.log('part:', part)
if (typeof part === 'string') {
yield part
} else {
let value
if (part.name === 'id' || part.name === 'subject') {
value = reftable.idForObject(item)
} else {
value = item[part.name]
}
// if (value === undefined) warn? error?
if (value === undefined) value = '(ValueUnknown)'
switch (typeof value) {
case 'string':
break
case 'number':
case 'boolean':
value = '' + value
break
case 'object':
if (part.type !== 'ref') {
console.error('object value found in slot without "ref" type, slot=%O, value=%O', part, value)
}
value = reftable.idForObject(value, needs)
break
default:
throw new Error('cant serialize: ' + JSON.stringify(value))
}
// does it need quoting?
if (index + 1 === t.parsed.length || // last field, no delim
value.indexOf(t.parsed[index + 1]) > -1 || // delim occurs in value
value.indexOf('"') > -1 ||
value.indexOf('\\') > -1) {
value = JSON.stringify(value) // is that the quoting we want? *shrug*
}
yield value
}
}
}
}
function mapper (...args) {
return new Mapper(...args)
}
module.exports = { Mapper, mapper }