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

New language for PostgreSQL SQL dialect and PL/pgSQL #1804

Merged
merged 12 commits into from
Aug 31, 2018
1 change: 1 addition & 0 deletions AUTHORS.en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,4 @@ Contributors:
- Morten Piibeleht <morten.piibeleht@gmail.com>
- Martin Clausen <martin.clausene@gmail.com>
- Ahmad Awais <me@AhmadAwais.com>
- Egor Rogov <e.rogov@postgrespro.ru>
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
## Master

New language:

- *PostgreSQL* SQL dialect and PL/pgSQL language by [Egor Rogov][].

New style:

- *Lightfair* by [Tristian Kelly][]

Improvements:

- New attribute endSameAsBegin for nested constructs with variable names
by [Egor Rogov][].

[Tristian Kelly]: https://github.com/TristianK3604
[Egor Rogov]: https://github.com/egor-rogov

## Version 9.12.0

Expand Down
2 changes: 2 additions & 0 deletions docs/css-classes-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ Language names and aliases
+-------------------------+---------------------------------------------------+
| Pony | pony |
+-------------------------+---------------------------------------------------+
| PostgreSQL & PL/pgSQL | pgsql, postgres, postgresql |
+-------------------------+---------------------------------------------------+
| PowerShell | powershell, ps |
+-------------------------+---------------------------------------------------+
| Processing | processing |
Expand Down
28 changes: 28 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,34 @@ tell it to end the function definition after itself:

(The ``end: /\B\b/`` regex tells function to never end by itself.)

.. _endSameAsBegin:

endSameAsBegin
^^^^^^^^^^^^^^

**type**: boolean

Acts as ``end`` matching exactly the same string that was found by the
corresponding ``begin`` regexp.

For example, in PostgreSQL string constants can uee "dollar quotes",
consisting of a dollar sign, an optional tag of zero or more characters,
and another dollar sign. String constant must be ended with the same
construct using the same tag. It is possible to nest dollar-quoted string
constants by choosing different tags at each nesting level:

::

$foo$
...
$bar$ nested $bar$
...
$foo$

In this case you can't simply specify the same regexp for ``begin`` and
``end`` (say, ``"\\$[a-z]\\$"``), but you can use ``begin: "\\$[a-z]\\$"``
and ``endSameAsBegin: true``.

.. _lexemes:

lexemes
Expand Down
12 changes: 12 additions & 0 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ https://highlightjs.org/
if (!mode.begin)
mode.begin = /\B|\b/;
mode.beginRe = langRe(mode.begin);
if (mode.endSameAsBegin)
mode.end = mode.begin;
if (!mode.end && !mode.endsWithParent)
mode.end = /\B|\b/;
if (mode.end)
Expand Down Expand Up @@ -322,11 +324,18 @@ https://highlightjs.org/
*/
function highlight(name, value, ignore_illegals, continuation) {

function escapeRe(value) {
return new RegExp(value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'm');
}

function subMode(lexeme, mode) {
var i, length;

for (i = 0, length = mode.contains.length; i < length; i++) {
if (testRe(mode.contains[i].beginRe, lexeme)) {
if (mode.contains[i].endSameAsBegin) {
mode.contains[i].endRe = escapeRe( mode.contains[i].beginRe.exec(lexeme)[0] );
}
return mode.contains[i];
}
}
Expand Down Expand Up @@ -472,6 +481,9 @@ https://highlightjs.org/
top = top.parent;
} while (top !== end_mode.parent);
if (end_mode.starts) {
if (end_mode.endSameAsBegin) {
end_mode.starts.endRe = end_mode.endRe;
}
startNewMode(end_mode.starts, '');
}
return origin.returnEnd ? 0 : lexeme.length;
Expand Down
504 changes: 504 additions & 0 deletions src/languages/pgsql.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions test/detect/pgsql/default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BEGIN;
SELECT sum(salary) OVER w, avg(salary) OVER w
FROM empsalary
WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);
END;

CREATE FUNCTION days_of_week() RETURNS SETOF text AS $$
BEGIN
FOR i IN 7 .. 13 LOOP
RETURN NEXT to_char(to_date(i::text,'J'),'TMDy');
END LOOP;
END;
$$ STABLE LANGUAGE plpgsql;
67 changes: 67 additions & 0 deletions test/markup/pgsql/clauses.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<span class="hljs-comment">-- clauses</span>

