refactoring and fixes + found a parser bug....

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-05-02 18:29:59 +03:00
parent a093281a37
commit fe8684f6ce

View File

@ -515,6 +515,7 @@ module.BaseParser = {
// STOP -- '\\>' or ')' // STOP -- '\\>' or ')'
// PREFIX -- 'inline' or 'elem' // PREFIX -- 'inline' or 'elem'
// //
// XXX BUG: @now(a) is not matched....
// XXX quote escaping??? // XXX quote escaping???
// /(?<quote>['"])(\\\k<quote>|[^\1])*\k<quote>/ // /(?<quote>['"])(\\\k<quote>|[^\1])*\k<quote>/
// ...this will work but we'll also need to remove the \ in the // ...this will work but we'll also need to remove the \ in the
@ -694,13 +695,21 @@ module.BaseParser = {
of (cur.argsInline ?? cur.argsOpen ?? '') of (cur.argsInline ?? cur.argsOpen ?? '')
.matchAll(macro_args_pattern)){ .matchAll(macro_args_pattern)){
i++ i++
args[groups.elemArgName ?? i] = args[groups.elemArgName
?? groups.inlineArgName
?? i] =
groups.elemSingleQuotedValue groups.elemSingleQuotedValue
?? groups.inlineSingleQuotedValue
?? groups.elemDoubleQuotedValue ?? groups.elemDoubleQuotedValue
?? groups.inlineDoubleQuotedValue
?? groups.elemValue ?? groups.elemValue
?? groups.inlineValue
?? groups.elemSingleQuotedArg ?? groups.elemSingleQuotedArg
?? groups.inlineSingleQuotedArg
?? groups.elemDoubleQuotedArg ?? groups.elemDoubleQuotedArg
?? groups.elemArg } ?? groups.inlineDoubleQuotedArg
?? groups.elemArg
?? groups.inlineArg }
// macro-spec... // macro-spec...
yield { yield {
@ -765,7 +774,7 @@ module.BaseParser = {
if(done){ if(done){
if(to){ if(to){
throw new Error( throw new Error(
'Premature end of inpit: Expected closing "'+ to +'"') } 'Premature end of input: Expected closing "'+ to +'"') }
return } return }
// special case: quoting -> collect text... // special case: quoting -> collect text...
@ -944,6 +953,8 @@ object.Constructor('Page', BasePage, {
return source }, return source },
}, },
// XXX need a good way to get the first positional arg without
// mixing it up with other args -- see src/name args below...
macros: { macros: {
// XXX move to docs... // XXX move to docs...
test: function*(args, body, state){ test: function*(args, body, state){
@ -974,7 +985,7 @@ object.Constructor('Page', BasePage, {
// | -<filter> <filter-spec> // | -<filter> <filter-spec>
// //
// XXX support .NO_FILTERS ... // XXX support .NO_FILTERS ...
filter: function*(args, body, state){ filter: function*(args, body, state, expand=true){
var filters = state.filters = var filters = state.filters =
state.filters ?? [] state.filters ?? []
// separate local filters... // separate local filters...
@ -994,7 +1005,11 @@ object.Constructor('Page', BasePage, {
&& state.filters.shift() && state.filters.shift()
// expand the body... // expand the body...
var ast = [...this.__parser__.expand(this, body, state)] var ast = expand ?
[...this.__parser__.expand(this, body, state)]
: body instanceof Array ?
body
: [body]
filters = state.filters filters = state.filters
state.filters = outer_filters state.filters = outer_filters
@ -1003,7 +1018,9 @@ object.Constructor('Page', BasePage, {
yield function(state){ yield function(state){
var outer_filters = state.filters var outer_filters = state.filters
state.filters = this.__parser__.normalizeFilters(filters) state.filters = this.__parser__.normalizeFilters(filters)
var res = [...this.__parser__.parse(this, ast, state)] var res = (expand ?
[...this.__parser__.parse(this, ast, state)]
: ast)
.flat() .flat()
.join('') .join('')
state.filters = outer_filters state.filters = outer_filters
@ -1024,7 +1041,7 @@ object.Constructor('Page', BasePage, {
// XXX should this be lazy??? // XXX should this be lazy???
include: function(args, body, state, key='included', handler){ include: function(args, body, state, key='included', handler){
// positional args... // positional args...
var src = args.src || args[0] var src = args.src //|| args[0]
var recursive = args.recursive || body var recursive = args.recursive || body
var isolated = this.__parser__.getPositional(args).includes('isolated') var isolated = this.__parser__.getPositional(args).includes('isolated')
@ -1069,7 +1086,7 @@ object.Constructor('Page', BasePage, {
return res }, return res },
source: function(args, body, state){ source: function(args, body, state){
var src = args.src || args[0] var src = args.src //|| args[0]
return this.macros.include.call(this, return this.macros.include.call(this,
args, body, state, 'sources', args, body, state, 'sources',
function(){ function(){
@ -1088,38 +1105,40 @@ object.Constructor('Page', BasePage, {
// //
// NOTE: src ant text arguments are mutually exclusive, src takes // NOTE: src ant text arguments are mutually exclusive, src takes
// priority. // priority.
// XXX handle interactions with page filters.... // NOTE: the filter argument has the same semantics as the filter
// macro with one exception, when used in quote, the body is
// not expanded...
//
// XXX need a way to escape macros...
quote: function(args, body, state){ quote: function(args, body, state){
var that = this var src = args.src //|| args[0]
var src = args.src
?? args[0]
var filters = args.filter
&& this.__parser__.normalizeFilters(
args.filter
.trim()
.split(/\\s+/g))
var text = args.text var text = args.text
?? body ?? body
?? [] ?? []
text = src ? text = src ?
// source page... // source page...
this.get(src).raw this.get(src).raw
// body/arg... : text instanceof Array ?
: text.join('') text.join('')
: text
return text ?
(filters ?
// apply filters...
// XXX handle interactions with page filters....
filters
.reduce(
function(res, filter){
return that.filters[filter].call(that, res)
?? res },
text)
: text)
// empty... // empty...
: '' }, if(!text){
return }
var filters = args.filter
&& args.filter
.trim()
.split(/\\s+/g)
return filters ?
[...this.macros.filter.call(this,
Object.fromEntries(
Object.entries(filters ?? [])),
text,
state,
false)]
: text },
// //
// <slot name=<name>/> // <slot name=<name>/>
// //
@ -1177,7 +1196,7 @@ object.Constructor('Page', BasePage, {
// XXX sorting not implemented yet.... // XXX sorting not implemented yet....
macro: function(args, body, state){ macro: function(args, body, state){
var that = this var that = this
var name = args.name ?? args[0] var name = args.name //?? args[0]
var src = args.src var src = args.src
var sort = (args.sort ?? '') var sort = (args.sort ?? '')
.split(/\s+/g) .split(/\s+/g)