1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-12 11:15:14 +02:00

Fixed parsing escaped new line character (#677)

This commit is contained in:
Tim Voronov 2021-10-17 23:11:27 -04:00 committed by GitHub
parent bd6463fa29
commit a049f30214
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 8 deletions

View File

@ -0,0 +1,5 @@
timeout: 240
query:
ref: file://../../../examples/split-new-line.fql
assert:
text: RETURN T::NOT::EMPTY(@lab.data.query.result)

View File

@ -0,0 +1,4 @@
LET doc = DOCUMENT('https://github.com/events', {driver: 'cdp'})
LET list = ELEMENT(doc, '.footer')[0]
LET str = INNER_TEXT(list)
RETURN SPLIT(str, "\\n")

View File

@ -1121,18 +1121,53 @@ func (v *visitor) visitIntegerLiteral(ctx fql.IIntegerLiteralContext) (core.Expr
return literals.NewIntLiteral(val), nil
}
func (v *visitor) visitStringLiteral(c fql.IStringLiteralContext) (core.Expression, error) {
ctx := c.(*fql.StringLiteralContext)
var text string
func (v *visitor) visitStringLiteral(ctx fql.IStringLiteralContext) (core.Expression, error) {
var b strings.Builder
strLiteral := ctx.StringLiteral()
for _, child := range ctx.GetChildren() {
tree := child.(antlr.TerminalNode)
sym := tree.GetSymbol()
input := sym.GetInputStream()
if strLiteral != nil {
text = strLiteral.GetText()
if input == nil {
continue
}
size := input.Size()
// skip quotes
start := sym.GetStart() + 1
stop := sym.GetStop() - 1
if stop >= size {
stop = size - 1
}
if start < size && stop < size {
for i := start; i <= stop; i++ {
c := input.GetText(i, i)
switch c {
case "\\":
c2 := input.GetText(i, i+1)
switch c2 {
case "\\n":
b.WriteString("\n")
case "\\t":
b.WriteString("\t")
default:
b.WriteString(c2)
}
i++
default:
b.WriteString(c)
}
}
}
}
// remove extra quotes
return literals.NewStringLiteral(text[1 : len(text)-1]), nil
return literals.NewStringLiteral(b.String()), nil
}
func (v *visitor) visitBooleanLiteral(ctx fql.IBooleanLiteralContext) (core.Expression, error) {