Skip to content

Commit

Permalink
Fixed parsing escaped new line character (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex authored Oct 18, 2021
1 parent bd6463f commit a049f30
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions e2e/tests/examples/split-new-line.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
timeout: 240
query:
ref: file://../../../examples/split-new-line.fql
assert:
text: RETURN T::NOT::EMPTY(@lab.data.query.result)
4 changes: 4 additions & 0 deletions examples/split-new-line.fql
Original file line number Diff line number Diff line change
@@ -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")
51 changes: 43 additions & 8 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a049f30

Please sign in to comment.