The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace build_tools
  16. {
  17. static bool isReservedKeyword (String::CharPointerType token, const int tokenLength) noexcept
  18. {
  19. static const char* const keywords2Char[] =
  20. { "do", "if", "or", nullptr };
  21. static const char* const keywords3Char[] =
  22. { "and", "asm", "for", "int", "new", "not", "try", "xor", nullptr };
  23. static const char* const keywords4Char[] =
  24. { "auto", "bool", "case", "char", "else", "enum", "goto",
  25. "long", "this", "true", "void", nullptr };
  26. static const char* const keywords5Char[] =
  27. { "bitor", "break", "catch", "class", "compl", "const", "false", "final",
  28. "float", "or_eq", "short", "throw", "union", "using", "while", nullptr };
  29. static const char* const keywords6Char[] =
  30. { "and_eq", "bitand", "delete", "double", "export", "extern", "friend",
  31. "import", "inline", "module", "not_eq", "public", "return", "signed",
  32. "sizeof", "static", "struct", "switch", "typeid", "xor_eq", nullptr };
  33. static const char* const keywords7Char[] =
  34. { "__cdecl", "_Pragma", "alignas", "alignof", "concept", "default",
  35. "mutable", "nullptr", "private", "typedef", "uint8_t", "virtual",
  36. "wchar_t", nullptr };
  37. static const char* const keywordsOther[] =
  38. { "@class", "@dynamic", "@end", "@implementation", "@interface", "@public",
  39. "@private", "@protected", "@property", "@synthesize", "__fastcall", "__stdcall",
  40. "atomic_cancel", "atomic_commit", "atomic_noexcept", "char16_t", "char32_t",
  41. "co_await", "co_return", "co_yield", "const_cast", "constexpr", "continue",
  42. "decltype", "dynamic_cast", "explicit", "namespace", "noexcept", "operator", "override",
  43. "protected", "register", "reinterpret_cast", "requires", "static_assert",
  44. "static_cast", "synchronized", "template", "thread_local", "typename", "unsigned",
  45. "volatile", nullptr };
  46. const char* const* k;
  47. switch (tokenLength)
  48. {
  49. case 2: k = keywords2Char; break;
  50. case 3: k = keywords3Char; break;
  51. case 4: k = keywords4Char; break;
  52. case 5: k = keywords5Char; break;
  53. case 6: k = keywords6Char; break;
  54. case 7: k = keywords7Char; break;
  55. default:
  56. if (tokenLength < 2 || tokenLength > 16)
  57. return false;
  58. k = keywordsOther;
  59. break;
  60. }
  61. for (int i = 0; k[i] != nullptr; ++i)
  62. if (token.compare (CharPointer_ASCII (k[i])) == 0)
  63. return true;
  64. return false;
  65. }
  66. static bool isReservedKeyword (const String& token) noexcept
  67. {
  68. return isReservedKeyword (token.getCharPointer(), token.length());
  69. }
  70. //==============================================================================
  71. /** Takes a UTF8 string and writes it to a stream using standard C++ escape sequences for any
  72. non-ascii bytes.
  73. Although not strictly a tokenising function, this is still a function that often comes in
  74. handy when working with C++ code!
  75. Note that addEscapeChars() is easier to use than this function if you're working with Strings.
  76. @see addEscapeChars
  77. */
  78. static void writeEscapeChars (OutputStream& out, const char* utf8, const int numBytesToRead,
  79. const int maxCharsOnLine, const bool breakAtNewLines,
  80. const bool replaceSingleQuotes, const bool allowStringBreaks)
  81. {
  82. int charsOnLine = 0;
  83. bool lastWasHexEscapeCode = false;
  84. bool trigraphDetected = false;
  85. for (int i = 0; i < numBytesToRead || numBytesToRead < 0; ++i)
  86. {
  87. auto c = (unsigned char) utf8[i];
  88. bool startNewLine = false;
  89. switch (c)
  90. {
  91. case '\t': out << "\\t"; trigraphDetected = false; lastWasHexEscapeCode = false; charsOnLine += 2; break;
  92. case '\r': out << "\\r"; trigraphDetected = false; lastWasHexEscapeCode = false; charsOnLine += 2; break;
  93. case '\n': out << "\\n"; trigraphDetected = false; lastWasHexEscapeCode = false; charsOnLine += 2; startNewLine = breakAtNewLines; break;
  94. case '\\': out << "\\\\"; trigraphDetected = false; lastWasHexEscapeCode = false; charsOnLine += 2; break;
  95. case '\"': out << "\\\""; trigraphDetected = false; lastWasHexEscapeCode = false; charsOnLine += 2; break;
  96. case '?':
  97. if (trigraphDetected)
  98. {
  99. out << "\\?";
  100. charsOnLine++;
  101. trigraphDetected = false;
  102. }
  103. else
  104. {
  105. out << "?";
  106. trigraphDetected = true;
  107. }
  108. lastWasHexEscapeCode = false;
  109. charsOnLine++;
  110. break;
  111. case 0:
  112. if (numBytesToRead < 0)
  113. return;
  114. out << "\\0";
  115. lastWasHexEscapeCode = true;
  116. trigraphDetected = false;
  117. charsOnLine += 2;
  118. break;
  119. case '\'':
  120. if (replaceSingleQuotes)
  121. {
  122. out << "\\\'";
  123. lastWasHexEscapeCode = false;
  124. trigraphDetected = false;
  125. charsOnLine += 2;
  126. break;
  127. }
  128. // deliberate fall-through...
  129. default:
  130. if (c >= 32 && c < 127 && ! (lastWasHexEscapeCode // (have to avoid following a hex escape sequence with a valid hex digit)
  131. && CharacterFunctions::getHexDigitValue (c) >= 0))
  132. {
  133. out << (char) c;
  134. lastWasHexEscapeCode = false;
  135. trigraphDetected = false;
  136. ++charsOnLine;
  137. }
  138. else if (allowStringBreaks && lastWasHexEscapeCode && c >= 32 && c < 127)
  139. {
  140. out << "\"\"" << (char) c;
  141. lastWasHexEscapeCode = false;
  142. trigraphDetected = false;
  143. charsOnLine += 3;
  144. }
  145. else
  146. {
  147. out << (c < 16 ? "\\x0" : "\\x") << String::toHexString ((int) c);
  148. lastWasHexEscapeCode = true;
  149. trigraphDetected = false;
  150. charsOnLine += 4;
  151. }
  152. break;
  153. }
  154. if ((startNewLine || (maxCharsOnLine > 0 && charsOnLine >= maxCharsOnLine))
  155. && (numBytesToRead < 0 || i < numBytesToRead - 1))
  156. {
  157. charsOnLine = 0;
  158. out << "\"" << newLine << "\"";
  159. lastWasHexEscapeCode = false;
  160. }
  161. }
  162. }
  163. }
  164. }