/
sqlformat.py
3385 lines (2678 loc) · 108 KB
/
sqlformat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import js2py
js = r"""
// https://github.com/zeroturnaround/sql-formatter
var factory = function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var _Db2Formatter = __webpack_require__(24);
var _Db2Formatter2 = _interopRequireDefault(_Db2Formatter);
var _N1qlFormatter = __webpack_require__(25);
var _N1qlFormatter2 = _interopRequireDefault(_N1qlFormatter);
var _PlSqlFormatter = __webpack_require__(26);
var _PlSqlFormatter2 = _interopRequireDefault(_PlSqlFormatter);
var _StandardSqlFormatter = __webpack_require__(27);
var _StandardSqlFormatter2 = _interopRequireDefault(_StandardSqlFormatter);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
exports["default"] = {
/**
* Format whitespaces in a query to make it easier to read.
*
* @param {String} query
* @param {Object} cfg
* @param {String} cfg.language Query language, default is Standard SQL
* @param {String} cfg.indent Characters used for indentation, default is " " (2 spaces)
* @param {Object} cfg.params Collection of params for placeholder replacement
* @return {String}
*/
format: function format(query, cfg) {
cfg = cfg || {};
switch (cfg.language) {
case "db2":
return new _Db2Formatter2["default"](cfg).format(query);
case "n1ql":
return new _N1qlFormatter2["default"](cfg).format(query);
case "pl/sql":
return new _PlSqlFormatter2["default"](cfg).format(query);
case "sql":
case undefined:
return new _StandardSqlFormatter2["default"](cfg).format(query);
default:
throw Error("Unsupported SQL dialect: " + cfg.language);
}
}
};
module.exports = exports["default"];
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
var freeGlobal = __webpack_require__(12);
/** Detect free variable `self`. */
var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
/** Used as a reference to the global object. */
var root = freeGlobal || freeSelf || Function('return this')();
module.exports = root;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
var Symbol = __webpack_require__(9),
getRawTag = __webpack_require__(48),
objectToString = __webpack_require__(57);
/** `Object#toString` result references. */
var nullTag = '[object Null]',
undefinedTag = '[object Undefined]';
/** Built-in value references. */
var symToStringTag = Symbol ? Symbol.toStringTag : undefined;
/**
* The base implementation of `getTag` without fallbacks for buggy environments.
*
* @private
* @param {*} value The value to query.
* @returns {string} Returns the `toStringTag`.
*/
function baseGetTag(value) {
if (value == null) {
return value === undefined ? undefinedTag : nullTag;
}
return (symToStringTag && symToStringTag in Object(value))
? getRawTag(value)
: objectToString(value);
}
module.exports = baseGetTag;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
var baseIsNative = __webpack_require__(39),
getValue = __webpack_require__(50);
/**
* Gets the native function at `key` of `object`.
*
* @private
* @param {Object} object The object to query.
* @param {string} key The key of the method to get.
* @returns {*} Returns the function if it's native, else `undefined`.
*/
function getNative(object, key) {
var value = getValue(object, key);
return baseIsNative(value) ? value : undefined;
}
module.exports = getNative;
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var _trimEnd = __webpack_require__(74);
var _trimEnd2 = _interopRequireDefault(_trimEnd);
var _tokenTypes = __webpack_require__(8);
var _tokenTypes2 = _interopRequireDefault(_tokenTypes);
var _Indentation = __webpack_require__(21);
var _Indentation2 = _interopRequireDefault(_Indentation);
var _InlineBlock = __webpack_require__(22);
var _InlineBlock2 = _interopRequireDefault(_InlineBlock);
var _Params = __webpack_require__(23);
var _Params2 = _interopRequireDefault(_Params);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Formatter = function () {
/**
* @param {Object} cfg
* @param {Object} cfg.indent
* @param {Object} cfg.params
* @param {Tokenizer} tokenizer
*/
function Formatter(cfg, tokenizer) {
_classCallCheck(this, Formatter);
this.cfg = cfg || {};
this.indentation = new _Indentation2["default"](this.cfg.indent);
this.inlineBlock = new _InlineBlock2["default"]();
this.params = new _Params2["default"](this.cfg.params);
this.tokenizer = tokenizer;
this.previousReservedWord = {};
this.tokens = [];
this.index = 0;
}
/**
* Formats whitespaces in a SQL string to make it easier to read.
*
* @param {String} query The SQL query string
* @return {String} formatted query
*/
Formatter.prototype.format = function format(query) {
this.tokens = this.tokenizer.tokenize(query);
var formattedQuery = this.getFormattedQueryFromTokens();
return formattedQuery.trim();
};
Formatter.prototype.getFormattedQueryFromTokens = function getFormattedQueryFromTokens() {
var _this = this;
var formattedQuery = "";
this.tokens.forEach(function (token, index) {
_this.index = index;
if (token.type === _tokenTypes2["default"].WHITESPACE) {
// ignore (we do our own whitespace formatting)
} else if (token.type === _tokenTypes2["default"].LINE_COMMENT) {
formattedQuery = _this.formatLineComment(token, formattedQuery);
} else if (token.type === _tokenTypes2["default"].BLOCK_COMMENT) {
formattedQuery = _this.formatBlockComment(token, formattedQuery);
} else if (token.type === _tokenTypes2["default"].RESERVED_TOPLEVEL) {
formattedQuery = _this.formatToplevelReservedWord(token, formattedQuery);
_this.previousReservedWord = token;
} else if (token.type === _tokenTypes2["default"].RESERVED_NEWLINE) {
formattedQuery = _this.formatNewlineReservedWord(token, formattedQuery);
_this.previousReservedWord = token;
} else if (token.type === _tokenTypes2["default"].RESERVED) {
formattedQuery = _this.formatWithSpaces(token, formattedQuery);
_this.previousReservedWord = token;
} else if (token.type === _tokenTypes2["default"].OPEN_PAREN) {
formattedQuery = _this.formatOpeningParentheses(token, formattedQuery);
} else if (token.type === _tokenTypes2["default"].CLOSE_PAREN) {
formattedQuery = _this.formatClosingParentheses(token, formattedQuery);
} else if (token.type === _tokenTypes2["default"].PLACEHOLDER) {
formattedQuery = _this.formatPlaceholder(token, formattedQuery);
} else if (token.value === ",") {
formattedQuery = _this.formatComma(token, formattedQuery);
} else if (token.value === ":") {
formattedQuery = _this.formatWithSpaceAfter(token, formattedQuery);
} else if (token.value === ".") {
formattedQuery = _this.formatWithoutSpaces(token, formattedQuery);
} else if (token.value === ";") {
formattedQuery = _this.formatQuerySeparator(token, formattedQuery);
} else {
formattedQuery = _this.formatWithSpaces(token, formattedQuery);
}
});
return formattedQuery;
};
Formatter.prototype.formatLineComment = function formatLineComment(token, query) {
return this.addNewline(query + token.value);
};
Formatter.prototype.formatBlockComment = function formatBlockComment(token, query) {
return this.addNewline(this.addNewline(query) + this.indentComment(token.value));
};
Formatter.prototype.indentComment = function indentComment(comment) {
return comment.replace(/\n/g, "\n" + this.indentation.getIndent());
};
Formatter.prototype.formatToplevelReservedWord = function formatToplevelReservedWord(token, query) {
this.indentation.decreaseTopLevel();
query = this.addNewline(query);
this.indentation.increaseToplevel();
query += this.equalizeWhitespace(token.value);
return this.addNewline(query);
};
Formatter.prototype.formatNewlineReservedWord = function formatNewlineReservedWord(token, query) {
return this.addNewline(query) + this.equalizeWhitespace(token.value) + " ";
};
// Replace any sequence of whitespace characters with single space
Formatter.prototype.equalizeWhitespace = function equalizeWhitespace(string) {
return string.replace(/\s+/g, " ");
};
// Opening parentheses increase the block indent level and start a new line
Formatter.prototype.formatOpeningParentheses = function formatOpeningParentheses(token, query) {
// Take out the preceding space unless there was whitespace there in the original query
// or another opening parens or line comment
var preserveWhitespaceFor = [_tokenTypes2["default"].WHITESPACE, _tokenTypes2["default"].OPEN_PAREN, _tokenTypes2["default"].LINE_COMMENT];
if (!preserveWhitespaceFor.includes(this.previousToken().type)) {
query = (0, _trimEnd2["default"])(query);
}
query += token.value;
this.inlineBlock.beginIfPossible(this.tokens, this.index);
if (!this.inlineBlock.isActive()) {
this.indentation.increaseBlockLevel();
query = this.addNewline(query);
}
return query;
};
// Closing parentheses decrease the block indent level
Formatter.prototype.formatClosingParentheses = function formatClosingParentheses(token, query) {
if (this.inlineBlock.isActive()) {
this.inlineBlock.end();
return this.formatWithSpaceAfter(token, query);
} else {
this.indentation.decreaseBlockLevel();
return this.formatWithSpaces(token, this.addNewline(query));
}
};
Formatter.prototype.formatPlaceholder = function formatPlaceholder(token, query) {
return query + this.params.get(token) + " ";
};
// Commas start a new line (unless within inline parentheses or SQL "LIMIT" clause)
Formatter.prototype.formatComma = function formatComma(token, query) {
query = this.trimTrailingWhitespace(query) + token.value + " ";
if (this.inlineBlock.isActive()) {
return query;
} else if (/^LIMIT$/i.test(this.previousReservedWord.value)) {
return query;
} else {
return this.addNewline(query);
}
};
Formatter.prototype.formatWithSpaceAfter = function formatWithSpaceAfter(token, query) {
return this.trimTrailingWhitespace(query) + token.value + " ";
};
Formatter.prototype.formatWithoutSpaces = function formatWithoutSpaces(token, query) {
return this.trimTrailingWhitespace(query) + token.value;
};
Formatter.prototype.formatWithSpaces = function formatWithSpaces(token, query) {
return query + token.value + " ";
};
Formatter.prototype.formatQuerySeparator = function formatQuerySeparator(token, query) {
return this.trimTrailingWhitespace(query) + token.value + "\n";
};
Formatter.prototype.addNewline = function addNewline(query) {
return (0, _trimEnd2["default"])(query) + "\n" + this.indentation.getIndent();
};
Formatter.prototype.trimTrailingWhitespace = function trimTrailingWhitespace(query) {
if (this.previousNonWhitespaceToken().type === _tokenTypes2["default"].LINE_COMMENT) {
return (0, _trimEnd2["default"])(query) + "\n";
} else {
return (0, _trimEnd2["default"])(query);
}
};
Formatter.prototype.previousNonWhitespaceToken = function previousNonWhitespaceToken() {
var n = 1;
while (this.previousToken(n).type === _tokenTypes2["default"].WHITESPACE) {
n++;
}
return this.previousToken(n);
};
Formatter.prototype.previousToken = function previousToken() {
var offset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return this.tokens[this.index - offset] || {};
};
return Formatter;
}();
exports["default"] = Formatter;
module.exports = exports["default"];
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var _isEmpty = __webpack_require__(66);
var _isEmpty2 = _interopRequireDefault(_isEmpty);
var _escapeRegExp = __webpack_require__(63);
var _escapeRegExp2 = _interopRequireDefault(_escapeRegExp);
var _tokenTypes = __webpack_require__(8);
var _tokenTypes2 = _interopRequireDefault(_tokenTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Tokenizer = function () {
/**
* @param {Object} cfg
* @param {String[]} cfg.reservedWords Reserved words in SQL
* @param {String[]} cfg.reservedToplevelWords Words that are set to new line separately
* @param {String[]} cfg.reservedNewlineWords Words that are set to newline
* @param {String[]} cfg.stringTypes String types to enable: "", '', ``, [], N''
* @param {String[]} cfg.openParens Opening parentheses to enable, like (, [
* @param {String[]} cfg.closeParens Closing parentheses to enable, like ), ]
* @param {String[]} cfg.indexedPlaceholderTypes Prefixes for indexed placeholders, like ?
* @param {String[]} cfg.namedPlaceholderTypes Prefixes for named placeholders, like @ and :
* @param {String[]} cfg.lineCommentTypes Line comments to enable, like # and --
* @param {String[]} cfg.specialWordChars Special chars that can be found inside of words, like @ and #
*/
function Tokenizer(cfg) {
_classCallCheck(this, Tokenizer);
this.WHITESPACE_REGEX = /^(\s+)/;
this.NUMBER_REGEX = /^((-\s*)?[0-9]+(\.[0-9]+)?|0x[0-9a-fA-F]+|0b[01]+)\b/;
this.OPERATOR_REGEX = /^(!=|<>|==|<=|>=|!<|!>|\|\||::|->>|->|~~\*|~~|!~~\*|!~~|~\*|!~\*|!~|.)/;
this.BLOCK_COMMENT_REGEX = /^(\/\*[^]*?(?:\*\/|$))/;
this.LINE_COMMENT_REGEX = this.createLineCommentRegex(cfg.lineCommentTypes);
this.RESERVED_TOPLEVEL_REGEX = this.createReservedWordRegex(cfg.reservedToplevelWords);
this.RESERVED_NEWLINE_REGEX = this.createReservedWordRegex(cfg.reservedNewlineWords);
this.RESERVED_PLAIN_REGEX = this.createReservedWordRegex(cfg.reservedWords);
this.WORD_REGEX = this.createWordRegex(cfg.specialWordChars);
this.STRING_REGEX = this.createStringRegex(cfg.stringTypes);
this.OPEN_PAREN_REGEX = this.createParenRegex(cfg.openParens);
this.CLOSE_PAREN_REGEX = this.createParenRegex(cfg.closeParens);
this.INDEXED_PLACEHOLDER_REGEX = this.createPlaceholderRegex(cfg.indexedPlaceholderTypes, "[0-9]*");
this.IDENT_NAMED_PLACEHOLDER_REGEX = this.createPlaceholderRegex(cfg.namedPlaceholderTypes, "[a-zA-Z0-9._$]+");
this.STRING_NAMED_PLACEHOLDER_REGEX = this.createPlaceholderRegex(cfg.namedPlaceholderTypes, this.createStringPattern(cfg.stringTypes));
}
Tokenizer.prototype.createLineCommentRegex = function createLineCommentRegex(lineCommentTypes) {
return new RegExp("^((?:" + lineCommentTypes.map(function (c) {
return (0, _escapeRegExp2["default"])(c);
}).join("|") + ").*?(?:\n|$))");
};
Tokenizer.prototype.createReservedWordRegex = function createReservedWordRegex(reservedWords) {
var reservedWordsPattern = reservedWords.join("|").replace(/ /g, "\\s+");
return new RegExp("^(" + reservedWordsPattern + ")\\b", "i");
};
Tokenizer.prototype.createWordRegex = function createWordRegex() {
var specialChars = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return new RegExp("^([\\w" + specialChars.join("") + "]+)");
};
Tokenizer.prototype.createStringRegex = function createStringRegex(stringTypes) {
return new RegExp("^(" + this.createStringPattern(stringTypes) + ")");
};
// This enables the following string patterns:
// 1. backtick quoted string using `` to escape
// 2. square bracket quoted string (SQL Server) using ]] to escape
// 3. double quoted string using "" or \" to escape
// 4. single quoted string using '' or \' to escape
// 5. national character quoted string using N'' or N\' to escape
Tokenizer.prototype.createStringPattern = function createStringPattern(stringTypes) {
var patterns = {
"``": "((`[^`]*($|`))+)",
"[]": "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)",
"\"\"": "((\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*(\"|$))+)",
"''": "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)",
"N''": "((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('|$))+)"
};
return stringTypes.map(function (t) {
return patterns[t];
}).join("|");
};
Tokenizer.prototype.createParenRegex = function createParenRegex(parens) {
var _this = this;
return new RegExp("^(" + parens.map(function (p) {
return _this.escapeParen(p);
}).join("|") + ")", "i");
};
Tokenizer.prototype.escapeParen = function escapeParen(paren) {
if (paren.length === 1) {
// A single punctuation character
return (0, _escapeRegExp2["default"])(paren);
} else {
// longer word
return "\\b" + paren + "\\b";
}
};
Tokenizer.prototype.createPlaceholderRegex = function createPlaceholderRegex(types, pattern) {
if ((0, _isEmpty2["default"])(types)) {
return false;
}
var typesRegex = types.map(_escapeRegExp2["default"]).join("|");
return new RegExp("^((?:" + typesRegex + ")(?:" + pattern + "))");
};
/**
* Takes a SQL string and breaks it into tokens.
* Each token is an object with type and value.
*
* @param {String} input The SQL string
* @return {Object[]} tokens An array of tokens.
* @return {String} token.type
* @return {String} token.value
*/
Tokenizer.prototype.tokenize = function tokenize(input) {
var tokens = [];
var token = void 0;
// Keep processing the string until it is empty
while (input.length) {
// Get the next token and the token type
token = this.getNextToken(input, token);
// Advance the string
input = input.substring(token.value.length);
tokens.push(token);
}
return tokens;
};
Tokenizer.prototype.getNextToken = function getNextToken(input, previousToken) {
return this.getWhitespaceToken(input) || this.getCommentToken(input) || this.getStringToken(input) || this.getOpenParenToken(input) || this.getCloseParenToken(input) || this.getPlaceholderToken(input) || this.getNumberToken(input) || this.getReservedWordToken(input, previousToken) || this.getWordToken(input) || this.getOperatorToken(input);
};
Tokenizer.prototype.getWhitespaceToken = function getWhitespaceToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].WHITESPACE,
regex: this.WHITESPACE_REGEX
});
};
Tokenizer.prototype.getCommentToken = function getCommentToken(input) {
return this.getLineCommentToken(input) || this.getBlockCommentToken(input);
};
Tokenizer.prototype.getLineCommentToken = function getLineCommentToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].LINE_COMMENT,
regex: this.LINE_COMMENT_REGEX
});
};
Tokenizer.prototype.getBlockCommentToken = function getBlockCommentToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].BLOCK_COMMENT,
regex: this.BLOCK_COMMENT_REGEX
});
};
Tokenizer.prototype.getStringToken = function getStringToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].STRING,
regex: this.STRING_REGEX
});
};
Tokenizer.prototype.getOpenParenToken = function getOpenParenToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].OPEN_PAREN,
regex: this.OPEN_PAREN_REGEX
});
};
Tokenizer.prototype.getCloseParenToken = function getCloseParenToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].CLOSE_PAREN,
regex: this.CLOSE_PAREN_REGEX
});
};
Tokenizer.prototype.getPlaceholderToken = function getPlaceholderToken(input) {
return this.getIdentNamedPlaceholderToken(input) || this.getStringNamedPlaceholderToken(input) || this.getIndexedPlaceholderToken(input);
};
Tokenizer.prototype.getIdentNamedPlaceholderToken = function getIdentNamedPlaceholderToken(input) {
return this.getPlaceholderTokenWithKey({
input: input,
regex: this.IDENT_NAMED_PLACEHOLDER_REGEX,
parseKey: function parseKey(v) {
return v.slice(1);
}
});
};
Tokenizer.prototype.getStringNamedPlaceholderToken = function getStringNamedPlaceholderToken(input) {
var _this2 = this;
return this.getPlaceholderTokenWithKey({
input: input,
regex: this.STRING_NAMED_PLACEHOLDER_REGEX,
parseKey: function parseKey(v) {
return _this2.getEscapedPlaceholderKey({ key: v.slice(2, -1), quoteChar: v.slice(-1) });
}
});
};
Tokenizer.prototype.getIndexedPlaceholderToken = function getIndexedPlaceholderToken(input) {
return this.getPlaceholderTokenWithKey({
input: input,
regex: this.INDEXED_PLACEHOLDER_REGEX,
parseKey: function parseKey(v) {
return v.slice(1);
}
});
};
Tokenizer.prototype.getPlaceholderTokenWithKey = function getPlaceholderTokenWithKey(_ref) {
var input = _ref.input,
regex = _ref.regex,
parseKey = _ref.parseKey;
var token = this.getTokenOnFirstMatch({ input: input, regex: regex, type: _tokenTypes2["default"].PLACEHOLDER });
if (token) {
token.key = parseKey(token.value);
}
return token;
};
Tokenizer.prototype.getEscapedPlaceholderKey = function getEscapedPlaceholderKey(_ref2) {
var key = _ref2.key,
quoteChar = _ref2.quoteChar;
return key.replace(new RegExp((0, _escapeRegExp2["default"])("\\") + quoteChar, "g"), quoteChar);
};
// Decimal, binary, or hex numbers
Tokenizer.prototype.getNumberToken = function getNumberToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].NUMBER,
regex: this.NUMBER_REGEX
});
};
// Punctuation and symbols
Tokenizer.prototype.getOperatorToken = function getOperatorToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].OPERATOR,
regex: this.OPERATOR_REGEX
});
};
Tokenizer.prototype.getReservedWordToken = function getReservedWordToken(input, previousToken) {
// A reserved word cannot be preceded by a "."
// this makes it so in "mytable.from", "from" is not considered a reserved word
if (previousToken && previousToken.value && previousToken.value === ".") {
return;
}
return this.getToplevelReservedToken(input) || this.getNewlineReservedToken(input) || this.getPlainReservedToken(input);
};
Tokenizer.prototype.getToplevelReservedToken = function getToplevelReservedToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].RESERVED_TOPLEVEL,
regex: this.RESERVED_TOPLEVEL_REGEX
});
};
Tokenizer.prototype.getNewlineReservedToken = function getNewlineReservedToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].RESERVED_NEWLINE,
regex: this.RESERVED_NEWLINE_REGEX
});
};
Tokenizer.prototype.getPlainReservedToken = function getPlainReservedToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].RESERVED,
regex: this.RESERVED_PLAIN_REGEX
});
};
Tokenizer.prototype.getWordToken = function getWordToken(input) {
return this.getTokenOnFirstMatch({
input: input,
type: _tokenTypes2["default"].WORD,
regex: this.WORD_REGEX
});
};
Tokenizer.prototype.getTokenOnFirstMatch = function getTokenOnFirstMatch(_ref3) {
var input = _ref3.input,
type = _ref3.type,
regex = _ref3.regex;
var matches = input.match(regex);
if (matches) {
return { type: type, value: matches[1] };
}
};
return Tokenizer;
}();
exports["default"] = Tokenizer;
module.exports = exports["default"];
/***/ }),
/* 6 */
/***/ (function(module, exports) {
/**
* Checks if `value` is the
* [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
* of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
*
* @static
* @memberOf _
* @since 0.1.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is an object, else `false`.
* @example
*
* _.isObject({});
* // => true
*
* _.isObject([1, 2, 3]);
* // => true
*
* _.isObject(_.noop);
* // => true
*
* _.isObject(null);
* // => false
*/
function isObject(value) {
var type = typeof value;
return value != null && (type == 'object' || type == 'function');
}
module.exports = isObject;
/***/ }),
/* 7 */
/***/ (function(module, exports) {
/**
* Checks if `value` is object-like. A value is object-like if it's not `null`
* and has a `typeof` result of "object".
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
* @example
*
* _.isObjectLike({});
* // => true
*
* _.isObjectLike([1, 2, 3]);
* // => true
*
* _.isObjectLike(_.noop);
* // => false
*
* _.isObjectLike(null);
* // => false
*/
function isObjectLike(value) {
return value != null && typeof value == 'object';
}
module.exports = isObjectLike;
/***/ }),
/* 8 */
/***/ (function(module, exports) {
"use strict";
exports.__esModule = true;
/**
* Constants for token types
*/
exports["default"] = {
WHITESPACE: "whitespace",
WORD: "word",
STRING: "string",
RESERVED: "reserved",
RESERVED_TOPLEVEL: "reserved-toplevel",
RESERVED_NEWLINE: "reserved-newline",
OPERATOR: "operator",
OPEN_PAREN: "open-paren",
CLOSE_PAREN: "close-paren",
LINE_COMMENT: "line-comment",
BLOCK_COMMENT: "block-comment",
NUMBER: "number",
PLACEHOLDER: "placeholder"
};
module.exports = exports["default"];
/***/ }),
/* 9 */
/***/ (function(module, exports, __webpack_require__) {
var root = __webpack_require__(1);
/** Built-in value references. */
var Symbol = root.Symbol;
module.exports = Symbol;
/***/ }),
/* 10 */
/***/ (function(module, exports, __webpack_require__) {
var baseToString = __webpack_require__(11);
/**
* Converts `value` to a string. An empty string is returned for `null`
* and `undefined` values. The sign of `-0` is preserved.
*
* @static
* @memberOf _
* @since 4.0.0
* @category Lang
* @param {*} value The value to convert.
* @returns {string} Returns the converted string.
* @example
*
* _.toString(null);
* // => ''
*
* _.toString(-0);
* // => '-0'
*
* _.toString([1, 2, 3]);
* // => '1,2,3'
*/
function toString(value) {
return value == null ? '' : baseToString(value);
}
module.exports = toString;
/***/ }),
/* 11 */
/***/ (function(module, exports, __webpack_require__) {
var Symbol = __webpack_require__(9),
arrayMap = __webpack_require__(33),
isArray = __webpack_require__(15),
isSymbol = __webpack_require__(19);
/** Used as references for various `Number` constants. */
var INFINITY = 1 / 0;
/** Used to convert symbols to primitives and strings. */
var symbolProto = Symbol ? Symbol.prototype : undefined,
symbolToString = symbolProto ? symbolProto.toString : undefined;
/**
* The base implementation of `_.toString` which doesn't convert nullish
* values to empty strings.
*
* @private
* @param {*} value The value to process.
* @returns {string} Returns the string.
*/
function baseToString(value) {
// Exit early for strings to avoid a performance hit in some environments.
if (typeof value == 'string') {
return value;
}
if (isArray(value)) {
// Recursively convert values (susceptible to call stack limits).
return arrayMap(value, baseToString) + '';
}
if (isSymbol(value)) {
return symbolToString ? symbolToString.call(value) : '';
}
var result = (value + '');
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
}
module.exports = baseToString;
/***/ }),
/* 12 */
/***/ (function(module, exports) {
/* WEBPACK VAR INJECTION */(function(global) {/** Detect free variable `global` from Node.js. */
var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
module.exports = freeGlobal;
/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))
/***/ }),
/* 13 */
/***/ (function(module, exports) {
/** Used for built-in method references. */
var objectProto = Object.prototype;
/**
* Checks if `value` is likely a prototype object.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
*/
function isPrototype(value) {
var Ctor = value && value.constructor,
proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
return value === proto;
}
module.exports = isPrototype;
/***/ }),