-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
LexHTML.cxx
2720 lines (2579 loc) · 88.1 KB
/
LexHTML.cxx
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
// Scintilla source code edit control
/** @file LexHTML.cxx
** Lexer for HTML.
**/
// Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cstdarg>
#include <string>
#include <string_view>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"
#include "InList.h"
#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"
#include "OptionSet.h"
#include "SubStyles.h"
#include "DefaultLexer.h"
using namespace Scintilla;
using namespace Lexilla;
namespace {
#define SCE_HA_JS (SCE_HJA_START - SCE_HJ_START)
#define SCE_HA_VBS (SCE_HBA_START - SCE_HB_START)
#define SCE_HA_PYTHON (SCE_HPA_START - SCE_HP_START)
enum script_type { eScriptNone = 0, eScriptJS, eScriptVBS, eScriptPython, eScriptPHP, eScriptXML, eScriptSGML, eScriptSGMLblock, eScriptComment };
enum script_mode { eHtml = 0, eNonHtmlScript, eNonHtmlPreProc, eNonHtmlScriptPreProc };
constexpr bool IsAWordChar(int ch) noexcept {
return IsAlphaNumeric(ch) || ch == '.' || ch == '_';
}
constexpr bool IsAWordStart(int ch) noexcept {
return IsAlphaNumeric(ch) || ch == '_';
}
bool IsOperator(int ch) noexcept {
if (IsAlphaNumeric(ch))
return false;
// '.' left out as it is used to make up numbers
if (ch == '%' || ch == '^' || ch == '&' || ch == '*' ||
ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
ch == '<' || ch == '>' || ch == ',' || ch == '/' ||
ch == '?' || ch == '!' || ch == '.' || ch == '~')
return true;
return false;
}
unsigned char SafeGetUnsignedCharAt(Accessor &styler, Sci_Position position, char chDefault = ' ') {
return styler.SafeGetCharAt(position, chDefault);
}
// Put an upper limit to bound time taken for unexpected text.
constexpr Sci_PositionU maxLengthCheck = 200;
std::string GetNextWord(Accessor &styler, Sci_PositionU start) {
std::string ret;
for (Sci_PositionU i = 0; i < maxLengthCheck; i++) {
const char ch = styler.SafeGetCharAt(start + i);
if ((i == 0) && !IsAWordStart(ch))
break;
if ((i > 0) && !IsAWordChar(ch))
break;
ret.push_back(ch);
}
return ret;
}
bool Contains(const std::string &s, std::string_view search) noexcept {
return s.find(search) != std::string::npos;
}
script_type segIsScriptingIndicator(Accessor &styler, Sci_PositionU start, Sci_PositionU end, script_type prevValue) {
const std::string s = styler.GetRangeLowered(start, end+1);
if (Contains(s, "vbs"))
return eScriptVBS;
if (Contains(s, "pyth"))
return eScriptPython;
// https://html.spec.whatwg.org/multipage/scripting.html#attr-script-type
// https://mimesniff.spec.whatwg.org/#javascript-mime-type
if (Contains(s, "javas") || Contains(s, "ecmas") || Contains(s, "module") || Contains(s, "jscr"))
return eScriptJS;
if (Contains(s, "php"))
return eScriptPHP;
if (Contains(s, "xml")) {
const size_t xml = s.find("xml");
if (xml != std::string::npos) {
for (size_t t = 0; t < xml; t++) {
if (!IsASpace(s[t])) {
return prevValue;
}
}
}
return eScriptXML;
}
return prevValue;
}
int PrintScriptingIndicatorOffset(Accessor &styler, Sci_PositionU start, Sci_PositionU end) {
int iResult = 0;
const std::string s = styler.GetRangeLowered(start, end+1);
if (0 == strncmp(s.c_str(), "php", 3)) {
iResult = 3;
}
return iResult;
}
script_type ScriptOfState(int state) noexcept {
if ((state >= SCE_HP_START) && (state <= SCE_HP_IDENTIFIER)) {
return eScriptPython;
} else if ((state >= SCE_HB_START && state <= SCE_HB_STRINGEOL) || (state == SCE_H_ASPAT || state == SCE_H_XCCOMMENT)) {
return eScriptVBS;
} else if ((state >= SCE_HJ_START) && (state <= SCE_HJ_REGEX)) {
return eScriptJS;
} else if ((state >= SCE_HPHP_DEFAULT && state <= SCE_HPHP_COMMENTLINE) || (state == SCE_HPHP_COMPLEX_VARIABLE)) {
return eScriptPHP;
} else if ((state >= SCE_H_SGML_DEFAULT) && (state < SCE_H_SGML_BLOCK_DEFAULT)) {
return eScriptSGML;
} else if (state == SCE_H_SGML_BLOCK_DEFAULT) {
return eScriptSGMLblock;
} else {
return eScriptNone;
}
}
constexpr int statePrintForState(int state, script_mode inScriptType) noexcept {
int StateToPrint = state;
if (state >= SCE_HJ_START) {
if ((state >= SCE_HP_START) && (state <= SCE_HP_IDENTIFIER)) {
StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_PYTHON);
} else if ((state >= SCE_HB_START) && (state <= SCE_HB_STRINGEOL)) {
StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_VBS);
} else if ((state >= SCE_HJ_START) && (state <= SCE_HJ_REGEX)) {
StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_JS);
}
}
return StateToPrint;
}
constexpr int stateForPrintState(int StateToPrint) noexcept {
int state = StateToPrint;
if ((StateToPrint >= SCE_HPA_START) && (StateToPrint <= SCE_HPA_IDENTIFIER)) {
state = StateToPrint - SCE_HA_PYTHON;
} else if ((StateToPrint >= SCE_HBA_START) && (StateToPrint <= SCE_HBA_STRINGEOL)) {
state = StateToPrint - SCE_HA_VBS;
} else if ((StateToPrint >= SCE_HJA_START) && (StateToPrint <= SCE_HJA_REGEX)) {
state = StateToPrint - SCE_HA_JS;
}
return state;
}
constexpr bool IsNumberChar(char ch) noexcept {
return IsADigit(ch) || ch == '.' || ch == '-' || ch == '#';
}
constexpr bool isStringState(int state) noexcept {
bool bResult = false;
switch (state) {
case SCE_HJ_DOUBLESTRING:
case SCE_HJ_SINGLESTRING:
case SCE_HJA_DOUBLESTRING:
case SCE_HJA_SINGLESTRING:
case SCE_HB_STRING:
case SCE_HBA_STRING:
case SCE_HP_STRING:
case SCE_HP_CHARACTER:
case SCE_HP_TRIPLE:
case SCE_HP_TRIPLEDOUBLE:
case SCE_HPA_STRING:
case SCE_HPA_CHARACTER:
case SCE_HPA_TRIPLE:
case SCE_HPA_TRIPLEDOUBLE:
case SCE_HPHP_HSTRING:
case SCE_HPHP_SIMPLESTRING:
case SCE_HPHP_HSTRING_VARIABLE:
case SCE_HPHP_COMPLEX_VARIABLE:
bResult = true;
break;
default :
break;
}
return bResult;
}
constexpr bool stateAllowsTermination(int state) noexcept {
bool allowTermination = !isStringState(state);
if (allowTermination) {
switch (state) {
case SCE_HPHP_COMMENT:
case SCE_HP_COMMENTLINE:
case SCE_HPA_COMMENTLINE:
allowTermination = false;
break;
default:
break;
}
}
return allowTermination;
}
bool isPreProcessorEndTag(int state, int ch) noexcept {
const script_type type = ScriptOfState(state);
if (state == SCE_H_ASP || AnyOf(type, eScriptVBS, eScriptJS, eScriptPython)) {
return ch == '%';
}
if (type == eScriptPHP) {
return ch == '%' || ch == '?';
}
return ch == '?';
}
// not really well done, since it's only comments that should lex the %> and <%
constexpr bool isCommentASPState(int state) noexcept {
bool bResult = false;
switch (state) {
case SCE_HJ_COMMENT:
case SCE_HJ_COMMENTLINE:
case SCE_HJ_COMMENTDOC:
case SCE_HB_COMMENTLINE:
case SCE_HP_COMMENTLINE:
case SCE_HPHP_COMMENT:
case SCE_HPHP_COMMENTLINE:
bResult = true;
break;
default :
break;
}
return bResult;
}
bool classifyAttribHTML(script_mode inScriptType, Sci_PositionU start, Sci_PositionU end, const WordList &keywords, const WordClassifier &classifier, Accessor &styler, const std::string &tag) {
int chAttr = SCE_H_ATTRIBUTEUNKNOWN;
bool isLanguageType = false;
if (IsNumberChar(styler[start])) {
chAttr = SCE_H_NUMBER;
} else {
const std::string s = styler.GetRangeLowered(start, end+1);
if (keywords.InList(s)) {
chAttr = SCE_H_ATTRIBUTE;
} else {
int subStyle = classifier.ValueFor(s);
if (subStyle < 0) {
// Didn't find attribute, check for tag.attribute
const std::string tagAttribute = tag + "." + s;
subStyle = classifier.ValueFor(tagAttribute);
}
if (subStyle >= 0) {
chAttr = subStyle;
}
}
if (inScriptType == eNonHtmlScript) {
// see https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model
if (s == "type" || s == "language") {
isLanguageType = true;
}
}
}
if ((chAttr == SCE_H_ATTRIBUTEUNKNOWN) && !keywords)
// No keywords -> all are known
chAttr = SCE_H_ATTRIBUTE;
styler.ColourTo(end, chAttr);
return isLanguageType;
}
// https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-core-concepts
bool isHTMLCustomElement(const std::string &tag) noexcept {
// check valid HTML custom element name: starts with an ASCII lower alpha and contains hyphen.
// IsUpperOrLowerCase() is used for `html.tags.case.sensitive=1`.
if (tag.length() < 2 || !IsUpperOrLowerCase(tag[0])) {
return false;
}
if (tag.find('-') == std::string::npos) {
return false;
}
return true;
}
int classifyTagHTML(Sci_PositionU start, Sci_PositionU end,
const WordList &keywords, const WordClassifier &classifier, Accessor &styler, bool &tagDontFold,
bool caseSensitive, bool isXml, bool allowScripts,
const std::set<std::string> &nonFoldingTags,
std::string &tag) {
tag.clear();
// Copy after the '<' and stop before ' '
for (Sci_PositionU cPos = start; cPos <= end; cPos++) {
const char ch = styler[cPos];
if (IsASpace(ch)) {
break;
}
if ((ch != '<') && (ch != '/')) {
tag.push_back(caseSensitive ? ch : MakeLowerCase(ch));
}
}
// if the current language is XML, I can fold any tag
// if the current language is HTML, I don't want to fold certain tags (input, meta, etc.)
//...to find it in the list of no-container-tags
tagDontFold = (!isXml) && (nonFoldingTags.count(tag) > 0);
// No keywords -> all are known
int chAttr = SCE_H_TAGUNKNOWN;
if (!tag.empty() && (tag[0] == '!')) {
chAttr = SCE_H_SGML_DEFAULT;
} else if (!keywords || keywords.InList(tag)) {
chAttr = SCE_H_TAG;
} else if (!isXml && isHTMLCustomElement(tag)) {
chAttr = SCE_H_TAG;
} else {
const int subStyle = classifier.ValueFor(tag);
if (subStyle >= 0) {
chAttr = subStyle;
}
}
if (chAttr != SCE_H_TAGUNKNOWN) {
styler.ColourTo(end, chAttr);
}
if (chAttr == SCE_H_TAG) {
if (allowScripts && (tag == "script")) {
// check to see if this is a self-closing tag by sniffing ahead
bool isSelfClose = false;
for (Sci_PositionU cPos = end; cPos <= end + maxLengthCheck; cPos++) {
const char ch = styler.SafeGetCharAt(cPos, '\0');
if (ch == '\0' || ch == '>')
break;
if (ch == '/' && styler.SafeGetCharAt(cPos + 1, '\0') == '>') {
isSelfClose = true;
break;
}
}
// do not enter a script state if the tag self-closed
if (!isSelfClose)
chAttr = SCE_H_SCRIPT;
} else if (!isXml && (tag == "comment")) {
chAttr = SCE_H_COMMENT;
}
}
return chAttr;
}
void classifyWordHTJS(Sci_PositionU start, Sci_PositionU end,
const WordList &keywords, const WordClassifier &classifier, const WordClassifier &classifierServer, Accessor &styler, script_mode inScriptType) {
const std::string s = styler.GetRange(start, end+1);
int chAttr = SCE_HJ_WORD;
const bool wordIsNumber = IsADigit(s[0]) || ((s[0] == '.') && IsADigit(s[1]));
if (wordIsNumber) {
chAttr = SCE_HJ_NUMBER;
} else if (keywords.InList(s)) {
chAttr = SCE_HJ_KEYWORD;
} else {
const int subStyle = (inScriptType == eNonHtmlScript) ? classifier.ValueFor(s) : classifierServer.ValueFor(s);
if (subStyle >= 0) {
chAttr = subStyle;
}
}
styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
}
int classifyWordHTVB(Sci_PositionU start, Sci_PositionU end, const WordList &keywords, const WordClassifier &classifier, Accessor &styler, script_mode inScriptType) {
int chAttr = SCE_HB_IDENTIFIER;
const bool wordIsNumber = IsADigit(styler[start]) || (styler[start] == '.');
if (wordIsNumber) {
chAttr = SCE_HB_NUMBER;
} else {
const std::string s = styler.GetRangeLowered(start, end+1);
if (keywords.InList(s)) {
chAttr = SCE_HB_WORD;
if (s == "rem")
chAttr = SCE_HB_COMMENTLINE;
} else {
const int subStyle = classifier.ValueFor(s);
if (subStyle >= 0) {
chAttr = subStyle;
}
}
}
styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
if (chAttr == SCE_HB_COMMENTLINE)
return SCE_HB_COMMENTLINE;
else
return SCE_HB_DEFAULT;
}
void classifyWordHTPy(Sci_PositionU start, Sci_PositionU end, const WordList &keywords, const WordClassifier &classifier, Accessor &styler, std::string &prevWord, script_mode inScriptType, bool isMako) {
const bool wordIsNumber = IsADigit(styler[start]);
const std::string s = styler.GetRange(start, end + 1);
int chAttr = SCE_HP_IDENTIFIER;
if (prevWord == "class")
chAttr = SCE_HP_CLASSNAME;
else if (prevWord == "def")
chAttr = SCE_HP_DEFNAME;
else if (wordIsNumber)
chAttr = SCE_HP_NUMBER;
else if (keywords.InList(s))
chAttr = SCE_HP_WORD;
else if (isMako && (s == "block"))
chAttr = SCE_HP_WORD;
else {
const int subStyle = classifier.ValueFor(s);
if (subStyle >= 0) {
chAttr = subStyle;
}
}
styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
prevWord = s;
}
// Update the word colour to default or keyword
// Called when in a PHP word
void classifyWordHTPHP(Sci_PositionU start, Sci_PositionU end, const WordList &keywords, const WordClassifier &classifier, Accessor &styler) {
int chAttr = SCE_HPHP_DEFAULT;
const bool wordIsNumber = IsADigit(styler[start]) || (styler[start] == '.' && start+1 <= end && IsADigit(styler[start+1]));
if (wordIsNumber) {
chAttr = SCE_HPHP_NUMBER;
} else {
const std::string s = styler.GetRangeLowered(start, end+1);;
if (keywords.InList(s)) {
chAttr = SCE_HPHP_WORD;
} else {
const int subStyle = classifier.ValueFor(s);
if (subStyle >= 0) {
chAttr = subStyle;
}
}
}
styler.ColourTo(end, chAttr);
}
bool isWordHSGML(Sci_PositionU start, Sci_PositionU end, const WordList &keywords, Accessor &styler) {
const std::string s = styler.GetRange(start, end + 1);
return keywords.InList(s);
}
bool isWordCdata(Sci_PositionU start, Sci_PositionU end, Accessor &styler) {
const std::string s = styler.GetRange(start, end + 1);
return s == "[CDATA[";
}
// Return the first state to reach when entering a scripting language
constexpr int StateForScript(script_type scriptLanguage) noexcept {
int Result = SCE_HJ_START;
switch (scriptLanguage) {
case eScriptVBS:
Result = SCE_HB_START;
break;
case eScriptPython:
Result = SCE_HP_START;
break;
case eScriptPHP:
Result = SCE_HPHP_DEFAULT;
break;
case eScriptXML:
Result = SCE_H_TAGUNKNOWN;
break;
case eScriptSGML:
Result = SCE_H_SGML_DEFAULT;
break;
case eScriptComment:
Result = SCE_H_COMMENT;
break;
default :
break;
}
return Result;
}
constexpr bool issgmlwordchar(int ch) noexcept {
return !IsASCII(ch) ||
(IsAlphaNumeric(ch) || ch == '.' || ch == '_' || ch == ':' || ch == '!' || ch == '#' || ch == '[');
}
constexpr bool IsPhpWordStart(int ch) noexcept {
return (IsUpperOrLowerCase(ch) || (ch == '_')) || (ch >= 0x7f);
}
constexpr bool IsPhpWordChar(int ch) noexcept {
return IsADigit(ch) || IsPhpWordStart(ch);
}
constexpr bool InTagState(int state) noexcept {
return AnyOf(state, SCE_H_TAG, SCE_H_TAGUNKNOWN, SCE_H_SCRIPT,
SCE_H_ATTRIBUTE, SCE_H_ATTRIBUTEUNKNOWN,
SCE_H_NUMBER, SCE_H_OTHER,
SCE_H_DOUBLESTRING, SCE_H_SINGLESTRING);
}
constexpr bool IsCommentState(const int state) noexcept {
return state == SCE_H_COMMENT || state == SCE_H_SGML_COMMENT;
}
constexpr bool IsScriptCommentState(const int state) noexcept {
return AnyOf(state, SCE_HJ_COMMENT, SCE_HJ_COMMENTLINE, SCE_HJA_COMMENT,
SCE_HJA_COMMENTLINE, SCE_HB_COMMENTLINE, SCE_HBA_COMMENTLINE);
}
constexpr bool isLineEnd(int ch) noexcept {
return ch == '\r' || ch == '\n';
}
bool isMakoBlockEnd(const int ch, const int chNext, const std::string &blockType) noexcept {
if (blockType.empty()) {
return ((ch == '%') && (chNext == '>'));
} else if (InList(blockType, { "inherit", "namespace", "include", "page" })) {
return ((ch == '/') && (chNext == '>'));
} else if (blockType == "%") {
if (ch == '/' && isLineEnd(chNext))
return true;
else
return isLineEnd(ch);
} else if (blockType == "{") {
return ch == '}';
} else {
return (ch == '>');
}
}
bool isDjangoBlockEnd(const int ch, const int chNext, const std::string &blockType) noexcept {
if (blockType.empty()) {
return false;
} else if (blockType == "%") {
return ((ch == '%') && (chNext == '}'));
} else if (blockType == "{") {
return ((ch == '}') && (chNext == '}'));
} else {
return false;
}
}
class PhpNumberState {
enum NumberBase { BASE_10 = 0, BASE_2, BASE_8, BASE_16 };
static constexpr const char *const digitList[] = { "_0123456789", "_01", "_01234567", "_0123456789abcdefABCDEF" };
NumberBase base = BASE_10;
bool decimalPart = false;
bool exponentPart = false;
bool invalid = false;
bool finished = false;
bool leadingZero = false;
bool invalidBase8 = false;
bool betweenDigits = false;
bool decimalChar = false;
bool exponentChar = false;
public:
[[nodiscard]] bool isInvalid() const noexcept { return invalid; }
[[nodiscard]] bool isFinished() const noexcept { return finished; }
bool init(int ch, int chPlus1, int chPlus2) noexcept {
base = BASE_10;
decimalPart = false;
exponentPart = false;
invalid = false;
finished = false;
leadingZero = false;
invalidBase8 = false;
betweenDigits = false;
decimalChar = false;
exponentChar = false;
if (ch == '.' && strchr(digitList[BASE_10] + !betweenDigits, chPlus1) != nullptr) {
decimalPart = true;
betweenDigits = true;
} else if (ch == '0' && (chPlus1 == 'b' || chPlus1 == 'B')) {
base = BASE_2;
} else if (ch == '0' && (chPlus1 == 'o' || chPlus1 == 'O')) {
base = BASE_8;
} else if (ch == '0' && (chPlus1 == 'x' || chPlus1 == 'X')) {
base = BASE_16;
} else if (strchr(digitList[BASE_10] + !betweenDigits, ch) != nullptr) {
leadingZero = ch == '0';
betweenDigits = true;
check(chPlus1, chPlus2);
if (finished && leadingZero) {
// single zero should be base 10
base = BASE_10;
}
} else {
return false;
}
return true;
}
bool check(int ch, int chPlus1) noexcept {
if (strchr(digitList[base] + !betweenDigits, ch) != nullptr) {
if (leadingZero) {
invalidBase8 = invalidBase8 || strchr(digitList[BASE_8] + !betweenDigits, ch) == nullptr;
}
betweenDigits = ch != '_';
decimalChar = false;
exponentChar = false;
} else if (ch == '_') {
invalid = true;
betweenDigits = false;
decimalChar = false;
// exponentChar is unchanged
} else if (base == BASE_10 && ch == '.' && (
!(decimalPart || exponentPart) || strchr(digitList[BASE_10] + !betweenDigits, chPlus1) != nullptr)
) {
invalid = invalid || !betweenDigits || decimalPart || exponentPart;
decimalPart = true;
betweenDigits = false;
decimalChar = true;
exponentChar = false;
} else if (base == BASE_10 && (ch == 'e' || ch == 'E')) {
invalid = invalid || !(betweenDigits || decimalChar) || exponentPart;
exponentPart = true;
betweenDigits = false;
decimalChar = false;
exponentChar = true;
} else if (base == BASE_10 && (ch == '-' || ch == '+') && exponentChar) {
invalid = invalid || strchr(digitList[BASE_10] + !betweenDigits, chPlus1) == nullptr;
betweenDigits = false;
decimalChar = false;
// exponentChar is unchanged
} else if (IsPhpWordChar(ch)) {
invalid = true;
betweenDigits = false;
decimalChar = false;
exponentChar = false;
} else {
invalid = invalid || !(betweenDigits || decimalChar);
finished = true;
if (base == BASE_10 && leadingZero && !decimalPart && !exponentPart) {
base = BASE_8;
invalid = invalid || invalidBase8;
}
}
return finished;
}
};
constexpr bool isPHPStringState(int state) noexcept {
return
(state == SCE_HPHP_HSTRING) ||
(state == SCE_HPHP_SIMPLESTRING) ||
(state == SCE_HPHP_HSTRING_VARIABLE) ||
(state == SCE_HPHP_COMPLEX_VARIABLE);
}
Sci_Position FindPhpStringDelimiter(std::string &phpStringDelimiter, Sci_Position i, const Sci_Position lengthDoc, Accessor &styler, bool &isSimpleString) {
const Sci_Position beginning = i - 1;
bool isQuoted = false;
while (i < lengthDoc && (styler[i] == ' ' || styler[i] == '\t'))
i++;
char ch = styler.SafeGetCharAt(i);
const char chNext = styler.SafeGetCharAt(i + 1);
phpStringDelimiter.clear();
if (!IsPhpWordStart(ch)) {
if ((ch == '\'' || ch == '\"') && IsPhpWordStart(chNext)) {
isSimpleString = ch == '\'';
isQuoted = true;
i++;
ch = chNext;
} else {
return beginning;
}
}
phpStringDelimiter.push_back(ch);
i++;
Sci_Position j = i;
for (; j < lengthDoc && !isLineEnd(styler[j]); j++) {
if (!IsPhpWordChar(styler[j]) && isQuoted) {
if (((isSimpleString && styler[j] == '\'') || (!isSimpleString && styler[j] == '\"')) && isLineEnd(styler.SafeGetCharAt(j + 1))) {
isQuoted = false;
j++;
break;
} else {
phpStringDelimiter.clear();
return beginning;
}
}
phpStringDelimiter.push_back(styler[j]);
}
if (isQuoted) {
phpStringDelimiter.clear();
return beginning;
}
return j - 1;
}
// Options used for LexerHTML
struct OptionsHTML {
int aspDefaultLanguage = eScriptJS;
bool caseSensitive = false;
bool allowScripts = true;
bool isMako = false;
bool isDjango = false;
bool fold = false;
bool foldHTML = false;
bool foldHTMLPreprocessor = true;
bool foldCompact = true;
bool foldComment = false;
bool foldHeredoc = false;
bool foldXmlAtTagOpen = false;
};
const char * const htmlWordListDesc[] = {
"HTML elements and attributes",
"JavaScript keywords",
"VBScript keywords",
"Python keywords",
"PHP keywords",
"SGML and DTD keywords",
nullptr,
};
const char * const phpscriptWordListDesc[] = {
"", //Unused
"", //Unused
"", //Unused
"", //Unused
"PHP keywords",
"", //Unused
nullptr,
};
struct OptionSetHTML : public OptionSet<OptionsHTML> {
explicit OptionSetHTML(bool isPHPScript_) {
DefineProperty("asp.default.language", &OptionsHTML::aspDefaultLanguage,
"Script in ASP code is initially assumed to be in JavaScript. "
"To change this to VBScript set asp.default.language to 2. Python is 3.");
DefineProperty("html.tags.case.sensitive", &OptionsHTML::caseSensitive,
"For XML and HTML, setting this property to 1 will make tags match in a case "
"sensitive way which is the expected behaviour for XML and XHTML.");
DefineProperty("lexer.xml.allow.scripts", &OptionsHTML::allowScripts,
"Set to 0 to disable scripts in XML.");
DefineProperty("lexer.html.mako", &OptionsHTML::isMako,
"Set to 1 to enable the mako template language.");
DefineProperty("lexer.html.django", &OptionsHTML::isDjango,
"Set to 1 to enable the django template language.");
DefineProperty("fold", &OptionsHTML::fold);
DefineProperty("fold.html", &OptionsHTML::foldHTML,
"Folding is turned on or off for HTML and XML files with this option. "
"The fold option must also be on for folding to occur.");
DefineProperty("fold.html.preprocessor", &OptionsHTML::foldHTMLPreprocessor,
"Folding is turned on or off for scripts embedded in HTML files with this option. "
"The default is on.");
DefineProperty("fold.compact", &OptionsHTML::foldCompact);
DefineProperty("fold.hypertext.comment", &OptionsHTML::foldComment,
"Allow folding for comments in scripts embedded in HTML. "
"The default is off.");
DefineProperty("fold.hypertext.heredoc", &OptionsHTML::foldHeredoc,
"Allow folding for heredocs in scripts embedded in HTML. "
"The default is off.");
DefineProperty("fold.xml.at.tag.open", &OptionsHTML::foldXmlAtTagOpen,
"Enable folding for XML at the start of open tag. "
"The default is off.");
DefineWordListSets(isPHPScript_ ? phpscriptWordListDesc : htmlWordListDesc);
}
};
constexpr char styleSubable[] = { SCE_H_TAG, SCE_H_ATTRIBUTE, SCE_HJ_WORD, SCE_HJA_WORD, SCE_HB_WORD, SCE_HP_WORD, SCE_HPHP_WORD, 0 };
// Allow normal styles to be contiguous using 0x80 to 0xBF by assigning sub-styles from 0xC0 to 0xFF
constexpr int SubStylesHTML = 0xC0;
const LexicalClass lexicalClassesHTML[] = {
// Lexer HTML SCLEX_HTML SCE_H_ SCE_HJ_ SCE_HJA_ SCE_HB_ SCE_HBA_ SCE_HP_ SCE_HPHP_ SCE_HPA_:
0, "SCE_H_DEFAULT", "default", "Text",
1, "SCE_H_TAG", "tag", "Tags",
2, "SCE_H_ERRORTAGUNKNOWN", "error tag", "Unknown Tags",
3, "SCE_H_ATTRIBUTE", "attribute", "Attributes",
4, "SCE_H_ATTRIBUTEUNKNOWN", "error attribute", "Unknown Attributes",
5, "SCE_H_NUMBER", "literal numeric", "Numbers",
6, "SCE_H_DOUBLESTRING", "literal string", "Double quoted strings",
7, "SCE_H_SINGLESTRING", "literal string", "Single quoted strings",
8, "SCE_H_OTHER", "tag operator", "Other inside tag, including space and '='",
9, "SCE_H_COMMENT", "comment", "Comment",
10, "SCE_H_ENTITY", "literal", "Entities",
11, "SCE_H_TAGEND", "tag", "XML style tag ends '/>'",
12, "SCE_H_XMLSTART", "identifier", "XML identifier start '<?'",
13, "SCE_H_XMLEND", "identifier", "XML identifier end '?>'",
14, "SCE_H_SCRIPT", "error", "Internal state which should never be visible",
15, "SCE_H_ASP", "preprocessor", "ASP <% ... %>",
16, "SCE_H_ASPAT", "preprocessor", "ASP <% ... %>",
17, "SCE_H_CDATA", "literal", "CDATA",
18, "SCE_H_QUESTION", "preprocessor", "PHP",
19, "SCE_H_VALUE", "literal string", "Unquoted values",
20, "SCE_H_XCCOMMENT", "comment", "ASP.NET, JSP Comment <%-- ... --%>",
21, "SCE_H_SGML_DEFAULT", "default", "SGML tags <! ... >",
22, "SCE_H_SGML_COMMAND", "preprocessor", "SGML command",
23, "SCE_H_SGML_1ST_PARAM", "preprocessor", "SGML 1st param",
24, "SCE_H_SGML_DOUBLESTRING", "literal string", "SGML double string",
25, "SCE_H_SGML_SIMPLESTRING", "literal string", "SGML single string",
26, "SCE_H_SGML_ERROR", "error", "SGML error",
27, "SCE_H_SGML_SPECIAL", "literal", "SGML special (#XXXX type)",
28, "SCE_H_SGML_ENTITY", "literal", "SGML entity",
29, "SCE_H_SGML_COMMENT", "comment", "SGML comment",
30, "SCE_H_SGML_1ST_PARAM_COMMENT", "error comment", "SGML first parameter - lexer internal. It is an error if any text is in this style.",
31, "SCE_H_SGML_BLOCK_DEFAULT", "default", "SGML block",
32, "", "predefined", "",
33, "", "predefined", "",
34, "", "predefined", "",
35, "", "predefined", "",
36, "", "predefined", "",
37, "", "predefined", "",
38, "", "predefined", "",
39, "", "predefined", "",
40, "SCE_HJ_START", "client javascript default", "JS Start - allows eol filled background to not start on same line as SCRIPT tag",
41, "SCE_HJ_DEFAULT", "client javascript default", "JS Default",
42, "SCE_HJ_COMMENT", "client javascript comment", "JS Comment",
43, "SCE_HJ_COMMENTLINE", "client javascript comment line", "JS Line Comment",
44, "SCE_HJ_COMMENTDOC", "client javascript comment documentation", "JS Doc comment",
45, "SCE_HJ_NUMBER", "client javascript literal numeric", "JS Number",
46, "SCE_HJ_WORD", "client javascript identifier", "JS Word",
47, "SCE_HJ_KEYWORD", "client javascript keyword", "JS Keyword",
48, "SCE_HJ_DOUBLESTRING", "client javascript literal string", "JS Double quoted string",
49, "SCE_HJ_SINGLESTRING", "client javascript literal string", "JS Single quoted string",
50, "SCE_HJ_SYMBOLS", "client javascript operator", "JS Symbols",
51, "SCE_HJ_STRINGEOL", "client javascript error literal string", "JavaScript EOL",
52, "SCE_HJ_REGEX", "client javascript literal regex", "JavaScript RegEx",
53, "", "unused", "",
54, "", "unused", "",
55, "SCE_HJA_START", "server javascript default", "JS Start - allows eol filled background to not start on same line as SCRIPT tag",
56, "SCE_HJA_DEFAULT", "server javascript default", "JS Default",
57, "SCE_HJA_COMMENT", "server javascript comment", "JS Comment",
58, "SCE_HJA_COMMENTLINE", "server javascript comment line", "JS Line Comment",
59, "SCE_HJA_COMMENTDOC", "server javascript comment documentation", "JS Doc comment",
60, "SCE_HJA_NUMBER", "server javascript literal numeric", "JS Number",
61, "SCE_HJA_WORD", "server javascript identifier", "JS Word",
62, "SCE_HJA_KEYWORD", "server javascript keyword", "JS Keyword",
63, "SCE_HJA_DOUBLESTRING", "server javascript literal string", "JS Double quoted string",
64, "SCE_HJA_SINGLESTRING", "server javascript literal string", "JS Single quoted string",
65, "SCE_HJA_SYMBOLS", "server javascript operator", "JS Symbols",
66, "SCE_HJA_STRINGEOL", "server javascript error literal string", "JavaScript EOL",
67, "SCE_HJA_REGEX", "server javascript literal regex", "JavaScript RegEx",
68, "", "unused", "",
69, "", "unused", "",
70, "SCE_HB_START", "client basic default", "Start",
71, "SCE_HB_DEFAULT", "client basic default", "Default",
72, "SCE_HB_COMMENTLINE", "client basic comment line", "Comment",
73, "SCE_HB_NUMBER", "client basic literal numeric", "Number",
74, "SCE_HB_WORD", "client basic keyword", "KeyWord",
75, "SCE_HB_STRING", "client basic literal string", "String",
76, "SCE_HB_IDENTIFIER", "client basic identifier", "Identifier",
77, "SCE_HB_STRINGEOL", "client basic literal string", "Unterminated string",
78, "", "unused", "",
79, "", "unused", "",
80, "SCE_HBA_START", "server basic default", "Start",
81, "SCE_HBA_DEFAULT", "server basic default", "Default",
82, "SCE_HBA_COMMENTLINE", "server basic comment line", "Comment",
83, "SCE_HBA_NUMBER", "server basic literal numeric", "Number",
84, "SCE_HBA_WORD", "server basic keyword", "KeyWord",
85, "SCE_HBA_STRING", "server basic literal string", "String",
86, "SCE_HBA_IDENTIFIER", "server basic identifier", "Identifier",
87, "SCE_HBA_STRINGEOL", "server basic literal string", "Unterminated string",
88, "", "unused", "",
89, "", "unused", "",
90, "SCE_HP_START", "client python default", "Embedded Python",
91, "SCE_HP_DEFAULT", "client python default", "Embedded Python",
92, "SCE_HP_COMMENTLINE", "client python comment line", "Comment",
93, "SCE_HP_NUMBER", "client python literal numeric", "Number",
94, "SCE_HP_STRING", "client python literal string", "String",
95, "SCE_HP_CHARACTER", "client python literal string character", "Single quoted string",
96, "SCE_HP_WORD", "client python keyword", "Keyword",
97, "SCE_HP_TRIPLE", "client python literal string", "Triple quotes",
98, "SCE_HP_TRIPLEDOUBLE", "client python literal string", "Triple double quotes",
99, "SCE_HP_CLASSNAME", "client python identifier", "Class name definition",
100, "SCE_HP_DEFNAME", "client python identifier", "Function or method name definition",
101, "SCE_HP_OPERATOR", "client python operator", "Operators",
102, "SCE_HP_IDENTIFIER", "client python identifier", "Identifiers",
103, "", "unused", "",
104, "SCE_HPHP_COMPLEX_VARIABLE", "server php identifier", "PHP complex variable",
105, "SCE_HPA_START", "server python default", "ASP Python",
106, "SCE_HPA_DEFAULT", "server python default", "ASP Python",
107, "SCE_HPA_COMMENTLINE", "server python comment line", "Comment",
108, "SCE_HPA_NUMBER", "server python literal numeric", "Number",
109, "SCE_HPA_STRING", "server python literal string", "String",
110, "SCE_HPA_CHARACTER", "server python literal string character", "Single quoted string",
111, "SCE_HPA_WORD", "server python keyword", "Keyword",
112, "SCE_HPA_TRIPLE", "server python literal string", "Triple quotes",
113, "SCE_HPA_TRIPLEDOUBLE", "server python literal string", "Triple double quotes",
114, "SCE_HPA_CLASSNAME", "server python identifier", "Class name definition",
115, "SCE_HPA_DEFNAME", "server python identifier", "Function or method name definition",
116, "SCE_HPA_OPERATOR", "server python operator", "Operators",
117, "SCE_HPA_IDENTIFIER", "server python identifier", "Identifiers",
118, "SCE_HPHP_DEFAULT", "server php default", "Default",
119, "SCE_HPHP_HSTRING", "server php literal string", "Double quoted String",
120, "SCE_HPHP_SIMPLESTRING", "server php literal string", "Single quoted string",
121, "SCE_HPHP_WORD", "server php keyword", "Keyword",
122, "SCE_HPHP_NUMBER", "server php literal numeric", "Number",
123, "SCE_HPHP_VARIABLE", "server php identifier", "Variable",
124, "SCE_HPHP_COMMENT", "server php comment", "Comment",
125, "SCE_HPHP_COMMENTLINE", "server php comment line", "One line comment",
126, "SCE_HPHP_HSTRING_VARIABLE", "server php literal string identifier", "PHP variable in double quoted string",
127, "SCE_HPHP_OPERATOR", "server php operator", "PHP operator",
};
const LexicalClass lexicalClassesXML[] = {
// Lexer.Secondary XML SCLEX_XML SCE_H_:
0, "SCE_H_DEFAULT", "default", "Default",
1, "SCE_H_TAG", "tag", "Tags",
2, "SCE_H_TAGUNKNOWN", "error tag", "Unknown Tags",
3, "SCE_H_ATTRIBUTE", "attribute", "Attributes",
4, "SCE_H_ERRORATTRIBUTEUNKNOWN", "error attribute", "Unknown Attributes",
5, "SCE_H_NUMBER", "literal numeric", "Numbers",
6, "SCE_H_DOUBLESTRING", "literal string", "Double quoted strings",
7, "SCE_H_SINGLESTRING", "literal string", "Single quoted strings",
8, "SCE_H_OTHER", "tag operator", "Other inside tag, including space and '='",
9, "SCE_H_COMMENT", "comment", "Comment",
10, "SCE_H_ENTITY", "literal", "Entities",
11, "SCE_H_TAGEND", "tag", "XML style tag ends '/>'",
12, "SCE_H_XMLSTART", "identifier", "XML identifier start '<?'",
13, "SCE_H_XMLEND", "identifier", "XML identifier end '?>'",
14, "", "unused", "",
15, "", "unused", "",
16, "", "unused", "",
17, "SCE_H_CDATA", "literal", "CDATA",
18, "SCE_H_QUESTION", "preprocessor", "Question",
19, "SCE_H_VALUE", "literal string", "Unquoted Value",
20, "", "unused", "",
21, "SCE_H_SGML_DEFAULT", "default", "SGML tags <! ... >",
22, "SCE_H_SGML_COMMAND", "preprocessor", "SGML command",
23, "SCE_H_SGML_1ST_PARAM", "preprocessor", "SGML 1st param",
24, "SCE_H_SGML_DOUBLESTRING", "literal string", "SGML double string",
25, "SCE_H_SGML_SIMPLESTRING", "literal string", "SGML single string",
26, "SCE_H_SGML_ERROR", "error", "SGML error",
27, "SCE_H_SGML_SPECIAL", "literal", "SGML special (#XXXX type)",
28, "SCE_H_SGML_ENTITY", "literal", "SGML entity",
29, "SCE_H_SGML_COMMENT", "comment", "SGML comment",
30, "", "unused", "",
31, "SCE_H_SGML_BLOCK_DEFAULT", "default", "SGML block",
};
const char * const tagsThatDoNotFold[] = {
"area",
"base",
"basefont",
"br",
"col",
"command",
"embed",
"frame",
"hr",
"img",
"input",
"isindex",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr"
};
}
class LexerHTML : public DefaultLexer {