diff --git a/datasette/utils/__init__.py b/datasette/utils/__init__.py index 7b9a90b9..42574d3b 100644 --- a/datasette/utils/__init__.py +++ b/datasette/utils/__init__.py @@ -1294,9 +1294,11 @@ class StartupError(Exception): _comments_and_strings_re = re.compile( r""" --[^\n]* # single line comment - | /\*.*?\*/ # multi line comment + | /\*.*?(?:\*/|\Z) # multi line comment, possibly to end-of-input | '(?:''|[^'])*' # single quoted string ('' escapes a quote) | "(?:""|[^"])*" # double quoted identifier ("" escapes a quote) + | \[(?:[^\]])*\] # square-bracket quoted identifier + | `(?:``|[^`])*` # backtick quoted identifier """, re.DOTALL | re.VERBOSE, ) diff --git a/tests/test_utils.py b/tests/test_utils.py index 0b2fcd2b..a535ca93 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -764,6 +764,11 @@ def test_parse_metadata(content, expected): # Parameters that live inside a comment should be ignored ("select :x -- and :ignored", ["x"]), ("select :x /* and :ignored */ from t", ["x"]), + ("select :x /* and :ignored", ["x"]), + # Parameters inside quoted identifiers should be ignored + ("select [a:b] from t where id = :id", ["id"]), + ("select `a:b` from t where id = :id", ["id"]), + ("select `a``:b` from t where id = :id", ["id"]), # Parameters inside a string literal should be ignored ("select ':ignored' || :real", ["real"]), ),