Experimenting with parser further.
This commit is contained in:
parent
f7a37681de
commit
e07e3a837c
1
docs/experiments/.gitignore
vendored
Normal file
1
docs/experiments/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules/
|
@ -1,9 +1,48 @@
|
||||
import { createLineData, useDocument } from '@terrace/core'
|
||||
import { createStringReader } from '@terrace/core/readers/js-string'
|
||||
|
||||
async function main() {
|
||||
const schema = {
|
||||
types: {
|
||||
subsection: {
|
||||
type: 'object',
|
||||
text: 'content',
|
||||
tail: 'number',
|
||||
values: {
|
||||
position: {
|
||||
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 lines = [
|
||||
`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.`,
|
||||
@ -12,8 +51,89 @@ async function main() {
|
||||
` Aute deserunt incididunt ad in sint adipisicing est officia velit pariatur ipsum deserunt quis nulla.`
|
||||
]
|
||||
|
||||
const rootDoc = useDocument(createStringReader(lines))
|
||||
console.log(await rootDoc.seek('subsection').then(r => r.content()))
|
||||
|
||||
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()
|
||||
|
4
packages/js/core/dist/document.cjs
vendored
4
packages/js/core/dist/document.cjs
vendored
@ -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(`
|
||||
`):(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;
|
||||
"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(((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;
|
||||
|
21
packages/js/core/dist/document.js
vendored
21
packages/js/core/dist/document.js
vendored
@ -1,18 +1,18 @@
|
||||
import { parseLine as f, createLineData as s } from "./parser.js";
|
||||
function d(t, l = " ") {
|
||||
function d(i, l = " ") {
|
||||
let n = s(null, l);
|
||||
const i = {
|
||||
const t = {
|
||||
ended: !1,
|
||||
clone() {
|
||||
return d(t.clone(), l);
|
||||
return d(i.clone(), l);
|
||||
},
|
||||
async next() {
|
||||
if (n.line = await t.next(), n.line === null)
|
||||
if (n.line = await i.next(), n.line === null)
|
||||
return !0;
|
||||
f(n);
|
||||
},
|
||||
current() {
|
||||
return i;
|
||||
return t;
|
||||
},
|
||||
line() {
|
||||
var e;
|
||||
@ -26,16 +26,19 @@ function d(t, l = " ") {
|
||||
var e;
|
||||
return (e = n.line) == null ? void 0 : e.slice(n.offsetTail);
|
||||
},
|
||||
level() {
|
||||
return n.level;
|
||||
},
|
||||
async content(e = -1, r = []) {
|
||||
var u;
|
||||
return e === -1 && (e = n.level + 1), await i.next() || n.level < e ? r.join(`
|
||||
`) : (r.push(((u = n.line) == null ? void 0 : u.slice(e)) || ""), i.content(e, r));
|
||||
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)) || ""), t.content(e, r));
|
||||
},
|
||||
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 {
|
||||
d as useDocument
|
||||
|
@ -45,6 +45,10 @@ export function useDocument (reader: Reader, indent: string = ' '): Document {
|
||||
return lineData.line?.slice(lineData.offsetTail)
|
||||
},
|
||||
|
||||
level () {
|
||||
return lineData.level
|
||||
},
|
||||
|
||||
async content (contentLevel = -1, lines: string[] = []): Promise<string> {
|
||||
if (contentLevel === -1) contentLevel = lineData.level + 1
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user