refactoring and cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-04-22 04:32:44 +03:00
parent dd4629c670
commit e4a1139be3
2 changed files with 122 additions and 129 deletions

View File

@ -338,7 +338,6 @@ module.UNDEFINED = ASIS(undefined)
var normalizeTabs = function(str){ var normalizeTabs = function(str){
str = str.split(/\n/g) str = str.split(/\n/g)
// get min number of leading tabs... // get min number of leading tabs...
var i = str.length == 2 && /^\t/.test(str[1]) ? var i = str.length == 2 && /^\t/.test(str[1]) ?
str[1].split(/^(\t+)/)[1].length - 1 str[1].split(/^(\t+)/)[1].length - 1
@ -352,7 +351,6 @@ var normalizeTabs = function(str){
return /^\t+/.test(l) ? return /^\t+/.test(l) ?
l.split(/^(\t+)/)[1].length l.split(/^(\t+)/)[1].length
: 0})) : 0}))
return (str[0] +'\n' return (str[0] +'\n'
+ str + str
.slice(1) .slice(1)
@ -361,8 +359,7 @@ var normalizeTabs = function(str){
// replace tabs... // replace tabs...
.replace(/\t/g, ' ')) .replace(/\t/g, ' '))
// remove leading and trailing whitespace... // remove leading and trailing whitespace...
.trim() .trim() }
}
var doWithRootAction = var doWithRootAction =
@ -374,9 +371,7 @@ function(func){
|| MetaActions.getHandlerList) || MetaActions.getHandlerList)
.apply(this, args) .apply(this, args)
return func.apply(this, [handlers.pop()].concat(args)) return func.apply(this, [handlers.pop()].concat(args)) } }
}
}
// //
@ -417,144 +412,142 @@ function(func){
// NOTE: identifiers are resolved as attributes of the context... // NOTE: identifiers are resolved as attributes of the context...
// XXX this is the same as ImageGrid's keyboard.parseActionCall(..), reuse // XXX this is the same as ImageGrid's keyboard.parseActionCall(..), reuse
// in a logical manner... // in a logical manner...
// placeholders...
var __Atom
var __Argument
var parseStringAction = var parseStringAction =
module.parseStringAction = module.parseStringAction =
function(txt){ Object.assign(
// split off the doc... // parser...
var c = txt.split('--') function(txt){
var doc = (c[1] || '').trim() // split off the doc...
// the actual code... var c = txt.split('--')
c = c[0].split(':') var doc = (c[1] || '').trim()
// the actual code...
c = c[0].split(':')
// action and no default flag... // action and no default flag...
var action = c[0].trim() var action = c[0].trim()
var no_default = action.slice(-1) == '!' var no_default = action.slice(-1) == '!'
action = no_default ? action.slice(0, -1) : action action = no_default ? action.slice(0, -1) : action
// parse arguments... // parse arguments...
var args = ((c[1] || '') var args = ((c[1] || '')
.match(RegExp([ .match(RegExp([
// strings... // strings...
'"[^"]*"', '"[^"]*"',
"'[^']*'", "'[^']*'",
'`[^`]*`', '`[^`]*`',
// objects... // objects...
// XXX hack-ish... // XXX hack-ish...
'\\{[^\\}]*\\}', '\\{[^\\}]*\\}',
// lists... // lists...
// XXX hack-ish... // XXX hack-ish...
'\\[[^\]]*\]', '\\[[^\]]*\]',
// numbers... // numbers...
'\\d+\\.\\d+|\\d+', '\\d+\\.\\d+|\\d+',
// identifiers... // identifiers...
'[a-zA-Z$@#_][a-zA-Z0-9$@#_]*', '[a-zA-Z$@#_][a-zA-Z0-9$@#_]*',
// rest args... // rest args...
'\\.\\.\\.', '\\.\\.\\.',
// null... // null...
'null', 'null',
].join('|'), 'gm')) ].join('|'), 'gm'))
|| []) || [])
.map(function(e){ .map(function(e){
// argument placeholder... // argument placeholder...
return /^\.\.\.$/.test(e) ? return /^\.\.\.$/.test(e) ?
parseStringAction.ALLARGS parseStringAction.ALLARGS
: /^\$[a-zA-Z0-9$@#_]*$/.test(e) ? : /^\$[a-zA-Z0-9$@#_]*$/.test(e) ?
new parseStringAction.Argument(e.slice(1)) new parseStringAction.Argument(e.slice(1))
// idetifiers... // idetifiers...
// NOTE: keep this last as it is the most general... // NOTE: keep this last as it is the most general...
: /^[a-zA-Z$@#_][a-zA-Z0-9$@#_]*$/.test(e) ? : /^[a-zA-Z$@#_][a-zA-Z0-9$@#_]*$/.test(e) ?
new parseStringAction.Identifier(e) new parseStringAction.Identifier(e)
: JSON.parse(e) }) : JSON.parse(e) })
return { return {
action: action, action: action,
arguments: args, arguments: args,
doc: doc, doc: doc,
no_default: no_default, no_default: no_default,
stop_propagation: false, stop_propagation: false,
code: txt, code: txt,
} }
} },{
// atoms...
Atom: (__Atom = object.Constructor('Atom', {
__init__: function(value){
this.value = value },
valueOf: function(){
return this.value },
})),
Identifier: object.Constructor('Identifier',
Object.create(__Atom.prototype)),
Argument: (__Argument = object.Constructor('Argument',
Object.create(__Atom.prototype))),
ALLARGS: new __Argument('...'),
parseStringAction.Identifier = // general API...
object.Constructor( resolveArgs: function(context, action_args, call_args){
'Identifier', var that = this
{ var rest
__init__: function(value){ var args = [].slice.call(action_args)
this.value = value // merge args...
}, .map(function(arg, i){
valueOf: function(){ return this.value }, return arg instanceof that.Argument ?
(arg === that.ALLARGS ?
(function(){
rest = i
return arg
})()
: call_args[parseInt(arg.value)])
// resolve idents...
: arg instanceof that.Identifier ?
context[arg.value]
: arg })
rest != null
&& args.splice(rest, 1, ...call_args)
return args },
// XXX should this break if action does not exist???
callAction: function(context, action, ...args){
action = typeof(action) == typeof('str') ?
this(action)
: action
// XXX should this break if action does not exist???
return context[action.action] instanceof Function ?
context[action.action]
.apply(context, this.resolveArgs(context, action.arguments, args))
// action not found or is not callable... (XXX)
: undefined },
applyAction: function(context, action, args){
return this.callAction(context, action, ...args) },
// XXX make this stricter...
isStringAction: function(txt){
try{
var parsed = typeof(txt) == typeof('str')
&& (this.parseStringAction || parseStringAction)(txt)
return parsed
&& /[a-zA-Z_][a-zA-Z0-9_]*/.test(parsed.action)
} catch(e){
return false } },
}) })
parseStringAction.Argument =
object.Constructor(
'Argument',
{
__init__: function(value){
this.value = value
},
valueOf: function(){ return this.value },
})
parseStringAction.ALLARGS = new parseStringAction.Argument('...')
parseStringAction.resolveArgs = function(context, action_args, call_args){ // shorthand...
var that = this
var rest
var args = [].slice.call(action_args)
// merge args...
.map(function(arg, i){
return arg instanceof that.Argument ?
(arg === that.ALLARGS ?
(function(){
rest = i
return arg
})()
: call_args[parseInt(arg.value)])
// resolve idents...
: arg instanceof that.Identifier ?
context[arg.value]
: arg })
rest != null
&& args.splice(rest, 1, ...call_args)
return args
}
// XXX should this break if action does not exist???
parseStringAction.callAction = function(context, action, ...args){
action = typeof(action) == typeof('str') ?
this(action)
: action
// XXX should this break if action does not exist???
return context[action.action] instanceof Function ?
context[action.action]
.apply(context, this.resolveArgs(context, action.arguments, args))
// action not found or is not callable... (XXX)
: undefined
}
parseStringAction.applyAction = function(context, action, args){
return this.callAction(context, action, ...args) }
// XXX make this stricter...
var isStringAction = var isStringAction =
module.isStringAction = module.isStringAction =
function(txt){ parseStringAction.isStringAction
try{
var parsed = typeof(txt) == typeof('str')
&& (this.parseStringAction || parseStringAction)(txt)
return parsed
&& /[a-zA-Z_][a-zA-Z0-9_]*/.test(parsed.action)
} catch(e){
return false
}
}

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-actions", "name": "ig-actions",
"version": "3.24.3", "version": "3.24.4",
"description": "", "description": "",
"main": "actions.js", "main": "actions.js",
"scripts": { "scripts": {