<span class="hljs-keyword">ADD</span>, <span class="hljs-keyword">ADD</span> <span class="hljs-keyword">COLUMN</span>,
<span class="hljs-keyword">ALTER</span>,
<span class="hljs-keyword">DROP</span>, <span class="hljs-keyword">DROP</span> <span class="hljs-keyword">COLUMN</span>,
<span class="hljs-keyword">SET</span>, <span class="hljs-keyword">SET</span> ( .. ),
<span class="hljs-keyword">ON</span>,
<span class="hljs-keyword">ACCESS</span> <span class="hljs-keyword">METHOD</span>,
<span class="hljs-keyword">AGGREGATE</span>,
<span class="hljs-keyword">ATTRIBUTE</span>,
<span class="hljs-keyword">CASCADE</span>,
<span class="hljs-keyword">COLLATION</span>,
<span class="hljs-keyword">COLUMN</span>,
<span class="hljs-keyword">CONFLICT</span>,
<span class="hljs-keyword">CONSTRAINT</span>,
<span class="hljs-keyword">CONVERSION</span>,
<span class="hljs-keyword">DATABASE</span>,
<span class="hljs-keyword">DEFAULT</span>,
<span class="hljs-keyword">DOMAIN</span>,
<span class="hljs-keyword">EVENT TRIGGER</span>,
<span class="hljs-keyword">EXTENSION</span>,
<span class="hljs-keyword">EVENT TRIGGER</span>,
<span class="hljs-keyword">FOREIGN DATA</span> <span class="hljs-keyword">WRAPPER</span>,
<span class="hljs-keyword">FOREIGN</span> <span class="hljs-keyword">TABLE</span>,
<span class="hljs-keyword">FROM</span> <span class="hljs-keyword">CURRENT</span>,
<span class="hljs-keyword">FUNCTION</span>,
<span class="hljs-keyword">IDENTITY</span>,
<span class="hljs-keyword">INDEX</span>,
<span class="hljs-keyword">ISOLATION</span> <span class="hljs-keyword">LEVEL</span> <span class="hljs-keyword">SERIALIZABLE</span>, <span class="hljs-keyword">ISOLATION</span> <span class="hljs-keyword">LEVEL</span> <span class="hljs-keyword">REPEATABLE</span> <span class="hljs-keyword">READ</span>,
<span class="hljs-keyword">ISOLATION</span> <span class="hljs-keyword">LEVEL</span> <span class="hljs-keyword">READ</span> <span class="hljs-keyword">COMMITTED</span>, <span class="hljs-keyword">ISOLATION</span> <span class="hljs-keyword">LEVEL</span> <span class="hljs-keyword">READ</span> <span class="hljs-keyword">UNCOMMITTED</span>,
<span class="hljs-keyword">LARGE</span> <span class="hljs-keyword">OBJECT</span>,
<span class="hljs-keyword">LOGGED</span>, <span class="hljs-keyword">UNLOGGED</span>,
<span class="hljs-keyword">MAPPING</span> <span class="hljs-keyword">FOR</span> .. <span class="hljs-keyword">WITH</span> .., <span class="hljs-keyword">MAPPING REPLACE</span> .. <span class="hljs-keyword">WITH</span> ..,
<span class="hljs-keyword">MATERIALIZED</span> <span class="hljs-keyword">VIEW</span>,
<span class="hljs-keyword">NOT</span> <span class="hljs-keyword">VALID</span>,
<span class="hljs-keyword">OPERATOR</span>,
<span class="hljs-keyword">OPERATOR</span> <span class="hljs-keyword">CLASS</span> .. <span class="hljs-keyword">USING</span> ..,
<span class="hljs-keyword">OPERATOR</span> <span class="hljs-keyword">FAMILY</span> .. <span class="hljs-keyword">USING</span> ..,
<span class="hljs-keyword">POLICY</span>,
<span class="hljs-keyword">PROCEDURAL</span> <span class="hljs-keyword">LANGUAGE</span>,
<span class="hljs-keyword">PROCEDURE</span>,
<span class="hljs-keyword">PUBLICATION</span>,
<span class="hljs-keyword">READ</span> <span class="hljs-keyword">WRITE</span>, <span class="hljs-keyword">READ</span> <span class="hljs-keyword">ONLY</span>,
<span class="hljs-keyword">RESTRICT</span>,
<span class="hljs-keyword">ROLE</span>,
<span class="hljs-keyword">ROUTINE</span>,
<span class="hljs-keyword">RULE</span>,
<span class="hljs-keyword">SCHEMA</span>,
<span class="hljs-keyword">SEQUENCE</span>,
<span class="hljs-keyword">SERVER</span>,
<span class="hljs-keyword">STATISTICS</span>,
<span class="hljs-keyword">STORAGE PLAIN</span>, <span class="hljs-keyword">STORAGE EXTERNAL</span>, <span class="hljs-keyword">STORAGE EXTENDED</span>, <span class="hljs-keyword">STORAGE MAIN</span>,
<span class="hljs-keyword">SUBSCRIPTION</span>,
<span class="hljs-keyword">TABLE</span>,
<span class="hljs-keyword">TABLESPACE</span>,
<span class="hljs-keyword">TEXT SEARCH</span> <span class="hljs-keyword">CONFIGURATION</span>,
<span class="hljs-keyword">TEXT SEARCH</span> <span class="hljs-keyword">DICTIONARY</span>,
<span class="hljs-keyword">TEXT SEARCH</span> <span class="hljs-keyword">PARSER</span>,
<span class="hljs-keyword">TEXT SEARCH</span> <span class="hljs-keyword">TEMPLATE</span>,
<span class="hljs-keyword">TRANSFORM</span> <span class="hljs-keyword">FOR</span> .. <span class="hljs-keyword">LANGUAGE</span> <span class="hljs-keyword">SQL</span>,
<span class="hljs-keyword">TRIGGER</span>,
<span class="hljs-keyword">TYPE</span>,
<span class="hljs-keyword">VALUE</span>,
<span class="hljs-keyword">VIEW</span>,
<span class="hljs-keyword">WITH</span> <span class="hljs-keyword">OIDS</span>, <span class="hljs-keyword">WITHOUT</span> <span class="hljs-keyword">OIDS</span>,
<span class="hljs-keyword">WITHOUT</span> <span class="hljs-keyword">CLUSTER</span>,
<span class="hljs-keyword">SET DATA</span> <span class="hljs-keyword">TYPE</span>;
67 changes: 67 additions & 0 deletions test/markup/pgsql/clauses.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- clauses

