Skip to content

Commit

Permalink
fix: avoid parsing ridiculously long strings
Browse files Browse the repository at this point in the history
  • Loading branch information
akvlad committed Apr 5, 2024
1 parent 2ebbf89 commit cf133c8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions parser/bnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cf133c8

Please sign in to comment.