OSDN Git Service

Add C# 6.0 reserved words to style list (#719)
[winmerge-jp/winmerge-jp.git] / Externals / crystaledit / editlib / parsers / csharp.cpp
index 963a138..984960c 100644 (file)
@@ -35,7 +35,7 @@
 #endif
 
 //  C# keywords
-static LPCTSTR s_apszCppKeywordList[] =
+static const TCHAR * s_apszCppKeywordList[] =
   {
     _T ("abstract"),
     _T ("base"),
@@ -78,6 +78,7 @@ static LPCTSTR s_apszCppKeywordList[] =
     _T ("is"),
     _T ("lock"),
     _T ("long"),
+    _T ("nameof"),
     _T ("namespace"),
     _T ("new"),
     _T ("null"),
@@ -113,256 +114,19 @@ static LPCTSTR s_apszCppKeywordList[] =
     _T ("using"),
     _T ("virtual"),
     _T ("void"),
+    _T ("when"),
+    _T ("while"),
   };
 
 
 static bool
-IsCppKeyword (LPCTSTR pszChars, int nLength)
+IsCppKeyword (const TCHAR *pszChars, int nLength)
 {
   return ISXKEYWORD (s_apszCppKeywordList, pszChars, nLength);
 }
 
-DWORD
-CrystalLineParser::ParseLineCSharp (DWORD dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems)
+unsigned
+CrystalLineParser::ParseLineCSharp (unsigned dwCookie, const TCHAR *pszChars, int nLength, TEXTBLOCK * pBuf, int &nActualItems)
 {
-  if (nLength == 0)
-    return dwCookie & COOKIE_EXT_COMMENT;
-
-  bool bFirstChar = (dwCookie & ~COOKIE_EXT_COMMENT) == 0;
-  LPCTSTR pszCommentBegin = nullptr;
-  LPCTSTR pszCommentEnd = nullptr;
-  bool bRedefineBlock = true;
-  bool bDecIndex = false;
-  int nIdentBegin = -1;
-  int nPrevI = -1;
-  int I=0;
-  for (I = 0;; nPrevI = I, I = static_cast<int>(::CharNext(pszChars+I) - pszChars))
-    {
-      if (I == nPrevI)
-        {
-          // CharNext did not advance, so we're at the end of the string
-          // and we already handled this character, so stop
-          break;
-        }
-
-      if (bRedefineBlock)
-        {
-          int nPos = I;
-          if (bDecIndex)
-            nPos = nPrevI;
-          if (dwCookie & (COOKIE_COMMENT | COOKIE_EXT_COMMENT))
-            {
-              DEFINE_BLOCK (nPos, COLORINDEX_COMMENT);
-            }
-          else if (dwCookie & (COOKIE_CHAR | COOKIE_STRING))
-            {
-              DEFINE_BLOCK (nPos, COLORINDEX_STRING);
-            }
-          else if (dwCookie & COOKIE_PREPROCESSOR)
-            {
-              DEFINE_BLOCK (nPos, COLORINDEX_PREPROCESSOR);
-            }
-          else
-            {
-              if (xisalnum (pszChars[nPos]) || pszChars[nPos] == '.' && nPos > 0 && (!xisalpha (*::CharPrev(pszChars, pszChars + nPos)) && !xisalpha (*::CharNext(pszChars + nPos))))
-                {
-                  DEFINE_BLOCK (nPos, COLORINDEX_NORMALTEXT);
-                }
-              else
-                {
-                  DEFINE_BLOCK (nPos, COLORINDEX_OPERATOR);
-                  bRedefineBlock = true;
-                  bDecIndex = true;
-                  goto out;
-                }
-            }
-          bRedefineBlock = false;
-          bDecIndex = false;
-        }
-out:
-
-      // Can be bigger than length if there is binary data
-      // See bug #1474782 Crash when comparing SQL with with binary data
-      if (I >= nLength || pszChars[I] == 0)
-        break;
-
-      if (dwCookie & COOKIE_COMMENT)
-        {
-          DEFINE_BLOCK (I, COLORINDEX_COMMENT);
-          dwCookie |= COOKIE_COMMENT;
-          break;
-        }
-
-      //  String constant "...."
-      if (dwCookie & COOKIE_STRING)
-        {
-          if (pszChars[I] == '"' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
-            {
-              dwCookie &= ~COOKIE_STRING;
-              bRedefineBlock = true;
-            }
-          continue;
-        }
-
-      //  Char constant '..'
-      if (dwCookie & COOKIE_CHAR)
-        {
-          if (pszChars[I] == '\'' && (I == 0 || I == 1 && pszChars[nPrevI] != '\\' || I >= 2 && (pszChars[nPrevI] != '\\' || *::CharPrev(pszChars, pszChars + nPrevI) == '\\')))
-            {
-              dwCookie &= ~COOKIE_CHAR;
-              bRedefineBlock = true;
-            }
-          continue;
-        }
-
-      //  Extended comment /*....*/
-      if (dwCookie & COOKIE_EXT_COMMENT)
-        {
-          if ((pszCommentBegin < pszChars + I) && (I > 0 && pszChars[I] == '/' && pszChars[nPrevI] == '*'))
-            {
-              dwCookie &= ~COOKIE_EXT_COMMENT;
-              bRedefineBlock = true;
-              pszCommentEnd = pszChars + I + 1;
-            }
-          continue;
-        }
-
-      if ((pszCommentEnd < pszChars + I) && (I > 0 && pszChars[I] == '/' && pszChars[nPrevI] == '/'))
-        {
-          DEFINE_BLOCK (nPrevI, COLORINDEX_COMMENT);
-          dwCookie |= COOKIE_COMMENT;
-          break;
-        }
-
-      //  Preprocessor directive #....
-      if (dwCookie & COOKIE_PREPROCESSOR)
-        {
-          if ((pszCommentEnd < pszChars + I) && (I > 0 && pszChars[I] == '*' && pszChars[nPrevI] == '/'))
-            {
-              DEFINE_BLOCK (nPrevI, COLORINDEX_COMMENT);
-              dwCookie |= COOKIE_EXT_COMMENT;
-            }
-          continue;
-        }
-
-      //  Normal text
-      if (pszChars[I] == '"')
-        {
-          DEFINE_BLOCK (I, COLORINDEX_STRING);
-          dwCookie |= COOKIE_STRING;
-          continue;
-        }
-      if (pszChars[I] == '\'')
-        {
-          // 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] == '\'')
-          if (!I || !xisalnum (pszChars[nPrevI]))
-            {
-              DEFINE_BLOCK (I, COLORINDEX_STRING);
-              dwCookie |= COOKIE_CHAR;
-              continue;
-            }
-        }
-      if ((pszCommentEnd < pszChars + I) && (I > 0 && pszChars[I] == '*' && pszChars[nPrevI] == '/'))
-        {
-          DEFINE_BLOCK (nPrevI, COLORINDEX_COMMENT);
-          dwCookie |= COOKIE_EXT_COMMENT;
-          pszCommentBegin = pszChars + I + 1;
-          continue;
-        }
-
-      if (bFirstChar)
-        {
-          if (pszChars[I] == '#')
-            {
-              DEFINE_BLOCK (I, COLORINDEX_PREPROCESSOR);
-              dwCookie |= COOKIE_PREPROCESSOR;
-              continue;
-            }
-          if (!xisspace (pszChars[I]))
-            bFirstChar = false;
-        }
-
-      if (pBuf == nullptr)
-        continue;               //  We don't need to extract keywords,
-      //  for faster parsing skip the rest of loop
-
-      if (xisalnum (pszChars[I]) || pszChars[I] == '.' && I > 0 && (!xisalpha (pszChars[nPrevI]) && !xisalpha (pszChars[I + 1])))
-        {
-          if (nIdentBegin == -1)
-            nIdentBegin = I;
-        }
-      else
-        {
-          if (nIdentBegin >= 0)
-            {
-              if (IsCppKeyword (pszChars + nIdentBegin, I - nIdentBegin))
-                {
-                  DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
-                }
-              else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
-                {
-                  DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
-                }
-              else
-                {
-                  bool bFunction = false;
-
-                  for (int j = I; j < nLength; j++)
-                    {
-                      if (!xisspace (pszChars[j]))
-                        {
-                          if (pszChars[j] == '(')
-                            {
-                              bFunction = true;
-                            }
-                          break;
-                        }
-                    }
-                  if (bFunction)
-                    {
-                      DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
-                    }
-                }
-              bRedefineBlock = true;
-              bDecIndex = true;
-              nIdentBegin = -1;
-            }
-        }
-    }
-
-  if (nIdentBegin >= 0)
-    {
-      if (IsCppKeyword (pszChars + nIdentBegin, I - nIdentBegin))
-        {
-          DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
-        }
-      else if (IsXNumber (pszChars + nIdentBegin, I - nIdentBegin))
-        {
-          DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
-        }
-      else
-        {
-          bool bFunction = false;
-
-          for (int j = I; j < nLength; j++)
-            {
-              if (!xisspace (pszChars[j]))
-                {
-                  if (pszChars[j] == '(')
-                    {
-                      bFunction = true;
-                    }
-                  break;
-                }
-            }
-          if (bFunction)
-            {
-              DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
-            }
-        }
-    }
-
-  if (pszChars[nLength - 1] != '\\' || IsMBSTrail(pszChars, nLength - 1))
-    dwCookie &= COOKIE_EXT_COMMENT;
-  return dwCookie;
+  return ParseLineCJava (dwCookie, pszChars, nLength, pBuf, nActualItems, IsCppKeyword, nullptr);
 }