refactoring + cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2026-07-03 17:05:26 +03:00
parent 2d2345a589
commit e8b0cb435c
2 changed files with 111 additions and 131 deletions

View File

@ -253,6 +253,14 @@ module.BaseParser = {
if(typeof(blocks) == 'string' if(typeof(blocks) == 'string'
|| join == null){ || join == null){
return blocks } return blocks }
// we do not need to rebuild the ast for each use...
// XXX this can break things -- need to store the parse stage
// in the structure so as to determine where we left off...
// ...the problem is that we can't tell the difference
// between a parsed and expanded stages -- one way, re-running
// a stage is not an issue but the other way, skipping can
// break...
//join = this.ast(page, join, state)
return blocks return blocks
.map(function(block, i, l){ .map(function(block, i, l){
return [ return [
@ -305,7 +313,7 @@ module.BaseParser = {
// //
// NOTE: this internally uses .macros' keys to generate the // NOTE: this internally uses .macros' keys to generate the
// lexing pattern. // lexing pattern.
lex: function*(page, str){ lex: function*(str){
str = typeof(str) != 'string' ? str = typeof(str) != 'string' ?
str+'' str+''
: str : str
@ -434,9 +442,9 @@ module.BaseParser = {
// //
// NOTE: this internaly uses .macros to check for propper nesting // NOTE: this internaly uses .macros to check for propper nesting
//group: function*(page, lex, to=false){ //group: function*(page, lex, to=false){
group: function*(page, lex, to=false, parent, context){ group: function*(lex, to=false, parent, context){
lex = typeof(lex) != 'object' ? lex = typeof(lex) != 'object' ?
this.lex(page, lex) this.lex(lex)
: lex : lex
var quoting = to var quoting = to
@ -488,8 +496,8 @@ module.BaseParser = {
// open block... // open block...
if(value.type == 'opening'){ if(value.type == 'opening'){
//value.body = [...this.group(page, lex, value.name)] //value.body = [...this.group(lex, value.name)]
value.body = [...this.group(page, lex, value.name, value)] value.body = [...this.group(lex, value.name, value)]
value.type = 'block' value.type = 'block'
// unify .body, .args.body and .args.text into .body... // unify .body, .args.body and .args.text into .body...
@ -499,7 +507,6 @@ module.BaseParser = {
?? value.args.text)){ ?? value.args.text)){
value.body = value.body =
[...this.group( [...this.group(
page,
value.args.body value.args.body
?? value.args.text, ?? value.args.text,
false, false,
@ -593,7 +600,7 @@ module.BaseParser = {
expand: function(page, ast, state={}){ expand: function(page, ast, state={}){
var that = this var that = this
ast = typeof(ast) != 'object' ? ast = typeof(ast) != 'object' ?
this.group(page, ast) this.group(ast)
: ast instanceof types.Generator ? : ast instanceof types.Generator ?
ast ast
: ast.iter() : ast.iter()
@ -629,6 +636,7 @@ module.BaseParser = {
continue } continue }
// expand down... // expand down...
// XXX cache this as an AST
body body
&& !that.macros[name].lazy && !that.macros[name].lazy
&& (body = this.expand(page, body, state)) && (body = this.expand(page, body, state))
@ -702,7 +710,8 @@ module.BaseParser = {
// resolve stage II macros and merge results... // resolve stage II macros and merge results...
// //
/* XXX // XXX DNF
//
// XXX these yeild different results: // XXX these yeild different results:
// r = parser.resolve( // r = parser.resolve(
// tests.P, // tests.P,
@ -722,84 +731,6 @@ module.BaseParser = {
// XXX it looks like the first slot is resolved before the last // XXX it looks like the first slot is resolved before the last
// slot has a chance to set the value as it is waiting for // slot has a chance to set the value as it is waiting for
// the first slot to finish... // the first slot to finish...
resolve: function(page, ast, state={}){
var that = this
ast = typeof(ast) != 'object' ?
this.expand(page, ast, state)
: ast instanceof types.Generator ?
ast
: ast.iter()
// merge resolved elements into the last item of elems...
var elems = []
var merge = function(...args){
var prev = ''
while(args.length > 0){
while(args.length > 0
&& typeof(args[0]) != 'object'
&& typeof(args[0]) != 'function'){
prev += args.shift() }
// merged section...
if(prev.length > 0){
if(elems.length > 0
&& typeof(elems.at(-1)) == 'string'){
elems[elems.length-1] += prev
} else {
elems.push(prev) }
prev = '' }
if(args.length > 0){
elems.push(args.shift()) } } }
// XXX can we make this more granular and wait only where we need
// to wait?
// ....the wait is logical as we need to return a string here
// eventually.
// should this be split into .resolve(..) nad .merge(..)???
return Promise.awaitOrRun(
state.wait,
function(){
// XXX can ast be a promise???
// XXX elems can be unresolved -- need a merge strategy for them...
//var waiting = []
for(var elem of ast){
// nesting...
while(elem && elem.value){
elem = elem.value
// exec stage II macros...
// XXX this should be called when all the promises before
// are resolved...
if(typeof(elem) == 'function'){
elem = elem(state) } }
if(elem == null){
continue }
// atomic values...
if(typeof(elem) != 'object'){
merge(elem)
continue }
// value is resolved but "empty" -> skip...
if('value' in elem
&& (elem.value == null
|| elem.value == '')){
continue }
// expand ast...
if(elem instanceof Array){
// XXX this can be or containe promises...
merge(...that.resolve(page, elem, state))
continue }
// expand .body attribute...
// XXX is this needed here???
if(elem.body instanceof Array){
console.warn('!!! RESOLVE_BODY')
// XXX this can be or containe promises...
elem.body = that.resolve(page, elem.body, state) }
// nested macro with no value set -- skip...
if(that.macros[elem.name] instanceof Array){
continue }
// unresolved...
merge(elem) }
return elems }) },
//*/
resolve: function(page, ast, state={}){ resolve: function(page, ast, state={}){
var that = this var that = this
ast = typeof(ast) != 'object' ? ast = typeof(ast) != 'object' ?
@ -1534,6 +1465,8 @@ module.parser = {
// not 100% correct manner focusing on path depth and ignoring // not 100% correct manner focusing on path depth and ignoring
// the context, this potentially can lead to false positives. // the context, this potentially can lead to false positives.
// //
// XXX might be a good idea to add a <content/> tag to place the
// loaded text...
// XXX add path recursion test to data -- fail if two paths resolve // XXX add path recursion test to data -- fail if two paths resolve
// to the same context... // to the same context...
// XXX need a way to make encode option transparent... // XXX need a way to make encode option transparent...
@ -1559,10 +1492,16 @@ module.parser = {
function(page, args, body, state, handler){ function(page, args, body, state, handler){
var that = this var that = this
/* XXX see .joinBlocks(..) join caching for more info...
// cache body ast...
body = body ?
this.ast(body)
: body
//*/
var recursive = var recursive =
state.recursive = state.recursive =
args.recursive args.recursive
?? body
?? state.recursive ?? state.recursive
var base = page.basepath var base = page.basepath
@ -1572,6 +1511,20 @@ module.parser = {
return Promise.awaitOrRun( return Promise.awaitOrRun(
this.parseNested(page, src, state), this.parseNested(page, src, state),
function(src){ function(src){
// check for recursion...
// XXX do we do this for pattern paths???
// XXX this does not catch the /A/A/A/... recursion...
var stack = state.include_stack ??= []
// XXX is this the right separator???
// ...need something that can't be in a path...
var base_src = base +'|'+ src
if(stack.includes(base_src)){
if(recursive){
return that.expand(page, recursive, state) }
throw new Error('Recursion:\n\t'+
[...stack, base_src].join('\n\t\t-> ')) }
stack.push(base_src)
var cache = state.cache ??= {} var cache = state.cache ??= {}
if(cache[src]){ if(cache[src]){
return cache[src] } return cache[src] }
@ -1586,20 +1539,6 @@ module.parser = {
// out of context... // out of context...
var depends = ((state.depends ??= {})[src] ??= {}) var depends = ((state.depends ??= {})[src] ??= {})
// check for recursion...
// XXX do we do this for pattern paths???
// XXX this does not catch the /A/A/A/... recursion...
var stack = state.include_stack ??= []
// XXX is this the right separator???
// ...need something that can't be in a path...
var base_src = base +'|'+ src
if(stack.includes(base_src)){
if(recursive){
return recursive }
throw new Error('Recursion:\n\t'+
[...stack, base_src].join('\n\t\t-> ')) }
stack.push(base_src)
// content handler... // content handler...
handler ??= handler ??=
function(page, text, state){ function(page, text, state){
@ -1616,12 +1555,15 @@ module.parser = {
: {}) : {})
: this.expand(page, text, state)} : this.expand(page, text, state)}
var pageHandler =
function(text){
// XXX handle body / <content/>...
// XXX
return handler.call(that, page, text, state) }
var resultHandler = var resultHandler =
function(pages){ function(pages){
// XXX not sure if this can happen or why... state.include_stack.at(-1) == base_src
if(state.include_stack.at(-1) != base_src){ && state.include_stack.pop()
throw new Error('Include stack error') }
state.include_stack.pop()
// cleanup... // cleanup...
if(state.include_stack.length == 0){ if(state.include_stack.length == 0){
delete state.include_stack delete state.include_stack
@ -1643,8 +1585,7 @@ module.parser = {
.iter( .iter(
that.joinBlocks( that.joinBlocks(
page, page,
pages.map(function(text){ pages.map(pageHandler),
return handler.call(that, page, text, state) }),
args.join, args.join,
state)) state))
.flat() .flat()

View File

@ -18,8 +18,14 @@ module.exports.PAGES = {
'/async/page': Promise.resolve('Page'), '/async/page': Promise.resolve('Page'),
'/includePage': '@include(/page)', '/includePage': '@include(/page)',
'/isolated': '@slot(slot original)', '/isolated': '@slot(slot original)',
'/includeSelf': '@include(/includeSelf)',
'/async/includeSelf': Promise.resolve('@include(/includeSelf)'), '/recursive/Self': '<< @include(/recursive/Self) >>',
'/recursive/OtherSelf': '<< @include(/recursive/SelfOther) >>',
'/recursive/SelfOther': '<< @include(/recursive/OtherSelf) >>',
'/async/recursive/Self': Promise.resolve('<< @include(/async/recursive/Self) >>'),
'/async/recursive/OtherSelf': Promise.resolve('<< @include(/async/recursive/SelfOther) >>'),
'/async/recursive/SelfOther': Promise.resolve('<< @include(/async/recursive/OtherSelf) >>'),
'/multi/page': [ 'A', 'B', 'C' ], '/multi/page': [ 'A', 'B', 'C' ],
} }
@ -219,10 +225,26 @@ test.Setups({
'original overloaded', 'original overloaded',
], ],
} }, } },
// XXX recursion... // recursion...
// XXX // XXX test path recursion: /A -> /A/A -> /A/A/A -> ...
include_recursive_a: function(assert){
return {
page: P,
code:[
'@include(/recursive/Self recursive="recursion found")',
'@include(/async/recursive/Self recursive="recursion found")',
'<< recursion found >>', ], } },
include_recursive_b: function(assert){
return {
page: P,
code:[
'@include(/recursive/SelfOther recursive="recursion found")',
'@include(/async/recursive/SelfOther recursive="recursion found")',
'<< << recursion found >> >>', ], } },
// quote... // quote...
// for inline quoting see: test.Modifiers.quote
// XXX <quote src=.. />
}) })
@ -251,11 +273,28 @@ test.Modifiers({
`[[ ${state.code.at(-1)} ]]`, `[[ ${state.code.at(-1)} ]]`,
] ]
return state }, return state },
quote: function(assert, state){
return state.code
.slice(0, -1)
.map(function(code){
code = code.replace(/<\/quote>/, '&lt;/quote&gt;')
return {
page: state.P,
code: [
`<quote>${ code }</quote>`,
code,
],
} }) },
}) })
test.Tests({ test.Tests({
parse: async function(assert, state){ parse: async function(assert, state){
var states =
state instanceof Array ?
state
: [state]
for(state of states){
var {page, code, st} = state var {page, code, st} = state
page ??= {} page ??= {}
st ??= {} st ??= {}
@ -276,7 +315,7 @@ test.Tests({
'Parsing:', 'Parsing:',
'\n\t in: "'+ input +'"', '\n\t in: "'+ input +'"',
'\n\t out: "'+ res +'"', '\n\t out: "'+ res +'"',
'\n\texpected: "'+ expect +'"') } }, '\n\texpected: "'+ expect +'"') } } },
//asyncParse: async function(assert, state){ //asyncParse: async function(assert, state){
// return await this.parse(assert, state) }, // return await this.parse(assert, state) },
}) })