๐Ÿ“ฆ getify / literalizer

๐Ÿ“„ lit.js ยท 1360 lines
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360/*! literalizer
    v0.4.0-a (c) Kyle Simpson
    MIT License: http://getify.mit-license.org
*/

(function UMD(name,context,definition) {
	if (typeof module != "undefined" && module.exports) module.exports = definition();
	else if (typeof define == "function" && define.amd) define(definition);
	else context[name] = definition(name,context);
})("LIT",this,function definition(name,context) {
	"use strict";

	function clone(obj) {
		return JSON.parse(JSON.stringify(obj));
	}

	function combineGeneralSegments(segmentsSlice) {
		var start, end, i, j;

		for (i=0; i<segmentsSlice.length; i++) {
			if (segmentsSlice[i].type === SEGMENT_GENERAL) {
				start = end = i;
				for (j=start+1; j<segmentsSlice.length; j++) {
					end = j;
					if (segmentsSlice[j].type !== SEGMENT_GENERAL) {
						end = j-1;
						break;
					}
				}
				if (end > start) {
					for (j=start+1; j<=end; j++) {
						segmentsSlice[start].val += segmentsSlice[j].val;
					}
					segmentsSlice.splice(start+1,end-start);
				}
				else i = j;
			}
		}

		return segmentsSlice;
	}

	// test for non-empty, non-whitespace, non-comment token
	function isUsefulToken(tok) {
		if (
			!tok.val ||
			// new-line?
			(
				tok.type === TOKEN_SPECIAL &&
				/^(?:\r?\n)+$/.test(tok.val)
			) ||
			// whitespace?
			(
				tok.type === SEGMENT_GENERAL &&
				/^\s*$/.test(tok.val)
			) ||
			// comment?
			tok.type === SEGMENT_COMMENT
		) {
			return false;
		}
		else {
			return true;
		}
	}

	function lex(code) {

		function saveText(text) {
			// can we add to a previous general segment?
			if (segments.length > 0 &&
				segments[segments.length-1].type === SEGMENT_GENERAL
			) {
				segments[segments.length-1].val += text;
			}
			// otherwise, just create a new one
			else {
				segments.push({
					type: SEGMENT_GENERAL,
					val: text
				});
			}
		}

		// find previous useful token
		function findUsefulToken(tokIdx) {
			while (
				tokIdx >= 0 &&
				!isUsefulToken(tokens[tokIdx])
			) {
				tokIdx--;
			}

			// did we find one?
			if (tokIdx >= 0) {
				return tokIdx;
			}
			else {
				return false;
			}
		}

		// Algorithm here comes (partly) from pseudo-code description at:
		// https://github.com/mozilla/sweet.js/wiki/design
		function regexLookback() {

			// try to find a balanced ( ) pair
			function findParenPair(tokIdx) {
				var i, tok, paren_count = 0;

				for (i=tokIdx; i>=0; i--) {
					tok = tokens[i];

					// skip over non-useful tokens
					if (!isUsefulToken(tok)) {
						continue;
					}

					// special-token to consider?
					if (tok.type === TOKEN_SPECIAL) {
						if (tok.val === ")") {
							paren_count++;
						}
						else if (tok.val === "(") {
							paren_count--;

							if (paren_count === 0) {
								return i;
							}
							// parens unbalanced? abort!
							else if (paren_count < 0) {
								return false;
							}
						}
					}
					// parens not yet found?
					else if (paren_count === 0) {
						// abort!
						return false;
					}
				}

				// if we get here, we didn't find a balanced ( ) pair
				return false;
			}

			// try to find a balanced { } pair
			function findBracePair(tokIdx) {
				var i, tok, brace_count = 0;

				for (i=tokIdx; i>=0; i--) {
					tok = tokens[i];

					// skip over non-useful tokens
					if (!isUsefulToken(tok)) {
						continue;
					}

					// special-token to consider?
					if (tok.type === TOKEN_SPECIAL) {
						if (tok.val === "}") {
							brace_count++;
						}
						else if (tok.val === "{") {
							brace_count--;

							if (brace_count === 0) {
								return i;
							}
							// braces unbalanced? abort!
							else if (brace_count < 0) {
								return false;
							}
						}
					}
					// braces not yet found?
					else if (brace_count === 0) {
						// abort!
						return false;
					}
				}

				// if we get here, we didn't find a balanced { } pair
				return false;
			}


			var tok_idx, i, tmp, tok;

			// look for most recent useful token
			tok_idx = findUsefulToken(tokens.length-1);

			// nothing useful found?
			if (tok_idx === false) {
				// assume a valid regex literal
				return true;
			}

			tok = tokens[tok_idx];

			// preceding token is valid before a regex literal?
			if (tok.type === TOKEN_SPECIAL) {
				// is preceeding special-token a ')'?
				if (tok.val === ")") {
					tok_idx = findParenPair(tok_idx);

					// did we find a balanced ( ) pair?
					if (tok_idx !== false) {
						// let's now look for a preceding if/while/for/with keyword
						for (i=tok_idx-1; i>=0; i--) {
							tok = tokens[i];

							// skip over non-useful tokens
							if (!isUsefulToken(tok)) {
								continue;
							}

							// special-token to consider?
							if (tok.type === TOKEN_SPECIAL) {
								// preceding if/while/for/with keyword implies
								// a valid regex literal
								if (/^(?:if|while|for|with)$/.test(tok.val)) {
									return true;
								}
								// otherwise, NOT a valid regex literal
								else {
									return false;
								}
							}
							// otherwise, NOT a valid regex literal
							else {
								return false;
							}
						}
					}
					// otherwise, NOT a valid regex literal
					else {
						return false;
					}
				}
				// is preceeding special-token a '}'?
				else if (tok.val === "}") {
					tok_idx = findBracePair(tok_idx);

					// did we find a balanced { } pair?
					if (tok_idx !== false) {
						// is the { } pair a statement block?
						if (tokens[tok_idx].is_block) {
							// let's look at the previous (useful) token
							tok_idx = findUsefulToken(tok_idx-1);

							// nothing useful found preceding?
							if (tok_idx === false) {
								// NOTE: we found a { } statement block already
								return true;
							}

							tok = tokens[tok_idx];

							if (tok.type === TOKEN_SPECIAL) {
								// ES6 fat-arrow function?
								if (tok.val === "=>") {
									// arrow functions are always function expressions,
									// and regex literals can't immediately follow those
									return false;
								}
								// look for normal function signature?
								else {
									tok_idx = findParenPair(tok_idx);

									// balanced ( ) pair not found?
									if (tok_idx === false) {
										// NOTE: we found a { } statement block already,
										// so subsequent regex literal allowed
										return true;
									}

									// look backward to try and find a "function" keyword
									tok_idx = findUsefulToken(tok_idx-1);

									// nothing useful found preceding?
									if (tok_idx === false) {
										// NOTE: we found a { } statement block already,
										// so subsequent regex literal allowed
										return true;
									}

									// account for function name, if any
									if (tokens[tok_idx].type === SEGMENT_GENERAL) {
										// skipping the name, see if we can find
										// the "function" keyword?
										tok_idx = findUsefulToken(tok_idx-1);

										// nothing useful found preceding?
										if (tok_idx === false) {
											// NOTE: we found a { } statement block already,
											// so subsequent regex literal allowed
											return true;
										}
									}

									if (
										// preceding is not a `function` keyword?
										!/function(?:\s*\*)?/.test(tokens[tok_idx].val) ||
										// preceding `function` is a declaration?
										tokens[tok_idx].is_decl
									) {
										// NOTE: we found a { } statement block already,
										// so subsequent regex literal allowed
										return true;
									}
									// otherwise, subsequent regex literal not allowed
									else {
										return false;
									}
								}
							}
							else {
								// NOTE: we found a { } statement block already,
								// so subsequent regex literal allowed
								return true;
							}
						}
						// otherwise, the { } pair is a value, so subsequent
						// regex literal not allowed
						else {
							return false;
						}
					}
					// otherwise, no balanced { } pair found
					else {
						// NOTE: unbalanced { } is an invalid case, but we're
						// erring on the side caution and assuming the }
						// was the end of a statement block, not a value
						return true;
					}
				}
				// is preceeding special-token a keyword or punctuator/operator that implies rval?
				else if (/^(?:new|return|throw|delete|in|else|void|typeof|yield|case|debugger|break|continue|(?:=>|[+\-*\/=~!%&,\|;:\?<>\(\{\[]))$/.test(tok.val)) {
					return true;
				}
				// otherwise, NOT a valid regex literal
				else {
					return false;
				}
			}
			// otherwise, NOT a valid regex literal
			else {
				return false;
			}
		}

		function checkAbandonPropertyFunctionCandidate(matched) {
			var tmp;

			if (
				// empty value token?
				!matched ||
				// whitespace only?
				/^\s+$/.test(matched) ||
				// not currently processing a property-function candidate anyway?
				block_allowed[block_allowed.length-1].type !== BLOCK_ALLOWED_PROPERTY_FUNCTION
			) {
				// don't abandon anything :)
				return;
			}

			if (
				// unbalanced ( ) pairs?
				block_allowed[block_allowed.length-1].paren_count < 0 ||
				// unbalanced { } pairs?
				block_allowed[block_allowed.length-1].brace_count < 0 ||
				(
					// expecting the opening { for the outer { } balanced pair?
					block_allowed[block_allowed.length-1].brace_count === 0 &&
					// but didn't find what was expected?
					matched !== "{"
				)
			) {
				// abandon as not actually a valid property-function
				tmp = block_allowed.pop();

				// update paren and brace counts after state pop, if need-be
				if (tmp.paren_count > 0) {
					if (block_allowed[block_allowed.length-1].paren_count === null) {
						block_allowed[block_allowed.length-1].paren_count = tmp.paren_count;
					}
					else if (typeof block_allowed[block_allowed.length-1].paren_count === "number") {
						block_allowed[block_allowed.length-1].paren_count += tmp.paren_count;
					}
				}
				if (tmp.brace_count > 0) {
					if (block_allowed[block_allowed.length-1].brace_count === null) {
						block_allowed[block_allowed.length-1].brace_count = tmp.brace_count;
					}
					else if (typeof block_allowed[block_allowed.length-1].brace_count === "number") {
						block_allowed[block_allowed.length-1].brace_count += tmp.brace_count;
					}
				}
			}
		}

		// rules come from: http://javascript.spec.whatwg.org/#comment-syntax
		function specialHTMLCommentMarker(text) {
			var i;

			if (text === "<!--") return true;
			if (text === "-->") {
				if (tokens.length > 0) {
					// look backward to the beginning of the current line
					for (i=tokens.length-1; i>=0; i--) {
						// stop once we pass the beginning of the line
						if (/\r?\n/.test(tokens[i].val)) {
							break;
						}
						if (!(
							// whitespace?
							/^\s*$/.test(tokens[i].val) ||
							// multi-line comment?
							tokens[i].type === SEGMENT_COMMENT
						)) {
							// not a valid single-line comment marker
							return false;
						}
					}
				}

				return true;
			}
		}

		function processGeneral() {
			var segmentsSlice, left_context, tok_idx,
				prev_tok_idx, i,
				preceding_dot_operator = false
			;

			// capture preceeding unmatched string, if any
			if (unmatched) {
				saveText(unmatched);

				tokens.push({
					type: SEGMENT_GENERAL,
					val: unmatched
				});

				// valid identifier (and optional surrounding whitespace)?
				if (/^\s*[^0-9\s\(\)\[\]\{\}<>,.:;=~+\-\*\/!%&\|\?\"\'][^\s\(\)\[\]\{\}<>,.:;=~+\-\*\/!%&\|\?\"\']*\s*$/.test(unmatched)) {
					// if block is allowed and an identifier is found,
					// it's a block-label candidate
					statement_block_label_candidate = block_allowed[block_allowed.length-1].allowed;
					// if a block is not allowed but an identifier is found,
					// it's a property-function candidate, as long as it's not
					// the name identifier of an existing function definition
					property_function_candidate = (
						!block_allowed[block_allowed.length-1].allowed &&
						!(
							block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FUNCTION &&
							block_allowed[block_allowed.length-1].paren_count === null
						) &&
						!(
							block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_ARROW_FUNCTION &&
							block_allowed[block_allowed.length-1].brace_count === null
						)
					);
				}
				// valid identifier not found where necessary for block label
				else {
					statement_block_label_candidate = false;
				}
			}

			// record whether there's a preceding . operator (not part of a number literal)
			prev_tok_idx = findUsefulToken(tokens.length-1);
			if (prev_tok_idx !== false &&
				tokens[prev_tok_idx].type !== SEGMENT_NUMBER_LITERAL
			) {
				preceding_dot_operator = /\.\s*$/.test(tokens[prev_tok_idx].val);
			}

			// do we need to implicitly end an arrow-function or for-loop block-state
			// processing because no outer { } balanced pair is going to be found?
			if (
				(
					(
						// currently processing an arrow-function?
						block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_ARROW_FUNCTION &&
						// expecting the opening { for the outer { } balanced pair?
						block_allowed[block_allowed.length-1].brace_count === null
					) ||
					(
						// currently processing a for-loop?
						block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FOR &&
						// expecting the opening { for the outer { } balanced pair?
						block_allowed[block_allowed.length-1].paren_count === 0
					)
				) &&
				(
					// non-whitespace/newline followed?
					/[^\s]/.test(unmatched) ||
					// non-comment, non { character followed?
					!/\/\/|\/\*|[\{]/.test(match[0])
				)
			) {
				block_allowed.pop();
			}

			if (match) {
				left_context = code.slice(0,next_match_idx - match[0].length);

				// starting a comment segment?
				if (
					match[0] === "//" ||
					match[0] === "/*" ||
					match[0] === "<!--" ||
					match[0] === "-->"
				) {
					if (
						// doesn't look like one of the special HTML-style
						// comment markers?
						!(
							match[0] === "<!--" ||
							match[0] === "-->"
						) ||
						// recognizable as HTML-style comment markers
						(
							!public_api.opts.overlook_html_comment_markers &&
							specialHTMLCommentMarker(match[0])
						)
					) {
						segments.push({
							type: SEGMENT_COMMENT,
							val: match[0]
						});
						tokens.push(clone(segments[segments.length-1]));

						if (match[0] === "/*") {
							lexing_state = STATE_MULTI_LINE_COMMENT;
						}
						else {
							lexing_state = STATE_SINGLE_LINE_COMMENT;
						}
					}
					// otherwise, is a `<!--` or `-->` sequence, but we cannot treat
					// it as an HTML-style comment marker, so we should treat as
					// separate operators instead
					else {
						saveText(match[0]);
						for (i=0; i<match[0].length; i++) {
							tokens.push({
								type: TOKEN_SPECIAL,
								val: match[0].charAt(i)
							});
						}

						// the end character of either operator sequence (either - or >)
						// is a disqualifier for subsequent statement block (see below too)
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						block_allowed[block_allowed.length-1].allowed = false;
					}
				}
				// starting a backtick-literal segment?
				else if (match[0] === "`") {
					segments.push({
						type: SEGMENT_BACKTICK_LITERAL,
						val: match[0]
					});
					tokens.push(clone(segments[segments.length-1]));

					lexing_state = STATE_BACKTICK_LITERAL;

					checkAbandonPropertyFunctionCandidate(match[0]);
				}
				// starting a string-literal segment?
				else if (match[0] === "\"" || match[0] === "'") {
					segments.push({
						type: SEGMENT_STRING_LITERAL,
						val: match[0],
						raw: match[0]
					});
					tokens.push(clone(segments[segments.length-1]));

					lexing_state = STATE_STRING_LITERAL;
					STATE_PATTERNS[STATE_STRING_LITERAL] = new RegExp(match[0] + "|\r?\n","g");

					checkAbandonPropertyFunctionCandidate(match[0]);
				}
				// number literal candidate?
				else if (/^(?:(?:0[xX][0-9a-fA-F]+)|(?:0[oO][0-7]+)|(?:0[bB][01]+)|(?:\d+\.\d*(?:[eE][+-]?\d+)?)|(?:\.\d+(?:[eE][+-]?\d+)?)|(?:\d+(?:[eE][+-]?\d+)?))$/.test(match[0])) {
					// number appears separate from an identifier?
					if (!/[^\s\(\)\[\]\{\}<>,.:;=~+\-\*\/!%&\|\?\"\']$/.test(left_context)) {
						segments.push({
							type: SEGMENT_NUMBER_LITERAL,
							val: match[0]
						});
						tokens.push(clone(segments[segments.length-1]));

						statement_block_label_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);
					}
					// otherwise, skip over, not a valid number literal to consider
					else {
						saveText(match[0]);
						if (tokens[tokens.length-1].type === SEGMENT_GENERAL) {
							tokens[tokens.length-1].val += match[0];
						}
						else {
							tokens.push({
								type: SEGMENT_GENERAL,
								val: match[0]
							});
						}

						switch_clause_candidate = false;
					}

					block_asi_allowed = true;
				}
				// what looks like a special keyword but is just a property
				// identifier (because of preceding . operator)?
				else if (preceding_dot_operator &&
					/\w/.test(match[0])
				) {
					saveText(match[0]);
					tokens.push({
						type: SEGMENT_GENERAL,
						val: match[0]
					});

					block_asi_allowed = true;
					statement_block_label_candidate = false;
					switch_clause_candidate = false;
					property_function_candidate = false;

					checkAbandonPropertyFunctionCandidate(match[0]);
				}
				// simple literal?
				else if (/^(?:true|false|null|Infinity|NaN|undefined)$/.test(match[0])) {
					segments.push({
						type: SEGMENT_SIMPLE_LITERAL,
						val: match[0]
					});
					tokens.push(clone(segments[segments.length-1]));

					block_asi_allowed = true;
					statement_block_label_candidate = false;
					property_function_candidate = false;

					checkAbandonPropertyFunctionCandidate(match[0]);
				}
				// special token?
				else if (/^(?:\b(?:new|return|throw|delete|in|of|else|void|typeof|yield|if|do|while|for|with|case|default|debugger|break|continue)\b|\bfunction(?:\s*\*)?|=>|[+\-*=~!%&,\|;:\?<>\(\)\{\}\[\]]|(?:\r?\n)+)$/.test(match[0])) {
					saveText(match[0]);
					tokens.push({
						type: TOKEN_SPECIAL,
						val: match[0]
					});

					// normal function?
					if (/function(?:\s*\*)?/.test(match[0])) {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						// NOTE: wherever a statement block can appear,
						// a function declaration can appear, and only there.
						tokens[tokens.length-1].is_decl = block_allowed[block_allowed.length-1].allowed;

						block_allowed.push({
							type: BLOCK_ALLOWED_FUNCTION,
							allowed: false,
							paren_count: null,
							brace_count: null
						});
					}
					// arrow function?
					else if (match[0] === "=>") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						block_allowed.push({
							type: BLOCK_ALLOWED_ARROW_FUNCTION,
							allowed: false,
							brace_count: null
						});
					}
					// starting a for-loop?
					else if (match[0] === "for") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						block_allowed.push({
							type: BLOCK_ALLOWED_FOR,
							allowed: false,
							paren_count: null
						});
					}
					// `new` operator?
					else if (match[0] === "new") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						block_allowed[block_allowed.length-1].allowed = false;
					}
					// switch clauses?
					else if (/case|default/.test(match[0])) {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = true;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);
					}
					// new-line?
					else if (/^(?:\r?\n)+$/.test(match[0])) {
						// check block ASI at this new-line
						if (block_asi_allowed) {
							block_allowed[block_allowed.length-1].allowed = true;
							block_asi_allowed = false;
						}
					}
					// opening ( parenthesis?
					else if (match[0] === "(") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						// are we continuing a property-function candidate with an outer ( ) pair?
						if (property_function_candidate) {
							property_function_candidate = false;
							block_allowed.push({
								type: BLOCK_ALLOWED_PROPERTY_FUNCTION,
								allowed: false,
								paren_count: 1,
								brace_count: null
							});
						}
						else if (
							// processing a for-loop currently?
							block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FOR ||
							(
								(
									// processing a normal function?
									block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FUNCTION ||
									// processing a property-function candidate currently?
									block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_PROPERTY_FUNCTION
								) &&
								// still haven't found its outer ( ) balanced pair?
								block_allowed[block_allowed.length-1].brace_count === null
							)
						) {
							// expecting the opening ( of the outer ( ) balanced pair?
							if (block_allowed[block_allowed.length-1].paren_count === null) {
								block_allowed[block_allowed.length-1].paren_count = 1;
							}
							// otherwise, keep count to find outer ( ) balanced pair
							else {
								block_allowed[block_allowed.length-1].paren_count++;
							}
						}
						// otherwise, just a non-special ( operator
						else {
							block_allowed[block_allowed.length-1].allowed = false;
						}
					}
					// closing ) parenthesis?
					else if (match[0] === ")") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						if (
							(
								// processing a for-loop currently?
								block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FOR ||
								(
									(
										// processing a property-function candidate currently?
										block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_PROPERTY_FUNCTION ||
										// processing a normal function?
										block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FUNCTION
									) &&
									// still haven't found its outer ( ) balanced pair?
									block_allowed[block_allowed.length-1].brace_count === null
								)
							) &&
							// already found the opening ( of the outer ( ) balanced pair?
							block_allowed[block_allowed.length-1].paren_count !== null
						) {
							block_allowed[block_allowed.length-1].paren_count--;

							// found the closing ) for the outer ( ) balanced pair?
							if (block_allowed[block_allowed.length-1].paren_count === 0) {
								block_allowed[block_allowed.length-1].allowed = true;
								// now look for the outer { } balanced pair
								block_allowed[block_allowed.length-1].brace_count = 0;
							}
							else {
								checkAbandonPropertyFunctionCandidate(match[0]);
							}
						}
						// otherwise, just a non-special ) operator
						else {
							block_asi_allowed = true;
						}
					}
					// opening { brace?
					else if (match[0] === "{") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						if (
							// general block-allowed processing state?
							block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_GENERAL ||
							(
								(
									// processing normal function?
									block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FUNCTION ||
									// processing arrow-function?
									block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_ARROW_FUNCTION ||
									// processing property-function?
									block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_PROPERTY_FUNCTION
								) &&
								// function body-block has started already?
								block_allowed[block_allowed.length-1].brace_count !== null
							)
						) {
							// if a block is allowed here, the { starts a block
							tokens[tokens.length-1].is_block = block_allowed[block_allowed.length-1].allowed;
							if ("brace_count" in block_allowed[block_allowed.length-1]) {
								block_allowed[block_allowed.length-1].brace_count++;
							}
						}
						else if (
							// processing an arrow-function?
							block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_ARROW_FUNCTION ||
							(
								(
									// processing a normal function?
									block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FUNCTION ||
									// processing a property-function candidate?
									block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_PROPERTY_FUNCTION
								) &&
								// already found the outer ( ) balanced pair?
								block_allowed[block_allowed.length-1].paren_count === 0
							)
						) {
							tokens[tokens.length-1].is_block = true;

							// expecting the opening { of the outer { } balanced pair?
							if (block_allowed[block_allowed.length-1].brace_count === null) {
								block_allowed[block_allowed.length-1].brace_count = 1;
							}
							// otherwise, keep count to find outer { } balanced pair
							else {
								block_allowed[block_allowed.length-1].brace_count++;
							}

							block_allowed[block_allowed.length-1].allowed = true;
						}
						else if (
							// processing a for-loop currently?
							block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FOR &&
							// already found the outer ( ) balanced pair?
							block_allowed[block_allowed.length-1].paren_count === 0
						) {
							tokens[tokens.length-1].is_block = true;

							block_allowed.pop();
							block_allowed[block_allowed.length-1].allowed = true;
						}
					}
					// closing } brace?
					else if (match[0] === "}") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						if (
							// not general block-allowed processing state?
							block_allowed[block_allowed.length-1].type !== BLOCK_ALLOWED_GENERAL &&
							// still looking for the closing } for the outer { } balanced pair?
							block_allowed[block_allowed.length-1].brace_count > 0
						) {
							block_allowed[block_allowed.length-1].brace_count--;

							// found the closing } for the outer { } balanced pair?
							if (block_allowed[block_allowed.length-1].brace_count === 0) {
								block_allowed.pop();
							}
							else {
								checkAbandonPropertyFunctionCandidate(match[0]);
							}
						}

						block_allowed[block_allowed.length-1].allowed = true;
					}
					// : is possibly a qualifier for subsequent statement block?
					else if (match[0] === ":") {
						checkAbandonPropertyFunctionCandidate(match[0]);

						// is : part of a statement block label or a switch clause?
						if (statement_block_label_candidate ||
							switch_clause_candidate
						) {
							// subsequent statement block allowed
							block_allowed[block_allowed.length-1].allowed = true;
						}
						else {
							// subsequent statement block not allowed
							block_allowed[block_allowed.length-1].allowed = false;
						}

						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;
					}
					// ; allows a subsequent { } block, except when inside a for-loop's clauses
					else if (match[0] === ";") {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;
						property_function_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						// processing a for-loop's clauses?
						if (block_allowed[block_allowed.length-1].type === BLOCK_ALLOWED_FOR) {
							// ; in a for-loop's clauses separates expression-statements only,
							// so statement blocks not allowed
							block_allowed[block_allowed.length-1].allowed = false;
						}
						else {
							block_allowed[block_allowed.length-1].allowed = true;
						}
					}
					// disqualifier for subsequent statement block?
					else if (/^(?:[\[+\-*\/%&\|=,\<\>])$/.test(match[0])) {
						block_asi_allowed = false;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						block_allowed[block_allowed.length-1].allowed = false;
					}
					// otherwise, subsequent statement block allowed
					else {
						block_asi_allowed = true;
						statement_block_label_candidate = false;
						switch_clause_candidate = false;

						checkAbandonPropertyFunctionCandidate(match[0]);

						block_allowed[block_allowed.length-1].allowed = false;
					}
				}
				// starting a regex-literal segment (candidate)?
				else if (match[0] === "/") {
					// lookback confirms it's an eligible regex?
					if (regexLookback()) {
						segments.push({
							type: SEGMENT_REGEX_LITERAL,
							val: match[0]
						});
						tokens.push(clone(segments[segments.length-1]));

						lexing_state = STATE_REGEX_LITERAL_CANDIDATE;
					}
					// otherwise, just text
					else {
						saveText(match[0]);
						tokens.push({
							type: TOKEN_SPECIAL,
							val: match[0]
						});
					}

					block_asi_allowed = false;
					statement_block_label_candidate = false;
					switch_clause_candidate = false;
					property_function_candidate = false;

					checkAbandonPropertyFunctionCandidate(match[0]);

					block_allowed[block_allowed.length-1].allowed = false;
				}
			}
		}

		function processSingleLineComment() {
			segments[segments.length-1].val += unmatched;
			if (match) {
				// don't capture the new-line in this comment segment,
				// leave it for next match
				next_match_idx -= match[0].length;
				lexing_state = STATE_GENERAL;
			}
		}

		function processMultiLineComment() {
			segments[segments.length-1].val += unmatched;
			if (match) {
				segments[segments.length-1].val += match[0];
				lexing_state = STATE_GENERAL;

				// did this multi-line comment span multiple lines?
				if (/\r?\n/.test(segments[segments.length-1].val)) {
					// check block ASI at this new-line
					if (block_asi_allowed) {
						block_allowed[block_allowed.length-1].allowed = true;
						block_asi_allowed = false;
					}
				}
			}
		}

		function processStringLiteral() {
			var left_context;

			segments[segments.length-1].val += unmatched;
			segments[segments.length-1].raw += unmatched;

			if (match) {
				left_context = code.slice(0,next_match_idx - match[0].length);

				// is the match at the beginning or is it NOT escaped?
				if (!left_context || not_escaped_pattern.test(left_context)) {
					// an unescaped new-line in a file's string literal
					// is a syntax error
					if (/\r?\n/.test(match[0])) {
						public_api.warnings.push("Unterminated string literal: " + segments[segments.length-1].val.replace(/[\r\n]+/g,""));
						next_match_idx -= match[0].length;
						prev_match_idx = next_match_idx;

						block_allowed[block_allowed.length-1].allowed = true;
						block_asi_allowed = false;
					}
					else {
						segments[segments.length-1].val += match[0];
						segments[segments.length-1].raw += match[0];
						block_asi_allowed = true;
					}

					lexing_state = STATE_GENERAL;
				}
				else {
					// include an escaped quote character?
					if (match[0] === "\"" || match[0] === "'") {
						segments[segments.length-1].val += match[0];
						segments[segments.length-1].raw += match[0];
					}
					// omit escaped new-lines including their \ escape character
					else {
						segments[segments.length-1].raw += match[0];
						segments[segments.length-1].val =
							segments[segments.length-1].val.slice(
								0,
								segments[segments.length-1].val.length-1
							)
						;
					}
				}
			}
		}

		function processBacktickLiteral() {
			var left_context;

			segments[segments.length-1].val += unmatched;

			if (match) {
				segments[segments.length-1].val += match[0];
				left_context = code.slice(0,next_match_idx - match[0].length);

				// is the match at the beginning or is it NOT escaped?
				if (!left_context || not_escaped_pattern.test(left_context)) {
					lexing_state = STATE_GENERAL;

					block_asi_allowed = true;
				}
			}
		}

		function processRegexLiteral() {
			var left_context;

			segments[segments.length-1].val += unmatched;

			if (match) {
				left_context = code.slice(0,next_match_idx - match[0].length);

				// any new-line in a file's regex literal is a syntax error
				if (/\r?\n/.test(match[0])) {
					public_api.warnings.push("Unterminated regular expression literal: " + segments[segments.length-1].val);
					next_match_idx -= match[0].length;
					prev_match_idx = next_match_idx;
					lexing_state = STATE_GENERAL;

					block_allowed[block_allowed.length-1].allowed = true;
					block_asi_allowed = false;
				}
				// unescaped [ ?
				else if (match[0] === "[" && not_escaped_pattern.test(left_context)) {
					segments[segments.length-1].val += match[0];
					regex_character_class = true;
				}
				// unescaped ] ?
				else if (match[0] === "]" && not_escaped_pattern.test(left_context)) {
					segments[segments.length-1].val += match[0];
					regex_character_class = false;
				}
				// otherwise, must be a /
				else {
					segments[segments.length-1].val += match[0];

					if (
						// NOT in a character-class?
						!regex_character_class &&
						(
							// not at the beginning?
							!left_context ||
							// not escaped?
							not_escaped_pattern.test(left_context)
						)
					) {
						lexing_state = STATE_GENERAL;
						block_asi_allowed = true;
					}
				}
			}
		}


		var segments = [],
			tokens = [],

			match,
			prev_match_idx = 0,
			next_match_idx = 0,
			unmatched = "",
			regex,
			segment,

			block_allowed = [
				{
					type: BLOCK_ALLOWED_GENERAL,
					allowed: true
				}
			],
			block_asi_allowed = false,
			statement_block_label_candidate = false,
			switch_clause_candidate = false,
			property_function_candidate = false,
			regex_character_class = false,
			normal_function_started = false,
			arrow_function_started = false,

			lexing_state = 0,

			STATE_PROCESSORS = [
				processGeneral,
				processSingleLineComment,
				processMultiLineComment,
				processStringLiteral,
				processBacktickLiteral,
				processRegexLiteral
			],

			general_state_patterns
		;

		code = String(code || "");
		if (!code || code.length === 0) return segments;

		// construct the general-state pattern based on config options
		general_state_patterns = [PATTERN_BASE_GENERAL.source];
		if (!public_api.opts.overlook_html_comment_markers) {
			general_state_patterns.unshift(PATTERN_HTML_COMMENT.source);
		}
		if (public_api.opts.identify_number_literals) {
			general_state_patterns.push(PATTERN_NUMBER_LITERAL.source);
		}
		if (public_api.opts.identify_simple_literals) {
			general_state_patterns.push(PATTERN_SIMPLE_LITERAL.source);
		}
		STATE_PATTERNS[STATE_GENERAL] = new RegExp(general_state_patterns.join("|"),"g");

		while (next_match_idx < code.length) {
			unmatched = "";

			regex = STATE_PATTERNS[lexing_state];

			regex.lastIndex = next_match_idx;
			match = regex.exec(code);

			if (match) {
				prev_match_idx = next_match_idx;
				next_match_idx = regex.lastIndex;

				// collect the previous string code not matched before this segment
				if (prev_match_idx < next_match_idx - match[0].length) {
					unmatched = code.slice(prev_match_idx,next_match_idx - match[0].length);
				}
			}
			else {
				prev_match_idx = next_match_idx;
				next_match_idx = code.length;
				unmatched = code.slice(prev_match_idx);
				if (!unmatched) break;
			}

			STATE_PROCESSORS[lexing_state]();
		}

		// did we end in an abnormal state?
		if (lexing_state === STATE_MULTI_LINE_COMMENT) {
			segments[segments.length-1].type = SEGMENT_GENERAL;
			public_api.warnings.push("Unterminated multi-line comment at end of file");
		}
		else if (lexing_state === STATE_STRING_LITERAL) {
			segments[segments.length-1].type = SEGMENT_GENERAL;
			public_api.warnings.push("Unterminated string literal at end of file");
		}
		else if (lexing_state === STATE_BACKTICK_LITERAL) {
			segments[segments.length-1].type = SEGMENT_GENERAL;
			public_api.warnings.push("Unterminated template string at end of file");
		}
		else if (lexing_state === STATE_REGEX_LITERAL_CANDIDATE) {
			segments[segments.length-1].type = SEGMENT_GENERAL;
			public_api.warnings.push("Unterminated regular expression literal at end of file");
		}

		tokens.length = 0;
		return combineGeneralSegments(segments);
	}

	function generate(segments) {
		var i, ret = "";
		for (i=0; i<segments.length; i++) {
			ret += (segments[i].raw || segments[i].val || "");
		}
		return ret;
	}

	function reset() {
		public_api.warnings.length = 0;
	}


	var SEGMENT_GENERAL = 0,
		SEGMENT_COMMENT = 1,
		SEGMENT_STRING_LITERAL = 2,
		SEGMENT_BACKTICK_LITERAL = 3,
		SEGMENT_REGEX_LITERAL = 4,
		SEGMENT_NUMBER_LITERAL = 5,
		SEGMENT_SIMPLE_LITERAL = 6,

		TOKEN_SPECIAL = 50,

		STATE_GENERAL = 0,
		STATE_SINGLE_LINE_COMMENT = 1,
		STATE_MULTI_LINE_COMMENT = 2,
		STATE_STRING_LITERAL = 3,
		STATE_BACKTICK_LITERAL = 4,
		STATE_REGEX_LITERAL_CANDIDATE = 5,

		BLOCK_ALLOWED_GENERAL = 0,
		BLOCK_ALLOWED_FUNCTION = 1,
		BLOCK_ALLOWED_ARROW_FUNCTION = 2,
		BLOCK_ALLOWED_PROPERTY_FUNCTION = 3,
		BLOCK_ALLOWED_FOR = 4,

		not_escaped_pattern = /(?:[^\\]|(?:^|[^\\])(?:\\\\)+)$/,

		// source patterns to be combined based on options config
		PATTERN_BASE_GENERAL = /\b(?:new|return|throw|delete|in|of|else|void|typeof|yield|if|while|for|with|case|default|debugger|break|continue)\b|\bfunction(?:\s*\*)?|\/\/|\/\*|=>|(?:\r?\n)|[`"'+\-*\/=~!%&,\|;:\?<>\(\)\{\}\[\]]/,
		PATTERN_HTML_COMMENT = /(?:<!--|-->)/,
		PATTERN_NUMBER_LITERAL = /(?:0[xX][0-9a-fA-F]+)|(?:0[oO][0-7]+)|(?:0[bB][01]+)|(?:\d+\.\d*(?:[eE][+-]?\d+)?)|(?:\.\d+(?:[eE][+-]?\d+)?)|(?:\d+(?:[eE][+-]?\d+)?)/,
		PATTERN_SIMPLE_LITERAL = /\b(?:true|false|null|Infinity|NaN|undefined)\b/,

		STATE_PATTERNS = [
			null,							// (placeholder) general-state pattern
			/\r?\n/g,						// end of single-line comment
			/\*\//g,						// end of multi-line comment
			null,							// (placeholder) end of string literal
			/[`]/g,							// end of backtick literal
			/\/[imgyn]*|\r?\n|[\[\]]/g		// end of regex
		],

		public_api,

		default_opts = {
			overlook_html_comment_markers: false,
			identify_number_literals: false,
			identify_simple_literals: false
		}
	;


	public_api = {
		lex: lex,
		generate: generate,
		reset: reset,

		// a list of warnings (if any) from the lexing
		warnings: [],

		opts: clone(default_opts),

		// public constants for interpreting segment type
		SEGMENT: {
			GENERAL: SEGMENT_GENERAL,
			COMMENT: SEGMENT_COMMENT,
			STRING_LITERAL: SEGMENT_STRING_LITERAL,
			BACKTICK_LITERAL: SEGMENT_BACKTICK_LITERAL,
			REGEX_LITERAL: SEGMENT_REGEX_LITERAL,
			NUMBER_LITERAL: SEGMENT_NUMBER_LITERAL,
			SIMPLE_LITERAL: SEGMENT_SIMPLE_LITERAL
		}
	};

	return public_api;
});