Page 1 of 1

Command line arguments

Posted: Wed Mar 18, 2015 5:22 pm
by flashgot.user
Giorgio, I think the regexp in FlashGotDMCust.makeArgs is broken.

Code: Select all

var rx = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + this.PLACEHOLDERS.join("|") + ")\\b(\\S*?)([\\s\\S]*?)\\]");
From what I managed to understand from the commentless :P code, it was supposed to allow the user to specify an alternative/default value for a placeholder that has no value and capture it in m[4]. The problem is, m[4] is always empty thanks to its lazy (not greedy) "\S*?" followed by another \S* (both patterns match the same input and the first pattern can (allowed to) match nothing, so the parser can always move on to the second pattern), so m[5] contains everything after the placeholder name, so the alternative/default value is never used because the code looks for it in m[4].

This regexp does what I think it was supposed to do - m[4] captures a non-whitespace sequence after the placeholder name, and m[5] captures everything after it:

Code: Select all

var rx = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + this.PLACEHOLDERS.join("|") + ")\\b(\\S*)([\\s\\S]*?)\\]");
I just replaced (\S*?) with (\S*) for m[4].

Here's my test results using "Z" as the only placeholder, re1 as the old (current) regexp, and re2 as the new regexp.

Code: Select all

var re1 = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + "Z" + ")\\b(\\S*?)([\\s\\S]*?)\\]");
var re2 = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + "Z" + ")\\b(\\S*)([\\s\\S]*?)\\]");
var a = ["[]", "[Z]", "[Z|]", "[Z |]", "[Z| ]", "[Z|aaa]", "[Z |aaa]", "[Z| aaa]", "[Z|aaa bbb]", "[Z |aaa bbb]", "[Z| aaa bbb]"];
function format_match(name, m){return m == null ? name + "=" + m : m.map(function(o,i){return name + "[" + i + "]: [" + typeof(o) + "]: '" + o + "'";}).join("\n");}
for (var i = 0; i < a.length; ++i) {
  var s = a[i], m1 = s.match(re1), m2 = s.match(re2);
  console.log("s: '" + s + "'\n" + format_match("m1", m1) + "\n\n" + format_match("m2", m2));
}
Output:

Code: Select all

=====
s: '[]'
m1=null

m2=null
=====
s: '[Z]'
m1[0]: [string]: '[Z]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ''

m2[0]: [string]: '[Z]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ''
=====
s: '[Z|]'
m1[0]: [string]: '[Z|]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|'

m2[0]: [string]: '[Z|]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ''
=====
s: '[Z |]'
m1[0]: [string]: '[Z |]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |'

m2[0]: [string]: '[Z |]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |'
=====
s: '[Z| ]'
m1[0]: [string]: '[Z| ]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| '

m2[0]: [string]: '[Z| ]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' '
=====
s: '[Z|aaa]'
m1[0]: [string]: '[Z|aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|aaa'

m2[0]: [string]: '[Z|aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|aaa'
m2[5]: [string]: ''
=====
s: '[Z |aaa]'
m1[0]: [string]: '[Z |aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |aaa'

m2[0]: [string]: '[Z |aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |aaa'
=====
s: '[Z| aaa]'
m1[0]: [string]: '[Z| aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| aaa'

m2[0]: [string]: '[Z| aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' aaa'
=====
s: '[Z|aaa bbb]'
m1[0]: [string]: '[Z|aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|aaa bbb'

m2[0]: [string]: '[Z|aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|aaa'
m2[5]: [string]: ' bbb'
=====
s: '[Z |aaa bbb]'
m1[0]: [string]: '[Z |aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |aaa bbb'

m2[0]: [string]: '[Z |aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |aaa bbb'
=====
s: '[Z| aaa bbb]'
m1[0]: [string]: '[Z| aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| aaa bbb'

m2[0]: [string]: '[Z| aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' aaa bbb'

Re: Command line arguments

Posted: Wed Mar 18, 2015 8:44 pm
by flashgot.user
Nope, making m[4] greedy matches too much. Here's a new regexp - using [^\s\]]* for m[4]:

Code: Select all

var rx = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + this.PLACEHOLDERS.join("|") + ")\\b([^\\s\\]]*)([\\s\\S]*?)\\]");
Test - I'm keeping the previous regexp (re2) to show where it doesn't work:

Code: Select all

var re1 = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + "Z|T" + ")\\b(\\S*?)([\\s\\S]*?)\\]");
var re2 = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + "Z|T" + ")\\b(\\S*)([\\s\\S]*?)\\]");
var re3 = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + "Z|T" + ")\\b([^\\s\\]]*)([\\s\\S]*?)\\]");
var a = ["[]", "[Z] [t T] [z Z]", "[Z|] [t T] [z Z]", "[Z |] [t T] [z Z]", "[Z| ] [t T] [z Z]", "[Z|aaa] [t T] [z Z]", "[Z |aaa] [t T] [z Z]", "[Z| aaa] [t T] [z Z]", "[Z|aaa bbb] [t T] [z Z]", "[Z |aaa bbb] [t T] [z Z]", "[Z| aaa bbb] [t T] [z Z]", "[Z|abc def=[ghi]] [t T]", "[Z|abc def=[ghi]]] [t T]", "[Z|abc def=[ghi] ] [t T]", "[Z|abc=[def]] [t T]"];
function format_match(name, m){
  return m == null ? name + "=" + m : name + ".index: " + m.index + "\n" + m.map(function(o,i){return name + "[" + i + "]: [" + typeof(o) + "]: '" + o + "'";}).join("\n");
}
for (var i = 0; i < a.length; ++i) {
  var s = a[i], m1 = s.match(re1), m2 = s.match(re2), m3 = s.match(re3);
  console.log("=====\ns: '" + s + "'\n" + format_match("m1", m1) + "\n\n" + format_match("m2", m2) + "\n\n" + format_match("m3", m3));
}
Output:

