generated from jsonicjs/directive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.js
480 lines (480 loc) · 16 KB
/
csv.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
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
"use strict";
/* Copyright (c) 2021-2024 Richard Rodger, MIT License */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Csv = void 0;
exports.buildCsvStringMatcher = buildCsvStringMatcher;
// Plugin implementation.
const Csv = (jsonic, options) => {
var _a;
// Normalize boolean options.
const strict = !!options.strict;
const objres = !!options.object;
const header = !!options.header;
// These may be changed below by superior options.
let trim = !!options.trim;
let comment = !!options.comment;
let opt_number = !!options.number;
let opt_value = !!options.value;
let record_empty = !!((_a = options.record) === null || _a === void 0 ? void 0 : _a.empty);
const stream = options.stream;
// In strict mode, Jsonic field content is not parsed.
if (strict) {
if (false !== options.string.csv) {
// jsonic.lex(buildCsvStringMatcher(options))
jsonic.options({
lex: {
match: {
stringcsv: { order: 1e5, make: buildCsvStringMatcher(options) },
},
},
});
}
jsonic.options({
rule: { exclude: 'jsonic,imp' },
});
}
// Fields may contain Jsonic content.
else {
if (true === options.string.csv) {
// jsonic.lex(buildCsvStringMatcher(options))
jsonic.options({
lex: {
match: {
stringcsv: { order: 1e5, make: buildCsvStringMatcher(options) },
},
},
});
}
trim = null === options.trim ? true : trim;
comment = null === options.comment ? true : comment;
opt_number = null === options.number ? true : opt_number;
opt_value = null === options.value ? true : opt_value;
jsonic.options({
rule: { exclude: 'imp' },
});
}
// Stream rows as they are parsed, do not store in result.
if (stream) {
let parser = jsonic.internal().parser;
let origStart = parser.start.bind(parser);
parser.start = (...args) => {
try {
return origStart(...args);
}
catch (e) {
stream('error', e);
}
};
}
let token = {};
if (strict) {
// Disable JSON structure tokens
token = {
'#OB': null,
'#CB': null,
'#OS': null,
'#CS': null,
'#CL': null,
};
}
// Custom "comma"
if (options.field.separation) {
token['#CA'] = options.field.separation;
}
// Usually [#TX, #SP, #NR, #VL]
let VAL = jsonic.tokenSet.VAL;
// Jsonic option overrides.
let jsonicOptions = {
rule: {
start: 'csv',
},
fixed: {
token,
},
tokenSet: {
// See jsonic/src/defaults.ts; and util.deep merging
// ignore: [
IGNORE: [
strict ? null : undefined, // Handle #SP space
null, // Handle #LN newlines
undefined, // Still ignore #CM comments
],
},
number: {
lex: opt_number,
},
value: {
lex: opt_value,
},
comment: {
lex: comment,
},
lex: {
emptyResult: [],
},
line: {
single: record_empty,
chars: null == options.record.separators
? undefined
: options.record.separators,
rowChars: null == options.record.separators
? undefined
: options.record.separators,
},
error: {
csv_extra_field: 'unexpected extra field value: $fsrc',
csv_missing_field: 'missing field',
},
hint: {
csv_extra_field: `Row $row has too many fields (the first of which is: $fsrc). Only $len
fields per row are expected.`,
csv_missing_field: `Row $row has too few fields. $len fields per row are expected.`,
},
};
jsonic.options(jsonicOptions);
let { LN, CA, SP, ZZ } = jsonic.token;
// Starting rule.
jsonic.rule('csv', (rs) => {
rs.bo((r, ctx) => {
ctx.u.recordI = 0; // Record counter.
stream && stream('start'); // If streaming, send 'start' event.
r.node = []; // Top level list of records - the result!
})
.open([
// End immediately if EOF
{ s: [ZZ] },
// Ignore empty lines from the start.
// !record_empty && { s: [LN], p: 'newline' },
!record_empty
? { s: [LN], p: 'newline' }
: null,
// Look for the first record.
{ p: 'record' },
])
.ac(() => {
stream && stream('end');
});
return rs;
});
// Ignore empty lines. Keep consuming LN until there's a record or EOF.
jsonic.rule('newline', (rs) => {
rs.open([
// NOTE: r in open means no close except final
{ s: [LN, LN], r: 'newline' },
{ s: [LN], r: 'newline' },
{ s: [ZZ] },
{ r: 'record' },
]).close([
{ s: [LN, LN], r: 'newline' },
{ s: [LN], r: 'newline' },
{ s: [ZZ] },
{ r: 'record' },
]);
});
// A CSV record line.
jsonic.rule('record', (rs) => {
rs.open([
// Reuse Jsonic list rule
{ p: 'list' },
])
.close([
// EOF also ends CSV
{ s: [ZZ] },
// Last LN is not a record.
{ s: [LN, ZZ] },
// Ignore (or not) empty lines.
{ s: [LN], r: record_empty ? 'record' : 'newline' },
])
.bc((rule, ctx) => {
// Record field names
let fields = ctx.u.fields || options.field.names;
// First line is fields if options.header=true
if (0 === ctx.u.recordI && header) {
ctx.u.fields = undefined === rule.child.node ? [] : rule.child.node;
}
// A normal record line.
else {
let record = rule.child.node || [];
// Return records as objects with names fields
if (objres) {
let obj = {};
let i = 0;
if (fields) {
if (options.field.exact) {
if (record.length !== fields.length) {
return ctx.t0.bad(record.length > fields.length
? 'csv_extra_field'
: 'csv_missing_field');
}
}
let fI = 0;
for (; fI < fields.length; fI++) {
obj[fields[fI]] =
undefined === record[fI] ? options.field.empty : record[fI];
}
i = fI;
}
// Handle extra unnamed fields.
for (; i < record.length; i++) {
let field_name = options.field.nonameprefix + i;
obj[field_name] =
undefined === record[i] ? options.field.empty : record[i];
}
record = obj;
}
// Return records as arrays.
else {
for (let i = 0; i < record.length; i++) {
record[i] =
undefined === record[i] ? options.field.empty : record[i];
}
}
if (stream) {
stream('record', record);
}
else {
rule.node.push(record);
}
}
ctx.u.recordI++;
});
return rs;
});
jsonic.rule('list', (rs) => {
return rs
.open([
// If not ignoring empty fields, don't consume LN used to close empty record.
{ s: [LN], b: 1 },
])
.close([
// LN ends record
{ s: [LN], b: 1 },
{ s: [ZZ] },
]);
});
jsonic.rule('elem', (rs) => {
return rs
.open([
// An empty element
{
s: [CA],
b: 1,
a: (r) => {
r.node.push(options.field.empty);
r.u.done = true;
},
},
])
.close([
// An empty element at the end of the line
{
s: [CA, [LN, ZZ]],
b: 1,
a: (r) => r.node.push(options.field.empty),
},
// LN ends record
{ s: [LN], b: 1 },
]);
});
jsonic.rule('val', (rs) => {
return rs.open([
// Handle text and space concatentation
{ s: [VAL, SP], b: 2, p: 'text' },
{ s: [SP], b: 1, p: 'text' },
// LN ends record
{ s: [LN], b: 1 },
]);
});
// Handle text and space concatentation
// NOTE: trim and string are complications.
jsonic.rule('text', (rs) => {
return (rs
// Space within non-space is preserved as part of text value.
.open([
{
// NOTE: r in open means no close except final
s: [VAL, SP],
b: 1,
r: 'text',
n: { text: 1 },
g: 'csv,space,follows',
a: (r) => {
// Keep appending space to prev node
let v = 1 === r.n.text ? r : r.prev;
r.node = v.node = (1 === r.n.text ? '' : r.prev.node) + r.o0.val;
},
},
{
s: [SP, VAL],
r: 'text',
n: { text: 1 },
g: 'csv,space,leads',
a: (r) => {
// Inner space
let v = 1 === r.n.text ? r : r.prev;
r.node = v.node =
(1 === r.n.text ? '' : r.prev.node) +
(2 <= r.n.text || !trim ? r.o0.src : '') +
r.o1.src;
},
},
{
s: [SP, [CA, LN, ZZ]],
b: 1,
n: { text: 1 },
g: 'csv,end',
a: (r) => {
// Final space
let v = 1 === r.n.text ? r : r.prev;
r.node = v.node =
(1 === r.n.text ? '' : r.prev.node) + (!trim ? r.o0.src : '');
},
},
{
s: [SP],
n: { text: 1 },
g: 'csv,space',
a: (r) => {
if (strict) {
let v = 1 === r.n.text ? r : r.prev;
r.node = v.node =
(1 === r.n.text ? '' : r.prev.node) + (!trim ? r.o0.src : '');
}
},
p: strict ? undefined : 'val',
},
// Accept anything after text.
{},
])
// Close is called on final rule - set parent val node
.bc((r) => {
r.parent.node = undefined === r.child.node ? r.node : r.child.node;
}));
});
};
exports.Csv = Csv;
// Custom CSV String matcher.
// Handles "a""b" -> "a"b" quoting wierdness.
// This is a reduced copy of the standard Jsonic string matcher.
function buildCsvStringMatcher(csvopts) {
return function makeCsvStringMatcher(cfg, _opts) {
return function csvStringMatcher(lex) {
let quoteMap = { [csvopts.string.quote]: true };
let { pnt, src } = lex;
let { sI, rI, cI } = pnt;
let srclen = src.length;
if (quoteMap[src[sI]]) {
const q = src[sI]; // Quote character
const qI = sI;
const qrI = rI;
++sI;
++cI;
let s = [];
// let rs: string | undefined
for (sI; sI < srclen; sI++) {
cI++;
let c = src[sI];
// Quote char.
if (q === c) {
sI++;
cI++;
if (q === src[sI]) {
s.push(q);
}
else {
break; // String finished.
}
}
// Body part of string.
else {
let bI = sI;
// TODO: move to cfgx
let qc = q.charCodeAt(0);
let cc = src.charCodeAt(sI);
while (sI < srclen && 32 <= cc && qc !== cc) {
cc = src.charCodeAt(++sI);
cI++;
}
cI--;
if (cfg.line.chars[src[sI]]) {
if (cfg.line.rowChars[src[sI]]) {
pnt.rI = ++rI;
}
cI = 1;
s.push(src.substring(bI, sI + 1));
}
else if (cc < 32) {
pnt.sI = sI;
pnt.cI = cI;
return lex.bad('unprintable', sI, sI + 1);
}
else {
s.push(src.substring(bI, sI));
sI--;
}
}
}
if (src[sI - 1] !== q || pnt.sI === sI - 1) {
pnt.rI = qrI;
return lex.bad('unterminated_string', qI, sI);
}
const tkn = lex.token('#ST',
// s.join(EMPTY),
s.join(''), src.substring(pnt.sI, sI), pnt);
pnt.sI = sI;
pnt.rI = rI;
pnt.cI = cI;
return tkn;
}
};
};
}
// Default option values.
Csv.defaults = {
// Trim surrounding space. Default: false (!strict=>true)
// trim$: 'Trim surrounding space',
trim: null,
// Support comments. Default: false (!strict=>true)
comment: null,
// Support numbers. Default: false (!strict=>true)
number: null,
// Support exact values (such as booleans). Default: false (!strict=>true)
value: null,
// First row is headers.
header: true,
// Records are returned as objects. If false, as arrays.
object: true,
// Stream records.
stream: null,
// Parse standard CSV, ignoring embedded JSON. Default: false.
// When true, changes some defaults, e.g. trim=>true
strict: true,
// Control field handling
field: {
// Separator string (only one!)
separation: null,
// Create numbered names for extra fields found in a record.
nonameprefix: 'field~',
// Value to insert for empty fields.
empty: '',
// Predefined field names (string[]).
names: undefined,
// Require each row to have an exact number of fields (same number as headers).
exact: false,
},
// Control record handling.
record: {
// Separator characters (not string!)
separators: null,
// Allow empty lines to generate records.
empty: false,
},
// Control string handling.
string: {
// Quote character for CSV-style strings.
quote: '"',
// If false, use Jsonic-style strings.
csv: null,
},
};
//# sourceMappingURL=csv.js.map