1 ///////////////////////////////////////////////////////////////////////////
\r
4 // Created: 17-May-2019
\r
6 // Copyright: Stcherbatchenko Andrei, portions by Takashi Sawanaka
\r
7 // E-mail: sdottaka@users.sourceforge.net
\r
9 // Lua syntax highlighing definition
\r
11 // You are free to use or modify this code to the following restrictions:
\r
12 // - Acknowledge me somewhere in your about box, simple "Parts of code by.."
\r
13 // will be enough. If you can't (or don't want to), contact me personally.
\r
14 // - LEAVE THIS HEADER INTACT
\r
15 ////////////////////////////////////////////////////////////////////////////
\r
18 #include "crystallineparser.h"
\r
19 #include "../SyntaxColors.h"
\r
20 #include "../utils/string_util.h"
\r
21 #include <algorithm>
\r
24 #define new DEBUG_NEW
\r
28 static const TCHAR * s_apszLuaKeywordList[] =
\r
54 IsLuaKeyword (const TCHAR *pszChars, int nLength)
\r
56 return ISXKEYWORD (s_apszLuaKeywordList, pszChars, nLength);
\r
60 CrystalLineParser::ParseLineLua (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems)
\r
63 return dwCookie & (COOKIE_EXT_COMMENT | COOKIE_RAWSTRING | 0xFF000000);
\r
65 bool bRedefineBlock = true;
\r
66 bool bDecIndex = false;
\r
67 int nIdentBegin = -1;
\r
70 for (I = 0;; nPrevI = I, I = static_cast<int>(::CharNext(pszChars+I) - pszChars))
\r
74 // CharNext did not advance, so we're at the end of the string
\r
75 // and we already handled this character, so stop
\r
84 if (dwCookie & (COOKIE_COMMENT | COOKIE_EXT_COMMENT))
\r
86 DEFINE_BLOCK (nPos, COLORINDEX_COMMENT);
\r
88 else if (dwCookie & (COOKIE_CHAR | COOKIE_STRING | COOKIE_RAWSTRING))
\r
90 DEFINE_BLOCK (nPos, COLORINDEX_STRING);
\r
94 if (xisalnum (pszChars[nPos]) || pszChars[nPos] == '.' && nPos > 0 && (!xisalpha (*::CharPrev(pszChars, pszChars + nPos)) && !xisalpha (*::CharNext(pszChars + nPos))))
\r
96 DEFINE_BLOCK (nPos, COLORINDEX_NORMALTEXT);
\r
100 DEFINE_BLOCK (nPos, COLORINDEX_OPERATOR);
\r
101 bRedefineBlock = true;
\r
106 bRedefineBlock = false;
\r
111 // Can be bigger than length if there is binary data
\r
112 // See bug #1474782 Crash when comparing SQL with with binary data
\r
113 if (I >= nLength || pszChars[I] == 0)
\r
116 if (dwCookie & COOKIE_COMMENT)
\r
118 DEFINE_BLOCK (I, COLORINDEX_COMMENT);
\r
119 dwCookie |= COOKIE_COMMENT;
\r
123 // String constant "...."
\r
124 if (dwCookie & COOKIE_STRING)
\r
126 if (pszChars[I] == '"' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
\r
128 dwCookie &= ~COOKIE_STRING;
\r
129 bRedefineBlock = true;
\r
134 // Char constant '..'
\r
135 if (dwCookie & COOKIE_CHAR)
\r
137 if (pszChars[I] == '\'' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
\r
139 dwCookie &= ~COOKIE_CHAR;
\r
140 bRedefineBlock = true;
\r
145 // Raw string constant [[ ... ]], [=[ ... ]=], [==[ ... ]==], ...
\r
146 if (dwCookie & COOKIE_RAWSTRING)
\r
148 const int nEqualsSignCount = COOKIE_GET_LUA_EQUALS_SIGN_COUNT(dwCookie);
\r
149 if (I >= nEqualsSignCount + 1 && pszChars[I] == ']' && pszChars[I - nEqualsSignCount - 1] == ']' &&
\r
150 std::all_of(pszChars + I - nEqualsSignCount, pszChars + I, [](const auto c) { return c == '='; }))
\r
152 dwCookie &= ~COOKIE_RAWSTRING;
\r
153 COOKIE_SET_LUA_EQUALS_SIGN_COUNT(dwCookie, 0);
\r
154 bRedefineBlock = true;
\r
159 // Extended comment --[[ ... ]] , --[=[ ... ]=], --[==[ ... ]==], ...
\r
160 if (dwCookie & COOKIE_EXT_COMMENT)
\r
162 const int nEqualsSignCount = COOKIE_GET_LUA_EQUALS_SIGN_COUNT(dwCookie);
\r
163 if (I >= nEqualsSignCount + 1 && pszChars[I] == ']' && pszChars[I - nEqualsSignCount - 1] == ']' &&
\r
164 std::all_of(pszChars + I - nEqualsSignCount, pszChars + I, [](const auto c) { return c == '='; }))
\r
166 dwCookie &= ~COOKIE_EXT_COMMENT;
\r
167 COOKIE_SET_LUA_EQUALS_SIGN_COUNT(dwCookie, 0);
\r
168 bRedefineBlock = true;
\r
173 if (I > 0 && pszChars[I] == '-' && pszChars[nPrevI] == '-')
\r
175 DEFINE_BLOCK (nPrevI, COLORINDEX_COMMENT);
\r
176 dwCookie |= COOKIE_COMMENT;
\r
181 if (pszChars[I] == '"')
\r
183 DEFINE_BLOCK (I, COLORINDEX_STRING);
\r
184 dwCookie |= COOKIE_STRING;
\r
187 if (pszChars[I] == '\'')
\r
189 // 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] == '\'')
\r
190 if (!I || !xisalnum (pszChars[nPrevI]))
\r
192 DEFINE_BLOCK (I, COLORINDEX_STRING);
\r
193 dwCookie |= COOKIE_CHAR;
\r
197 // Raw string [[ ... ]], [=[ ... ]=], [==[ ... ]==], ...
\r
198 if (pszChars[I] == '[' && I + 1 < nLength && (pszChars[I + 1] == '[' || pszChars[I + 1] == '='))
\r
200 int nEqualsSignCount = 0;
\r
201 while (I + 1 + nEqualsSignCount < nLength && pszChars[I + 1 + nEqualsSignCount] == '=')
\r
202 ++nEqualsSignCount;
\r
203 if (I + 1 + nEqualsSignCount < nLength && pszChars[I + 1 + nEqualsSignCount] == '[')
\r
205 DEFINE_BLOCK (I, COLORINDEX_STRING);
\r
206 dwCookie |= COOKIE_RAWSTRING;
\r
207 COOKIE_SET_LUA_EQUALS_SIGN_COUNT(dwCookie, nEqualsSignCount);
\r
211 // Extended comment [[ ... ]], [=[ ... ]=], [==[ ... ]==], ...
\r
212 if (pszChars[I] == '-' && I + 3 < nLength && pszChars[I + 1] == '-' && pszChars[I + 2] == '[' && (pszChars[I + 3] == '[' || pszChars[I + 3] == '='))
\r
214 int nEqualsSignCount = 0;
\r
215 while (I + 3 + nEqualsSignCount < nLength && pszChars[I + 3 + nEqualsSignCount] == '=')
\r
216 ++nEqualsSignCount;
\r
217 if (I + 3 + nEqualsSignCount < nLength && pszChars[I + 3 + nEqualsSignCount] == '[')
\r
219 DEFINE_BLOCK (I, COLORINDEX_COMMENT);
\r
220 dwCookie |= COOKIE_EXT_COMMENT;
\r
221 COOKIE_SET_LUA_EQUALS_SIGN_COUNT(dwCookie, nEqualsSignCount);
\r
226 if (pBuf == nullptr)
\r
227 continue; // We don't need to extract keywords,
\r
228 // for faster parsing skip the rest of loop
\r
230 if (xisalnum (pszChars[I]) || pszChars[I] == '.' && I > 0 && (!xisalpha (pszChars[nPrevI]) && !xisalpha (pszChars[I + 1])))
\r
232 if (nIdentBegin == -1)
\r
237 if (nIdentBegin >= 0)
\r
239 if (IsLuaKeyword (pszChars + nIdentBegin, I - nIdentBegin))
\r
241 DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
\r
243 else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
\r
245 DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
\r
249 bool bFunction = false;
\r
251 for (int j = I; j < nLength; j++)
\r
253 if (!xisspace (pszChars[j]))
\r
255 if (pszChars[j] == '(')
\r
264 DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
\r
267 bRedefineBlock = true;
\r
274 if (nIdentBegin >= 0)
\r
276 if (IsLuaKeyword (pszChars + nIdentBegin, I - nIdentBegin))
\r
278 DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
\r
280 else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
\r
282 DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
\r
286 bool bFunction = false;
\r
288 for (int j = I; j < nLength; j++)
\r
290 if (!xisspace (pszChars[j]))
\r
292 if (pszChars[j] == '(')
\r
301 DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
\r
306 dwCookie &= COOKIE_EXT_COMMENT | COOKIE_RAWSTRING | 0xFF000000;
\r