1 ///////////////////////////////////////////////////////////////////////////
\r
4 // Created: 23-Jul-2017
\r
6 // Copyright: Stcherbatchenko Andrei, portions by Takashi Sawanaka
\r
7 // E-mail: sdottaka@users.sourceforge.net
\r
9 // Rust 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 "string_util.h"
\r
21 #include <algorithm>
\r
24 #define new DEBUG_NEW
\r
28 static LPCTSTR s_apszRustKeywordList[] =
\r
84 static LPCTSTR s_apszUser1KeywordList[] =
\r
107 IsRustKeyword (LPCTSTR pszChars, int nLength)
\r
109 return ISXKEYWORD (s_apszRustKeywordList, pszChars, nLength);
\r
113 IsUser1Keyword (LPCTSTR pszChars, int nLength)
\r
115 return ISXKEYWORD (s_apszUser1KeywordList, pszChars, nLength);
\r
119 CrystalLineParser::ParseLineRust (DWORD dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems)
\r
122 return dwCookie & (COOKIE_EXT_COMMENT | COOKIE_RAWSTRING | COOKIE_STRING | 0xFF000000);
\r
124 LPCTSTR pszRawStringBegin = nullptr;
\r
125 LPCTSTR pszCommentBegin = nullptr;
\r
126 LPCTSTR pszCommentEnd = nullptr;
\r
127 bool bRedefineBlock = true;
\r
128 bool bDecIndex = false;
\r
129 int nIdentBegin = -1;
\r
132 for (I = 0;; nPrevI = I, I = static_cast<int>(::CharNext(pszChars+I) - pszChars))
\r
136 // CharNext did not advance, so we're at the end of the string
\r
137 // and we already handled this character, so stop
\r
141 if (bRedefineBlock)
\r
146 if (dwCookie & (COOKIE_COMMENT | COOKIE_EXT_COMMENT))
\r
148 DEFINE_BLOCK (nPos, COLORINDEX_COMMENT);
\r
150 else if (dwCookie & (COOKIE_STRING | COOKIE_RAWSTRING))
\r
152 DEFINE_BLOCK (nPos, COLORINDEX_STRING);
\r
156 if (xisalnum (pszChars[nPos]) || pszChars[nPos] == '.' && nPos > 0 && (!xisalpha (*::CharPrev(pszChars, pszChars + nPos)) && !xisalpha (*::CharNext(pszChars + nPos))))
\r
158 DEFINE_BLOCK (nPos, COLORINDEX_NORMALTEXT);
\r
162 DEFINE_BLOCK (nPos, COLORINDEX_OPERATOR);
\r
163 bRedefineBlock = true;
\r
168 bRedefineBlock = false;
\r
173 // Can be bigger than length if there is binary data
\r
174 // See bug #1474782 Crash when comparing SQL with with binary data
\r
175 if (I >= nLength || pszChars[I] == 0)
\r
178 if (dwCookie & COOKIE_COMMENT)
\r
180 DEFINE_BLOCK (I, COLORINDEX_COMMENT);
\r
181 dwCookie |= COOKIE_COMMENT;
\r
185 // String constant "...."
\r
186 if (dwCookie & COOKIE_STRING)
\r
188 if (pszChars[I] == '"' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
\r
190 dwCookie &= ~COOKIE_STRING;
\r
191 bRedefineBlock = true;
\r
196 // Raw string constant r"...." , r#"..."# , r##"..."##
\r
197 if (dwCookie & COOKIE_RAWSTRING)
\r
199 const int nNumberCount = COOKIE_GET_RAWSTRING_NUMBER_COUNT(dwCookie);
\r
200 if (I >= nNumberCount && pszChars[I - nNumberCount] == '"')
\r
202 if ((pszRawStringBegin < pszChars + I - nNumberCount) &&
\r
203 (nNumberCount == 0 ||
\r
204 std::all_of(pszChars + I - nNumberCount + 1, pszChars + I + 1, [](const auto c) { return c == '#'; })))
\r
206 dwCookie &= ~COOKIE_RAWSTRING;
\r
207 bRedefineBlock = true;
\r
208 pszRawStringBegin = nullptr;
\r
214 // Extended comment /*....*/
\r
215 if (dwCookie & COOKIE_EXT_COMMENT)
\r
217 const int depth = COOKIE_GET_EXT_COMMENT_DEPTH(dwCookie);
\r
218 if ((pszCommentEnd < pszChars + I) && (I > 0 && pszChars[I] == '*' && pszChars[nPrevI] == '/'))
\r
220 COOKIE_SET_EXT_COMMENT_DEPTH(dwCookie, depth + 1);
\r
221 pszCommentBegin = pszChars + I + 1;
\r
223 else if ((pszCommentBegin < pszChars + I) && (I > 0 && pszChars[I] == '/' && pszChars[nPrevI] == '*'))
\r
227 dwCookie &= ~COOKIE_EXT_COMMENT;
\r
228 bRedefineBlock = true;
\r
232 COOKIE_SET_EXT_COMMENT_DEPTH(dwCookie, depth - 1);
\r
234 pszCommentEnd = pszChars + I + 1;
\r
239 if (I > 0 && pszChars[I] == '/' && pszChars[nPrevI] == '/')
\r
241 DEFINE_BLOCK (nPrevI, COLORINDEX_COMMENT);
\r
242 dwCookie |= COOKIE_COMMENT;
\r
247 if (pszChars[I] == '"')
\r
249 DEFINE_BLOCK (I, COLORINDEX_STRING);
\r
250 dwCookie |= COOKIE_STRING;
\r
255 if ((pszChars[I] == 'r' && I + 1 < nLength && (pszChars[I + 1] == '"' || pszChars[I + 1] == '#')) ||
\r
256 (pszChars[I] == 'b' && I + 2 < nLength && pszChars[I + 1] == 'r' && (pszChars[I + 2] == '"' || pszChars[I + 2] == '#')))
\r
258 const int nprefix = (pszChars[I] == 'r' ? 1 : 2);
\r
259 const TCHAR *p = std::find_if(pszChars + I + nprefix, pszChars + nLength,
\r
260 [](const auto c) { return c != '#'; });
\r
261 if (p != pszChars + nLength && *p == '"')
\r
263 DEFINE_BLOCK (I, COLORINDEX_STRING);
\r
264 dwCookie |= COOKIE_RAWSTRING;
\r
265 COOKIE_SET_RAWSTRING_NUMBER_COUNT(dwCookie, static_cast<int>(p - (pszChars + I + nprefix)));
\r
266 pszRawStringBegin = p + 1;
\r
270 if ((pszCommentEnd < pszChars + I) && (I > 0 && pszChars[I] == '*' && pszChars[nPrevI] == '/'))
\r
272 DEFINE_BLOCK (nPrevI, COLORINDEX_COMMENT);
\r
273 dwCookie |= COOKIE_EXT_COMMENT;
\r
274 pszCommentBegin = pszChars + I + 1;
\r
278 if (pBuf == nullptr)
\r
279 continue; // We don't need to extract keywords,
\r
280 // for faster parsing skip the rest of loop
\r
282 if (xisalnum (pszChars[I]) || pszChars[I] == '.' && I > 0 && (!xisalpha (pszChars[nPrevI]) && !xisalpha (pszChars[I + 1])))
\r
284 if (nIdentBegin == -1)
\r
289 if (nIdentBegin >= 0)
\r
291 if (IsRustKeyword (pszChars + nIdentBegin, I - nIdentBegin))
\r
293 DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
\r
295 else if (IsUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
\r
297 DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
\r
299 else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
\r
301 DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
\r
305 bool bFunction = false;
\r
307 for (int j = I; j < nLength; j++)
\r
309 if (!xisspace (pszChars[j]) && pszChars[j] != '!')
\r
311 if (pszChars[j] == '(')
\r
320 DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
\r
323 bRedefineBlock = true;
\r
330 if (nIdentBegin >= 0)
\r
332 if (IsRustKeyword (pszChars + nIdentBegin, I - nIdentBegin))
\r
334 DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
\r
336 else if (IsUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
\r
338 DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
\r
340 else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
\r
342 DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
\r
346 bool bFunction = false;
\r
348 for (int j = I; j < nLength; j++)
\r
350 if (!xisspace (pszChars[j]) && pszChars[j] != '!')
\r
352 if (pszChars[j] == '(')
\r
361 DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
\r
366 dwCookie &= COOKIE_EXT_COMMENT | COOKIE_RAWSTRING | COOKIE_STRING | 0xFF000000;
\r