Subject: Re: bug#38302: SQL[ANSI] mode sees into comments

From: Lars Ingebrigtsen <[email-masked]>
Date: Fri, 20 May 2022 13:28:41 +0200

積丹尼 Dan Jacobson <[email-masked]> writes:

> Here you can see SQL[ANSI] mode has tons of errors:
> wrong colors for keywords that are actually in comments.
> And then a "-- what --" making the whole rest into a comment.
> $ emacs -nw -Q m.sql.bz2 # attachment below
> emacs-version "26.3"
>
> LOCK TABLES `comment` WRITE;
> /*!40000 ALTER TABLE `comment` DISABLE KEYS */;
> INSERT INTO `comment` VALUES (1,0,'',NULL),(2,-1141372718,_binary 'Importing text file',NULL),(3,28325391,_binary 'Created page with \'2009-07-12  [My answer 

The only problem I could see (in Emacs 29) at least was that sql-mode
expects quotes to be quoted like '', not like \'.  I.e., if I replaced
all the \' with '', then everything looked fine to me (but it was a long
snippet, so I'm not quite sure).

Is \' the correct way to escape a quote in some SQL dialects?  Hm...
googling seems to indicate that MySQL uses \' as an escape for '?  I
know nothing about MySQL -- does that seem correct?  Anybody?

In that case, the following should fix the problem (at the expense of
computing it the rules runtime)...

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index 8d25986090..ef8375e859 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -4161,28 +4161,33 @@ sql-mode
   (setq-local sql-contains-names t)
   (setq-local escaped-string-quote "'")
   (setq-local syntax-propertize-function
-              (syntax-propertize-rules
-               ;; Handle escaped apostrophes within strings.
-               ("''"
-                (0
-                 (if (save-excursion (nth 3 (syntax-ppss (match-beginning 0))))
-	             (string-to-syntax ".")
-                   (forward-char -1)
-                   nil)))
-               ;; Propertize rules to not have /- and -* start comments.
-               ("\\(/-\\)" (1 "."))
-               ("\\(-\\*\\)"
-                (1
-                 (if (save-excursion
-                       (not (ppss-comment-depth
-                             (syntax-ppss (match-beginning 1)))))
-                     ;; If we're outside a comment, we don't let -*
-                     ;; start a comment.
-	             (string-to-syntax ".")
-                   ;; Inside a comment, ignore it to avoid -*/ not
-                   ;; being interpreted as a comment end.
-                   (forward-char -1)
-                   nil)))))
+              (eval
+               '(syntax-propertize-rules
+                 ;; Handle escaped apostrophes within strings.
+                 ((if (eq sql-product 'mysql)
+                      "\\\\'"
+                    "''")
+                  (0
+                   (if (save-excursion
+                         (nth 3 (syntax-ppss (match-beginning 0))))
+	               (string-to-syntax ".")
+                     (forward-char -1)
+                     nil)))
+                 ;; Propertize rules to not have /- and -* start comments.
+                 ("\\(/-\\)" (1 "."))
+                 ("\\(-\\*\\)"
+                  (1
+                   (if (save-excursion
+                         (not (ppss-comment-depth
+                               (syntax-ppss (match-beginning 1)))))
+                       ;; If we're outside a comment, we don't let -*
+                       ;; start a comment.
+	               (string-to-syntax ".")
+                     ;; Inside a comment, ignore it to avoid -*/ not
+                     ;; being interpreted as a comment end.
+                     (forward-char -1)
+                     nil))))
+               t))
   ;; Set syntax and font-face highlighting
   ;; Catch changes to sql-product and highlight accordingly
   (sql-set-product (or sql-product 'ansi)) ; Fixes bug#13591

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no