Skip to content

Commit

Permalink
fix: empty strings hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
akvlad committed Apr 12, 2024
1 parent 0d8a059 commit 18dadf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
16 changes: 14 additions & 2 deletions parser/bnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ compiler._ParseScript = compiler.ParseScript
compiler.ParseScript = function (script) {
const qLiterals = []
const aqLiterals = []
const quotedStrings = script.replaceAll(/"([^"\\]|\\.)+"/g, (str) => {
const quotedStrings = script.replaceAll(/"([^"\\]|\\.)*"/g, (str) => {
if (str.length < 512) {
return str
}
qLiterals.push(str)
return `"QL_${qLiterals.length - 1}"`
})
const aQuotedStrings = quotedStrings.replaceAll(/`([^`\\]|\\.)+`/g, (str) => {
const aQuotedStrings = quotedStrings.replaceAll(/`([^`\\]|\\.)*`/g, (str) => {
if (str.length < 512) {
return str
}
aqLiterals.push(str)
return `\`AL_${aqLiterals.length - 1}\``
})
Expand All @@ -92,10 +98,16 @@ compiler.ParseScript = function (script) {
return parsedScript
}
for (const t of parsedScript.rootToken.Children('QLITERAL')) {
if (!t.value.match(/^"QL_\d+"$/)) {
continue
}
t._value = qLiterals[parseInt(t.value.slice(4, t.value.length - 1))]
t.tokens = []
}
for (const t of parsedScript.rootToken.Children('AQLITERAL')) {
if (!t.value.match(/^`AL_\d+`$/)) {
continue
}
t._value = aqLiterals[parseInt(t.value.slice(4, t.value.length - 1))]
t.tokens = []
}
Expand Down
12 changes: 12 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,15 @@ it('should parse parameterized fns', () => {
exp = bnf.ParseScript('topk(5, sum(rate({a="b"}|unwrap b[1s]) by (a)) by (b) > 1)')
expect(exp.rootToken.value).toEqual('topk(5, sum(rate({a="b"}|unwrap b[1s]) by (a)) by (b) > 1)')
})

it('should parse a default request', () => {
const req = '{job="svc", level=~"ERR|WRN|INF"} |= `` | json Environment="req[\\"depl.env\\"]", AccountId="att[\\"AccId\\"]", ClientId="att[\\"ClId\\"]", RId="att[\\"Id\\"]", body="body", RequestPath="att[\\"ReqPath\\"]", id="att[\\"id\\"]", Transaction="att[\\"Txn\\"]", Scope="IS[\\"name\\"]", Whence="att[\\"Wnc\\"]", att="att[\\"ex.msg\\"]" | Environment = `A`'
const exp = bnf.ParseScript(req)
expect(exp.rootToken.value).toEqual(req)
})

it('should parse a long string', () => {
const req = '{job="svc", level=~"json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24|json1|json2|json3|json4|json5|json6|json7|json8|json9|json10|json11|json12|json13|json14|json15|json16|json17|json18|json19|json20|json21|json22|json23|json24"}'
const exp = bnf.ParseScript(req)
expect(exp.rootToken.value).toEqual(req)
})

0 comments on commit 18dadf5

Please sign in to comment.