From 4699e0434e089da878dfd46f7b0f5e85386c2873 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 2 Aug 2020 02:50:54 +0300 Subject: [PATCH] split out the lang.js example.... Signed-off-by: Alex A. Naanou --- argv.js | 8 +++- examples/lang.js | 110 +++++++++++++++++++++++++++++++++++++++++++++++ test.js | 109 ++-------------------------------------------- 3 files changed, 119 insertions(+), 108 deletions(-) create mode 100644 examples/lang.js diff --git a/argv.js b/argv.js index f08e720..2f6ae9c 100644 --- a/argv.js +++ b/argv.js @@ -44,6 +44,10 @@ module.THEN = // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// NOTE: it would be great to make .message a prop and handle '$ARG' +// replacement but JS uses it internally in a non standard way +// so the prop is circumvented internally... (XXX) +// ...currently the substitution is done in .printError(..) module.ParserError = object.Constructor('ParserError', Error, { // NOTE: I do not get why JavaScript's Error implements this @@ -286,7 +290,7 @@ object.Constructor('Parser', { }, { // config... // - // NOTE: this must contain two goups the first is the prefix and the + // NOTE: this must contain two groups the first is the prefix and the // second must contain the option name... // NOTE: we only care about differentiating an option from a command // here by design... @@ -851,7 +855,7 @@ object.Constructor('Parser', { // Post parsing callbacks... // - // .then(callback(unhandleed, root_value, rest)) + // .then(callback(unhandled, root_value, rest)) // // .stop(callback(arg, rest)) // .error(callback(reason, arg, rest)) diff --git a/examples/lang.js b/examples/lang.js new file mode 100644 index 0000000..a0fbeac --- /dev/null +++ b/examples/lang.js @@ -0,0 +1,110 @@ +#!/usr/bin/env node + +var argv = require('../argv') + +var parser = +exports.parser = +argv.Parser({ + // XXX for testing, remove when done... + '-echo': function(...args){ + console.log('ECHO:', ...args)}, + + // helpers... + push: function(...items){ + this.unhandled.splice(this.unhandled.length, 0, ...items) + return this }, + exec: function(...items){ + this.rest.splice(0, 0, ...items) + return this }, + + pre_ns: argv.Parser({ + }), + + // XXX do not like the split namespaces.... + ns: { + '[': [ 'blockto', '[:]' ], + '(': [ 'blockto', ')', 'exec' ], + 'quote': [ 'quotenn', '0', '1' ], + }, + + '@*': function(code, value){ + this.unhandled.push(...( + // type-convert... + /^[+-]?[0-9]+$/.test(value) ? + [parseInt(value)] + : /^[+-]?[0-9.]+$/.test(value) ? + [parseFloat(value)] + // call user macros... + : value in this.ns ? + (this.exec(...this.ns[value]), []) + // unhandled... + : [value])) }, + + // XXX hanck... + '@quotenn': function(code){ + var skip = code.shift() + var quote = code.shift() + this.push(...code.splice(skip, quote)) }, + + // XXX this needs blocks to be already made... + // :: ( | name code -- | ) + '@::': function(code){ + this.ns[code.shift()] = code.shift() }, + + // XXX revise... + // groupb ( B | .. B -- | [ .. ]) + // groupb ( A:B | .. A .. B .. B -- | [ .. [ .. ] .. ]) + '@blockto': function(code, do_pack, value){ + value = value || code.shift() + value = value instanceof Array ? + value + : value.split(':') + var [f, t] = value.length == 1 ? + [undefined, ...value] + : value + var pack = [] + var cur = code.shift() + while(code.length > 0 && cur != t){ + cur = cur == f ? + this['@blockto'](code, false, value) + : cur + pack.push(cur) + cur = code.shift() } + do_pack !== false + && code.unshift(pack) + return pack }, + '@exec': function(code){ + var c = this.unhandled.pop() + code.splice(0, 0, ...(c instanceof Array ? c : [c])) }, + + '@exit': '-', + + '@dup': function(){ + this.push(...this.unhandled.slice(-1)) }, + '@dup2': function(){ + this.push(...this.unhandled.slice(-2)) }, + + '@print': function(){ + this.print(this.unhandled.pop()) }, + + '@add': function(){ + var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] + this.unhandled.push(a + b) }, + '@sub': function(){ + var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] + this.unhandled.push(a - b) }, + '@mul': function(){ + var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] + this.unhandled.push(a * b) }, + '@div': function(){ + var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] + this.unhandled.push(a / b) }, + +}) +.then(function(){ + this.print('>>>', this.unhandled) }) + + + + +// vim:set ts=4 sw=4 spell : diff --git a/test.js b/test.js index 2fc7a31..a6d4da0 100644 --- a/test.js +++ b/test.js @@ -16,6 +16,7 @@ var argv = require('./argv') var bare = module.bare = require('./examples/bare').parser var options = module.options = require('./examples/options').parser +var lang = module.lang = require('./examples/lang').parser @@ -158,6 +159,8 @@ argv.Parser({ '@bare': bare, '@opts': options, + '@lang': lang, + // collision test... // NOTE: values of these will shadow the API... @@ -176,112 +179,6 @@ argv.Parser({ -var lang = -module.lang = -argv.Parser({ - // handle both +x and -x - optionInputPattern: /^([+-])\1?([^+-].*|)$/, - - // XXX for testing, remove when done... - '-echo': function(...args){ - console.log('ECHO:', ...args)}, - - // helpers... - push: function(...items){ - this.unhandled.splice(this.unhandled.length, 0, ...items) - return this }, - exec: function(...items){ - this.rest.splice(0, 0, ...items) - return this }, - - pre_ns: argv.Parser({ - }), - - // XXX do not like the split namespaces.... - ns: { - '[': [ 'blockto', '[:]' ], - '(': [ 'blockto', ')', 'exec' ], - 'quote': [ 'quotenn', '0', '1' ], - }, - - '@*': function(code, value){ - this.unhandled.push(...( - // type-convert... - /^[+-]?[0-9]+$/.test(value) ? - [parseInt(value)] - : /^[+-]?[0-9.]+$/.test(value) ? - [parseFloat(value)] - // call user macros... - : value in this.ns ? - (this.exec(...this.ns[value]), []) - // unhandled... - : [value])) }, - - // XXX hanck... - '@quotenn': function(code){ - var skip = code.shift() - var quote = code.shift() - this.push(...code.splice(skip, quote)) }, - - // XXX this needs blocks to be already made... - // :: ( | name code -- | ) - '@::': function(code){ - this.ns[code.shift()] = code.shift() }, - - // XXX revise... - // groupb ( B | .. B -- | [ .. ]) - // groupb ( A:B | .. A .. B .. B -- | [ .. [ .. ] .. ]) - '@blockto': function(code, do_pack, value){ - value = value || code.shift() - value = value instanceof Array ? - value - : value.split(':') - var [f, t] = value.length == 1 ? - [undefined, ...value] - : value - var pack = [] - var cur = code.shift() - while(code.length > 0 && cur != t){ - cur = cur == f ? - this['@blockto'](code, false, value) - : cur - pack.push(cur) - cur = code.shift() } - do_pack !== false - && code.unshift(pack) - return pack }, - '@exec': function(code){ - var c = this.unhandled.pop() - code.splice(0, 0, ...(c instanceof Array ? c : [c])) }, - - '@exit': '-', - - '@dup': function(){ - this.push(...this.unhandled.slice(-1)) }, - '@dup2': function(){ - this.push(...this.unhandled.slice(-2)) }, - - '@print': function(){ - this.print(this.unhandled.pop()) }, - - '@add': function(){ - var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] - this.unhandled.push(a + b) }, - '@sub': function(){ - var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] - this.unhandled.push(a - b) }, - '@mul': function(){ - var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] - this.unhandled.push(a * b) }, - '@div': function(){ - var [b, a] = [this.unhandled.pop(), this.unhandled.pop()] - this.unhandled.push(a / b) }, - -}) -.then(function(){ - this.print('>>>', this.unhandled) }) - - /* console.log(' ->', p(['test', '--verbose', 'a', 'b', 'c']))