Add initial support for tails.
This commit is contained in:
parent
d568c3efe8
commit
e5244ba2b0
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/experiments
|
||||
/experiments/
|
||||
node_modules/
|
||||
|
@ -37,7 +37,7 @@ const lines = [
|
||||
` author`,
|
||||
` name Joshua Bemenderfer`,
|
||||
` email josh@thederf.com`,
|
||||
` `,
|
||||
``,
|
||||
` Further comments below. As I will now demonstrate, there is no simple`,
|
||||
` even if embedded`,
|
||||
` way of dealing with this problem.`,
|
||||
|
2
packages/js/core/dist/document.cjs
vendored
2
packages/js/core/dist/document.cjs
vendored
@ -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;
|
||||
|
65
packages/js/core/dist/document.js
vendored
65
packages/js/core/dist/document.js
vendored
@ -1,5 +1,5 @@
|
||||
import { parseLine as v, createLineData as H } from "./parser.js";
|
||||
function g(p, w = " ") {
|
||||
function g(y, w = " ") {
|
||||
let c = 0;
|
||||
const i = H("", w);
|
||||
async function s() {
|
||||
@ -10,74 +10,75 @@ function g(p, w = " ") {
|
||||
case 2:
|
||||
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);
|
||||
}
|
||||
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();
|
||||
async function h(e) {
|
||||
const n = c === 0 ? -1 : l();
|
||||
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 d(e) {
|
||||
const a = c === 0 ? -1 : o();
|
||||
for (; ; ) {
|
||||
if (await s())
|
||||
return;
|
||||
if (l() <= n)
|
||||
return m();
|
||||
if (o() <= a)
|
||||
return p();
|
||||
if (await e())
|
||||
return;
|
||||
}
|
||||
}
|
||||
async function d(e = -1, n = [y()]) {
|
||||
var a;
|
||||
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));
|
||||
async function m(e = -1, a = [h()]) {
|
||||
var 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) {
|
||||
const a = [];
|
||||
async function j(e = {}, a = !1) {
|
||||
const n = [];
|
||||
let t = {};
|
||||
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]);
|
||||
}) : t = { "#any": { type: n ? "collection" : "normal", handle: () => f().trim() } }, await h(async () => {
|
||||
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: a ? "collection" : "normal", handle: () => f().trim() } }, await d(async () => {
|
||||
const r = u();
|
||||
if (!r)
|
||||
return;
|
||||
const o = t[r] || t["#any"];
|
||||
!o || (o.type === "normal" ? a.push(await o.handle()) : o.type === "collection" && a.push({ [r]: await o.handle() }));
|
||||
}), a;
|
||||
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 x(e = {}) {
|
||||
const n = {};
|
||||
let a = {};
|
||||
const a = {};
|
||||
let n = {};
|
||||
return Object.keys(e).length ? Object.keys(e).forEach((t) => {
|
||||
t === "#text" ? a[t] = { type: "text", 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]);
|
||||
}) : a = { "#any": { type: "normal", handle: () => f().trim() } }, await h(async () => {
|
||||
t === "#tail" ? n[t] = { type: "tail", handle: () => {
|
||||
} } : t === "#text" ? n[t] = { type: "text", handle: () => {
|
||||
} } : 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();
|
||||
if (!t)
|
||||
return;
|
||||
const r = a[t] || a["#any"] || a["#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)))
|
||||
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 m(o())), n && Object.keys(n).every((l) => ["collection"].includes(n[l].type) ? !1 : a[l] !== void 0)))
|
||||
return !0;
|
||||
}), n;
|
||||
}), a;
|
||||
}
|
||||
async function O() {
|
||||
const e = [["root", []]];
|
||||
for (; !await s(); ) {
|
||||
const n = l(), a = n + 1, t = e[n];
|
||||
const a = o(), n = a + 1, t = e[a];
|
||||
if (!t)
|
||||
continue;
|
||||
e.length = a;
|
||||
const r = e[a] = [y(), []];
|
||||
e.length = n;
|
||||
const r = e[n] = [h(), []];
|
||||
t[1].push(r);
|
||||
}
|
||||
return e[0];
|
||||
}
|
||||
return {
|
||||
next: s,
|
||||
line: y,
|
||||
line: h,
|
||||
head: u,
|
||||
tail: f,
|
||||
level: l,
|
||||
level: o,
|
||||
match: b,
|
||||
each: h,
|
||||
blockAsText: d,
|
||||
each: d,
|
||||
blockAsText: m,
|
||||
toObject: x,
|
||||
toArray: j,
|
||||
toLineArray: O
|
||||
|
@ -134,7 +134,8 @@ export function useDocument (reader: Reader, indent: string = ' '): Document {
|
||||
matchers = { '#any': { type: 'normal', handle: () => tail().trim() } }
|
||||
} else {
|
||||
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 }
|
||||
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() }
|
||||
@ -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 () => {
|
||||
const currHead = head()
|
||||
if (!currHead) return
|
||||
|
1249
pnpm-lock.yaml
generated
Normal file
1249
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
3
pnpm-workspace.yaml
Normal file
3
pnpm-workspace.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
packages:
|
||||
- "./packages/js/*"
|
||||
- "./experiments/*"
|
Loading…
x
Reference in New Issue
Block a user