some refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-12-17 04:27:04 +03:00
parent aacf5cbe29
commit 1166f2472c

View File

@ -343,13 +343,36 @@ function(func){
} }
//
// Syntax:
// ALIAS ::=
// <action-name>
// | <action-name>: <args>
// | <action-name>: <args> <comment>
// <args> ::=
// <arg>
// | <arg> <args>
// <arg> ::=
// Number|String|Array|Object
// IDENTIFIER
// | ...
// | '$[0-9]'
// <comment> ::=
// '--.*$'
//
// Special args:
// IDENTIFIER
// - expanded to context[IDENTIFIER]
// $N - expanded to and instance of parseStringAction.Argument
// ... - expanded to parseStringAction.ALLARGS (singleton)
//
//
// 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...
var parseStringAction = var parseStringAction =
module.parseStringAction = module.parseStringAction =
function(txt, context){ function(txt){
context = context || this
// split off the doc... // split off the doc...
var c = txt.split('--') var c = txt.split('--')
var doc = (c[1] || '').trim() var doc = (c[1] || '').trim()
@ -395,11 +418,11 @@ function(txt, context){
return /^\.\.\.$/.test(e) ? return /^\.\.\.$/.test(e) ?
parseStringAction.ALLARGS parseStringAction.ALLARGS
: /^\$[a-zA-Z0-9$@#_]*$/.test(e) ? : /^\$[a-zA-Z0-9$@#_]*$/.test(e) ?
new StringActionArgument(e.slice(1)) new parseStringAction.Argument(e.slice(1))
// context 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) ?
context[e] new parseStringAction.Identifier(e)
: JSON.parse(e) }) : JSON.parse(e) })
return { return {
@ -413,17 +436,25 @@ function(txt, context){
} }
} }
StringActionArgument = parseStringAction.Identifier =
parseStringAction.StringActionArgument =
object.makeConstructor( object.makeConstructor(
'StringActionArgument', 'Identifier',
{ {
__init__: function(value){ __init__: function(value){
this.value = value this.value = value
}, },
valueOf: function(){ return this.value }, valueOf: function(){ return this.value },
}) })
parseStringAction.ALLARGS = new StringActionArgument('...') parseStringAction.Argument =
object.makeConstructor(
'Argument',
{
__init__: function(value){
this.value = value
},
valueOf: function(){ return this.value },
})
parseStringAction.ALLARGS = new parseStringAction.Argument('...')
// XXX make this stricter... // XXX make this stricter...
@ -798,6 +829,12 @@ Action.prototype.chainCall = function(context, inner){
// that this expects the target to be a string compatible with // that this expects the target to be a string compatible with
// .parseStringAction(..)... // .parseStringAction(..)...
// //
// This will resolve special alias args:
// name -> parseStringAction.Identifier(name) -> this[name]
// $N -> parseStringAction.Argument(N) -> arguments[n]
// ... -> parseStringAction.ALLARGS -> arguments
//
//
// XXX alias parsing is dependant on the action set, move this functionality // XXX alias parsing is dependant on the action set, move this functionality
// to the ActionSet.alias(..) method/action... // to the ActionSet.alias(..) method/action...
// XXX handle alias args and pass them to the target... // XXX handle alias args and pass them to the target...
@ -847,13 +884,15 @@ function Alias(alias, doc, ldoc, attrs, target){
var args = action.arguments.slice() var args = action.arguments.slice()
// merge args... // merge args...
.map(function(arg, i){ .map(function(arg, i){
return arg instanceof that.parseStringAction.StringActionArgument ? return arg instanceof that.parseStringAction.Argument ?
(arg === that.parseStringAction.ALLARGS ? (arg === that.parseStringAction.ALLARGS ?
(function(){ (function(){
rest = i rest = i
return arg return arg
})() })()
: in_args[parseInt(arg.value)]) : in_args[parseInt(arg.value)])
: arg instanceof that.parseStringAction.Identifier ?
that[arg.value]
: arg }) : arg })
rest != null rest != null
&& args.splice(rest, 1, ...in_args) && args.splice(rest, 1, ...in_args)