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.

233 lines
7.0KB

  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. struct LuaTokeniserFunctions
  16. {
  17. static bool isReservedKeyword (String::CharPointerType token, const int tokenLength) noexcept
  18. {
  19. static const char* const keywords2Char[] =
  20. { "if", "or", "in", "do", nullptr };
  21. static const char* const keywords3Char[] =
  22. { "and", "end", "for", "nil", "not", nullptr };
  23. static const char* const keywords4Char[] =
  24. { "then", "true", "else", nullptr };
  25. static const char* const keywords5Char[] =
  26. { "false", "local", "until", "while", "break", nullptr };
  27. static const char* const keywords6Char[] =
  28. { "repeat", "return", "elseif", nullptr};
  29. static const char* const keywordsOther[] =
  30. { "function", "@interface", "@end", "@synthesize", "@dynamic", "@public",
  31. "@private", "@property", "@protected", "@class", nullptr };
  32. const char* const* k;
  33. switch (tokenLength)
  34. {
  35. case 2: k = keywords2Char; break;
  36. case 3: k = keywords3Char; break;
  37. case 4: k = keywords4Char; break;
  38. case 5: k = keywords5Char; break;
  39. case 6: k = keywords6Char; break;
  40. default:
  41. if (tokenLength < 2 || tokenLength > 16)
  42. return false;
  43. k = keywordsOther;
  44. break;
  45. }
  46. for (int i = 0; k[i] != nullptr; ++i)
  47. if (token.compare (CharPointer_ASCII (k[i])) == 0)
  48. return true;
  49. return false;
  50. }
  51. template <typename Iterator>
  52. static int parseIdentifier (Iterator& source) noexcept
  53. {
  54. int tokenLength = 0;
  55. String::CharPointerType::CharType possibleIdentifier[100];
  56. String::CharPointerType possible (possibleIdentifier);
  57. while (CppTokeniserFunctions::isIdentifierBody (source.peekNextChar()))
  58. {
  59. auto c = source.nextChar();
  60. if (tokenLength < 20)
  61. possible.write (c);
  62. ++tokenLength;
  63. }
  64. if (tokenLength > 1 && tokenLength <= 16)
  65. {
  66. possible.writeNull();
  67. if (isReservedKeyword (String::CharPointerType (possibleIdentifier), tokenLength))
  68. return LuaTokeniser::tokenType_keyword;
  69. }
  70. return LuaTokeniser::tokenType_identifier;
  71. }
  72. template <typename Iterator>
  73. static int readNextToken (Iterator& source)
  74. {
  75. source.skipWhitespace();
  76. auto firstChar = source.peekNextChar();
  77. switch (firstChar)
  78. {
  79. case 0:
  80. break;
  81. case '0': case '1': case '2': case '3': case '4':
  82. case '5': case '6': case '7': case '8': case '9':
  83. case '.':
  84. {
  85. auto result = CppTokeniserFunctions::parseNumber (source);
  86. if (result == LuaTokeniser::tokenType_error)
  87. {
  88. source.skip();
  89. if (firstChar == '.')
  90. return LuaTokeniser::tokenType_punctuation;
  91. }
  92. return result;
  93. }
  94. case ',':
  95. case ';':
  96. case ':':
  97. source.skip();
  98. return LuaTokeniser::tokenType_punctuation;
  99. case '(': case ')':
  100. case '{': case '}':
  101. case '[': case ']':
  102. source.skip();
  103. return LuaTokeniser::tokenType_bracket;
  104. case '"':
  105. case '\'':
  106. CppTokeniserFunctions::skipQuotedString (source);
  107. return LuaTokeniser::tokenType_string;
  108. case '+':
  109. source.skip();
  110. CppTokeniserFunctions::skipIfNextCharMatches (source, '+', '=');
  111. return LuaTokeniser::tokenType_operator;
  112. case '-':
  113. {
  114. source.skip();
  115. auto result = CppTokeniserFunctions::parseNumber (source);
  116. if (source.peekNextChar() == '-')
  117. {
  118. source.skipToEndOfLine();
  119. return LuaTokeniser::tokenType_comment;
  120. }
  121. if (result == LuaTokeniser::tokenType_error)
  122. {
  123. CppTokeniserFunctions::skipIfNextCharMatches (source, '-', '=');
  124. return LuaTokeniser::tokenType_operator;
  125. }
  126. return result;
  127. }
  128. case '*': case '%':
  129. case '=': case '!':
  130. source.skip();
  131. CppTokeniserFunctions::skipIfNextCharMatches (source, '=');
  132. return LuaTokeniser::tokenType_operator;
  133. case '?':
  134. case '~':
  135. source.skip();
  136. return LuaTokeniser::tokenType_operator;
  137. case '<': case '>':
  138. case '|': case '&': case '^':
  139. source.skip();
  140. CppTokeniserFunctions::skipIfNextCharMatches (source, firstChar);
  141. CppTokeniserFunctions::skipIfNextCharMatches (source, '=');
  142. return LuaTokeniser::tokenType_operator;
  143. default:
  144. if (CppTokeniserFunctions::isIdentifierStart (firstChar))
  145. return parseIdentifier (source);
  146. source.skip();
  147. break;
  148. }
  149. return LuaTokeniser::tokenType_error;
  150. }
  151. };
  152. //==============================================================================
  153. LuaTokeniser::LuaTokeniser() {}
  154. LuaTokeniser::~LuaTokeniser() {}
  155. int LuaTokeniser::readNextToken (CodeDocument::Iterator& source)
  156. {
  157. return LuaTokeniserFunctions::readNextToken (source);
  158. }
  159. CodeEditorComponent::ColourScheme LuaTokeniser::getDefaultColourScheme()
  160. {
  161. static const CodeEditorComponent::ColourScheme::TokenType types[] =
  162. {
  163. { "Error", Colour (0xffcc0000) },
  164. { "Comment", Colour (0xff3c3c3c) },
  165. { "Keyword", Colour (0xff0000cc) },
  166. { "Operator", Colour (0xff225500) },
  167. { "Identifier", Colour (0xff000000) },
  168. { "Integer", Colour (0xff880000) },
  169. { "Float", Colour (0xff885500) },
  170. { "String", Colour (0xff990099) },
  171. { "Bracket", Colour (0xff000055) },
  172. { "Punctuation", Colour (0xff004400) }
  173. };
  174. CodeEditorComponent::ColourScheme cs;
  175. for (auto& t : types)
  176. cs.set (t.name, Colour (t.colour));
  177. return cs;
  178. }
  179. } // namespace juce