OSDN Git Service

crystaledit: Separate JavaScript and Java parsers. osdn.net #41083
[winmerge-jp/winmerge-jp.git] / Externals / crystaledit / editlib / parsers / html.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 //  File:    html.cpp
3 //  Version: 1.1.0.4
4 //  Updated: 19-Jul-1998
5 //
6 //  Copyright:  Ferdinand Prantl, portions by Stcherbatchenko Andrei
7 //  E-mail:     prantl@ff.cuni.cz
8 //
9 //  HTML syntax highlighing definition
10 //
11 //  You are free to use or modify this code to the following restrictions:
12 //  - Acknowledge me somewhere in your about box, simple "Parts of code by.."
13 //  will be enough. If you can't (or don't want to), contact me personally.
14 //  - LEAVE THIS HEADER INTACT
15 ////////////////////////////////////////////////////////////////////////////
16
17 #include "StdAfx.h"
18 #include "crystallineparser.h"
19 #include "../SyntaxColors.h"
20 #include "../utils/string_util.h"
21
22 #ifdef _DEBUG
23 #define new DEBUG_NEW
24 #endif
25
26 unsigned
27 CrystalLineParser::ParseLineHtmlEx (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems, int nEmbeddedLanguage)
28 {
29   if (nLength == 0)
30     return dwCookie & (COOKIE_EXT_COMMENT|COOKIE_EXT_USER1|COOKIE_ELEMENT|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE|COOKIE_EXT_DEFINITION|COOKIE_EXT_VALUE);
31
32   bool bRedefineBlock = true;
33   if (!(dwCookie & COOKIE_ELEMENT))
34     bRedefineBlock = !(dwCookie & (COOKIE_EXT_USER1|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE));
35   bool bDecIndex = false;
36   int nIdentBegin = -1;
37   int nPrevI = -1;
38   int I=0;
39   for (I = 0;; nPrevI = I, I = static_cast<int>(::CharNext(pszChars+I) - pszChars))
40     {
41       if (I == nPrevI)
42         {
43           // CharNext did not advance, so we're at the end of the string
44           // and we already handled this character, so stop
45           break;
46         }
47
48       if (bRedefineBlock)
49         {
50           int nPos = I;
51           if (bDecIndex)
52             nPos = nPrevI;
53           if (dwCookie & COOKIE_EXT_COMMENT)
54             {
55               DEFINE_BLOCK (nPos, COLORINDEX_COMMENT);
56             }
57           else if (dwCookie & (COOKIE_CHAR | COOKIE_STRING))
58             {
59               DEFINE_BLOCK (nPos, COLORINDEX_STRING);
60             }
61           else if (dwCookie & COOKIE_ELEMENT)
62             {
63               DEFINE_BLOCK (nPos, COLORINDEX_PREPROCESSOR);
64             }
65           else
66             {
67               if (xisalnum (pszChars[nPos]) || pszChars[nPos] == '.')
68                 {
69                   DEFINE_BLOCK (nPos, COLORINDEX_NORMALTEXT);
70                 }
71               else
72                 {
73                   DEFINE_BLOCK (nPos, COLORINDEX_OPERATOR);
74                   bRedefineBlock = true;
75                   bDecIndex = true;
76                   goto out;
77                 }
78             }
79           bRedefineBlock = false;
80           bDecIndex = false;
81         }
82 out:
83
84       // Can be bigger than length if there is binary data
85       // See bug #1474782 Crash when comparing SQL with with binary data
86       if (I >= nLength || pszChars[I] == 0)
87         break;
88
89       if (!(dwCookie & COOKIE_ELEMENT) && (dwCookie & (COOKIE_EXT_USER1|COOKIE_BLOCK_SCRIPT|COOKIE_BLOCK_STYLE)))
90         {
91           if (dwCookie & COOKIE_BLOCK_SCRIPT)
92             {
93               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("</script>"));
94               int nextI = pszEnd ? static_cast<int>(pszEnd - pszChars) : nLength;
95               dwCookie = ParseLineJavaScript(dwCookie & ~COOKIE_BLOCK_SCRIPT, pszChars + I, nextI - I, pBuf, nActualItems);
96               if (!pszEnd)
97                 dwCookie |= COOKIE_BLOCK_SCRIPT;
98               else
99                 {
100                   dwCookie = 0;
101                   bRedefineBlock = true;
102                 }
103               I = nextI - 1;
104             }
105           else if (dwCookie & COOKIE_BLOCK_STYLE)
106             {
107               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("</style>"));
108               int nextI = pszEnd ? static_cast<int>(pszEnd - pszChars) : nLength;
109               dwCookie = ParseLineCss(dwCookie & ~COOKIE_BLOCK_STYLE, pszChars + I, nextI - I, pBuf, nActualItems);
110               if (!pszEnd)
111                 dwCookie |= COOKIE_BLOCK_STYLE;
112               else
113                 {
114                   dwCookie = 0;
115                   bRedefineBlock = true;
116                 }
117               I = nextI - 1;
118             }
119           else if ((dwCookie & COOKIE_EXT_USER1))
120             {
121               const TCHAR *pszEnd = _tcsstr(pszChars + I, _T("?>"));
122               if (!pszEnd)
123                 pszEnd = _tcsstr(pszChars + I, _T("%>"));
124               int nextI = pszEnd ? static_cast<int>(pszEnd - pszChars) : nLength;
125               unsigned (*pParseLineFunc)(unsigned, const TCHAR *, int, TEXTBLOCK *, int &);
126               switch (nEmbeddedLanguage)
127               {
128               case SRC_BASIC: pParseLineFunc = ParseLineBasic; break;
129               case SRC_PHP: pParseLineFunc = ParseLinePhpLanguage; break;
130               default: pParseLineFunc = ParseLineJavaScript; break;
131               }
132               dwCookie = pParseLineFunc(dwCookie & ~COOKIE_EXT_USER1, pszChars + I, nextI - I, pBuf, nActualItems);
133               if (!pszEnd)
134                 dwCookie |= COOKIE_EXT_USER1;
135               else
136                 {
137                   dwCookie = 0;
138                   bRedefineBlock = true;
139                 }
140               I = nextI - 1;
141             }
142           continue;
143         }
144
145       //  String constant "...."
146       if (dwCookie & COOKIE_STRING)
147         {
148           if (pszChars[I] == '"' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
149             {
150               dwCookie &= ~COOKIE_STRING;
151               bRedefineBlock = true;
152             }
153           continue;
154         }
155
156       //  Char constant '..'
157       if (dwCookie & COOKIE_CHAR)
158         {
159           if (pszChars[I] == '\'' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
160             {
161               dwCookie &= ~COOKIE_CHAR;
162               bRedefineBlock = true;
163             }
164           continue;
165         }
166
167       //  Extended comment <!--....-->
168       if (dwCookie & COOKIE_EXT_COMMENT)
169         {
170           if (I > 1 && pszChars[I] == '>' && pszChars[nPrevI] == '-' && *::CharPrev(pszChars, pszChars + nPrevI) == '-')
171             {
172               dwCookie &= ~COOKIE_EXT_COMMENT;
173               bRedefineBlock = true;
174             }
175           continue;
176         }
177
178       //  Normal text
179       if ((dwCookie & COOKIE_ELEMENT) && pszChars[I] == '"')
180         {
181           DEFINE_BLOCK (I, COLORINDEX_STRING);
182           dwCookie |= COOKIE_STRING;
183           continue;
184         }
185
186       if ((dwCookie & COOKIE_ELEMENT) && pszChars[I] == '\'')
187         {
188           // if (I + 1 < nLength && pszChars[I + 1] == '\'' || I + 2 < nLength && pszChars[I + 1] != '\\' && pszChars[I + 2] == '\'' || I + 3 < nLength && pszChars[I + 1] == '\\' && pszChars[I + 3] == '\'')
189           if (!I || !xisalnum (pszChars[nPrevI]))
190             {
191               DEFINE_BLOCK (I, COLORINDEX_STRING);
192               dwCookie |= COOKIE_CHAR;
193               continue;
194             }
195         }
196
197       if (I < nLength - 3 && pszChars[I] == '<' && pszChars[I + 1] == '!' && pszChars[I + 2] == '-' && pszChars[I + 3] == '-')
198         {
199           DEFINE_BLOCK (I, COLORINDEX_COMMENT);
200           I += 3;
201           dwCookie |= COOKIE_EXT_COMMENT;
202           dwCookie &= ~COOKIE_ELEMENT;
203           continue;
204         }
205
206       //  User1 start: <?
207       if (I < nLength && pszChars[I] == '<' && I < nLength - 1 && (pszChars[I + 1] == '?' || pszChars[I + 1] == '%'))
208         {
209           DEFINE_BLOCK (I, COLORINDEX_NORMALTEXT);
210           dwCookie |= COOKIE_EXT_USER1;
211           nIdentBegin = -1;
212           continue;
213         }
214
215       if (pBuf == nullptr)
216         continue;               //  We don't need to extract keywords,
217       //  for faster parsing skip the rest of loop
218
219       if (xisalnum (pszChars[I]) || pszChars[I] == '.')
220         {
221           if (nIdentBegin == -1)
222             nIdentBegin = I;
223         }
224       else
225         {
226           if (nIdentBegin >= 0)
227             {
228               if (dwCookie & COOKIE_ELEMENT)
229                 {
230                   if (IsHtmlKeyword (pszChars + nIdentBegin, I - nIdentBegin) && (pszChars[nIdentBegin - 1] == _T ('<') || pszChars[nIdentBegin - 1] == _T ('/')))
231                     {
232                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
233                       if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<script"), sizeof("<script") - 1) == 0)
234                         dwCookie |= COOKIE_BLOCK_SCRIPT;
235                       else if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<style"), sizeof("<style") - 1) == 0)
236                         dwCookie |= COOKIE_BLOCK_STYLE;
237                     }
238                   else if (IsHtmlUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
239                     {
240                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
241                     }
242                   else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
243                     {
244                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
245                     }
246                   else
247                     {
248                       goto next;
249                     }
250                 }
251               else if (dwCookie & COOKIE_USER1)
252                 {
253                   if (IsHtmlUser2Keyword (pszChars + nIdentBegin, I - nIdentBegin))
254                     {
255                       DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER2);
256                     }
257                   else
258                     {
259                       goto next;
260                     }
261                 }
262               bRedefineBlock = true;
263               bDecIndex = true;
264               nIdentBegin = -1;
265 next:
266               ;
267             }
268
269           //  Preprocessor start: < or bracket
270           if (I < nLength && pszChars[I] == '<' && !(I < nLength - 3 && pszChars[I + 1] == '!' && pszChars[I + 2] == '-' && pszChars[I + 3] == '-'))
271             {
272               DEFINE_BLOCK (I, COLORINDEX_OPERATOR);
273               DEFINE_BLOCK (I + 1, COLORINDEX_PREPROCESSOR);
274               dwCookie |= COOKIE_ELEMENT;
275               nIdentBegin = -1;
276               continue;
277             }
278
279           //  Preprocessor end: > or bracket
280           if (dwCookie & COOKIE_ELEMENT)
281             {
282               if (pszChars[I] == '>')
283                 {
284                   dwCookie &= ~COOKIE_ELEMENT;
285                   nIdentBegin = -1;
286                   bRedefineBlock = true;
287                   bDecIndex = true;
288                   continue;
289                 }
290             }
291
292           //  Preprocessor start: &
293           if (pszChars[I] == '&')
294             {
295               dwCookie |= COOKIE_USER1;
296               nIdentBegin = -1;
297               continue;
298             }
299
300           //  Preprocessor end: ;
301           if (dwCookie & COOKIE_USER1)
302             {
303               if (pszChars[I] == ';')
304                 {
305                   dwCookie &= ~COOKIE_USER1;
306                   nIdentBegin = -1;
307                   continue;
308                 }
309             }
310         }
311     }
312
313   if (nIdentBegin >= 0 && (dwCookie & COOKIE_ELEMENT))
314     {
315       if (IsHtmlKeyword (pszChars + nIdentBegin, I - nIdentBegin) && (pszChars[nIdentBegin - 1] == _T ('<') || pszChars[nIdentBegin - 1] == _T ('/')))
316         {
317           DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
318           if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<script"), sizeof("<script") - 1) == 0)
319             dwCookie |= COOKIE_BLOCK_SCRIPT;
320           else if (nIdentBegin > 0 && _tcsnicmp(pszChars + nIdentBegin - 1, _T("<style"), sizeof("<style") - 1) == 0)
321             dwCookie |= COOKIE_BLOCK_STYLE;
322         }
323       else if (IsHtmlUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
324         {
325           DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
326         }
327       else if (IsHtmlUser2Keyword (pszChars + nIdentBegin, I - nIdentBegin))
328         {
329           DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER2);
330         }
331       else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
332         {
333           DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
334         }
335     }
336
337   dwCookie &= (COOKIE_EXT_COMMENT | COOKIE_STRING | COOKIE_ELEMENT | COOKIE_EXT_USER1 | COOKIE_BLOCK_SCRIPT | COOKIE_BLOCK_STYLE | COOKIE_EXT_DEFINITION | COOKIE_EXT_VALUE);
338   return dwCookie;
339 }
340
341 unsigned
342 CrystalLineParser::ParseLineHtml (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems)
343 {
344   return ParseLineHtmlEx(dwCookie, pszChars, nLength, pBuf, nActualItems, SRC_JAVA);
345 }