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

@@ -0,0 +1,77 @@
import { useDocument } from '@terrace/core'
import { createStringReader } from '@terrace/core/readers/js-string'
export async function parse(lines) {
const { tail, each, match, buildObject } = useDocument(createStringReader(lines))
const structure = {
name: null,
version: null,
license: null,
exports: null,
scripts: null,
devDependencies: null,
author: null
}
await each(async () => {
if (match('name')) structure.name = tail().trim()
if (match('version')) structure.version = tail().trim()
if (match('license')) structure.license = tail().trim()
// FIXME: Order of operations causes other parts to break if this doesn't run first?!
if (match('exports')) structure.exports = await parseObjectKV(null, async () => {
const section = { import: null, require: null }
await each(() => {
if (match('import')) section.import = tail().trim()
if (match('require')) section.require = tail().trim()
if (section.import && section.require) return true
})
return section
})
if (match('scripts')) structure.scripts = await buildObject()
if (match('devDependencies')) structure.devDependencies = await buildObject()
if (match('author')) structure.author = await buildObject(['name', 'email', '#text'])
return structure.name &&
structure.version &&
structure.license &&
structure.exports &&
structure.scripts &&
structure.devDependencies &&
structure.author
})
return structure
}
export async function toArrays(lines) {
const { next, level, line } = useDocument(createStringReader(lines))
const levelTracker = []
function createScope(level, line) {
levelTracker.length = level
const scope = levelTracker[level] = [line, []]
return scope
}
createScope(0, 'root')
// Simple parser that produces canonical array structure for blocks.
while (true) {
// If next() returns true we've ended the document.
if (await next()) break;
// Determine parent for this scope.
const parent = levelTracker[level()]
// If there's no parent, skip this line.
if (!parent) continue
// Create new scope
const scope = createScope(level() + 1, line())
// Add current scope to parent.
parent[1].push(scope)
}
return levelTracker[0]
}

View File

@@ -0,0 +1,109 @@
import { parse, toArrays } from './core.js'
const linesArrays = [
`title Example`,
`options`,
` parameter1 30`,
` parameter2 Enim eu id anim minim reprehenderit nostrud eu amet deserunt ea ut do cupidatat ea.`,
`options`,
` parameter1 0`,
` parameter2 Esse incididunt et est adipisicing eiusmod aliqua enim ea aliqua id enim.`,
` deep Enim fugiat do in est commodo culpa dolore.`,
`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.`,
` Ea dolore in aliquip fugiat anim adipisicing amet aute tempor et deserunt est duis sint.`,
`list`,
` - item 1`,
` - item 2`,
`collection`,
` section`,
` lorem ipsum 1`,
` section`,
` lorem ipsum 2`,
`collection2`,
` section`,
` position 3`,
` Laborum aute anim occaecat occaecat pariatur tempor proident magna sit magna non non.`,
` list`,
` 1`,
` 2`
]
const linesParse = [
`name @terrace/core`,
`version 0.0.1`,
`randomthing test`,
`license MIT`,
`license GPL`,
`exports`,
` .`,
` import ./dist/index.js`,
` require ./dist/index.cjs`,
` ./parser`,
` import ./dist/parser.js`,
` require ./dist/parser.cjs`,
``,
` ./document`,
` import ./dist/document.js`,
` require ./dist/document.cjs`,
``,
` ./readers/node-readline`,
` import ./dist/readers/node-readline.js`,
` require ./dist/readers/node-readline.cjs`,
``,
` ./readers/js-string`,
` import ./dist/readers/js-string.js`,
` require ./dist/readers/js-string.cjs`,
`scripts`,
` test vitest ./src`,
` build vite build`,
`devDependencies`,
` vite ^3.2.3`,
` vitest ^0.24.5`,
``,
`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.`,
]
const schema = {
"name": {count: 1},
"version": {count: 1},
"license": {count: 1},
"exports": {count: 1, children: {
"?": {count: -1, children: {
"import": {count: 1},
"require": {count: 1}
}}
}},
"scripts": {count: 1, children: {
"?": { count: -1 }
}},
"devDependencies": {count: 1, children: {
"?": { count: -1 }
}},
"author": { count: 1, children: {
"name": { count: 1 },
"email": { count: 1 },
"? literal": { count: -1 }
}}
}
async function main() {
const resultArrays = await toArrays(linesArrays)
// console.dir(resultArrays, { depth: null })
const resultParse = await parse(linesParse)
console.dir(resultParse, { depth: null })
}
main()