Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit e3afcc9

Browse files
author
mikesamuel
committed
a language handler for SQL
1 parent a3dbad8 commit e3afcc9

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/lang-sql.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2008 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
17+
/**
18+
* @fileoverview
19+
* Registers a language handler for SQL.
20+
*
21+
*
22+
* To use, include prettify.js and this file in your HTML page.
23+
* Then put your code in an HTML tag like
24+
* <pre class="prettyprint lang-sql">(my SQL code)</pre>
25+
*
26+
*
27+
* http://savage.net.au/SQL/sql-99.bnf.html is the basis for the grammar, and
28+
* http://msdn.microsoft.com/en-us/library/aa238507(SQL.80).aspx as the basis
29+
* for the keyword list.
30+
*
31+
32+
*/
33+
34+
PR.registerLangHandler(
35+
PR.createSimpleLexer(
36+
[
37+
// Whitespace
38+
[PR.PR_PLAIN, /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
39+
// A double or single quoted, possibly multi-line, string.
40+
[PR.PR_STRING, /^(?:"(?:[^\"\\]|\\.)*"|'(?:[^\'\\]|\\.)*')/, null,
41+
'"\'']
42+
],
43+
[
44+
// A comment is either a line comment that starts with two dashes, or
45+
// two dashes preceding a long bracketed block.
46+
[PR.PR_COMMENT, /^(?:--[^\r\n]*|\/\*[\s\S]*?(?:\*\/|$))/],
47+
[PR.PR_KEYWORD, /^(?:ADD|ALL|ALTER|AND|ANY|AS|ASC|AUTHORIZATION|BACKUP|BEGIN|BETWEEN|BREAK|BROWSE|BULK|BY|CASCADE|CASE|CHECK|CHECKPOINT|CLOSE|CLUSTERED|COALESCE|COLLATE|COLUMN|COMMIT|COMPUTE|CONSTRAINT|CONTAINS|CONTAINSTABLE|CONTINUE|CONVERT|CREATE|CROSS|CURRENT|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|CURRENT_USER|CURSOR|DATABASE|DBCC|DEALLOCATE|DECLARE|DEFAULT|DELETE|DENY|DESC|DISK|DISTINCT|DISTRIBUTED|DOUBLE|DROP|DUMMY|DUMP|ELSE|END|ERRLVL|ESCAPE|EXCEPT|EXEC|EXECUTE|EXISTS|EXIT|FETCH|FILE|FILLFACTOR|FOR|FOREIGN|FREETEXT|FREETEXTTABLE|FROM|FULL|FUNCTION|GOTO|GRANT|GROUP|HAVING|HOLDLOCK|IDENTITY|IDENTITYCOL|IDENTITY_INSERT|IF|IN|INDEX|INNER|INSERT|INTERSECT|INTO|IS|JOIN|KEY|KILL|LEFT|LIKE|LINENO|LOAD|NATIONAL|NOCHECK|NONCLUSTERED|NOT|NULL|NULLIF|OF|OFF|OFFSETS|ON|OPEN|OPENDATASOURCE|OPENQUERY|OPENROWSET|OPENXML|OPTION|OR|ORDER|OUTER|OVER|PERCENT|PLAN|PRECISION|PRIMARY|PRINT|PROC|PROCEDURE|PUBLIC|RAISERROR|READ|READTEXT|RECONFIGURE|REFERENCES|REPLICATION|RESTORE|RESTRICT|RETURN|REVOKE|RIGHT|ROLLBACK|ROWCOUNT|ROWGUIDCOL|RULE|SAVE|SCHEMA|SELECT|SESSION_USER|SET|SETUSER|SHUTDOWN|SOME|STATISTICS|SYSTEM_USER|TABLE|TEXTSIZE|THEN|TO|TOP|TRAN|TRANSACTION|TRIGGER|TRUNCATE|TSEQUAL|UNION|UNIQUE|UPDATE|UPDATETEXT|USE|USER|VALUES|VARYING|VIEW|WAITFOR|WHEN|WHERE|WHILE|WITH|WRITETEXT)(?=[^\w-]|$)/i, null],
48+
// A number is a hex integer literal, a decimal real literal, or in
49+
// scientific notation.
50+
[PR.PR_LITERAL,
51+
/^[+-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i],
52+
// An identifier
53+
[PR.PR_PLAIN, /^[a-z_][\w-]*/i],
54+
// A run of punctuation
55+
[PR.PR_PUNCTUATION, /^[^\w\t\n\r \xA0]+/]
56+
]),
57+
['sql']);

tests/prettify_test.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
onerror="alert('Error: failed to load ' + this.src)"></script>
1111
<script src="../src/lang-ml.js" type="text/javascript"
1212
onerror="alert('Error: failed to load ' + this.src)"></script>
13+
<script src="../src/lang-sql.js" type="text/javascript"
14+
onerror="alert('Error: failed to load ' + this.src)"></script>
1315
<script type="text/javascript">
1416
// get accurate timing
1517
PR_SHOULD_USE_CONTINUATION = false;
@@ -449,6 +451,19 @@ <h1>Python w/ language specified</h1>
449451
/* not a comment and not keywords: null char true */
450452
</pre>
451453

454+
<h1>SQL w/ language specified</h1>
455+
<pre class="prettyprint lang-sql" id="sql_lang">
456+
/* A multi-line
457+
* comment */
458+
'Another string /* Isn\'t a comment',
459+
"A string */"
460+
-- A line comment
461+
SELECT * FROM users WHERE id IN (1, 2.0, +30e-1);
462+
-- keywords are case-insensitive.
463+
-- Note: user-table is a single identifier, not a pair of keywords
464+
select * from user-table where id in (x, y, z);
465+
</pre>
466+
452467
<h1>XML</h1>
453468
<pre class="prettyprint" id="xml">
454469
&lt;!DOCTYPE series PUBLIC "fibonacci numbers"&gt;
@@ -1292,6 +1307,23 @@ <h1>Bug 45 - Square brackets in strings</h1>
12921307
'`END`PUN/*`END`PLN `END`KWDnot`END`PLN a comment `END`KWDand`END' +
12931308
'`PLN `END`KWDnot`END`PLN keywords`END`PUN:`END' +
12941309
'`PLN null char true `END`PUN*/`END'),
1310+
sql_lang: (
1311+
'`COM/* A multi-line<br>' +
1312+
'&nbsp;* comment */`END`PLN<br>' +
1313+
'`END`STR\'Another string /* Isn\\\'t a comment\'`END`PUN,`END`PLN<br>' +
1314+
'`END`STR"A string */"`END`PLN<br>' +
1315+
'`END`COM-- A line comment`END`PLN<br>' +
1316+
'`END`KWDSELECT`END`PLN `END`PUN*`END`PLN `END`KWDFROM`END' +
1317+
'`PLN users `END`KWDWHERE`END`PLN id `END`KWDIN`END`PLN `END' +
1318+
'`PUN(`END`LIT1`END`PUN,`END`PLN `END`LIT2.0`END`PUN,`END`PLN `END' +
1319+
'`LIT+30e-1`END`PUN);`END`PLN<br>' +
1320+
'`END`COM-- keywords are case-insensitive.`END`PLN<br>' +
1321+
'`END`COM-- Note: user-table is a single identifier, not a pair of' +
1322+
' keywords`END`PLN<br>' +
1323+
'`END`KWDselect`END`PLN `END`PUN*`END`PLN `END`KWDfrom`END' +
1324+
'`PLN user-table `END`KWDwhere`END`PLN id `END`KWDin`END`PLN `END' +
1325+
'`PUN(`END`PLNx`END`PUN,`END`PLN y`END`PUN,`END`PLN z`END`PUN);`END'
1326+
),
12951327
xml: (
12961328
'`DEC&lt;!DOCTYPE series PUBLIC "fibonacci numbers"&gt;`END`PLN<br>' +
12971329
'<br>' +

0 commit comments

Comments
 (0)