widget: use balanced-match instead of regexp to parse advanced syntaxes (fixes #714)

This commit is contained in:
bordun 2021-04-29 13:48:43 +02:00
parent d72dad50e1
commit 385ed003de
4 changed files with 43 additions and 26 deletions

View file

@ -5,6 +5,7 @@
- bug fixes
- scripting: set() not working from a slider to a pad
- tabs not sending messages / triggering scripts when clicked in editing mode
- prevent hang with some syntax errors in advanced syntaxes
- editor
- tree: allow specifying multiple type filters

View file

@ -27,6 +27,7 @@
"ansi-html": "0.0.7",
"babel-eslint": "10.1.0",
"babelify": "10.0.0",
"balanced-match": "1.0.0",
"bonjour": "github:jean-emmanuel/bonjour",
"browserify": "17.0.0",
"chokidar": "3.5.1",

View file

@ -2,7 +2,7 @@ var EventEmitter = require('../../events/event-emitter'),
osc = require('../../osc'),
{nanoid} = require('nanoid'),
widgetManager = require('../../managers/widgets'),
{urlParser} = require('../utils'),
{urlParser, balancedReplace} = require('../utils'),
Vm = require('../vm'),
vm = new Vm(),
scopeCss = require('scope-css'),
@ -504,25 +504,17 @@ class Widget extends EventEmitter {
if (typeof propValue == 'string') {
propValue = propValue.replace(/@\{[^{}]*((@\{[^{}]*\}|[^{}]*)*)\}/g, (m, nested)=>{
// pretty regexp:
// @\{
// [^{}]*
// ((
// @\{
// [^{}]*
// \}
// |
// [^{}]*
// )*)
//
// \}
propValue = balancedReplace('@{', '}', propValue, (content)=>{
if (content.indexOf('@{') >= 0) {
content = balancedReplace('@{', '}', content, (subcontent)=>{
return this.resolveProp(propName, '@{' + subcontent + '}', storeLinks ? 'nested' : false, this)
})
if (nested) {
m = m.replace(nested, this.resolveProp(propName, nested, storeLinks ? 'nested' : false, this))
}
let id = m.substr(2, m.length - 3).split('.'),
let id = content.split('.'),
k, subk
if (id.length > 1) {
@ -625,9 +617,11 @@ class Widget extends EventEmitter {
}
return 'undefined'
})
propValue = propValue.replace(/OSC\{([^}]+)\}/g, (m, args)=>{
propValue = balancedReplace('OSC{', '}', propValue, (args)=>{
let [address, value, usePreArgs] = args.split(',')
.map(x=>x.trim())
@ -665,7 +659,7 @@ class Widget extends EventEmitter {
})
try {
propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{
propValue = balancedReplace('JS{{', '}}', propValue, (code)=>{
if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope)
@ -681,12 +675,8 @@ class Widget extends EventEmitter {
}
try {
propValue = propValue.replace(/#\{(?:[^{}]|\{[^{}]*\})*\}/g, (m)=>{
// one bracket nesting allowed, if we need two: #\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\}
var code = m.substr(2, m.length - 3).trim()
if (!this.parsers[code]) this.parsers[code] = vm.compile('return ' + code, defaultScope)
propValue = balancedReplace('#{', '}', propValue, (code)=>{
if (!this.parsers[code]) this.parsers[code] = vm.compile('return ' + code.trim(), defaultScope)
let r = this.parsers[code](jsScope, this.parsersLocalScope)
@ -696,6 +686,7 @@ class Widget extends EventEmitter {
})
} catch (err) {
this.errorProp(propName, '#{}', err)
}

View file

@ -1,3 +1,5 @@
var balanced = require('balanced-match')
module.exports = {
clip: function(value,range) {
@ -113,6 +115,28 @@ module.exports = {
return parser
}
})()
})(),
balancedReplace(open, close, string, replacement) {
var b
while (b = balanced(open, close, string)) {
if (typeof replacement === 'function') {
string = b.pre + replacement(b.body) + b.post
} else {
string = b.pre + replacement + b.post
}
}
return string
}
}