Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix syntax highlight for pgsql mode for backslash and escape constant #6081

Merged
merged 3 commits into from
Dec 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions mode/sql/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
// charset casting: _utf8'str', N'str', n'str'
// ref: http://dev.mysql.com/doc/refman/5.5/en/string-literals.html
return "keyword";
} else if (support.escapeConstant && (ch == "e" || ch == "E")
&& (stream.peek() == "'" || (stream.peek() == '"' && support.doubleQuote))) {
// escape constant: E'str', e'str'
// ref: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE
state.tokenize = tokenLiteral(stream.peek(), true);
return "keyword";
} else if (support.commentSlashSlash && ch == "/" && stream.eat("/")) {
// 1-line comment
stream.skipToEnd();
Expand Down Expand Up @@ -122,15 +128,19 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
}

// 'string', with char specified in quote escaped by '\'
function tokenLiteral(quote) {
function tokenLiteral(quote, withEscapeConst) {
return function(stream, state) {
var escaped = false, ch;
/* eat the first quote in case of escape constant */
if(withEscapeConst) {
ch = stream.eat(quote)
}
while ((ch = stream.next()) != null) {
if (ch == quote && !escaped) {
state.tokenize = tokenBase;
break;
}
escaped = backslashStringEscapes && !escaped && ch == "\\";
escaped = (backslashStringEscapes || withEscapeConst) && !escaped && ch == "\\";
}
return "string";
};
Expand Down Expand Up @@ -413,8 +423,9 @@ CodeMirror.defineMode("sql", function(config, parserConfig) {
builtin: set("bigint int8 bigserial serial8 bit varying varbit boolean bool box bytea character char varchar cidr circle date double precision float8 inet integer int int4 interval json jsonb line lseg macaddr macaddr8 money numeric decimal path pg_lsn point polygon real float4 smallint int2 smallserial serial2 serial serial4 text time without zone with timetz timestamp timestamptz tsquery tsvector txid_snapshot uuid xml"),
atoms: set("false true null unknown"),
operatorChars: /^[*\/+\-%<>!=&|^\/#@?~]/,
backslashStringEscapes: false,
dateSQL: set("date time timestamp"),
support: set("ODBCdotTable decimallessFloat zerolessFloat binaryNumber hexNumber nCharCast charsetCast")
support: set("ODBCdotTable decimallessFloat zerolessFloat binaryNumber hexNumber nCharCast charsetCast escapeConstant")
});

// Google's SQL-like query language, GQL
Expand Down