Start making more generic.
This commit is contained in:
114
docs/experiments/parsers/example.js
Normal file
114
docs/experiments/parsers/example.js
Normal file
@@ -0,0 +1,114 @@
|
||||
import { parse } from './core.js'
|
||||
|
||||
const schemaTCE = `
|
||||
types
|
||||
section object, text as content, tail as pos
|
||||
content string
|
||||
pos number
|
||||
position number
|
||||
options object
|
||||
parameter1 number
|
||||
parameter2 string
|
||||
|
||||
root object
|
||||
title string
|
||||
options options
|
||||
options2 options
|
||||
subsection section
|
||||
collate collection
|
||||
list array
|
||||
- string
|
||||
collection collection
|
||||
section section
|
||||
`
|
||||
|
||||
const schema = {
|
||||
types: {
|
||||
section: {
|
||||
type: 'object',
|
||||
text: 'content',
|
||||
tail: 'pos',
|
||||
values: {
|
||||
content: 'string',
|
||||
pos: 'number',
|
||||
position: {
|
||||
type: 'number'
|
||||
}
|
||||
}
|
||||
},
|
||||
options: {
|
||||
type: 'object',
|
||||
values: {
|
||||
parameter1: 'number',
|
||||
parameter2: 'string'
|
||||
}
|
||||
},
|
||||
advList: {
|
||||
type: 'array',
|
||||
values: {
|
||||
section: 'section'
|
||||
}
|
||||
}
|
||||
},
|
||||
root: {
|
||||
type: 'object',
|
||||
values: {
|
||||
title: 'string',
|
||||
options: 'options',
|
||||
options2: 'options',
|
||||
subsection: {
|
||||
type: 'section',
|
||||
// Allows a particular repeated key to collect itself under a multi-value root as an array.
|
||||
collate: 'collection'
|
||||
},
|
||||
list: {
|
||||
// Defines an array where all entries must conform to specific types. No other keys are permitted.
|
||||
// Ex: [ value, value ]
|
||||
type: 'array',
|
||||
values: {
|
||||
'-': 'string'
|
||||
}
|
||||
},
|
||||
collection: {
|
||||
// Defines an array where all entries must conform to specific types. They will be segregated by key. No other keys are permitted.
|
||||
// Ex: [ {key: value }, { key: value }]
|
||||
type: 'collection',
|
||||
values: {
|
||||
section: 'section'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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.`,
|
||||
`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`
|
||||
]
|
||||
|
||||
async function main() {
|
||||
console.log(await parse(lines, schema))
|
||||
}
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user