From a049f30214594309b85cf89714dd7dccce266b98 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Sun, 17 Oct 2021 23:11:27 -0400 Subject: [PATCH] Fixed parsing escaped new line character (#677) --- e2e/tests/examples/split-new-line.yaml | 5 +++ examples/split-new-line.fql | 4 ++ pkg/compiler/visitor.go | 51 ++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 e2e/tests/examples/split-new-line.yaml create mode 100644 examples/split-new-line.fql diff --git a/e2e/tests/examples/split-new-line.yaml b/e2e/tests/examples/split-new-line.yaml new file mode 100644 index 00000000..e4e6efd6 --- /dev/null +++ b/e2e/tests/examples/split-new-line.yaml @@ -0,0 +1,5 @@ +timeout: 240 +query: + ref: file://../../../examples/split-new-line.fql +assert: + text: RETURN T::NOT::EMPTY(@lab.data.query.result) \ No newline at end of file diff --git a/examples/split-new-line.fql b/examples/split-new-line.fql new file mode 100644 index 00000000..852d356d --- /dev/null +++ b/examples/split-new-line.fql @@ -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") \ No newline at end of file diff --git a/pkg/compiler/visitor.go b/pkg/compiler/visitor.go index bd4a74b9..3e637815 100644 --- a/pkg/compiler/visitor.go +++ b/pkg/compiler/visitor.go @@ -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) {