Merge pull request #6070 from AJ0070/sonnet-js-zone-link

fix: tokenize linked monitor expressions with parentheses
This commit is contained in:
Isaac Connor authored and GitHub committed 2026-11-15 21:13:00 -05:00
commit 511b18ef92
2 files changed
+56 -1

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const src = fs.readFileSync(
path.join(__dirname, '../../web/js/MonitorLinkExpression.js'), 'utf8');
const ctx = vm.createContext({console: console});
vm.runInContext(src, ctx);
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(' ok ' + name);
passed++;
} catch (e) {
console.error(' FAIL ' + name);
console.error(' ' + (e.stack || e.message));
failed++;
}
}
function tok(expr) {
const out = vm.runInContext(
'tokenize(' + JSON.stringify(expr) + ').map((t) => t.type + ":" + t.value)', ctx);
return Array.from(out);
}
test('plain or expression', () => {
assert.deepStrictEqual(tok('1|2:3'), ['link:1', '|:|', 'link:2:3']);
});
test('single parentheses', () => {
assert.deepStrictEqual(tok('(1|2)&3'),
['(:(', 'link:1', '|:|', 'link:2', '):)', '&:&', 'link:3']);
});
test('repeated parentheses are one token', () => {
assert.deepStrictEqual(tok('((1|2)&3)'),
['(:((', 'link:1', '|:|', 'link:2', '):)', '&:&', 'link:3', '):)']);
assert.deepStrictEqual(tok('(1&(2|3))'),
['(:(', 'link:1', '&:&', '(:(', 'link:2', '|:|', 'link:3', '):))']);
});
test('empty expression gives no tokens', () => {
assert.deepStrictEqual(tok(''), []);
});
console.log(passed + ' passed, ' + failed + ' failed');
process.exit(failed ? 1 : 0);
+2 -1
View File
@@ -39,7 +39,7 @@ function tokenize(expr) {
let third = second_index+1;
for (; third<expr.length; third++) {
if (expr.at(i) != character) break;
if (expr.at(third) != character) break;
}
if (third != second_index+1) {
tokens[tokens.length] = {type: character, value: expr.substring(second_index, third)};
@@ -47,6 +47,7 @@ function tokenize(expr) {
tokens[tokens.length] = {type: character, value: character};
}
first_index = third;
second_index = third - 1;
}
second_index++;
} // end for second_index