Experimenting with parser further.

This commit is contained in:
Joshua Bemenderfer 2022-11-13 09:50:23 -05:00
parent f7a37681de
commit e07e3a837c
5 changed files with 151 additions and 23 deletions

1
docs/experiments/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

View File

@ -1,19 +1,139 @@
import { createLineData, useDocument } from '@terrace/core' import { createLineData, useDocument } from '@terrace/core'
import { createStringReader } from '@terrace/core/readers/js-string' import { createStringReader } from '@terrace/core/readers/js-string'
async function main() { const schema = {
const lines = [ types: {
`title Example`, subsection: {
`subsection`, type: 'object',
` position 1`, text: 'content',
` Ea dolore in aliquip fugiat anim adipisicing amet aute tempor et deserunt est duis sint.`, tail: 'number',
`subsection 2`, values: {
` position 2`, position: {
` Aute deserunt incididunt ad in sint adipisicing est officia velit pariatur ipsum deserunt quis nulla.` type: 'number'
] }
}
},
options: {
type: 'object',
values: {
parameter1: 'number',
parameter2: 'string'
}
}
},
root: {
type: 'object',
values: {
title: 'string',
options: 'options',
options2: 'options',
subsection: {
type: 'collection',
collection: 'subsection'
}
}
}
}
const rootDoc = useDocument(createStringReader(lines)) const lines = [
console.log(await rootDoc.seek('subsection').then(r => r.content())) `title Example`,
`options`,
` parameter1 30`,
` parameter2 Enim eu id anim minim reprehenderit nostrud eu amet deserunt ea ut do cupidatat ea.`,
`options2`,
` parameter1 0`,
` parameter2 Esse incididunt et est adipisicing eiusmod aliqua enim ea aliqua id enim.`,
`subsection`,
` position 1`,
` Ea dolore in aliquip fugiat anim adipisicing amet aute tempor et deserunt est duis sint.`,
`subsection 2`,
` position 2`,
` Aute deserunt incididunt ad in sint adipisicing est officia velit pariatur ipsum deserunt quis nulla.`
]
const typeHandlers = {
string (key, definition, add) {
return (doc, handlers) => {
if (doc.head() !== key) return
add(key, doc.tail().slice(1))
}
},
number (key, definition, add) {
return (doc, handlers) => {
if (doc.head() !== key) return
add(key, +doc.tail().slice(1))
}
},
object (key, definition, add) {
definition = resolve(definition)
return (doc, handlers) => {
if (doc.head() !== key) return
const object = {}
add(key, object)
for (const [key, child] of Object.entries(definition.values)) {
addHandler(handlers, doc.level() + 1, key, child, (key, value) => (object[key] = value))
}
}
},
collection (key, definition, add) {
return (doc, handlers) => {
if (doc.head() !== key) return
const collection = []
add(key, collection)
const child = resolve(definition.collection)
addHandler(handlers, doc.level() + 1, '*', child, (key, value) => ( collection.push(value) ))
}
}
}
function resolve (definition) {
const type = typeof definition === 'string' ? definition : definition.type
if (schema.types[type]) return schema.types[type]
else return definition
}
function registerTypes(typeHandlers, types) {
for (const [key, definition] of Object.entries(types)) {
const existingType = typeof definition === 'string' ? definition : definition.type
typeHandlers[key] = typeHandlers[existingType]
}
}
function addHandler(handlers, level, key, definition, add) {
const type = typeof definition === 'string' ? definition : definition.type
if (!handlers[level]) handlers[level] = []
handlers[level].push(typeHandlers[type](key, definition, add))
}
async function main() {
const doc = useDocument(createStringReader(lines))
const handlers = []
registerTypes(typeHandlers, schema.types)
const root = {}
for (const [key, child] of Object.entries(schema.root.values)) {
addHandler(handlers, 0, key, child, (key, value) => (root[key] = value))
}
let ended = false
let lastLevel = 0
while (!ended) {
ended = await doc.next()
if (ended) break;
const level = doc.level()
handlers.length = level + 1
for (const handler of handlers[level] || []) {
handler(doc, handlers)
}
}
console.log(root)
} }
main() main()

View File

@ -1,2 +1,2 @@
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const s=require("./parser.cjs");function a(i,u=" "){let r=s.createLineData(null,u);const t={ended:!1,clone(){return a(i.clone(),u)},async next(){if(r.line=await i.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)},async content(e=-1,n=[]){var l;return e===-1&&(e=r.level+1),await t.next()||r.level<e?n.join(` "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.join(`
`):(n.push(((l=r.line)==null?void 0:l.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; `):(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;

View File

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

View File

@ -45,6 +45,10 @@ export function useDocument (reader: Reader, indent: string = ' '): Document {
return lineData.line?.slice(lineData.offsetTail) return lineData.line?.slice(lineData.offsetTail)
}, },
level () {
return lineData.level
},
async content (contentLevel = -1, lines: string[] = []): Promise<string> { async content (contentLevel = -1, lines: string[] = []): Promise<string> {
if (contentLevel === -1) contentLevel = lineData.level + 1 if (contentLevel === -1) contentLevel = lineData.level + 1