PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/source/scite227/scintilla/lexers/LexHTML.cxx

http://scite-for-php.googlecode.com/
C++ | 2009 lines | 1791 code | 109 blank | 109 comment | 1336 complexity | ed0584e438440e0423cfc98ac0fc1254 MD5 | raw file

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

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