experimenting...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-12-06 23:40:57 +03:00
parent dd5a9496a2
commit 2afbcea8c3
2 changed files with 76 additions and 25 deletions

View File

@ -437,26 +437,26 @@ module.BaseParser = {
// one should only be used for parsing and be forgotten after // one should only be used for parsing and be forgotten after
// the ast is constructed the other should be part of the ast... // the ast is constructed the other should be part of the ast...
// XXX ASYNC... // XXX ASYNC...
expand: async function*(page, ast, state={}){ _expand: function*(page, ast, state={}){
/*/ var that = this
expand: function*(page, ast, state={}){
//*/
try{ try{
ast = ast == null ? ast = ast == null ?
// XXX ASYNC... Promise.awaitOrRun(
this.group(page, await page.raw ?? '') page.raw,
/*/ function(raw){
page.raw return that.group(page, raw ?? '') })
.then(function(raw){
return this.group(page, raw ?? '') })
//*/
: typeof(ast) != 'object' ? : typeof(ast) != 'object' ?
this.group(page, ast) this.group(page, ast)
: ast instanceof types.Generator ? : ast instanceof types.Generator ?
ast ast
: ast.iter() : ast.iter()
//XXX ASYNC need to .awaitOrRun(ast, ...)... Promise.sync
.resolve(ast)
.then(function(ast){
})
// XXX SYNC need to optionally await for ast...
while(true){ while(true){
var {value, done} = ast.next() var {value, done} = ast.next()
if(done){ if(done){
@ -474,7 +474,7 @@ module.BaseParser = {
yield {...value, skip: true} yield {...value, skip: true}
continue } continue }
// XXX ASYNC... // XXX
var res = var res =
await this.callMacro(page, name, args, body, state) await this.callMacro(page, name, args, body, state)
?? '' ?? ''
@ -485,17 +485,65 @@ module.BaseParser = {
yield* res yield* res
} else { } else {
yield res } } yield res } }
/*/
yield* Promise.awaitOrRun( // error...
this.callMacro(page, name, args, body, state), }catch(err){
function*(res){ console.error(err)
res = res ?? '' yield page.parse(
// XXX add line number and page path...
'@include("./ParseError'
+':path='
// XXX use pwpath.encodeElem(..) ???
+ page.path
+':msg='
+ err.message
// quote html stuff...
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
// quote argument syntax...
.replace(/["']/g, function(c){
return '%'+ c.charCodeAt().toString(16) })
.replace(/:/g, '&colon;')
.replace(/=/g, '&equals;')
+'")') } },
expand: async function*(page, ast, state={}){
try{
ast = ast == null ?
this.group(page, await page.raw ?? '')
: typeof(ast) != 'object' ?
this.group(page, ast)
: ast instanceof types.Generator ?
ast
: ast.iter()
while(true){
var {value, done} = ast.next()
if(done){
return }
// text block...
if(typeof(value) == 'string'){
yield value
continue }
// macro...
var {name, args, body} = value
// nested macro -- skip...
if(typeof(page.macros[name]) != 'function'){
yield {...value, skip: true}
continue }
var res =
await this.callMacro(page, name, args, body, state)
?? ''
// result...
if(res instanceof Array if(res instanceof Array
|| page.macros[name] instanceof types.Generator){ || page.macros[name] instanceof types.Generator){
yield* res yield* res
} else { } else {
yield res } }) yield res } }
//*/
// error... // error...
}catch(err){ }catch(err){

View File

@ -40,9 +40,12 @@
* *
* XXX ASAP need to add error handling to .awaitOrRun(..) * XXX ASAP need to add error handling to .awaitOrRun(..)
* ...might be even better to make it support the same interface as Promise... * ...might be even better to make it support the same interface as Promise...
* XXX BUG: for some reason editing the root page ('/') hangs /tree...
* ...the problem is most likely is in that tree appends tree to
* each path in the list and '/' + '/tree' is ....
* ...why aren't we catching the recursion here???
* XXX BUG: seems that page caching stopped working... * XXX BUG: seems that page caching stopped working...
* ...are we over-refreshing??? * ...are we over-refreshing???
* XXX BUG: for some reason editing the root page ('/') hangs /tree...
* XXX .parse(..): should we handle pattern paths and lists of text??? * XXX .parse(..): should we handle pattern paths and lists of text???
* XXX parser: error handling: add line number + context... (???) * XXX parser: error handling: add line number + context... (???)
* XXX BUG: parser: * XXX BUG: parser: