From cf133c8945ea6787fc4cfb5b46bbd3ad1d0e644a Mon Sep 17 00:00:00 2001 From: akvlad Date: Fri, 5 Apr 2024 11:45:46 +0300 Subject: [PATCH] fix: avoid parsing ridiculously long strings --- parser/bnf.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/parser/bnf.js b/parser/bnf.js index 531d4817..50ce5c71 100644 --- a/parser/bnf.js +++ b/parser/bnf.js @@ -70,4 +70,33 @@ for (const [name, rule] of Object.entries(compiler.languages.logql.rules)) { } } +compiler._ParseScript = compiler.ParseScript +/** + * hack to avoid ridiculously long strings + * @param script {string} + * @constructor + */ +compiler.ParseScript = function (script) { + const qLiterals = [] + const aqLiterals = [] + const quotedStrings = script.replaceAll(/"([^"\\]|\\.)+"/g, (str) => { + qLiterals.push(str) + return `"QL_${qLiterals.length - 1}"` + }) + const aQuotedStrings = quotedStrings.replaceAll(/`([^`\\]|\\.)+`/g, (str) => { + aqLiterals.push(str) + return `\`AL_${qLiterals.length - 1}\`` + }) + const parsedScript = this._ParseScript(aQuotedStrings) + for (const t of parsedScript.rootToken.Children('QLITERAL')) { + t._value = qLiterals[parseInt(t.value.slice(4, t.value.length - 1))] + t.tokens = [] + } + for (const t of parsedScript.rootToken.Children('AQLITERAL')) { + t._value = aqLiterals[parseInt(t.value.slice(4, t.value.length - 1))] + t.tokens = [] + } + return parsedScript +} + module.exports = compiler