Subject: sql.el: patch for syntax table propertizing escaped apostrophes in SQL strings as punctuation syntax class

From: "Florian v. Savigny" <[email-masked]>
Date: Wed, 25 Mar 2020 22:42:58 +0100 (CET)

Dear Emacs folks, dear Michael,

sql.el parses SQL strings containing escaped (double) apostrophes as separate strings (e.g. jumping right between the two apostrophes with forward-sexp) because it uses only parsing by syntax table, which cannot handle this construct.

This should be an adequate description of the limitation/bug, but more details are available at: 

   https://emacs.stackexchange.com/questions/56352/can-syntax-parsing-recognize-the-sql-escape-apostrophe-single-quote-constru

The attached is a diff against sql.el version 3.6 as shipped with Emacs 26.3. that implements propertizing such double apostrophes within strings with a punctuation syntax table property. (I have posted the code with exhaustive comments, which I thought excessive for working code, at the above URL.) My tests were movement commands across sexps, which crossed the whole string, and the return value of 

  (syntax-ppss (point))

which returned a list such as 

  (1 129 139 39 nil nil 0 nil 154 (129) nil)

when between two such apostrophes, just like when elsewhere in a string. I have done no further testing.

I do hope this meets the standards for a patch, as I've never submitted one before.

Best regards to everybody, and stay well!

Florian

Florian v. Savigny 
Siebenpfeiffer Str. 25 
66482 Zweibrücken 
Germany

0175 - 365 24 17

--- sql-prepatch.el	2020-03-25 20:16:42.162589800 +0100
+++ sql.el	2020-03-25 22:09:48.157006300 +0100
@@ -1,6 +1,6 @@
 ;;; sql.el --- specialized comint.el for SQL interpreters  -*- lexical-binding: t -*-
 
-;; Copyright (C) 1998-2019 Free Software Foundation, Inc.
+;; Copyright (C) 1998-2020 Free Software Foundation, Inc.
 
 ;; Author: Alex Schroeder <[email-masked]>
 ;; Maintainer: Michael Mauger <[email-masked]>
@@ -1373,6 +1373,25 @@
     table)
   "Syntax table used in `sql-mode' and `sql-interactive-mode'.")
 
+;;; Syntax Properties
+
+;; `sql--syntax-propertize-escaped-apostrophe', as follows, was
+;; (analysed and) adapted from `pascal--syntax-propertize' in
+;; pascal.el on 2020-02-25 because basic syntax parsing cannot handle
+;; the SQL '' construct within strings. For an in-depth explanation,
+;; please see:
+;;
+;; https://emacs.stackexchange.com/questions/56352/can-syntax-parsing-recognize-the-sql-escape-apostrophe-single-quote-constru
+
+(defconst sql--syntax-propertize-escaped-apostrophe
+  (syntax-propertize-rules
+   ("''"
+    (0
+     (if (save-excursion (nth 3 (syntax-ppss (match-beginning 0))))
+	 (string-to-syntax ".")
+       (forward-char -1)
+       nil)))))
+
 ;; Font lock support
 
 (defvar sql-mode-font-lock-object-name
@@ -3865,6 +3884,10 @@
   (setq-local abbrev-all-caps 1)
   ;; Contains the name of database objects
   (set (make-local-variable 'sql-contains-names) t)
+  ;; Activate punctuation syntax table property for
+  ;; escaped apostrophes within strings:
+  (setq-local syntax-propertize-function sql--syntax-propertize-escaped-apostrophe)
+  (setq-local parse-sexp-lookup-properties t) ; just to make sure it is used
   ;; Set syntax and font-face highlighting
   ;; Catch changes to sql-product and highlight accordingly
   (sql-set-product (or sql-product 'ansi)) ; Fixes bug#13591