-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoover.ts
391 lines (333 loc) · 8.06 KB
/
hoover.ts
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
/* Copyright (c) 2021-2023 Richard Rodger, MIT License */
/*
support regexp
indent removal
more rule cases
*/
// TODO: line continuation ("\" at end) should be a feature of standard JSONIC strings
import {
Jsonic,
Rule,
RuleSpec,
Plugin,
Config,
Options,
Lex,
Point,
Context,
MakeLexMatcher,
makeStringMatcher,
makePoint,
Token,
AltAction,
} from 'jsonic'
type HooverOptions = {
block: {
[name: string]: {
start?: {
fixed?: string | string[]
consume?: null | boolean | string[] // explicit false to turn off
}
end?: {
fixed?: string | string[]
consume?: null | boolean | string[] // explicit false to turn off
}
escapeChar?: string
escape?: {
[char: string]: string
}
allowUnknownEscape: boolean
preserveEscapeChar: boolean
trim: boolean
}
}
lex?: {
order?: number
}
action?: AltAction
}
const Hoover: Plugin = (jsonic: Jsonic, options: HooverOptions) => {
const { entries } = jsonic.util
let blocks = entries(options.block).map((entry: any[]) => ({
allowUnknownEscape: true,
preserveEscapeChar: false,
token: '#HV',
...entry[1],
name: entry[0],
}))
let tokenMap: any = {}
for (let block of blocks) {
// Create a hoover token
block.TOKEN = jsonic.token(block.token)
if (!tokenMap[block.token]) {
jsonic.rule('val', (rs) => {
rs.open({
s: [block.TOKEN],
a: options.action,
})
})
}
tokenMap[block.token] = block.TOKEN
}
let makeHooverMatcher: MakeLexMatcher = (cfg: Config, _opts: Options) => {
return function hooverMatcher(lex: Lex) {
for (let block of blocks) {
// TODO: Point.clone ?
const hvpnt = makePoint(lex.pnt.len, lex.pnt.sI, lex.pnt.rI, lex.pnt.cI)
let startResult = matchStart(lex, hvpnt, block)
if (startResult.match) {
let result = parseToEnd(lex, hvpnt, block, cfg)
if (result.done) {
let tkn = lex.token(
block.TOKEN,
result.val,
lex.src.substring(lex.pnt.sI, hvpnt.sI),
hvpnt,
)
tkn.use = { block: block.name }
lex.pnt.sI = hvpnt.sI
lex.pnt.rI = hvpnt.rI
lex.pnt.cI = hvpnt.cI
return tkn
} else {
return result.bad || lex.bad('invalid_text', lex.pnt.sI, hvpnt.sI)
}
}
}
return undefined
}
}
jsonic.options({
lex: {
match: {
hoover: { order: options.lex?.order, make: makeHooverMatcher },
},
},
})
}
function matchStart(
lex: Lex,
hvpnt: Point,
block: any,
): {
match: boolean
start?: string
} {
let src = lex.src
let sI = hvpnt.sI // Current point in src
let rI = hvpnt.rI // Current row
let cI = hvpnt.cI // Current column
let start = block.start || {}
let rulespec = start.rule || {}
let matchRule: null | boolean = null
// NOTE: Default rules:
// - parent is pair,elem
// - state is open
if (rulespec.parent) {
if (rulespec.parent.include) {
matchRule =
rulespec.parent.include.includes(lex.ctx.rule.parent.name) &&
(null === matchRule ? true : matchRule)
}
if (rulespec.parent.exclude) {
matchRule =
!rulespec.parent.exclude.includes(lex.ctx.rule.parent.name) &&
(null === matchRule ? true : matchRule)
}
}
if (rulespec.current) {
if (rulespec.current.include) {
matchRule =
rulespec.current.include.includes(lex.ctx.rule.name) &&
(null === matchRule ? true : matchRule)
}
if (rulespec.current.exclude) {
matchRule =
!rulespec.current.exclude.includes(lex.ctx.rule.name) &&
(null === matchRule ? true : matchRule)
}
}
// '': don't check, 'oc'|'c'|'o' check, default 'o'
let rulestate = '' === rulespec.state ? '' : rulespec.state || 'o'
if (rulestate) {
matchRule =
rulestate.includes(lex.ctx.rule.state) &&
(null === matchRule ? true : matchRule)
}
let matchFixed = true
let fixed = start.fixed
if (matchRule && null != fixed) {
matchFixed = false
fixed = Array.isArray(fixed) ? fixed : [fixed]
for (let fI = 0; !matchFixed && fI < fixed.length; fI++) {
if (src.substring(hvpnt.sI).startsWith(fixed[fI])) {
matchFixed = true
if (false !== start.consume) {
if (
!Array.isArray(start.consume) ||
start.consume.includes(fixed[fI])
) {
let endI = hvpnt.sI + fixed[fI].length
for (let fsI = hvpnt.sI; fsI < endI; fsI++) {
sI++
cI++
if ('\n' === src[fsI]) {
rI++
cI = 0
}
}
}
}
break
}
}
}
if (matchRule && matchFixed) {
let startsrc = src.substring(hvpnt.sI, sI)
if (false !== block.trim) {
startsrc = startsrc.trim()
}
hvpnt.sI = sI
hvpnt.rI = rI
hvpnt.cI = cI
return {
match: true,
start: startsrc,
}
} else {
return { match: false }
}
}
function parseToEnd(
lex: Lex,
hvpnt: Point,
block: any,
cfg: Config,
): {
done: boolean
val: string
bad?: Token
} {
let valc = []
let src = lex.src
let endspec = block.end
let fixed: string[] = endspec.fixed
fixed = 'string' === typeof fixed ? [fixed] : fixed
let endchars = fixed.map((end) => end[0])
let endseqs = fixed.map((end) => end.substring(1))
let escapeChar = block.escapeChar
let sI = hvpnt.sI // Current point in src
let rI = hvpnt.rI // Current row
let cI = hvpnt.cI // Current column
let done = false
let c: string = ''
let endI = sI
let endCharIndex = 0
top: do {
c = src[sI]
// Check for end
if (-1 < (endCharIndex = endchars.indexOf(c))) {
let tail = endseqs[endCharIndex]
// EOF
if (undefined === tail || '' === tail) {
endI = sI + 1
done = true
break top
}
// Match tail
else if (
'string' === typeof tail &&
tail === src.substring(sI + 1, sI + 1 + tail.length)
) {
endI = sI + 1 + tail.length
done = true
break top
}
// // regexp
// else if (tail.exec && (m = tail.exec(src.substring(sI)))) {
// pI = 1 + m[0].length
// done = true
// break endseq
// }
// }
// break top
}
if (escapeChar === c) {
let replacement = block.escape[src[sI + 1]]
if (null != replacement) {
c = replacement
sI++
cI++
} else if (block.allowUnknownEscape) {
c = block.preserveEscapeChar ? src.substring(sI, sI + 2) : src[sI + 1]
sI++
} else {
return {
done: false,
val: '',
bad: lex.bad('invalid_escape', sI, sI + 1),
}
}
}
valc.push(c)
sI++
cI++
if ('\n' === c) {
rI++
cI = 0
}
} while (sI <= src.length)
if (done) {
if (false !== endspec.consume) {
let endfixed = src.substring(sI, endI)
if (
!Array.isArray(endspec.consume) ||
endspec.consume.includes(endfixed)
) {
let esI = sI
for (; esI < endI; esI++) {
sI++
cI++
if ('\n' === src[esI]) {
rI++
cI = 0
}
}
}
}
hvpnt.sI = sI
hvpnt.rI = rI
hvpnt.cI = cI
}
let val: any = valc.join('')
if (block.trim) {
val = val.trim()
}
if (cfg.value.lex && undefined !== cfg.value.def[val]) {
val = cfg.value.def[val].val
}
return {
done,
val,
}
}
Hoover.defaults = {
block: {
// // TODO: normalize with defaults
// "'''": {
// open: "'''",
// close: "'''",
// indent: true,
// trim: true,
// doubleEscape: false,
// lineReplace: null,
// },
// // TODO: FOR TEST
// // '<<': { close: '>>', indent: false }
},
lex: {
order: 4.5e6, // before string, number
},
} as HooverOptions
export { parseToEnd, Hoover }
export type { HooverOptions }