Skip to content

Commit

Permalink
Handle parentheses in decomposeWhenClause #296
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Dec 12, 2023
1 parent 960e1a2 commit 841f5e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
23 changes: 22 additions & 1 deletion generator/gen_wrapper_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,28 @@ const decomposeWhenClause = function(when) {
return result;
};
const tokens = when.split(/(\|\||\&\&)/);
const ors_of_ands = splitArray(tokens, '||').map(a =>
let nest = 0;
const outMostTokens = [];
for (let i = 0; i < tokens.length; i++) {
let str = tokens[i];
let open = 0, close = 0;
let m;
while ((m = str.match(/^\s*(\!\s*)*[(]/)) !== null) {
open += 1;
str = str.slice(m[0].length);
}
while ((m = str.match(/[)]\s*$/)) !== null) {
close += 1;
str = str.slice(0, str.length - m[0].length);
}
if (nest === 0) {
outMostTokens.push(tokens[i]);
} else {
outMostTokens[outMostTokens.length - 1] += tokens[i];
}
nest += open - close;
}
const ors_of_ands = splitArray(outMostTokens, '||').map(a =>
splitArray(a, '&&').map(x => x.join('').trim())
);
return ors_of_ands;
Expand Down
15 changes: 12 additions & 3 deletions test/suite/gen_wrapper_util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ describe('gen_wrapper_util', () => {
assert.deepStrictEqual(decomposeWhenClause('!c'), [['!c']]);
assert.deepStrictEqual(decomposeWhenClause('c1 || !c2 || !!c3'), [['c1'], ['!c2'], ['!!c3']]);
});
// TODO: https://github.com/tshino/vscode-kb-macro/issues/296
/*
it('should leave parenthesized portion unchanged', () => {
assert.deepStrictEqual(decomposeWhenClause('(c1 || c2)'), [['(c1 || c2)']]);
assert.deepStrictEqual(decomposeWhenClause('!(c1 || c2)'), [['!(c1 || c2)']]);
assert.deepStrictEqual(decomposeWhenClause('c1 && (c2 || c3)'), [['c1', '(c2 || c3)']]);
assert.deepStrictEqual(decomposeWhenClause('c1 || (c2 && c3)'), [['c1'], ['(c2 && c3)']]);
});
*/
it('should handle nested parenthesized portion', () => {
assert.deepStrictEqual(decomposeWhenClause('((c1 || c2))'), [['((c1 || c2))']]);
assert.deepStrictEqual(decomposeWhenClause('(!(c1 || c2))'), [['(!(c1 || c2))']]);
assert.deepStrictEqual(decomposeWhenClause('c1 && ((c2 && c3) || c4)'), [['c1', '((c2 && c3) || c4)']]);
assert.deepStrictEqual(decomposeWhenClause('(c1 || !(c2 && c3)) && c4'), [['(c1 || !(c2 && c3))', 'c4']]);
});
it('should handle spaces around parentheses', () => {
assert.deepStrictEqual(decomposeWhenClause('( (c1 || c2)) || c3'), [['( (c1 || c2))'], ['c3']]);
assert.deepStrictEqual(decomposeWhenClause('((c1 || c2) ) || c3'), [['((c1 || c2) )'], ['c3']]);
assert.deepStrictEqual(decomposeWhenClause('! (c1 || c2)'), [['! (c1 || c2)']]);
assert.deepStrictEqual(decomposeWhenClause('( !(c1 || c2))'), [['( !(c1 || c2))']]);
});
});
describe('addWhenContext', () => {
const addWhenContext = genWrapperUtil.addWhenContext;
Expand Down

0 comments on commit 841f5e2

Please sign in to comment.