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.

277 lines
8.4KB

  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. #pragma once
  14. //==============================================================================
  15. struct CppParserHelpers
  16. {
  17. static bool parseHexInt (const String& text, int64& result)
  18. {
  19. CppTokeniserFunctions::StringIterator i (text);
  20. if (CppTokeniserFunctions::parseHexLiteral (i))
  21. {
  22. result = text.fromFirstOccurrenceOf ("x", false, true).getHexValue64();
  23. return true;
  24. }
  25. return false;
  26. }
  27. static bool parseOctalInt (const String& text, int64& result)
  28. {
  29. CppTokeniserFunctions::StringIterator it (text);
  30. if (CppTokeniserFunctions::parseOctalLiteral (it))
  31. {
  32. result = 0;
  33. for (int i = 0; i < text.length(); ++i)
  34. {
  35. const auto digit = (int) (text[i] - '0');
  36. if (digit < 0 || digit > 7)
  37. break;
  38. result = result * 8 + digit;
  39. }
  40. return true;
  41. }
  42. return false;
  43. }
  44. static bool parseDecimalInt (const String& text, int64& result)
  45. {
  46. CppTokeniserFunctions::StringIterator i (text);
  47. if (CppTokeniserFunctions::parseDecimalLiteral (i))
  48. {
  49. result = text.getLargeIntValue();
  50. return true;
  51. }
  52. return false;
  53. }
  54. static bool parseInt (const String& text, int64& result)
  55. {
  56. return parseHexInt (text, result)
  57. || parseOctalInt (text, result)
  58. || parseDecimalInt (text, result);
  59. }
  60. static bool parseFloat (const String& text, double& result)
  61. {
  62. CppTokeniserFunctions::StringIterator i (text);
  63. if (CppTokeniserFunctions::parseFloatLiteral (i))
  64. {
  65. result = text.getDoubleValue();
  66. return true;
  67. }
  68. return false;
  69. }
  70. static int parseSingleToken (const String& text)
  71. {
  72. if (text.isEmpty())
  73. return CPlusPlusCodeTokeniser::tokenType_error;
  74. CppTokeniserFunctions::StringIterator i (text);
  75. i.skipWhitespace();
  76. const int tok = CppTokeniserFunctions::readNextToken (i);
  77. i.skipWhitespace();
  78. i.skip();
  79. return i.isEOF() ? tok : CPlusPlusCodeTokeniser::tokenType_error;
  80. }
  81. static String getIntegerSuffix (const String& s) { return s.retainCharacters ("lLuU"); }
  82. static String getFloatSuffix (const String& s) { return s.retainCharacters ("fF"); }
  83. static String getReplacementStringInSameFormat (const String& old, double newValue)
  84. {
  85. {
  86. CppTokeniserFunctions::StringIterator i (old);
  87. if (CppTokeniserFunctions::parseFloatLiteral (i))
  88. {
  89. String s (newValue);
  90. if (! s.containsChar ('.'))
  91. s += ".0";
  92. return s + getFloatSuffix (old);
  93. }
  94. }
  95. return getReplacementStringInSameFormat (old, (int64) newValue);
  96. }
  97. static String getReplacementStringInSameFormat (const String& old, int64 newValue)
  98. {
  99. {
  100. CppTokeniserFunctions::StringIterator i (old);
  101. if (CppTokeniserFunctions::parseHexLiteral (i))
  102. {
  103. String s ("0x" + String::toHexString (newValue) + getIntegerSuffix (old));
  104. if (old.toUpperCase() == old)
  105. s = s.toUpperCase();
  106. return s;
  107. }
  108. }
  109. {
  110. CppTokeniserFunctions::StringIterator i (old);
  111. if (CppTokeniserFunctions::parseDecimalLiteral (i))
  112. return String (newValue) + getIntegerSuffix (old);
  113. }
  114. return old;
  115. }
  116. // Given a type name which could be a smart pointer or other pointer/ref, this extracts
  117. // the essential class name of the thing that it points to.
  118. static String getSignificantClass (String cls)
  119. {
  120. int firstAngleBracket = cls.indexOfChar ('<');
  121. if (firstAngleBracket > 0)
  122. cls = cls.substring (firstAngleBracket + 1).upToLastOccurrenceOf (">", false, false).trim();
  123. while (cls.endsWithChar ('*') || cls.endsWithChar ('&'))
  124. cls = cls.dropLastCharacters (1).trim();
  125. return cls;
  126. }
  127. //==============================================================================
  128. struct ValidCppIdentifierRestriction : public TextEditor::InputFilter
  129. {
  130. ValidCppIdentifierRestriction (bool allowTemplatesAndNamespaces)
  131. : className (allowTemplatesAndNamespaces) {}
  132. String filterNewText (TextEditor& ed, const String& text)
  133. {
  134. String allowedChars ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_");
  135. if (className)
  136. allowedChars += "<>:";
  137. if (ed.getHighlightedRegion().getStart() > 0)
  138. allowedChars += "0123456789";
  139. String s = text.retainCharacters (allowedChars);
  140. if (CPlusPlusCodeTokeniser::isReservedKeyword (ed.getText().replaceSection (ed.getHighlightedRegion().getStart(),
  141. ed.getHighlightedRegion().getLength(),
  142. s)))
  143. return String();
  144. return s;
  145. }
  146. bool className;
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValidCppIdentifierRestriction)
  148. };
  149. };
  150. //==============================================================================
  151. struct CodeChange
  152. {
  153. CodeChange (Range<int> r, const String& t) : range (r), text (t)
  154. {
  155. }
  156. bool mergeWith (const CodeChange& next)
  157. {
  158. if (text.isEmpty())
  159. {
  160. if (next.text.isNotEmpty()
  161. && next.range.isEmpty()
  162. && next.range.getStart() == range.getStart())
  163. {
  164. text = next.text;
  165. return true;
  166. }
  167. if (next.text.isEmpty())
  168. {
  169. Range<int> nextRange (next.range);
  170. if (nextRange.getStart() >= range.getStart())
  171. nextRange += range.getLength();
  172. else if (nextRange.getEnd() > range.getStart())
  173. nextRange.setEnd (nextRange.getEnd() + range.getLength());
  174. if (range.intersects (nextRange)
  175. || range.getEnd() == nextRange.getStart()
  176. || range.getStart() == nextRange.getEnd())
  177. {
  178. range = range.getUnionWith (nextRange);
  179. return true;
  180. }
  181. }
  182. }
  183. else if (next.text.isEmpty())
  184. {
  185. if (next.range.getEnd() == range.getStart())
  186. {
  187. range.setStart (next.range.getStart());
  188. return true;
  189. }
  190. if (next.range.getStart() == range.getStart() + text.length())
  191. {
  192. range.setLength (range.getLength() + next.range.getLength());
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. void addToList (Array<CodeChange>& list) const
  199. {
  200. if (list.size() == 0 || ! list.getReference (list.size() - 1).mergeWith (*this))
  201. list.add (*this);
  202. }
  203. Range<int> range;
  204. String text;
  205. };
  206. //==============================================================================
  207. static inline String concatenateListOfStrings (const StringArray& s)
  208. {
  209. return s.joinIntoString ("\x01");
  210. }
  211. static inline StringArray separateJoinedStrings (const String& s)
  212. {
  213. return StringArray::fromTokens (s, "\x01", juce::StringRef());
  214. }