Add initial support for tails.

This commit is contained in:
Joshua Bemenderfer 2023-01-30 21:56:08 -05:00
parent d568c3efe8
commit e5244ba2b0
7 changed files with 1294 additions and 36 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
/experiments /experiments/
node_modules/

View File

@ -37,7 +37,7 @@ const lines = [
` author`, ` author`,
` name Joshua Bemenderfer`, ` name Joshua Bemenderfer`,
` email josh@thederf.com`, ` email josh@thederf.com`,
` `, ``,
` Further comments below. As I will now demonstrate, there is no simple`, ` Further comments below. As I will now demonstrate, there is no simple`,
` even if embedded`, ` even if embedded`,
` way of dealing with this problem.`, ` way of dealing with this problem.`,

View File

@ -1 +1 @@
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const m=require("./parser.cjs");function g(w,b=" "){let c=0;const i=m.createLineData("",b);async function f(){switch(c){case 0:c=1;break;case 2:return c=1,!1}const e=await w.next();return e===null?(c=3,!0):(i.line=e,m.parseLine(i),!1)}const p=()=>c=2,o=()=>i.level,d=()=>i.line.slice(i.offsetHead),u=()=>i.line.slice(i.offsetHead,i.offsetTail),s=()=>i.line.slice(i.offsetTail),j=e=>e===u();async function y(e){const n=c===0?-1:o();for(;;){if(await f())return;if(o()<=n)return p();if(await e())return}}async function h(e=-1,n=[d()]){var r;return e===-1&&(e=o()+1),await f()?n:o()<e?(p(),n):(n.push(((r=i.line)==null?void 0:r.slice(e))||""),h(e,n))}async function x(e={},n=!1){const r=[];let t={};return Object.keys(e).length?Object.keys(e).forEach(a=>{e[a]===!0&&(t[a]={type:n?"collection":"normal",handle:()=>s().trim()}),typeof e[a]=="function"&&(t[a]={type:n?"collection":"normal",handle:e[a]}),typeof e[a]=="object"&&(t[a]=e[a])}):t={"#any":{type:n?"collection":"normal",handle:()=>s().trim()}},await y(async()=>{const a=u();if(!a)return;const l=t[a]||t["#any"];!l||(l.type==="normal"?r.push(await l.handle()):l.type==="collection"&&r.push({[a]:await l.handle()}))}),r}async function O(e={}){const n={};let r={};return Object.keys(e).length?Object.keys(e).forEach(t=>{t==="#text"?r[t]={type:"text",handle:()=>{}}:e[t]===!0?r[t]={type:"normal",handle:()=>s().trim()}:typeof e[t]=="function"?r[t]={type:"normal",handle:e[t]}:typeof e[t]=="object"&&(r[t]=e[t])}):r={"#any":{type:"normal",handle:()=>s().trim()}},await y(async()=>{const t=u();if(!t)return;const a=r[t]||r["#any"]||r["#text"];if(!!a&&(a.type==="normal"?n[t]=await a.handle():a.type==="collection"?(n[t]||(n[t]=[]),n[t].push(await a.handle())):a.type==="text"&&(n["#text"]=await h(o())),r&&Object.keys(r).every(l=>["collection"].includes(r[l].type)?!1:n[l]!==void 0)))return!0}),n}async function v(){const e=[["root",[]]];for(;!await f();){const n=o(),r=n+1,t=e[n];if(!t)continue;e.length=r;const a=e[r]=[d(),[]];t[1].push(a)}return e[0]}return{next:f,line:d,head:u,tail:s,level:o,match:j,each:y,blockAsText:h,toObject:O,toArray:x,toLineArray:v}}exports.useDocument=g; "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const m=require("./parser.cjs");function g(w,b=" "){let c=0;const i=m.createLineData("",b);async function f(){switch(c){case 0:c=1;break;case 2:return c=1,!1}const e=await w.next();return e===null?(c=3,!0):(i.line=e,m.parseLine(i),!1)}const y=()=>c=2,o=()=>i.level,d=()=>i.line.slice(i.offsetHead),u=()=>i.line.slice(i.offsetHead,i.offsetTail),s=()=>i.line.slice(i.offsetTail),j=e=>e===u();async function h(e){const a=c===0?-1:o();for(;;){if(await f())return;if(o()<=a)return y();if(await e())return}}async function p(e=-1,a=[d()]){var n;return e===-1&&(e=o()+1),await f()?a:o()<e?(y(),a):(a.push(((n=i.line)==null?void 0:n.slice(e))||""),p(e,a))}async function x(e={},a=!1){const n=[];let t={};return Object.keys(e).length?Object.keys(e).forEach(r=>{e[r]===!0&&(t[r]={type:a?"collection":"normal",handle:()=>s().trim()}),typeof e[r]=="function"&&(t[r]={type:a?"collection":"normal",handle:e[r]}),typeof e[r]=="object"&&(t[r]=e[r])}):t={"#any":{type:a?"collection":"normal",handle:()=>s().trim()}},await h(async()=>{const r=u();if(!r)return;const l=t[r]||t["#any"];!l||(l.type==="normal"?n.push(await l.handle()):l.type==="collection"&&n.push({[r]:await l.handle()}))}),n}async function O(e={}){const a={};let n={};return Object.keys(e).length?Object.keys(e).forEach(t=>{t==="#tail"?n[t]={type:"tail",handle:()=>{}}:t==="#text"?n[t]={type:"text",handle:()=>{}}:e[t]===!0?n[t]={type:"normal",handle:()=>s().trim()}:typeof e[t]=="function"?n[t]={type:"normal",handle:e[t]}:typeof e[t]=="object"&&(n[t]=e[t])}):n={"#any":{type:"normal",handle:()=>s().trim()}},n["#tail"]&&(a["#tail"]=s().trim()),await h(async()=>{const t=u();if(!t)return;const r=n[t]||n["#any"]||n["#text"];if(!!r&&(r.type==="normal"?a[t]=await r.handle():r.type==="collection"?(a[t]||(a[t]=[]),a[t].push(await r.handle())):r.type==="text"&&(a["#text"]=await p(o())),n&&Object.keys(n).every(l=>["collection"].includes(n[l].type)?!1:a[l]!==void 0)))return!0}),a}async function v(){const e=[["root",[]]];for(;!await f();){const a=o(),n=a+1,t=e[a];if(!t)continue;e.length=n;const r=e[n]=[d(),[]];t[1].push(r)}return e[0]}return{next:f,line:d,head:u,tail:s,level:o,match:j,each:h,blockAsText:p,toObject:O,toArray:x,toLineArray:v}}exports.useDocument=g;

View File

@ -1,5 +1,5 @@
import { parseLine as v, createLineData as H } from "./parser.js"; import { parseLine as v, createLineData as H } from "./parser.js";
function g(p, w = " ") { function g(y, w = " ") {
let c = 0; let c = 0;
const i = H("", w); const i = H("", w);
async function s() { async function s() {
@ -10,74 +10,75 @@ function g(p, w = " ") {
case 2: case 2:
return c = 1, !1; return c = 1, !1;
} }
const e = await p.next(); const e = await y.next();
return e === null ? (c = 3, !0) : (i.line = e, v(i), !1); return e === null ? (c = 3, !0) : (i.line = e, v(i), !1);
} }
const m = () => c = 2, l = () => i.level, y = () => i.line.slice(i.offsetHead), u = () => i.line.slice(i.offsetHead, i.offsetTail), f = () => i.line.slice(i.offsetTail), b = (e) => e === u(); const p = () => c = 2, o = () => i.level, h = () => i.line.slice(i.offsetHead), u = () => i.line.slice(i.offsetHead, i.offsetTail), f = () => i.line.slice(i.offsetTail), b = (e) => e === u();
async function h(e) { async function d(e) {
const n = c === 0 ? -1 : l(); const a = c === 0 ? -1 : o();
for (; ; ) { for (; ; ) {
if (await s()) if (await s())
return; return;
if (l() <= n) if (o() <= a)
return m(); return p();
if (await e()) if (await e())
return; return;
} }
} }
async function d(e = -1, n = [y()]) { async function m(e = -1, a = [h()]) {
var a; var n;
return e === -1 && (e = l() + 1), await s() ? n : l() < e ? (m(), n) : (n.push(((a = i.line) == null ? void 0 : a.slice(e)) || ""), d(e, n)); return e === -1 && (e = o() + 1), await s() ? a : o() < e ? (p(), a) : (a.push(((n = i.line) == null ? void 0 : n.slice(e)) || ""), m(e, a));
} }
async function j(e = {}, n = !1) { async function j(e = {}, a = !1) {
const a = []; const n = [];
let t = {}; let t = {};
return Object.keys(e).length ? Object.keys(e).forEach((r) => { return Object.keys(e).length ? Object.keys(e).forEach((r) => {
e[r] === !0 && (t[r] = { type: n ? "collection" : "normal", handle: () => f().trim() }), typeof e[r] == "function" && (t[r] = { type: n ? "collection" : "normal", handle: e[r] }), typeof e[r] == "object" && (t[r] = e[r]); e[r] === !0 && (t[r] = { type: a ? "collection" : "normal", handle: () => f().trim() }), typeof e[r] == "function" && (t[r] = { type: a ? "collection" : "normal", handle: e[r] }), typeof e[r] == "object" && (t[r] = e[r]);
}) : t = { "#any": { type: n ? "collection" : "normal", handle: () => f().trim() } }, await h(async () => { }) : t = { "#any": { type: a ? "collection" : "normal", handle: () => f().trim() } }, await d(async () => {
const r = u(); const r = u();
if (!r) if (!r)
return; return;
const o = t[r] || t["#any"]; const l = t[r] || t["#any"];
!o || (o.type === "normal" ? a.push(await o.handle()) : o.type === "collection" && a.push({ [r]: await o.handle() })); !l || (l.type === "normal" ? n.push(await l.handle()) : l.type === "collection" && n.push({ [r]: await l.handle() }));
}), a; }), n;
} }
async function x(e = {}) { async function x(e = {}) {
const n = {}; const a = {};
let a = {}; let n = {};
return Object.keys(e).length ? Object.keys(e).forEach((t) => { return Object.keys(e).length ? Object.keys(e).forEach((t) => {
t === "#text" ? a[t] = { type: "text", handle: () => { t === "#tail" ? n[t] = { type: "tail", handle: () => {
} } : e[t] === !0 ? a[t] = { type: "normal", handle: () => f().trim() } : typeof e[t] == "function" ? a[t] = { type: "normal", handle: e[t] } : typeof e[t] == "object" && (a[t] = e[t]); } } : t === "#text" ? n[t] = { type: "text", handle: () => {
}) : a = { "#any": { type: "normal", handle: () => f().trim() } }, await h(async () => { } } : e[t] === !0 ? n[t] = { type: "normal", handle: () => f().trim() } : typeof e[t] == "function" ? n[t] = { type: "normal", handle: e[t] } : typeof e[t] == "object" && (n[t] = e[t]);
}) : n = { "#any": { type: "normal", handle: () => f().trim() } }, n["#tail"] && (a["#tail"] = f().trim()), await d(async () => {
const t = u(); const t = u();
if (!t) if (!t)
return; return;
const r = a[t] || a["#any"] || a["#text"]; const r = n[t] || n["#any"] || n["#text"];
if (!!r && (r.type === "normal" ? n[t] = await r.handle() : r.type === "collection" ? (n[t] || (n[t] = []), n[t].push(await r.handle())) : r.type === "text" && (n["#text"] = await d(l())), a && Object.keys(a).every((o) => ["collection"].includes(a[o].type) ? !1 : n[o] !== void 0))) if (!!r && (r.type === "normal" ? a[t] = await r.handle() : r.type === "collection" ? (a[t] || (a[t] = []), a[t].push(await r.handle())) : r.type === "text" && (a["#text"] = await m(o())), n && Object.keys(n).every((l) => ["collection"].includes(n[l].type) ? !1 : a[l] !== void 0)))
return !0; return !0;
}), n; }), a;
} }
async function O() { async function O() {
const e = [["root", []]]; const e = [["root", []]];
for (; !await s(); ) { for (; !await s(); ) {
const n = l(), a = n + 1, t = e[n]; const a = o(), n = a + 1, t = e[a];
if (!t) if (!t)
continue; continue;
e.length = a; e.length = n;
const r = e[a] = [y(), []]; const r = e[n] = [h(), []];
t[1].push(r); t[1].push(r);
} }
return e[0]; return e[0];
} }
return { return {
next: s, next: s,
line: y, line: h,
head: u, head: u,
tail: f, tail: f,
level: l, level: o,
match: b, match: b,
each: h, each: d,
blockAsText: d, blockAsText: m,
toObject: x, toObject: x,
toArray: j, toArray: j,
toLineArray: O toLineArray: O

View File

@ -134,7 +134,8 @@ export function useDocument (reader: Reader, indent: string = ' '): Document {
matchers = { '#any': { type: 'normal', handle: () => tail().trim() } } matchers = { '#any': { type: 'normal', handle: () => tail().trim() } }
} else { } else {
Object.keys(inputMatchers).forEach(key => { Object.keys(inputMatchers).forEach(key => {
if (key === '#text') matchers[key] = { type: 'text', handle: () => {} } if (key === '#tail') matchers[key] = { type: 'tail', handle: () => {} }
else if (key === '#text') matchers[key] = { type: 'text', handle: () => {} }
// If a matcher is specified as `true`, treat as a key-value pair where { [head]: tail } // If a matcher is specified as `true`, treat as a key-value pair where { [head]: tail }
else if (inputMatchers[key] === true) matchers[key] = { type: 'normal', handle: () => tail().trim() } else if (inputMatchers[key] === true) matchers[key] = { type: 'normal', handle: () => tail().trim() }
// If a matcher is specified as a function, treat as a key-value pair where { [head]: handle() } // If a matcher is specified as a function, treat as a key-value pair where { [head]: handle() }
@ -144,6 +145,9 @@ export function useDocument (reader: Reader, indent: string = ' '): Document {
}) })
} }
// TODO: Rework this so it fits bettwer with the model already established here.
if (matchers['#tail']) obj['#tail'] = tail().trim()
await each(async () => { await each(async () => {
const currHead = head() const currHead = head()
if (!currHead) return if (!currHead) return

1249
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

3
pnpm-workspace.yaml Normal file
View File

@ -0,0 +1,3 @@
packages:
- "./packages/js/*"
- "./experiments/*"