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.

206 lines
8.3KB

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