ADD, ADD COLUMN,
ALTER,
DROP, DROP COLUMN,
SET, SET ( .. ),
ON,
ACCESS METHOD,
AGGREGATE,
ATTRIBUTE,
CASCADE,
COLLATION,
COLUMN,
CONFLICT,
CONSTRAINT,
CONVERSION,
DATABASE,
DEFAULT,
DOMAIN,
EVENT TRIGGER,
EXTENSION,
EVENT TRIGGER,
FOREIGN DATA WRAPPER,
FOREIGN TABLE,
FROM CURRENT,
FUNCTION,
IDENTITY,
INDEX,
ISOLATION LEVEL SERIALIZABLE, ISOLATION LEVEL REPEATABLE READ,
ISOLATION LEVEL READ COMMITTED, ISOLATION LEVEL READ UNCOMMITTED,
LARGE OBJECT,
LOGGED, UNLOGGED,
MAPPING FOR .. WITH .., MAPPING REPLACE .. WITH ..,
MATERIALIZED VIEW,
NOT VALID,
OPERATOR,
OPERATOR CLASS .. USING ..,
OPERATOR FAMILY .. USING ..,
POLICY,
PROCEDURAL LANGUAGE,
PROCEDURE,
PUBLICATION,
READ WRITE, READ ONLY,
RESTRICT,
ROLE,
ROUTINE,
RULE,
SCHEMA,
SEQUENCE,
SERVER,
STATISTICS,
STORAGE PLAIN, STORAGE EXTERNAL, STORAGE EXTENDED, STORAGE MAIN,
SUBSCRIPTION,
TABLE,
TABLESPACE,
TEXT SEARCH CONFIGURATION,
TEXT SEARCH DICTIONARY,
TEXT SEARCH PARSER,
TEXT SEARCH TEMPLATE,
TRANSFORM FOR .. LANGUAGE SQL,
TRIGGER,
TYPE,
VALUE,
VIEW,
WITH OIDS, WITHOUT OIDS,
WITHOUT CLUSTER,
SET DATA TYPE;
Loading