You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB
JavaScript

"use strict";
let parseableString = "MAGIC[4]toot[5]hello[5]world[1]!END";
let chunk1 = parseableString.slice(0, 10);
let chunk2 = parseableString.slice(10);
// FIXME: bytes
// FIXME: emit for streaming parse? instead of return at the end
// FIXME: How to deal with eg. regexes when the match extends beyond the buffer?
function* Root() {
yield "MAGIC";
let items = yield repeat(Item);
yield "END";
yield EndOfInput;
return items;
}
function* Item() {
yield "[";
let length = yield Integer();
yield "]";
let contents = yield read(length);
return contents;
}
function* Integer() {
// FIXME: auto anchor
let [ match ] = yield /[0-9]+/;
return parseInt(match);
}
function* parseByte(context) {
let position = context.position;
// FIXME: detect end
context.position += 1;
return context.input[position];
}
function* parseBytes(context, length) {
let bytes = Buffer.alloc(length);
for (let i = 0; i < length; i++) {
bytes[i] = parseByte(context); // FIXME
}
return bytes;
}
// TODO: parseBytesUntil, parseStringUntil? or a generic toString wrapper function instead?
function* parseString(context, length) {
}