PageRenderTime 44ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 1ms

/Branches/4.0/Scintilla/src/Lexers/LexHTML.cxx

#
C++ | 2131 lines | 1904 code | 118 blank | 109 comment | 1394 complexity | 7119e180093fefd3798b01ae02bf5b27 MD5 | raw file
Possible License(s): MIT

Large files files are truncated, but you can click here to view the full file

  1. // Scintilla source code edit control
  2. /** @file LexHTML.cxx
  3. ** Lexer for HTML.
  4. **/
  5. // Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
  6. // The License.txt file describes the conditions under which this software may be distributed.
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <stdarg.h>
  12. #include "Platform.h"
  13. #include "PropSet.h"
  14. #include "Accessor.h"
  15. #include "StyleContext.h"
  16. #include "KeyWords.h"
  17. #include "Scintilla.h"
  18. #include "SciLexer.h"
  19. #include "CharacterSet.h"
  20. #ifdef SCI_NAMESPACE
  21. using namespace Scintilla;
  22. #endif
  23. #define SCE_HA_JS (SCE_HJA_START - SCE_HJ_START)
  24. #define SCE_HA_VBS (SCE_HBA_START - SCE_HB_START)
  25. #define SCE_HA_PYTHON (SCE_HPA_START - SCE_HP_START)
  26. enum script_type { eScriptNone = 0, eScriptJS, eScriptVBS, eScriptPython, eScriptPHP, eScriptXML, eScriptSGML, eScriptSGMLblock, eScriptComment };
  27. enum script_mode { eHtml = 0, eNonHtmlScript, eNonHtmlPreProc, eNonHtmlScriptPreProc };
  28. static inline bool IsAWordChar(const int ch) {
  29. return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
  30. }
  31. static inline bool IsAWordStart(const int ch) {
  32. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  33. }
  34. inline bool IsOperator(int ch) {
  35. if (isascii(ch) && isalnum(ch))
  36. return false;
  37. // '.' left out as it is used to make up numbers
  38. if (ch == '%' || ch == '^' || ch == '&' || ch == '*' ||
  39. ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
  40. ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
  41. ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
  42. ch == '<' || ch == '>' || ch == ',' || ch == '/' ||
  43. ch == '?' || ch == '!' || ch == '.' || ch == '~')
  44. return true;
  45. return false;
  46. }
  47. static inline int MakeLowerCase(int ch) {
  48. if (ch < 'A' || ch > 'Z')
  49. return ch;
  50. else
  51. return ch - 'A' + 'a';
  52. }
  53. static void GetTextSegment(Accessor &styler, unsigned int start, unsigned int end, char *s, size_t len) {
  54. size_t i = 0;
  55. for (; (i < end - start + 1) && (i < len-1); i++) {
  56. s[i] = static_cast<char>(MakeLowerCase(styler[start + i]));
  57. }
  58. s[i] = '\0';
  59. }
  60. static const char *GetNextWord(Accessor &styler, unsigned int start, char *s, size_t sLen) {
  61. size_t i = 0;
  62. for (; i < sLen-1; i++) {
  63. char ch = static_cast<char>(styler.SafeGetCharAt(start + i));
  64. if ((i == 0) && !IsAWordStart(ch))
  65. break;
  66. if ((i > 0) && !IsAWordChar(ch))
  67. break;
  68. s[i] = ch;
  69. }
  70. s[i] = '\0';
  71. return s;
  72. }
  73. static script_type segIsScriptingIndicator(Accessor &styler, unsigned int start, unsigned int end, script_type prevValue) {
  74. char s[100];
  75. GetTextSegment(styler, start, end, s, sizeof(s));
  76. //Platform::DebugPrintf("Scripting indicator [%s]\n", s);
  77. if (strstr(s, "src")) // External script
  78. return eScriptNone;
  79. if (strstr(s, "vbs"))
  80. return eScriptVBS;
  81. if (strstr(s, "pyth"))
  82. return eScriptPython;
  83. if (strstr(s, "javas"))
  84. return eScriptJS;
  85. if (strstr(s, "jscr"))
  86. return eScriptJS;
  87. if (strstr(s, "php"))
  88. return eScriptPHP;
  89. if (strstr(s, "xml")) {
  90. const char *xml = strstr(s, "xml");
  91. for (const char *t=s; t<xml; t++) {
  92. if (!IsASpace(*t)) {
  93. return prevValue;
  94. }
  95. }
  96. return eScriptXML;
  97. }
  98. return prevValue;
  99. }
  100. static int PrintScriptingIndicatorOffset(Accessor &styler, unsigned int start, unsigned int end) {
  101. int iResult = 0;
  102. char s[100];
  103. GetTextSegment(styler, start, end, s, sizeof(s));
  104. if (0 == strncmp(s, "php", 3)) {
  105. iResult = 3;
  106. }
  107. return iResult;
  108. }
  109. static script_type ScriptOfState(int state) {
  110. if ((state >= SCE_HP_START) && (state <= SCE_HP_IDENTIFIER)) {
  111. return eScriptPython;
  112. } else if ((state >= SCE_HB_START) && (state <= SCE_HB_STRINGEOL)) {
  113. return eScriptVBS;
  114. } else if ((state >= SCE_HJ_START) && (state <= SCE_HJ_REGEX)) {
  115. return eScriptJS;
  116. } else if ((state >= SCE_HPHP_DEFAULT) && (state <= SCE_HPHP_COMMENTLINE)) {
  117. return eScriptPHP;
  118. } else if ((state >= SCE_H_SGML_DEFAULT) && (state < SCE_H_SGML_BLOCK_DEFAULT)) {
  119. return eScriptSGML;
  120. } else if (state == SCE_H_SGML_BLOCK_DEFAULT) {
  121. return eScriptSGMLblock;
  122. } else {
  123. return eScriptNone;
  124. }
  125. }
  126. static int statePrintForState(int state, script_mode inScriptType) {
  127. int StateToPrint = state;
  128. if (state >= SCE_HJ_START) {
  129. if ((state >= SCE_HP_START) && (state <= SCE_HP_IDENTIFIER)) {
  130. StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_PYTHON);
  131. } else if ((state >= SCE_HB_START) && (state <= SCE_HB_STRINGEOL)) {
  132. StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_VBS);
  133. } else if ((state >= SCE_HJ_START) && (state <= SCE_HJ_REGEX)) {
  134. StateToPrint = state + ((inScriptType == eNonHtmlScript) ? 0 : SCE_HA_JS);
  135. }
  136. }
  137. return StateToPrint;
  138. }
  139. static int stateForPrintState(int StateToPrint) {
  140. int state;
  141. if ((StateToPrint >= SCE_HPA_START) && (StateToPrint <= SCE_HPA_IDENTIFIER)) {
  142. state = StateToPrint - SCE_HA_PYTHON;
  143. } else if ((StateToPrint >= SCE_HBA_START) && (StateToPrint <= SCE_HBA_STRINGEOL)) {
  144. state = StateToPrint - SCE_HA_VBS;
  145. } else if ((StateToPrint >= SCE_HJA_START) && (StateToPrint <= SCE_HJA_REGEX)) {
  146. state = StateToPrint - SCE_HA_JS;
  147. } else {
  148. state = StateToPrint;
  149. }
  150. return state;
  151. }
  152. static inline bool IsNumber(unsigned int start, Accessor &styler) {
  153. return IsADigit(styler[start]) || (styler[start] == '.') ||
  154. (styler[start] == '-') || (styler[start] == '#');
  155. }
  156. static inline bool isStringState(int state) {
  157. bool bResult;
  158. switch (state) {
  159. case SCE_HJ_DOUBLESTRING:
  160. case SCE_HJ_SINGLESTRING:
  161. case SCE_HJA_DOUBLESTRING:
  162. case SCE_HJA_SINGLESTRING:
  163. case SCE_HB_STRING:
  164. case SCE_HBA_STRING:
  165. case SCE_HP_STRING:
  166. case SCE_HP_CHARACTER:
  167. case SCE_HP_TRIPLE:
  168. case SCE_HP_TRIPLEDOUBLE:
  169. case SCE_HPA_STRING:
  170. case SCE_HPA_CHARACTER:
  171. case SCE_HPA_TRIPLE:
  172. case SCE_HPA_TRIPLEDOUBLE:
  173. case SCE_HPHP_HSTRING:
  174. case SCE_HPHP_SIMPLESTRING:
  175. case SCE_HPHP_HSTRING_VARIABLE:
  176. case SCE_HPHP_COMPLEX_VARIABLE:
  177. bResult = true;
  178. break;
  179. default :
  180. bResult = false;
  181. break;
  182. }
  183. return bResult;
  184. }
  185. static inline bool stateAllowsTermination(int state) {
  186. bool allowTermination = !isStringState(state);
  187. if (allowTermination) {
  188. switch (state) {
  189. case SCE_HB_COMMENTLINE:
  190. case SCE_HPHP_COMMENT:
  191. case SCE_HP_COMMENTLINE:
  192. case SCE_HPA_COMMENTLINE:
  193. allowTermination = false;
  194. }
  195. }
  196. return allowTermination;
  197. }
  198. // not really well done, since it's only comments that should lex the %> and <%
  199. static inline bool isCommentASPState(int state) {
  200. bool bResult;
  201. switch (state) {
  202. case SCE_HJ_COMMENT:
  203. case SCE_HJ_COMMENTLINE:
  204. case SCE_HJ_COMMENTDOC:
  205. case SCE_HB_COMMENTLINE:
  206. case SCE_HP_COMMENTLINE:
  207. case SCE_HPHP_COMMENT:
  208. case SCE_HPHP_COMMENTLINE:
  209. bResult = true;
  210. break;
  211. default :
  212. bResult = false;
  213. break;
  214. }
  215. return bResult;
  216. }
  217. static void classifyAttribHTML(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  218. bool wordIsNumber = IsNumber(start, styler);
  219. char chAttr = SCE_H_ATTRIBUTEUNKNOWN;
  220. if (wordIsNumber) {
  221. chAttr = SCE_H_NUMBER;
  222. } else {
  223. char s[100];
  224. GetTextSegment(styler, start, end, s, sizeof(s));
  225. if (keywords.InList(s))
  226. chAttr = SCE_H_ATTRIBUTE;
  227. }
  228. if ((chAttr == SCE_H_ATTRIBUTEUNKNOWN) && !keywords)
  229. // No keywords -> all are known
  230. chAttr = SCE_H_ATTRIBUTE;
  231. styler.ColourTo(end, chAttr);
  232. }
  233. static int classifyTagHTML(unsigned int start, unsigned int end,
  234. WordList &keywords, Accessor &styler, bool &tagDontFold,
  235. bool caseSensitive, bool isXml, bool allowScripts) {
  236. char s[30 + 2];
  237. // Copy after the '<'
  238. unsigned int i = 0;
  239. for (unsigned int cPos = start; cPos <= end && i < 30; cPos++) {
  240. char ch = styler[cPos];
  241. if ((ch != '<') && (ch != '/')) {
  242. s[i++] = caseSensitive ? ch : static_cast<char>(MakeLowerCase(ch));
  243. }
  244. }
  245. //The following is only a quick hack, to see if this whole thing would work
  246. //we first need the tagname with a trailing space...
  247. s[i] = ' ';
  248. s[i+1] = '\0';
  249. // if the current language is XML, I can fold any tag
  250. // if the current language is HTML, I don't want to fold certain tags (input, meta, etc.)
  251. //...to find it in the list of no-container-tags
  252. tagDontFold = (!isXml) && (NULL != strstr("meta link img area br hr input ", s));
  253. //now we can remove the trailing space
  254. s[i] = '\0';
  255. // No keywords -> all are known
  256. char chAttr = SCE_H_TAGUNKNOWN;
  257. if (s[0] == '!') {
  258. chAttr = SCE_H_SGML_DEFAULT;
  259. } else if (!keywords || keywords.InList(s)) {
  260. chAttr = SCE_H_TAG;
  261. }
  262. styler.ColourTo(end, chAttr);
  263. if (chAttr == SCE_H_TAG) {
  264. if (allowScripts && 0 == strcmp(s, "script")) {
  265. // check to see if this is a self-closing tag by sniffing ahead
  266. bool isSelfClose = false;
  267. for (unsigned int cPos = end; cPos <= end + 100; cPos++) {
  268. char ch = styler.SafeGetCharAt(cPos, '\0');
  269. if (ch == '\0' || ch == '>')
  270. break;
  271. else if (ch == '/' && styler.SafeGetCharAt(cPos + 1, '\0') == '>') {
  272. isSelfClose = true;
  273. break;
  274. }
  275. }
  276. // do not enter a script state if the tag self-closed
  277. if (!isSelfClose)
  278. chAttr = SCE_H_SCRIPT;
  279. } else if (!isXml && 0 == strcmp(s, "comment")) {
  280. chAttr = SCE_H_COMMENT;
  281. }
  282. }
  283. return chAttr;
  284. }
  285. static void classifyWordHTJS(unsigned int start, unsigned int end,
  286. WordList &keywords, Accessor &styler, script_mode inScriptType) {
  287. char chAttr = SCE_HJ_WORD;
  288. bool wordIsNumber = IsADigit(styler[start]) || (styler[start] == '.');
  289. if (wordIsNumber)
  290. chAttr = SCE_HJ_NUMBER;
  291. else {
  292. char s[30 + 1];
  293. unsigned int i = 0;
  294. for (; i < end - start + 1 && i < 30; i++) {
  295. s[i] = styler[start + i];
  296. }
  297. s[i] = '\0';
  298. if (keywords.InList(s))
  299. chAttr = SCE_HJ_KEYWORD;
  300. }
  301. styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
  302. }
  303. static int classifyWordHTVB(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler, script_mode inScriptType) {
  304. char chAttr = SCE_HB_IDENTIFIER;
  305. bool wordIsNumber = IsADigit(styler[start]) || (styler[start] == '.');
  306. if (wordIsNumber)
  307. chAttr = SCE_HB_NUMBER;
  308. else {
  309. char s[100];
  310. GetTextSegment(styler, start, end, s, sizeof(s));
  311. if (keywords.InList(s)) {
  312. chAttr = SCE_HB_WORD;
  313. if (strcmp(s, "rem") == 0)
  314. chAttr = SCE_HB_COMMENTLINE;
  315. }
  316. }
  317. styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
  318. if (chAttr == SCE_HB_COMMENTLINE)
  319. return SCE_HB_COMMENTLINE;
  320. else
  321. return SCE_HB_DEFAULT;
  322. }
  323. static void classifyWordHTPy(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler, char *prevWord, script_mode inScriptType) {
  324. bool wordIsNumber = IsADigit(styler[start]);
  325. char s[30 + 1];
  326. unsigned int i = 0;
  327. for (; i < end - start + 1 && i < 30; i++) {
  328. s[i] = styler[start + i];
  329. }
  330. s[i] = '\0';
  331. char chAttr = SCE_HP_IDENTIFIER;
  332. if (0 == strcmp(prevWord, "class"))
  333. chAttr = SCE_HP_CLASSNAME;
  334. else if (0 == strcmp(prevWord, "def"))
  335. chAttr = SCE_HP_DEFNAME;
  336. else if (wordIsNumber)
  337. chAttr = SCE_HP_NUMBER;
  338. else if (keywords.InList(s))
  339. chAttr = SCE_HP_WORD;
  340. styler.ColourTo(end, statePrintForState(chAttr, inScriptType));
  341. strcpy(prevWord, s);
  342. }
  343. // Update the word colour to default or keyword
  344. // Called when in a PHP word
  345. static void classifyWordHTPHP(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  346. char chAttr = SCE_HPHP_DEFAULT;
  347. bool wordIsNumber = IsADigit(styler[start]) || (styler[start] == '.' && start+1 <= end && IsADigit(styler[start+1]));
  348. if (wordIsNumber)
  349. chAttr = SCE_HPHP_NUMBER;
  350. else {
  351. char s[100];
  352. GetTextSegment(styler, start, end, s, sizeof(s));
  353. if (keywords.InList(s))
  354. chAttr = SCE_HPHP_WORD;
  355. }
  356. styler.ColourTo(end, chAttr);
  357. }
  358. static bool isWordHSGML(unsigned int start, unsigned int end, WordList &keywords, Accessor &styler) {
  359. char s[30 + 1];
  360. unsigned int i = 0;
  361. for (; i < end - start + 1 && i < 30; i++) {
  362. s[i] = styler[start + i];
  363. }
  364. s[i] = '\0';
  365. return keywords.InList(s);
  366. }
  367. static bool isWordCdata(unsigned int start, unsigned int end, Accessor &styler) {
  368. char s[30 + 1];
  369. unsigned int i = 0;
  370. for (; i < end - start + 1 && i < 30; i++) {
  371. s[i] = styler[start + i];
  372. }
  373. s[i] = '\0';
  374. return (0 == strcmp(s, "[CDATA["));
  375. }
  376. // Return the first state to reach when entering a scripting language
  377. static int StateForScript(script_type scriptLanguage) {
  378. int Result;
  379. switch (scriptLanguage) {
  380. case eScriptVBS:
  381. Result = SCE_HB_START;
  382. break;
  383. case eScriptPython:
  384. Result = SCE_HP_START;
  385. break;
  386. case eScriptPHP:
  387. Result = SCE_HPHP_DEFAULT;
  388. break;
  389. case eScriptXML:
  390. Result = SCE_H_TAGUNKNOWN;
  391. break;
  392. case eScriptSGML:
  393. Result = SCE_H_SGML_DEFAULT;
  394. break;
  395. case eScriptComment:
  396. Result = SCE_H_COMMENT;
  397. break;
  398. default :
  399. Result = SCE_HJ_START;
  400. break;
  401. }
  402. return Result;
  403. }
  404. static inline bool ishtmlwordchar(int ch) {
  405. return !isascii(ch) ||
  406. (isalnum(ch) || ch == '.' || ch == '-' || ch == '_' || ch == ':' || ch == '!' || ch == '#');
  407. }
  408. static inline bool issgmlwordchar(int ch) {
  409. return !isascii(ch) ||
  410. (isalnum(ch) || ch == '.' || ch == '_' || ch == ':' || ch == '!' || ch == '#' || ch == '[');
  411. }
  412. static inline bool IsPhpWordStart(int ch) {
  413. return (isascii(ch) && (isalpha(ch) || (ch == '_'))) || (ch >= 0x7f);
  414. }
  415. static inline bool IsPhpWordChar(int ch) {
  416. return IsADigit(ch) || IsPhpWordStart(ch);
  417. }
  418. static bool InTagState(int state) {
  419. return state == SCE_H_TAG || state == SCE_H_TAGUNKNOWN ||
  420. state == SCE_H_SCRIPT ||
  421. state == SCE_H_ATTRIBUTE || state == SCE_H_ATTRIBUTEUNKNOWN ||
  422. state == SCE_H_NUMBER || state == SCE_H_OTHER ||
  423. state == SCE_H_DOUBLESTRING || state == SCE_H_SINGLESTRING;
  424. }
  425. static bool IsCommentState(const int state) {
  426. return state == SCE_H_COMMENT || state == SCE_H_SGML_COMMENT;
  427. }
  428. static bool IsScriptCommentState(const int state) {
  429. return state == SCE_HJ_COMMENT || state == SCE_HJ_COMMENTLINE || state == SCE_HJA_COMMENT ||
  430. state == SCE_HJA_COMMENTLINE || state == SCE_HB_COMMENTLINE || state == SCE_HBA_COMMENTLINE;
  431. }
  432. static bool isLineEnd(int ch) {
  433. return ch == '\r' || ch == '\n';
  434. }
  435. static bool isOKBeforeRE(int ch) {
  436. return (ch == '(') || (ch == '=') || (ch == ',');
  437. }
  438. static bool isMakoBlockEnd(const int ch, const int chNext, const char *blockType) {
  439. if (strlen(blockType) == 0) {
  440. return ((ch == '%') && (chNext == '>'));
  441. } else if ((0 == strcmp(blockType, "inherit")) ||
  442. (0 == strcmp(blockType, "namespace")) ||
  443. (0 == strcmp(blockType, "include")) ||
  444. (0 == strcmp(blockType, "page"))) {
  445. return ((ch == '/') && (chNext == '>'));
  446. } else if (0 == strcmp(blockType, "%")) {
  447. return isLineEnd(ch);
  448. } else if (0 == strcmp(blockType, "{")) {
  449. return ch == '}';
  450. } else {
  451. return (ch == '>');
  452. }
  453. }
  454. static bool isDjangoBlockEnd(const int ch, const int chNext, const char *blockType) {
  455. if (strlen(blockType) == 0) {
  456. return 0;
  457. } else if (0 == strcmp(blockType, "%")) {
  458. return ((ch == '%') && (chNext == '}'));
  459. } else if (0 == strcmp(blockType, "{")) {
  460. return ((ch == '}') && (chNext == '}'));
  461. } else {
  462. return 0;
  463. }
  464. }
  465. static bool isPHPStringState(int state) {
  466. return
  467. (state == SCE_HPHP_HSTRING) ||
  468. (state == SCE_HPHP_SIMPLESTRING) ||
  469. (state == SCE_HPHP_HSTRING_VARIABLE) ||
  470. (state == SCE_HPHP_COMPLEX_VARIABLE);
  471. }
  472. static int FindPhpStringDelimiter(char *phpStringDelimiter, const int phpStringDelimiterSize, int i, const int lengthDoc, Accessor &styler, bool &isSimpleString) {
  473. int j;
  474. const int beginning = i - 1;
  475. bool isValidSimpleString = false;
  476. while (i < lengthDoc && (styler[i] == ' ' || styler[i] == '\t'))
  477. i++;
  478. char ch = styler.SafeGetCharAt(i);
  479. const char chNext = styler.SafeGetCharAt(i + 1);
  480. if (!IsPhpWordStart(ch)) {
  481. if (ch == '\'' && IsPhpWordStart(chNext)) {
  482. i++;
  483. ch = chNext;
  484. isSimpleString = true;
  485. } else {
  486. phpStringDelimiter[0] = '\0';
  487. return beginning;
  488. }
  489. }
  490. phpStringDelimiter[0] = ch;
  491. i++;
  492. for (j = i; j < lengthDoc && !isLineEnd(styler[j]); j++) {
  493. if (!IsPhpWordChar(styler[j])) {
  494. if (isSimpleString && (styler[j] == '\'') && isLineEnd(styler.SafeGetCharAt(j + 1))) {
  495. isValidSimpleString = true;
  496. j++;
  497. break;
  498. } else {
  499. phpStringDelimiter[0] = '\0';
  500. return beginning;
  501. }
  502. }
  503. if (j - i < phpStringDelimiterSize - 2)
  504. phpStringDelimiter[j-i+1] = styler[j];
  505. else
  506. i++;
  507. }
  508. if (isSimpleString && !isValidSimpleString) {
  509. phpStringDelimiter[0] = '\0';
  510. return beginning;
  511. }
  512. phpStringDelimiter[j-i+1 - (isSimpleString ? 1 : 0)] = '\0';
  513. return j - 1;
  514. }
  515. static void ColouriseHyperTextDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
  516. Accessor &styler, bool isXml) {
  517. WordList &keywords = *keywordlists[0];
  518. WordList &keywords2 = *keywordlists[1];
  519. WordList &keywords3 = *keywordlists[2];
  520. WordList &keywords4 = *keywordlists[3];
  521. WordList &keywords5 = *keywordlists[4];
  522. WordList &keywords6 = *keywordlists[5]; // SGML (DTD) keywords
  523. // Lexer for HTML requires more lexical states (8 bits worth) than most lexers
  524. styler.StartAt(startPos, static_cast<char>(STYLE_MAX));
  525. char prevWord[200];
  526. prevWord[0] = '\0';
  527. char phpStringDelimiter[200]; // PHP is not limited in length, we are
  528. phpStringDelimiter[0] = '\0';
  529. int StateToPrint = initStyle;
  530. int state = stateForPrintState(StateToPrint);
  531. char makoBlockType[200];
  532. makoBlockType[0] = '\0';
  533. char djangoBlockType[2];
  534. djangoBlockType[0] = '\0';
  535. // If inside a tag, it may be a script tag, so reread from the start to ensure any language tags are seen
  536. if (InTagState(state)) {
  537. while ((startPos > 0) && (InTagState(styler.StyleAt(startPos - 1)))) {
  538. startPos--;
  539. length++;
  540. }
  541. state = SCE_H_DEFAULT;
  542. }
  543. // String can be heredoc, must find a delimiter first. Reread from beginning of line containing the string, to get the correct lineState
  544. if (isPHPStringState(state)) {
  545. while (startPos > 0 && (isPHPStringState(state) || !isLineEnd(styler[startPos - 1]))) {
  546. startPos--;
  547. length++;
  548. state = styler.StyleAt(startPos);
  549. }
  550. if (startPos == 0)
  551. state = SCE_H_DEFAULT;
  552. }
  553. styler.StartAt(startPos, static_cast<char>(STYLE_MAX));
  554. int lineCurrent = styler.GetLine(startPos);
  555. int lineState;
  556. if (lineCurrent > 0) {
  557. lineState = styler.GetLineState(lineCurrent);
  558. } else {
  559. // Default client and ASP scripting language is JavaScript
  560. lineState = eScriptJS << 8;
  561. // property asp.default.language
  562. // Script in ASP code is initially assumed to be in JavaScript.
  563. // To change this to VBScript set asp.default.language to 2. Python is 3.
  564. lineState |= styler.GetPropertyInt("asp.default.language", eScriptJS) << 4;
  565. }
  566. script_mode inScriptType = script_mode((lineState >> 0) & 0x03); // 2 bits of scripting mode
  567. bool tagOpened = (lineState >> 2) & 0x01; // 1 bit to know if we are in an opened tag
  568. bool tagClosing = (lineState >> 3) & 0x01; // 1 bit to know if we are in a closing tag
  569. bool tagDontFold = false; //some HTML tags should not be folded
  570. script_type aspScript = script_type((lineState >> 4) & 0x0F); // 4 bits of script name
  571. script_type clientScript = script_type((lineState >> 8) & 0x0F); // 4 bits of script name
  572. int beforePreProc = (lineState >> 12) & 0xFF; // 8 bits of state
  573. script_type scriptLanguage = ScriptOfState(state);
  574. // If eNonHtmlScript coincides with SCE_H_COMMENT, assume eScriptComment
  575. if (inScriptType == eNonHtmlScript && state == SCE_H_COMMENT) {
  576. scriptLanguage = eScriptComment;
  577. }
  578. script_type beforeLanguage = ScriptOfState(beforePreProc);
  579. // property fold.html
  580. // Folding is turned on or off for HTML and XML files with this option.
  581. // The fold option must also be on for folding to occur.
  582. const bool foldHTML = styler.GetPropertyInt("fold.html", 0) != 0;
  583. const bool fold = foldHTML && styler.GetPropertyInt("fold", 0);
  584. // property fold.html.preprocessor
  585. // Folding is turned on or off for scripts embedded in HTML files with this option.
  586. // The default is on.
  587. const bool foldHTMLPreprocessor = foldHTML && styler.GetPropertyInt("fold.html.preprocessor", 1);
  588. const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
  589. // property fold.hypertext.comment
  590. // Allow folding for comments in scripts embedded in HTML.
  591. // The default is off.
  592. const bool foldComment = fold && styler.GetPropertyInt("fold.hypertext.comment", 0) != 0;
  593. // property fold.hypertext.heredoc
  594. // Allow folding for heredocs in scripts embedded in HTML.
  595. // The default is off.
  596. const bool foldHeredoc = fold && styler.GetPropertyInt("fold.hypertext.heredoc", 0) != 0;
  597. // property html.tags.case.sensitive
  598. // For XML and HTML, setting this property to 1 will make tags match in a case
  599. // sensitive way which is the expected behaviour for XML and XHTML.
  600. const bool caseSensitive = styler.GetPropertyInt("html.tags.case.sensitive", 0) != 0;
  601. // property lexer.xml.allow.scripts
  602. // Set to 0 to disable scripts in XML.
  603. const bool allowScripts = styler.GetPropertyInt("lexer.xml.allow.scripts", 1) != 0;
  604. // property lexer.html.mako
  605. // Set to 1 to enable the mako template language.
  606. const bool isMako = styler.GetPropertyInt("lexer.html.mako", 0) != 0;
  607. // property lexer.html.django
  608. // Set to 1 to enable the django template language.
  609. const bool isDjango = styler.GetPropertyInt("lexer.html.django", 0) != 0;
  610. const CharacterSet setHTMLWord(CharacterSet::setAlphaNum, ".-_:!#", 0x80, true);
  611. const CharacterSet setTagContinue(CharacterSet::setAlphaNum, ".-_:!#[", 0x80, true);
  612. const CharacterSet setAttributeContinue(CharacterSet::setAlphaNum, ".-_:!#/", 0x80, true);
  613. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  614. int levelCurrent = levelPrev;
  615. int visibleChars = 0;
  616. int lineStartVisibleChars = 0;
  617. int chPrev = ' ';
  618. int ch = ' ';
  619. int chPrevNonWhite = ' ';
  620. // look back to set chPrevNonWhite properly for better regex colouring
  621. if (scriptLanguage == eScriptJS && startPos > 0) {
  622. int back = startPos;
  623. int style = 0;
  624. while (--back) {
  625. style = styler.StyleAt(back);
  626. if (style < SCE_HJ_DEFAULT || style > SCE_HJ_COMMENTDOC)
  627. // includes SCE_HJ_COMMENT & SCE_HJ_COMMENTLINE
  628. break;
  629. }
  630. if (style == SCE_HJ_SYMBOLS) {
  631. chPrevNonWhite = static_cast<unsigned char>(styler.SafeGetCharAt(back));
  632. }
  633. }
  634. styler.StartSegment(startPos);
  635. const int lengthDoc = startPos + length;
  636. for (int i = startPos; i < lengthDoc; i++) {
  637. const int chPrev2 = chPrev;
  638. chPrev = ch;
  639. if (!IsASpace(ch) && state != SCE_HJ_COMMENT &&
  640. state != SCE_HJ_COMMENTLINE && state != SCE_HJ_COMMENTDOC)
  641. chPrevNonWhite = ch;
  642. ch = static_cast<unsigned char>(styler[i]);
  643. int chNext = static_cast<unsigned char>(styler.SafeGetCharAt(i + 1));
  644. const int chNext2 = static_cast<unsigned char>(styler.SafeGetCharAt(i + 2));
  645. // Handle DBCS codepages
  646. if (styler.IsLeadByte(static_cast<char>(ch))) {
  647. chPrev = ' ';
  648. i += 1;
  649. continue;
  650. }
  651. if ((!IsASpace(ch) || !foldCompact) && fold)
  652. visibleChars++;
  653. if (!IsASpace(ch))
  654. lineStartVisibleChars++;
  655. // decide what is the current state to print (depending of the script tag)
  656. StateToPrint = statePrintForState(state, inScriptType);
  657. // handle script folding
  658. if (fold) {
  659. switch (scriptLanguage) {
  660. case eScriptJS:
  661. case eScriptPHP:
  662. //not currently supported case eScriptVBS:
  663. if ((state != SCE_HPHP_COMMENT) && (state != SCE_HPHP_COMMENTLINE) && (state != SCE_HJ_COMMENT) && (state != SCE_HJ_COMMENTLINE) && (state != SCE_HJ_COMMENTDOC) && (!isStringState(state))) {
  664. //Platform::DebugPrintf("state=%d, StateToPrint=%d, initStyle=%d\n", state, StateToPrint, initStyle);
  665. //if ((state == SCE_HPHP_OPERATOR) || (state == SCE_HPHP_DEFAULT) || (state == SCE_HJ_SYMBOLS) || (state == SCE_HJ_START) || (state == SCE_HJ_DEFAULT)) {
  666. if ((ch == '{') || (ch == '}') || (foldComment && (ch == '/') && (chNext == '*'))) {
  667. levelCurrent += ((ch == '{') || (ch == '/')) ? 1 : -1;
  668. }
  669. } else if (((state == SCE_HPHP_COMMENT) || (state == SCE_HJ_COMMENT)) && foldComment && (ch == '*') && (chNext == '/')) {
  670. levelCurrent--;
  671. }
  672. break;
  673. case eScriptPython:
  674. if (state != SCE_HP_COMMENTLINE) {
  675. if ((ch == ':') && ((chNext == '\n') || (chNext == '\r' && chNext2 == '\n'))) {
  676. levelCurrent++;
  677. } else if ((ch == '\n') && !((chNext == '\r') && (chNext2 == '\n')) && (chNext != '\n')) {
  678. // check if the number of tabs is lower than the level
  679. int Findlevel = (levelCurrent & ~SC_FOLDLEVELBASE) * 8;
  680. for (int j = 0; Findlevel > 0; j++) {
  681. char chTmp = styler.SafeGetCharAt(i + j + 1);
  682. if (chTmp == '\t') {
  683. Findlevel -= 8;
  684. } else if (chTmp == ' ') {
  685. Findlevel--;
  686. } else {
  687. break;
  688. }
  689. }
  690. if (Findlevel > 0) {
  691. levelCurrent -= Findlevel / 8;
  692. if (Findlevel % 8)
  693. levelCurrent--;
  694. }
  695. }
  696. }
  697. break;
  698. default:
  699. break;
  700. }
  701. }
  702. if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
  703. // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win) or on LF alone (Unix)
  704. // Avoid triggering two times on Dos/Win
  705. // New line -> record any line state onto /next/ line
  706. if (fold) {
  707. int lev = levelPrev;
  708. if (visibleChars == 0)
  709. lev |= SC_FOLDLEVELWHITEFLAG;
  710. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  711. lev |= SC_FOLDLEVELHEADERFLAG;
  712. styler.SetLevel(lineCurrent, lev);
  713. visibleChars = 0;
  714. levelPrev = levelCurrent;
  715. }
  716. lineCurrent++;
  717. lineStartVisibleChars = 0;
  718. styler.SetLineState(lineCurrent,
  719. ((inScriptType & 0x03) << 0) |
  720. ((tagOpened & 0x01) << 2) |
  721. ((tagClosing & 0x01) << 3) |
  722. ((aspScript & 0x0F) << 4) |
  723. ((clientScript & 0x0F) << 8) |
  724. ((beforePreProc & 0xFF) << 12));
  725. }
  726. // Allow falling through to mako handling code if newline is going to end a block
  727. if (((ch == '\r' && chNext != '\n') || (ch == '\n')) &&
  728. (!isMako || (0 != strcmp(makoBlockType, "%")))) {
  729. }
  730. // generic end of script processing
  731. else if ((inScriptType == eNonHtmlScript) && (ch == '<') && (chNext == '/')) {
  732. // Check if it's the end of the script tag (or any other HTML tag)
  733. switch (state) {
  734. // in these cases, you can embed HTML tags (to confirm !!!!!!!!!!!!!!!!!!!!!!)
  735. case SCE_H_DOUBLESTRING:
  736. case SCE_H_SINGLESTRING:
  737. case SCE_HJ_COMMENT:
  738. case SCE_HJ_COMMENTDOC:
  739. //case SCE_HJ_COMMENTLINE: // removed as this is a common thing done to hide
  740. // the end of script marker from some JS interpreters.
  741. case SCE_HB_COMMENTLINE:
  742. case SCE_HBA_COMMENTLINE:
  743. case SCE_HJ_DOUBLESTRING:
  744. case SCE_HJ_SINGLESTRING:
  745. case SCE_HJ_REGEX:
  746. case SCE_HB_STRING:
  747. case SCE_HBA_STRING:
  748. case SCE_HP_STRING:
  749. case SCE_HP_TRIPLE:
  750. case SCE_HP_TRIPLEDOUBLE:
  751. case SCE_HPHP_HSTRING:
  752. case SCE_HPHP_SIMPLESTRING:
  753. case SCE_HPHP_COMMENT:
  754. case SCE_HPHP_COMMENTLINE:
  755. break;
  756. default :
  757. // check if the closing tag is a script tag
  758. if (const char *tag =
  759. state == SCE_HJ_COMMENTLINE || isXml ? "script" :
  760. state == SCE_H_COMMENT ? "comment" : 0) {
  761. int j = i + 2;
  762. int chr;
  763. do {
  764. chr = static_cast<int>(*tag++);
  765. } while (chr != 0 && chr == MakeLowerCase(styler.SafeGetCharAt(j++)));
  766. if (chr != 0) break;
  767. }
  768. // closing tag of the script (it's a closing HTML tag anyway)
  769. styler.ColourTo(i - 1, StateToPrint);
  770. state = SCE_H_TAGUNKNOWN;
  771. inScriptType = eHtml;
  772. scriptLanguage = eScriptNone;
  773. clientScript = eScriptJS;
  774. i += 2;
  775. visibleChars += 2;
  776. tagClosing = true;
  777. continue;
  778. }
  779. }
  780. /////////////////////////////////////
  781. // handle the start of PHP pre-processor = Non-HTML
  782. else if ((state != SCE_H_ASPAT) &&
  783. !isPHPStringState(state) &&
  784. (state != SCE_HPHP_COMMENT) &&
  785. (ch == '<') &&
  786. (chNext == '?') &&
  787. !IsScriptCommentState(state) ) {
  788. scriptLanguage = segIsScriptingIndicator(styler, i + 2, i + 6, eScriptPHP);
  789. if (scriptLanguage != eScriptPHP && isStringState(state)) continue;
  790. styler.ColourTo(i - 1, StateToPrint);
  791. beforePreProc = state;
  792. i++;
  793. visibleChars++;
  794. i += PrintScriptingIndicatorOffset(styler, styler.GetStartSegment() + 2, i + 6);
  795. if (scriptLanguage == eScriptXML)
  796. styler.ColourTo(i, SCE_H_XMLSTART);
  797. else
  798. styler.ColourTo(i, SCE_H_QUESTION);
  799. state = StateForScript(scriptLanguage);
  800. if (inScriptType == eNonHtmlScript)
  801. inScriptType = eNonHtmlScriptPreProc;
  802. else
  803. inScriptType = eNonHtmlPreProc;
  804. // Fold whole script, but not if the XML first tag (all XML-like tags in this case)
  805. if (foldHTMLPreprocessor && (scriptLanguage != eScriptXML)) {
  806. levelCurrent++;
  807. }
  808. // should be better
  809. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  810. continue;
  811. }
  812. // handle the start Mako template Python code
  813. else if (isMako && scriptLanguage == eScriptNone && ((ch == '<' && chNext == '%') ||
  814. (lineStartVisibleChars == 1 && ch == '%') ||
  815. (ch == '$' && chNext == '{') ||
  816. (ch == '<' && chNext == '/' && chNext2 == '%'))) {
  817. if (ch == '%')
  818. strcpy(makoBlockType, "%");
  819. else if (ch == '$')
  820. strcpy(makoBlockType, "{");
  821. else if (chNext == '/')
  822. GetNextWord(styler, i+3, makoBlockType, sizeof(makoBlockType));
  823. else
  824. GetNextWord(styler, i+2, makoBlockType, sizeof(makoBlockType));
  825. styler.ColourTo(i - 1, StateToPrint);
  826. beforePreProc = state;
  827. if (inScriptType == eNonHtmlScript)
  828. inScriptType = eNonHtmlScriptPreProc;
  829. else
  830. inScriptType = eNonHtmlPreProc;
  831. if (chNext == '/') {
  832. i += 2;
  833. visibleChars += 2;
  834. } else if (ch != '%') {
  835. i++;
  836. visibleChars++;
  837. }
  838. state = SCE_HP_START;
  839. scriptLanguage = eScriptPython;
  840. styler.ColourTo(i, SCE_H_ASP);
  841. if (foldHTMLPreprocessor && ch == '<')
  842. levelCurrent++;
  843. if (ch != '%' && ch != '$') {
  844. i += strlen(makoBlockType);
  845. visibleChars += strlen(makoBlockType);
  846. if (keywords4.InList(makoBlockType))
  847. styler.ColourTo(i, SCE_HP_WORD);
  848. else
  849. styler.ColourTo(i, SCE_H_TAGUNKNOWN);
  850. }
  851. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  852. continue;
  853. }
  854. // handle the start Django template code
  855. else if (isDjango && scriptLanguage != eScriptPython && (ch == '{' && (chNext == '%' || chNext == '{'))) {
  856. if (chNext == '%')
  857. strcpy(djangoBlockType, "%");
  858. else
  859. strcpy(djangoBlockType, "{");
  860. styler.ColourTo(i - 1, StateToPrint);
  861. beforePreProc = state;
  862. if (inScriptType == eNonHtmlScript)
  863. inScriptType = eNonHtmlScriptPreProc;
  864. else
  865. inScriptType = eNonHtmlPreProc;
  866. i += 1;
  867. visibleChars += 1;
  868. state = SCE_HP_START;
  869. beforeLanguage = scriptLanguage;
  870. scriptLanguage = eScriptPython;
  871. styler.ColourTo(i, SCE_H_ASP);
  872. if (foldHTMLPreprocessor && chNext == '%')
  873. levelCurrent++;
  874. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  875. continue;
  876. }
  877. // handle the start of ASP pre-processor = Non-HTML
  878. else if (!isMako && !isDjango && !isCommentASPState(state) && (ch == '<') && (chNext == '%') && !isPHPStringState(state)) {
  879. styler.ColourTo(i - 1, StateToPrint);
  880. beforePreProc = state;
  881. if (inScriptType == eNonHtmlScript)
  882. inScriptType = eNonHtmlScriptPreProc;
  883. else
  884. inScriptType = eNonHtmlPreProc;
  885. if (chNext2 == '@') {
  886. i += 2; // place as if it was the second next char treated
  887. visibleChars += 2;
  888. state = SCE_H_ASPAT;
  889. } else if ((chNext2 == '-') && (styler.SafeGetCharAt(i + 3) == '-')) {
  890. styler.ColourTo(i + 3, SCE_H_ASP);
  891. state = SCE_H_XCCOMMENT;
  892. scriptLanguage = eScriptVBS;
  893. continue;
  894. } else {
  895. if (chNext2 == '=') {
  896. i += 2; // place as if it was the second next char treated
  897. visibleChars += 2;
  898. } else {
  899. i++; // place as if it was the next char treated
  900. visibleChars++;
  901. }
  902. state = StateForScript(aspScript);
  903. }
  904. scriptLanguage = eScriptVBS;
  905. styler.ColourTo(i, SCE_H_ASP);
  906. // fold whole script
  907. if (foldHTMLPreprocessor)
  908. levelCurrent++;
  909. // should be better
  910. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  911. continue;
  912. }
  913. /////////////////////////////////////
  914. // handle the start of SGML language (DTD)
  915. else if (((scriptLanguage == eScriptNone) || (scriptLanguage == eScriptXML)) &&
  916. (chPrev == '<') &&
  917. (ch == '!') &&
  918. (StateToPrint != SCE_H_CDATA) &&
  919. (!IsCommentState(StateToPrint)) &&
  920. (!IsScriptCommentState(StateToPrint)) ) {
  921. beforePreProc = state;
  922. styler.ColourTo(i - 2, StateToPrint);
  923. if ((chNext == '-') && (chNext2 == '-')) {
  924. state = SCE_H_COMMENT; // wait for a pending command
  925. styler.ColourTo(i + 2, SCE_H_COMMENT);
  926. i += 2; // follow styling after the --
  927. } else if (isWordCdata(i + 1, i + 7, styler)) {
  928. state = SCE_H_CDATA;
  929. } else {
  930. styler.ColourTo(i, SCE_H_SGML_DEFAULT); // <! is default
  931. scriptLanguage = eScriptSGML;
  932. state = SCE_H_SGML_COMMAND; // wait for a pending command
  933. }
  934. // fold whole tag (-- when closing the tag)
  935. if (foldHTMLPreprocessor || (state == SCE_H_COMMENT))
  936. levelCurrent++;
  937. continue;
  938. }
  939. // handle the end of Mako Python code
  940. else if (isMako &&
  941. ((inScriptType == eNonHtmlPreProc) || (inScriptType == eNonHtmlScriptPreProc)) &&
  942. (scriptLanguage != eScriptNone) && stateAllowsTermination(state) &&
  943. isMakoBlockEnd(ch, chNext, makoBlockType)) {
  944. if (state == SCE_H_ASPAT) {
  945. aspScript = segIsScriptingIndicator(styler,
  946. styler.GetStartSegment(), i - 1, aspScript);
  947. }
  948. if (state == SCE_HP_WORD) {
  949. classifyWordHTPy(styler.GetStartSegment(), i - 1, keywords4, styler, prevWord, inScriptType);
  950. } else {
  951. styler.ColourTo(i - 1, StateToPrint);
  952. }
  953. if (0 != strcmp(makoBlockType, "%") && (0 != strcmp(makoBlockType, "{")) && ch != '>') {
  954. i++;
  955. visibleChars++;
  956. }
  957. if (0 != strcmp(makoBlockType, "%")) {
  958. styler.ColourTo(i, SCE_H_ASP);
  959. }
  960. state = beforePreProc;
  961. if (inScriptType == eNonHtmlScriptPreProc)
  962. inScriptType = eNonHtmlScript;
  963. else
  964. inScriptType = eHtml;
  965. if (foldHTMLPreprocessor && ch != '\n' && ch != '\r') {
  966. levelCurrent--;
  967. }
  968. scriptLanguage = eScriptNone;
  969. continue;
  970. }
  971. // handle the end of Django template code
  972. else if (isDjango &&
  973. ((inScriptType == eNonHtmlPreProc) || (inScriptType == eNonHtmlScriptPreProc)) &&
  974. (scriptLanguage != eScriptNone) && stateAllowsTermination(state) &&
  975. isDjangoBlockEnd(ch, chNext, djangoBlockType)) {
  976. if (state == SCE_H_ASPAT) {
  977. aspScript = segIsScriptingIndicator(styler,
  978. styler.GetStartSegment(), i - 1, aspScript);
  979. }
  980. if (state == SCE_HP_WORD) {
  981. classifyWordHTPy(styler.GetStartSegment(), i - 1, keywords4, styler, prevWord, inScriptType);
  982. } else {
  983. styler.ColourTo(i - 1, StateToPrint);
  984. }
  985. i += 1;
  986. visibleChars += 1;
  987. styler.ColourTo(i, SCE_H_ASP);
  988. state = beforePreProc;
  989. if (inScriptType == eNonHtmlScriptPreProc)
  990. inScriptType = eNonHtmlScript;
  991. else
  992. inScriptType = eHtml;
  993. if (foldHTMLPreprocessor) {
  994. levelCurrent--;
  995. }
  996. scriptLanguage = beforeLanguage;
  997. continue;
  998. }
  999. // handle the end of a pre-processor = Non-HTML
  1000. else if ((!isMako && !isDjango && ((inScriptType == eNonHtmlPreProc) || (inScriptType == eNonHtmlScriptPreProc)) &&
  1001. (((scriptLanguage != eScriptNone) && stateAllowsTermination(state))) &&
  1002. (((ch == '%') || (ch == '?')) && (chNext == '>'))) ||
  1003. ((scriptLanguage == eScriptSGML) && (ch == '>') && (state != SCE_H_SGML_COMMENT))) {
  1004. if (state == SCE_H_ASPAT) {
  1005. aspScript = segIsScriptingIndicator(styler,
  1006. styler.GetStartSegment(), i - 1, aspScript);
  1007. }
  1008. // Bounce out of any ASP mode
  1009. switch (state) {
  1010. case SCE_HJ_WORD:
  1011. classifyWordHTJS(styler.GetStartSegment(), i - 1, keywords2, styler, inScriptType);
  1012. break;
  1013. case SCE_HB_WORD:
  1014. classifyWordHTVB(styler.GetStartSegment(), i - 1, keywords3, styler, inScriptType);
  1015. break;
  1016. case SCE_HP_WORD:
  1017. classifyWordHTPy(styler.GetStartSegment(), i - 1, keywords4, styler, prevWord, inScriptType);
  1018. break;
  1019. case SCE_HPHP_WORD:
  1020. classifyWordHTPHP(styler.GetStartSegment(), i - 1, keywords5, styler);
  1021. break;
  1022. case SCE_H_XCCOMMENT:
  1023. styler.ColourTo(i - 1, state);
  1024. break;
  1025. default :
  1026. styler.ColourTo(i - 1, StateToPrint);
  1027. break;
  1028. }
  1029. if (scriptLanguage != eScriptSGML) {
  1030. i++;
  1031. visibleChars++;
  1032. }
  1033. if (ch == '%')
  1034. styler.ColourTo(i, SCE_H_ASP);
  1035. else if (scriptLanguage == eScriptXML)
  1036. styler.ColourTo(i, SCE_H_XMLEND);
  1037. else if (scriptLanguage == eScriptSGML)
  1038. styler.ColourTo(i, SCE_H_SGML_DEFAULT);
  1039. else
  1040. styler.ColourTo(i, SCE_H_QUESTION);
  1041. state = beforePreProc;
  1042. if (inScriptType == eNonHtmlScriptPreProc)
  1043. inScriptType = eNonHtmlScript;
  1044. else
  1045. inScriptType = eHtml;
  1046. // Unfold all scripting languages, except for XML tag
  1047. if (foldHTMLPreprocessor && (scriptLanguage != eScriptXML)) {
  1048. levelCurrent--;
  1049. }
  1050. scriptLanguage = eScriptNone;
  1051. continue;
  1052. }
  1053. /////////////////////////////////////
  1054. switch (state) {
  1055. case SCE_H_DEFAULT:
  1056. if (ch == '<') {
  1057. // in HTML, fold on tag open and unfold on tag close
  1058. tagOpened = true;
  1059. tagClosing = (chNext == '/');
  1060. styler.ColourTo(i - 1, StateToPrint);
  1061. if (chNext != '!')
  1062. state = SCE_H_TAGUNKNOWN;
  1063. } else if (ch == '&') {
  1064. styler.ColourTo(i - 1, SCE_H_DEFAULT);
  1065. state = SCE_H_ENTITY;
  1066. }
  1067. break;
  1068. case SCE_H_SGML_DEFAULT:
  1069. case SCE_H_SGML_BLOCK_DEFAULT:
  1070. // if (scriptLanguage == eScriptSGMLblock)
  1071. // StateToPrint = SCE_H_SGML_BLOCK_DEFAULT;
  1072. if (ch == '\"') {
  1073. styler.ColourTo(i - 1, StateToPrint);
  1074. state = SCE_H_SGML_DOUBLESTRING;
  1075. } else if (ch == '\'') {
  1076. styler.ColourTo(i - 1, StateToPrint);
  1077. state = SCE_H_SGML_SIMPLESTRING;
  1078. } else if ((ch == '-') && (chPrev == '-')) {
  1079. if (static_cast<int>(styler.GetStartSegment()) <= (i - 2)) {
  1080. styler.ColourTo(i - 2, StateToPrint);
  1081. }
  1082. state = SCE_H_SGML_COMMENT;
  1083. } else if (isascii(ch) && isalpha(ch) && (chPrev == '%')) {
  1084. styler.ColourTo(i - 2, StateToPrint);
  1085. state = SCE_H_SGML_ENTITY;
  1086. } else if (ch == '#') {
  1087. styler.ColourTo(i - 1, StateToPrint);
  1088. state = SCE_H_SGML_SPECIAL;
  1089. } else if (ch == '[') {
  1090. styler.ColourTo(i - 1, StateToPrint);
  1091. scriptLanguage = eScriptSGMLblock;
  1092. state = SCE_H_SGML_BLOCK_DEFAULT;
  1093. } else if (ch == ']') {
  1094. if (scriptLanguage == eScriptSGMLblock) {
  1095. styler.ColourTo(i, StateToPrint);
  1096. scriptLanguage = eScriptSGML;
  1097. } else {
  1098. styler.ColourTo(i - 1, StateToPrint);
  1099. styler.ColourTo(i, SCE_H_SGML_ERROR);
  1100. }
  1101. state = SCE_H_SGML_DEFAULT;
  1102. } else if (scriptLanguage == eScriptSGMLblock) {
  1103. if ((ch == '!') && (chPrev == '<')) {
  1104. styler.ColourTo(i - 2, StateToPrint);
  1105. styler.ColourTo(i, SCE_H_SGML_DEFAULT);
  1106. state = SCE_H_SGML_COMMAND;
  1107. } else if (ch == '>') {
  1108. styler.ColourTo(i - 1, StateToPrint);
  1109. styler.ColourTo(i, SCE_H_SGML_DEFAULT);
  1110. }
  1111. }
  1112. break;
  1113. case SCE_H_SGML_COMMAND:
  1114. if ((ch == '-') && (chPrev == '-')) {
  1115. styler.ColourTo(i - 2, StateToPrint);
  1116. state = SCE_H_SGML_COMMENT;
  1117. } else if (!issgmlwordchar(ch)) {
  1118. if (isWordHSGML(styler.GetStartSegment(), i - 1, keywords6, styler)) {
  1119. styler.ColourTo(i - 1, StateToPrint);
  1120. state = SCE_H_SGML_1ST_PARAM;
  1121. } else {
  1122. state = SCE_H_SGML_ERROR;
  1123. }
  1124. }
  1125. break;
  1126. case SCE_H_SGML_1ST_PARAM:
  1127. // wait for the beginning of the word
  1128. if ((ch == '-') && (chPrev == '-')) {
  1129. if (scriptLanguage == eScriptSGMLblock) {
  1130. styler.ColourTo(i - 2, SCE_H_SGML_BLOCK_DEFAULT);
  1131. } else {
  1132. styler.ColourTo(i - 2, SCE_H_SGML_DEFAULT);
  1133. }
  1134. state = SCE_H_SGML_1ST_PARAM_COMMENT;
  1135. } else if (issgmlwordchar(ch)) {
  1136. if (scriptLanguage == eScriptSGMLblock) {
  1137. styler.ColourTo(i - 1, SCE_H_SGML_BLOCK_DEFAULT);
  1138. } else {
  1139. styler.ColourTo(i - 1, SCE_H_SGML_DEFAULT);
  1140. }
  1141. // find the length of the word
  1142. int size = 1;
  1143. while (setHTMLWord.Contains(static_cast<unsigned char>(styler.SafeGetCharAt(i + size))))
  1144. size++;
  1145. styler.ColourTo(i + size - 1, StateToPrint);
  1146. i += size - 1;
  1147. visibleChars += size - 1;
  1148. ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
  1149. if (scriptLanguage == eScriptSGMLblock) {
  1150. state = SCE_H_SGML_BLOCK_DEFAULT;
  1151. } else {
  1152. state = SCE_H_SGML_DEFAULT;
  1153. }
  1154. continue;
  1155. }
  1156. break;
  1157. case SCE_H_SGML_ERROR:
  1158. if ((ch == '-') && (chPrev == '-')) {
  1159. styler.ColourTo(i - 2, StateToPrint);
  1160. state = SCE_H_SGML_COMMENT;
  1161. }
  1162. case SCE_H_SGML_DOUBLESTRING:
  1163. if (ch == '\"') {
  1164. styler.ColourTo(i, StateToPrint);
  1165. state = SCE_H_SGML_DEFAULT;
  1166. }
  1167. break;
  1168. case SCE_H_SGML_SIMPLESTRING:
  1169. if (ch == '\'') {
  1170. styler.ColourTo(i, StateToPrint);
  1171. state = SCE_H_SGML_DEFAULT;
  1172. }
  1173. break;
  1174. case SCE_H_SGML_COMMENT:
  1175. if ((ch == '-') && (chPrev == '-')) {
  1176. styler.ColourTo(i, StateToPrint);
  1177. state = SCE_H_SGML_DEFAULT;
  1178. }
  1179. break;
  1180. case SCE_H_CDATA:
  1181. if ((chPrev2 == ']') && (chPrev == ']') && (ch == '>')) {
  1182. styler.ColourTo(i, StateToPrint);
  1183. state = SCE_H_DEFAULT;
  1184. levelCurrent--;
  1185. }
  1186. break;
  1187. case SCE_H_COMMENT:
  1188. if ((scriptLanguage != eScriptComment) && (chPrev2 == '-') && (chPrev == '-') && (ch == '>')) {
  1189. styler.ColourTo(i, StateToPrint);
  1190. state = SCE_H_DEFAULT;
  1191. levelCurrent--;
  1192. }
  1193. break;
  1194. case SCE_H_SGML_1ST_PARAM_COMMENT:
  1195. if ((ch == '-') && (chPrev == '-')) {
  1196. styler.ColourTo(i, SCE_H_SGML_COMMENT);
  1197. state = SCE_H_SGML_1ST_PARAM;
  1198. }
  1199. break;
  1200. case SCE_H_SGML_SPECIAL:
  1201. if (!(isascii(ch) && isupper(ch))) {
  1202. styler.ColourTo(i - 1, StateToPrint);
  1203. if (isalnum(ch)) {
  1204. state = SCE_H_SGML_ERROR;
  1205. } else {
  1206. state = SCE_H_SGML_DEFAULT;
  1207. }
  1208. }
  1209. break;
  1210. case SCE_H_SGML_ENTITY:
  1211. if (ch == ';') {
  1212. styler.ColourTo(i, StateToPrint);
  1213. state = SCE_H_SGML_DEFAULT;
  1214. } else if (!(isascii(ch) && isalnum(ch)) && ch != '-' && ch != '.') {
  1215. styler.ColourTo(i, SCE_H_SGML_ERROR);
  1216. state = SCE_H_SGML_DEFAULT;
  1217. }
  1218. break;
  1219. case SCE_H_ENTITY:
  1220. if (ch == ';') {
  1221. styler.ColourTo(i, StateToPrint);
  1222. state = SCE_H_DEFAULT;
  1223. }
  1224. if (ch != '#' && !(isascii(ch) && isalnum(ch)) // Should check that '#' follows '&', but it is unlikely anyway...
  1225. && ch != '.' && ch != '-' && ch != '_' && ch != ':') { // valid in XML
  1226. if (!isascii(ch)) // Possibly start of a multibyte character so don't allow this byte to be in entity style
  1227. styler.ColourTo(i-1, SCE_H_TAGUNKNOWN);
  1228. else
  1229. styler.ColourTo(i, SCE_H_TAGUNKNOWN);
  1230. state = SCE_H_DEFAULT;
  1231. }
  1232. break;
  1233. case SCE_H_TAGUNKNOWN:
  1234. if (!setTagContinue.Contains(ch) && !((ch == '/') && (chPrev == '<'))) {
  1235. int eClass = classifyTagHTML(styler.GetStartSegment(),
  1236. i - 1, keywords, styler, tagDontFold, caseSensitive, isXml, allowScripts);
  1237. if (eClass == SCE_H_SCRIPT || eClass == SCE_H_COMMENT) {
  1238. if (!tagClosing) {
  1239. inScriptType = eNonHtmlScript;
  1240. scriptLanguage = eClass == SCE_H_SCRIPT ? clientScript : eScriptComment;
  1241. } else {
  1242. scriptLanguage = eScriptNone;
  1243. }
  1244. eClass = SCE_H_TAG;
  1245. }
  1246. if (ch == '>') {
  1247. styler.ColourTo(i, eClass);
  1248. if (inScriptType == eNonHtmlScript) {
  1249. state = StateForScript(scriptLanguage);
  1250. } else {
  1251. state = SCE_H_DEFAULT;
  1252. }
  1253. tagOpened = false;
  1254. if (!tagDontFold) {
  1255. if (tagClosing) {
  1256. levelCurrent--;
  1257. } else {
  1258. levelCurrent++;
  1259. }
  1260. }
  1261. tagClosing = false;
  1262. } else if (ch == '/' && chNext == '>') {
  1263. if (eClass == SCE_H_TAGUNKNOWN) {
  1264. styler.ColourTo(i + 1, SCE_H_TAGUNKNOWN);
  1265. } else {
  1266. styler.ColourTo(i - 1, StateToPrint);
  1267. styler.ColourTo(i + 1, SCE_H_TAGEND);
  1268. }
  1269. i++;
  1270. ch = chNext;
  1271. state = SCE_H_DEFAULT;
  1272. tagOpened = false;
  1273. } else {
  1274. if (eClass != SCE_H_TAGUNKNOWN) {
  1275. if (eClass == SCE_H_SGML_DEFAULT) {
  1276. state = SCE_H_SGML_DEFAULT;
  1277. } else {
  1278. state = SCE_H_OTHER;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. break;
  1284. case SCE_H_ATTRIBUTE:
  1285. if (!setAttributeContinue.Contains(ch)) {
  1286. if (inScriptType == eNonHtmlScript) {
  1287. int scriptLanguagePrev = scriptLanguage;
  1288. clientScript = segIsScriptingIndicator(styler, styler.GetStartSegment(), i - 1, scriptLanguage);
  1289. scriptLanguage = clientScript;
  1290. if ((scriptLanguagePrev != scriptLanguage) && (scriptLanguage == eScriptNone))
  1291. inScriptType = eHtml;
  1292. }
  1293. classifyAttribHTML(styler.GetStartSegment(), i - 1, keywords, styler);
  1294. if (ch == '>') {
  1295. styler.ColourTo(i, SCE_H_TAG);
  1296. if (inScriptType == eNonHtmlScript) {
  1297. state = StateForScript(scriptLanguage);
  1298. } else {
  1299. state = SCE_H_DEFAULT;
  1300. }
  1301. tagOpened = false;
  1302. if (!tagDontFold) {
  1303. if (tagClosing) {
  1304. levelCurrent--;
  1305. } else {
  1306. levelCurrent++;
  1307. }
  1308. }
  1309. tagClosing = false;
  1310. } else if (ch == '=') {
  1311. styler.ColourTo(i, SCE_H_OTHER);
  1312. state = SCE_H_VALUE;
  1313. } else {
  1314. state = SCE_H_OTHER;
  1315. }
  1316. }
  1317. break;
  1318. case SCE_H_OTHER:
  1319. if (ch == '>') {
  1320. styler.ColourTo(i - 1, StateToPrint);
  1321. styler.ColourTo(i, SCE_H_TAG);
  1322. if (inScriptType == eNonHtmlScript) {
  1323. state = StateForScript(scriptLanguage);
  1324. } else {
  1325. state = SCE_H_DEFAULT;
  1326. }
  1327. tagOpened = false;
  1328. if (!tagDontFold) {
  1329. if (tagClosing) {
  1330. levelCurrent--;
  1331. } else {
  1332. levelCurrent++;
  1333. }
  1334. }
  1335. tagClosing = false;
  1336. } else if (ch == '\"') {
  1337. styler.ColourTo(i - 1, StateToPrint);
  1338. state = SCE_H_DOUBLESTRING;
  1339. } else if (ch == '\'') {
  1340. styler.ColourTo(i - 1, StateToPrint);
  1341. state = SCE_H_SINGLESTRING;
  1342. } else if (ch == '=') {
  1343. styler.ColourTo(i, StateToPrint);
  1344. state = SCE_H_VALUE;
  1345. } else if (ch == '/' && chNext == '>') {
  1346. styler.ColourTo(i - 1, StateToPrint);
  1347. styler.ColourTo(i + 1, SCE_H_TAGEND);
  1348. i++;
  1349. ch = chNext;
  1350. state = SCE_H_DEFAULT;
  1351. tagOpened = false;
  1352. } else if (ch == '?' && chNext == '>') {
  1353. styler.ColourTo(i - 1, StateToPrint);
  1354. styler.ColourTo(i + 1, SCE_H_XMLEND);
  1355. i++;
  1356. ch = chNext;
  1357. state = SCE_H_DEFAULT;
  1358. } else if (setHTMLWord.Contains(ch)) {
  1359. styler.ColourTo(i - 1, StateToPrint);
  1360. state = SCE_H_ATTRIBUTE;
  1361. }
  1362. break;
  1363. case SCE_H_DOUBLESTRING:
  1364. if (ch == '\"') {
  1365. if (inScriptType == eNonHtmlScript) {
  1366. scriptLanguage = segIsScriptingIndicator(styler, styler.GetStartSegment(), i, scriptLanguage);
  1367. }
  1368. styler.ColourTo(i, SCE_H_DOUBLESTRING);
  1369. state = SCE_H_OTHER;
  1370. }
  1371. break;
  1372. case SCE_H_SINGLESTRING:
  1373. if (ch == '\'') {
  1374. if (inScriptType == eNonHtmlScript) {
  1375. scriptLanguage = segIsScriptingIndicator(styler, styler.GetStartSegment(), i, scriptLanguage);
  1376. }
  1377. styler.ColourTo(i, SCE_H_SINGLESTRING);
  1378. state = SCE_H_OTHER;
  1379. }
  1380. break;
  1381. case SCE_H_VALUE:
  1382. if (!setHTMLWord.Contains(ch)) {
  1383. if (ch == '\"' && chPrev == '=') {
  1384. // Should really test for being first character
  1385. state = SCE_H_DOUBLESTRING;
  1386. } else if (ch == '\'' && chPrev == '=') {
  1387. state = SCE_H_SINGLESTRING;
  1388. } else {
  1389. if (IsNumber(styler.GetStartSegment(), styler)) {
  1390. styler.ColourTo(i - 1, SCE_H_NUMBER);
  1391. } else {
  1392. styler.ColourTo(i - 1, StateToPrint);
  1393. }
  1394. if (ch == '>') {
  1395. styler.ColourTo(i, SCE_H_TAG);
  1396. if (inScriptType == eNonHtmlScript) {
  1397. state = StateForScript(scriptLanguage);
  1398. } else {
  1399. state = SCE_H_DEFAULT;
  1400. }
  1401. tagOpened = false;
  1402. if (!tagDontFold) {
  1403. if (tagClosing) {
  1404. levelCurrent--;
  1405. } else {
  1406. levelCurrent++;
  1407. }
  1408. }
  1409. tagClosing = false;
  1410. } else {
  1411. state = SCE_H_OTHER;
  1412. }
  1413. }
  1414. }
  1415. break;
  1416. case SCE_HJ_DEFAULT:
  1417. case SCE_HJ_START:
  1418. case SCE_HJ_SYMBOLS:
  1419. if (IsAWordStart(ch)) {
  1420. styler.ColourTo(i - 1, StateToPrint);
  1421. state = SCE_HJ_WORD;
  1422. } else if (ch == '/' && chNext == '*') {
  1423. styler.ColourTo(i - 1, StateToPrint);
  1424. if (chNext2 == '*')
  1425. state = SCE_HJ_COMMENTDOC;
  1426. else
  1427. state = SCE_HJ_COMMENT;
  1428. } else if (ch == '/' && chNext == '/') {
  1429. styler.ColourTo(i - 1, StateToPrint);
  1430. state = SCE_HJ_COMMENTLINE;
  1431. } else if (ch == '/' && isOKBeforeRE(chPrevNonWhite)) {
  1432. styler.ColourTo(i - 1, StateToPrint);
  1433. state = SCE_HJ_REGEX;
  1434. } else if (ch == '\"') {
  1435. styler.ColourTo(i - 1, StateToPrint);
  1436. state = SCE_HJ_DOUBLESTRING;
  1437. } else if (ch == '\'') {
  1438. styler.ColourTo(i - 1, StateToPrint);
  1439. state = SCE_HJ_SIN

Large files files are truncated, but you can click here to view the full file