Skip to content

Commit

Permalink
fix escaping variant: "a \\\n b"
Browse files Browse the repository at this point in the history
  • Loading branch information
ea-rus committed Mar 6, 2024
1 parent 446f673 commit 218018f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions mindsdb_sql/parser/dialects/mindsdb/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ def FLOAT(self, t):
def INTEGER(self, t):
return t

@_(r"'(?:[^\'\\]|\\.)*'")
@_(r"'(?:\\.|[^'])*'")
def QUOTE_STRING(self, t):
t.value = t.value.replace('\\"', '"').replace("\\'", "'")
return t

@_(r'"(?:[^\"\\]|\\.)*"')
@_(r'"(?:\\.|[^"])*"')
def DQUOTE_STRING(self, t):
t.value = t.value.replace('\\"', '"').replace("\\'", "'")
return t
Expand Down
34 changes: 19 additions & 15 deletions tests/test_parser/test_base_sql/test_base_sql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pytest
from textwrap import dedent
from mindsdb_sql import parse_sql

from mindsdb_sql.parser.ast import *
Expand Down Expand Up @@ -34,22 +34,26 @@ def test_not_equal(self):

def test_escaping(self):
expected_ast = Select(
targets=[Constant(value="a ' \" b")]
targets=[
Constant(value="a ' \" b"),
Constant(value="a ' \" b"),
Constant(value="a \\n b"),
Constant(value="a \\\n b"),
Constant(value="a \\\n b"),
Constant(value="a\nb"),
]
)

sql = """
select 'a \\' \\" b'
"""

ast = parse_sql(sql)

assert str(ast).lower() == str(expected_ast).lower()
assert ast.to_tree() == expected_ast.to_tree()

# in double quotes
sql = """
select "a \\' \\" b"
"""
sql = dedent('''
select
'a \\' \\" b', -- double quote
"a \\' \\" b", -- single quote
"a \\n b",
"a \\\n b", -- double quote
'a \\\n b', -- single quote
"a
b"
''')

ast = parse_sql(sql)

Expand Down

0 comments on commit 218018f

Please sign in to comment.