Code: Select all

=====
s: '[]'
m1=null

m2=null

m3=null
=====
s: '[Z] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ''

m2.index: 0
m2[0]: [string]: '[Z] [t T]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ']'
m2[5]: [string]: ' [t T'

m3.index: 0
m3[0]: [string]: '[Z]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: ''
m3[5]: [string]: ''
=====
s: '[Z|] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z|]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|'

m2.index: 0
m2[0]: [string]: '[Z|] [t T]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|]'
m2[5]: [string]: ' [t T'

m3.index: 0
m3[0]: [string]: '[Z|]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|'
m3[5]: [string]: ''
=====
s: '[Z |] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z |]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |'

m2.index: 0
m2[0]: [string]: '[Z |]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |'

m3.index: 0
m3[0]: [string]: '[Z |]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: ''
m3[5]: [string]: ' |'
=====
s: '[Z| ] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z| ]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| '

m2.index: 0
m2[0]: [string]: '[Z| ]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' '

m3.index: 0
m3[0]: [string]: '[Z| ]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|'
m3[5]: [string]: ' '
=====
s: '[Z|aaa] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z|aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|aaa'

m2.index: 0
m2[0]: [string]: '[Z|aaa] [t T]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|aaa]'
m2[5]: [string]: ' [t T'

m3.index: 0
m3[0]: [string]: '[Z|aaa]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|aaa'
m3[5]: [string]: ''
=====
s: '[Z |aaa] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z |aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |aaa'

m2.index: 0
m2[0]: [string]: '[Z |aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |aaa'

m3.index: 0
m3[0]: [string]: '[Z |aaa]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: ''
m3[5]: [string]: ' |aaa'
=====
s: '[Z| aaa] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z| aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| aaa'

m2.index: 0
m2[0]: [string]: '[Z| aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' aaa'

m3.index: 0
m3[0]: [string]: '[Z| aaa]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|'
m3[5]: [string]: ' aaa'
=====
s: '[Z|aaa bbb] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z|aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|aaa bbb'

m2.index: 0
m2[0]: [string]: '[Z|aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|aaa'
m2[5]: [string]: ' bbb'

m3.index: 0
m3[0]: [string]: '[Z|aaa bbb]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|aaa'
m3[5]: [string]: ' bbb'
=====
s: '[Z |aaa bbb] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z |aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |aaa bbb'

m2.index: 0
m2[0]: [string]: '[Z |aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |aaa bbb'

m3.index: 0
m3[0]: [string]: '[Z |aaa bbb]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: ''
m3[5]: [string]: ' |aaa bbb'
=====
s: '[Z| aaa bbb] [t T] [z Z]'
m1.index: 0
m1[0]: [string]: '[Z| aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| aaa bbb'

m2.index: 0
m2[0]: [string]: '[Z| aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' aaa bbb'

m3.index: 0
m3[0]: [string]: '[Z| aaa bbb]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|'
m3[5]: [string]: ' aaa bbb'
=====
s: '[Z|abc def=[ghi]] [t T]'
m1.index: 0
m1[0]: [string]: '[Z|abc def=[ghi]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|abc def=[ghi'

m2.index: 0
m2[0]: [string]: '[Z|abc def=[ghi]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|abc'
m2[5]: [string]: ' def=[ghi'

m3.index: 0
m3[0]: [string]: '[Z|abc def=[ghi]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|abc'
m3[5]: [string]: ' def=[ghi'
=====
s: '[Z|abc def=[ghi]]] [t T]'
m1.index: 0
m1[0]: [string]: '[Z|abc def=[ghi]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|abc def=[ghi'

m2.index: 0
m2[0]: [string]: '[Z|abc def=[ghi]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|abc'
m2[5]: [string]: ' def=[ghi'

m3.index: 0
m3[0]: [string]: '[Z|abc def=[ghi]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|abc'
m3[5]: [string]: ' def=[ghi'
=====
s: '[Z|abc def=[ghi] ] [t T]'
m1.index: 0
m1[0]: [string]: '[Z|abc def=[ghi]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|abc def=[ghi'

m2.index: 0
m2[0]: [string]: '[Z|abc def=[ghi]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|abc'
m2[5]: [string]: ' def=[ghi'

m3.index: 0
m3[0]: [string]: '[Z|abc def=[ghi]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|abc'
m3[5]: [string]: ' def=[ghi'
=====
s: '[Z|abc=[def]] [t T]'
m1.index: 0
m1[0]: [string]: '[Z|abc=[def]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|abc=[def'

m2.index: 0
m2[0]: [string]: '[Z|abc=[def]] [t T]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|abc=[def]]'
m2[5]: [string]: ' [t T'

m3.index: 0
m3[0]: [string]: '[Z|abc=[def]'
m3[1]: [string]: ''
m3[2]: [string]: ''
m3[3]: [string]: 'Z'
m3[4]: [string]: '|abc=[def'
m3[5]: [string]: ''