diff --git a/README.md b/README.md index 774adaab..41820184 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Other languages are supported via extensions: [Basic](src/lang-basic.js); [Clojure](src/lang-clj.js); [CSS](src/lang-css.js); +[CQL](src/lang-cql.js); [Dart](src/lang-dart.js); [Erlang](src/lang-erlang.js); [Go](src/lang-go.js); diff --git a/src/lang-cql.js b/src/lang-cql.js new file mode 100644 index 00000000..12344229 --- /dev/null +++ b/src/lang-cql.js @@ -0,0 +1,55 @@ +/** + * @license + * Copyright (C) 2008 Peter Janssen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview + * Registers a language handler for CQL. + * + * + * To use, include prettify.js and this file in your HTML page. + * Then put your code in an HTML tag like + *
(my CQL code)+ * + * https://docs.datastax.com/en/cql/3.1/cql/cql_reference/cql_lexicon_c.html + * + * @author Peter Janssen + */ + +PR['registerLangHandler']( + PR['createSimpleLexer']( + [ + // Whitespace + [PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], + // A double or single quoted, possibly multi-line, string. + [PR['PR_STRING'], /^(?:"(?:[^\"\\]|\\.)*"|'(?:[^\'\\]|\\.)*')/, null, + '"\''] + ], + [ + // A comment is either a line comment that starts with two dashes, or + // two dashes preceding a long bracketed block. + [PR['PR_COMMENT'], /^(?:--[^\r\n]*|\/\*[\s\S]*?(?:\*\/|$))/], + [PR['PR_KEYWORD'], /^(?:ADD|ALL|ALLOW|ALTER|AND|ANY|APPLY|AS|ASC|ASCII|AUTHORIZE|BATCH|BEGIN|BIGINT|BLOB|BOOLEAN|BY|CLUSTERING|COLUMNFAMILY|COMPACT|CONSISTENCY|COUNT|COUNTER|CREATE|CUSTOM|DECIMAL|DELETE|DESC|DISTINCT|DOUBLE|DROP|EACH_QUORUM|EXISTS|FILTERING|FLOAT|FROM|FROZEN|FULL|GRANT|IF|IN|INDEX|INET|INFINITY|INSERT|INT|INTO|KEY|KEYSPACE|KEYSPACES|LEVEL|LIMIT|LIST|LOCAL_ONE|LOCAL_QUORUM|MAP|MODIFY|NAN|NORECURSIVE|NOSUPERUSER|NOT|OF|ON|ONE|ORDER|PASSWORD|PERMISSION|PERMISSIONS|PRIMARY|QUORUM|RENAME|REVOKE|SCHEMA|SELECT|SET|STATIC|STORAGE|SUPERUSER|TABLE|TEXT|TIMESTAMP|TIMEUUID|THREE|TO|TOKEN|TRUNCATE|TTL|TUPLE|TWO|TYPE|UNLOGGED|UPDATE|USE|USER|USERS|USING|UUID|VALUES|VARCHAR|VARINT|WHERE|WITH|WRITETIME)(?=[^\w-]|$)/i, null], + // A number is a hex integer literal, a decimal real literal, or in + // scientific notation. + [PR['PR_LITERAL'], + /^[+-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i], + // An identifier + [PR['PR_PLAIN'], /^[a-z_][\w-]*/i], + // A run of punctuation + [PR['PR_PUNCTUATION'], /^[^\w\t\n\r \xA0\"\'][^\w\t\n\r \xA0+\-\"\']*/] + ]), + ['cql']); diff --git a/tests/prettify_test.html b/tests/prettify_test.html index 04f55f22..ce207d54 100644 --- a/tests/prettify_test.html +++ b/tests/prettify_test.html @@ -22,6 +22,7 @@ var base = /[&?]loader\b/.test(location.search) ? '../loader/' : '../src/'; var sources = [ 'lang-css.js', + 'lang-cql.js', 'lang-erlang.js', 'lang-go.js', 'lang-hs.js', @@ -217,6 +218,19 @@
+/* A multi-line + * comment */ +'Another string /* Isn\'t a comment', +"A string */" +-- A line comment +USE KEYSPACE killrvideo; +SELECT * FROM usernames WHERE id IN (1, 2.0, +30e-1); +-- keywords are case-insensitive. +-- Note: user-table is a single identifier, not a pair of keywords +select * from user-table where id in (x, y, z); +
diff --git a/tests/prettify_test.js b/tests/prettify_test.js
index 9d40aa7b..0ac0e4fe 100644
--- a/tests/prettify_test.js
+++ b/tests/prettify_test.js
@@ -184,6 +184,24 @@ var goldens = {
'`PUN,`END`PLN `END`LIT1U`END`PUN);`END`PLN\n' +
'`END`PUN}`END'
),
+ cql_lang: (
+ '`COM/* A multi-line\n' +
+ ' * comment *\/`END`PLN\n' +
+ '`END`STR\'Another string /* Isn\\\'t a comment\'`END`PUN,`END`PLN\n' +
+ '`END`STR"A string *\/"`END`PLN\n' +
+ '`END`COM-- A line comment`END`PLN\n' +
+ '`END`KWDUSE`END`PLN `END`KWDKEYSPACE`END`PLN killrvideo`END`PUN;`END`PLN\n' +
+ '`END`KWDSELECT`END`PLN `END`PUN*`END`PLN `END`KWDFROM`END' +
+ '`PLN usernames `END`KWDWHERE`END`PLN id `END`KWDIN`END`PLN `END' +
+ '`PUN(`END`LIT1`END`PUN,`END`PLN `END`LIT2.0`END`PUN,`END`PLN `END' +
+ '`LIT+30e-1`END`PUN);`END`PLN\n' +
+ '`END`COM-- keywords are case-insensitive.`END`PLN\n' +
+ '`END`COM-- Note: user-table is a single identifier, not a pair of' +
+ ' keywords`END`PLN\n' +
+ '`END`KWDselect`END`PLN `END`PUN*`END`PLN `END`KWDfrom`END' +
+ '`PLN user-table `END`KWDwhere`END`PLN id `END`KWDin`END`PLN `END' +
+ '`PUN(`END`PLNx`END`PUN,`END`PLN y`END`PUN,`END`PLN z`END`PUN);`END'
+ ),
java: (
'`KWDpackage`END`PLN foo`END`PUN;`END`PLN\n' +
'\n' +