I am trying to enhance SQL mode such that editing of files of SQL code becomes more convenient, especially as far as jumping around is concerned (e.g. things like sql-goto-end-of-string etc.). I have found syntax parsing to be very useful in that, but have stumbled across one curious thing:
SQL has the apostrophe as the string delimiter and the (as would seem to me) quirky idea of using, of all possibilities, a double apostrophe to denote a literal apostrophe within a string, i.e. the '
doubles as its own escape character when it occurs two times in a row, such that 'McDonald''s'
in SQL means what would be written as e.g. "McDonald's"
or 'McDonald\'s'
in some other languages. It would seem to mean to me that any SQL parser, when encountering an apostrophe inside a string, must always read the next character before it can decide whether this is the end of the string or rather a literal apostrophe inside it.
As to Emacs in sql-mode
, (nth 3 (parse-partial-sexp (point-min) pos))
in sql-mode returns the following when parsing over such a construct (^
denotes point):
'McDonald''s'
^ --> 39
'McDonald''s'
^ --> nil
'McDonald''s'
^ --> 39
In other words, it treats the sequence as two separate, abutting strings, 'McDonald'
and 's'
(which of course do not exist in valid SQL syntax), presumably because '
is simply the string terminator in sql-mode-syntax-table
((char-to-string (char-syntax ?'))
returns "\""
and is simply described as " which means: string
by describe-syntax
).
My question is whether Emacs is in principle capable of parsing this construct correctly, if, e.g., given a more complex syntax table entry for '
.
I see it is capable of parsing two-character comment delimiters, such that /
has a specific meaning when followed by *
, and *
is specific when inside a comment and followed by /
. The provisions in syntax tables for such sequences, which AFAIU seem to be the syntax flags, seem to be specific to comments, however, and the exact problem also seems to be different (I cannot think of an example of a single "end comment" character that is escaped with itself in the languages that I am familiar with, at least), as far as I can tell (I have not looked into prefix characters, admittedly).
I would readily accept if this is simply a limitation of Emacs' syntax parsing (and would not think it too hard to come up with workarounds for my purposes), but my impression is that syntax parsing is the most efficient method, so it would be really cool to do it this way if it can be done.
(I do wonder whether using syntax-table properties would work, e.g. by assigning the punctuation syntax class to ''
within strings, and if so, how substantial the overhead would be.)
I would greatly appreciate any pointers through the jungle! Thanks!