Start on parser v4.

This commit is contained in:
Joshua Bemenderfer
2023-01-29 17:25:43 -05:00
parent ef3c59fb74
commit b87fdfbd83
6 changed files with 145 additions and 97 deletions

View File

@@ -1 +1 @@
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const s=require("./parser.cjs");function a(l,u=" "){let r=s.createLineData(null,u);const t={ended:!1,clone(){return a(l.clone(),u)},async next(){if(r.line=await l.next(),r.line===null)return!0;s.parseLine(r)},current(){return t},line(){var e;return(e=r.line)==null?void 0:e.slice(r.offsetHead)},head(){var e;return(e=r.line)==null?void 0:e.slice(r.offsetHead,r.offsetTail)},tail(){var e;return(e=r.line)==null?void 0:e.slice(r.offsetTail)},level(){return r.level},async content(e=-1,n=[]){var i;return e===-1&&(e=r.level+1),await t.next()||r.level<e?n:(n.push(((i=r.line)==null?void 0:i.slice(e))||""),t.content(e,n))},async seek(e,n=-1){return n===-1&&(n=r.level),await t.next()?!1:t.head()===e?t:r.level<n?!1:t.seek(e,n)}};return t}exports.useDocument=a;
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("./parser.cjs");function s(l,a=" "){let r=u.createLineData(null,a);const t={ended:!1,clone(){return s(l.clone(),a)},async next(){if(r.line=await l.next(),r.line===null)return!0;u.parseLine(r)},current(){return t},line(){var e;return(e=r.line)==null?void 0:e.slice(r.offsetHead)},head(){var e;return(e=r.line)==null?void 0:e.slice(r.offsetHead,r.offsetTail)},tail(){var e;return(e=r.line)==null?void 0:e.slice(r.offsetTail)},level(){return r.level},async content(e=-1,n=[]){var i;return e===-1&&(e=r.level+1),await t.next()||r.level<e?n:(n.push(((i=r.line)==null?void 0:i.slice(e))||""),t.content(e,n))},match(e){return e===t.head()},async each(e){const n=t.line()!==void 0?t.level():-1;for(;!(await t.next()||t.level()<=n||await e()););},async seek(e,n=-1){return n===-1&&(n=r.level),await t.next()?!1:t.head()===e?t:r.level<n?!1:t.seek(e,n)}};return t}exports.useDocument=s;

View File

@@ -1,10 +1,10 @@
import { parseLine as f, createLineData as s } from "./parser.js";
function d(l, i = " ") {
let r = s(null, i);
const t = {
function c(l, a = " ") {
let r = s(null, a);
const n = {
ended: !1,
clone() {
return d(l.clone(), i);
return c(l.clone(), a);
},
async next() {
if (r.line = await l.next(), r.line === null)
@@ -12,7 +12,7 @@ function d(l, i = " ") {
f(r);
},
current() {
return t;
return n;
},
line() {
var e;
@@ -29,16 +29,24 @@ function d(l, i = " ") {
level() {
return r.level;
},
async content(e = -1, n = []) {
var u;
return e === -1 && (e = r.level + 1), await t.next() || r.level < e ? n : (n.push(((u = r.line) == null ? void 0 : u.slice(e)) || ""), t.content(e, n));
async content(e = -1, t = []) {
var i;
return e === -1 && (e = r.level + 1), await n.next() || r.level < e ? t : (t.push(((i = r.line) == null ? void 0 : i.slice(e)) || ""), n.content(e, t));
},
async seek(e, n = -1) {
return n === -1 && (n = r.level), await t.next() ? !1 : t.head() === e ? t : r.level < n ? !1 : t.seek(e, n);
match(e) {
return e === n.head();
},
async each(e) {
const t = n.line() !== void 0 ? n.level() : -1;
for (; !(await n.next() || n.level() <= t || await e()); )
;
},
async seek(e, t = -1) {
return t === -1 && (t = r.level), await n.next() ? !1 : n.head() === e ? n : r.level < t ? !1 : n.seek(e, t);
}
};
return t;
return n;
}
export {
d as useDocument
c as useDocument
};

View File

@@ -10,6 +10,9 @@ type Document = {
head: () => string,
tail: () => string,
content: (contentLevel: number, lines: string[]) => Promise<string>,
match: (matchHead: string) => boolean,
each: (handler: Function) => void,
buildObject: (allowList: Array<string>, handler: Function) => object,
seek: (matchHead: string, contentLevel: number) => Promise<Document|false>
}
@@ -61,6 +64,42 @@ export function useDocument (reader: Reader, indent: string = ' '): Document {
return document.content(contentLevel, lines)
},
match(matchHead: string): boolean {
return matchHead === document.head()
},
async each(handler: Function) {
// Set startLevel to -1 if we haven't started parsing the document yet.
// Otherwise we'll break to early, as the default value for doc.level() is 0.
const startLevel = document.line() !== undefined ? document.level() : -1
while(true) {
if (await document.next()) break
if (document.level() <= startLevel) break
if (await handler()) break
}
},
async buildObject(allowList = [], valHandler?: () => any) {
if (!valHandler) valHandler = () => document.tail().trim()
const obj = {}
await document.each(async () => {
if (!document.head()) return
if (!allowList.length || allowList.includes(document.head())) {
obj[document.head()] = await valHandler()
return
}
// Parse unspecified text into an array of lines and save to #text key.
if (allowList && !allowList.includes(document.head()) && allowList.includes('#text')) {
obj['#text'] = [document.line(), ...await document.content(document.level())]
return
}
})
return obj
},
async seek (matchHead: string, contentLevel = -1): Promise<Document|false> {
if (contentLevel === -1) contentLevel = lineData.level