JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_LuaCodeTokeniser.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30{
31 static bool isReservedKeyword (String::CharPointerType token, const int tokenLength) noexcept
32 {
33 static const char* const keywords2Char[] =
34 { "if", "or", "in", "do", nullptr };
35
36 static const char* const keywords3Char[] =
37 { "and", "end", "for", "nil", "not", nullptr };
38
39 static const char* const keywords4Char[] =
40 { "then", "true", "else", nullptr };
41
42 static const char* const keywords5Char[] =
43 { "false", "local", "until", "while", "break", nullptr };
44
45 static const char* const keywords6Char[] =
46 { "repeat", "return", "elseif", nullptr};
47
48 static const char* const keywordsOther[] =
49 { "function", "@interface", "@end", "@synthesize", "@dynamic", "@public",
50 "@private", "@property", "@protected", "@class", nullptr };
51
52 const char* const* k;
53
54 switch (tokenLength)
55 {
56 case 2: k = keywords2Char; break;
57 case 3: k = keywords3Char; break;
58 case 4: k = keywords4Char; break;
59 case 5: k = keywords5Char; break;
60 case 6: k = keywords6Char; break;
61
62 default:
64 return false;
65
66 k = keywordsOther;
67 break;
68 }
69
70 for (int i = 0; k[i] != nullptr; ++i)
71 if (token.compare (CharPointer_ASCII (k[i])) == 0)
72 return true;
73
74 return false;
75 }
76
77 template <typename Iterator>
78 static int parseIdentifier (Iterator& source) noexcept
79 {
80 int tokenLength = 0;
81 String::CharPointerType::CharType possibleIdentifier[100] = {};
83
84 while (CppTokeniserFunctions::isIdentifierBody (source.peekNextChar()))
85 {
86 auto c = source.nextChar();
87
88 if (tokenLength < 20)
89 possible.write (c);
90
92 }
93
94 if (tokenLength > 1 && tokenLength <= 16)
95 {
96 possible.writeNull();
97
99 return LuaTokeniser::tokenType_keyword;
100 }
101
102 return LuaTokeniser::tokenType_identifier;
103 }
104
105 template <typename Iterator>
106 static int readNextToken (Iterator& source)
107 {
108 source.skipWhitespace();
109
110 auto firstChar = source.peekNextChar();
111
112 switch (firstChar)
113 {
114 case 0:
115 break;
116
117 case '0': case '1': case '2': case '3': case '4':
118 case '5': case '6': case '7': case '8': case '9':
119 case '.':
120 {
121 auto result = CppTokeniserFunctions::parseNumber (source);
122
123 if (result == LuaTokeniser::tokenType_error)
124 {
125 source.skip();
126
127 if (firstChar == '.')
128 return LuaTokeniser::tokenType_punctuation;
129 }
130
131 return result;
132 }
133
134 case ',':
135 case ';':
136 case ':':
137 source.skip();
138 return LuaTokeniser::tokenType_punctuation;
139
140 case '(': case ')':
141 case '{': case '}':
142 case '[': case ']':
143 source.skip();
144 return LuaTokeniser::tokenType_bracket;
145
146 case '"':
147 case '\'':
148 CppTokeniserFunctions::skipQuotedString (source);
149 return LuaTokeniser::tokenType_string;
150
151 case '+':
152 source.skip();
153 CppTokeniserFunctions::skipIfNextCharMatches (source, '+', '=');
154 return LuaTokeniser::tokenType_operator;
155
156 case '-':
157 {
158 source.skip();
159 auto result = CppTokeniserFunctions::parseNumber (source);
160
161 if (source.peekNextChar() == '-')
162 {
163 source.skipToEndOfLine();
164 return LuaTokeniser::tokenType_comment;
165 }
166
167 if (result == LuaTokeniser::tokenType_error)
168 {
169 CppTokeniserFunctions::skipIfNextCharMatches (source, '-', '=');
170 return LuaTokeniser::tokenType_operator;
171 }
172
173 return result;
174 }
175
176 case '*': case '%':
177 case '=': case '!':
178 source.skip();
179 CppTokeniserFunctions::skipIfNextCharMatches (source, '=');
180 return LuaTokeniser::tokenType_operator;
181
182 case '?':
183 case '~':
184 source.skip();
185 return LuaTokeniser::tokenType_operator;
186
187 case '<': case '>':
188 case '|': case '&': case '^':
189 source.skip();
190 CppTokeniserFunctions::skipIfNextCharMatches (source, firstChar);
191 CppTokeniserFunctions::skipIfNextCharMatches (source, '=');
192 return LuaTokeniser::tokenType_operator;
193
194 default:
195 if (CppTokeniserFunctions::isIdentifierStart (firstChar))
196 return parseIdentifier (source);
197
198 source.skip();
199 break;
200 }
201
202 return LuaTokeniser::tokenType_error;
203 }
204};
205
206//==============================================================================
207LuaTokeniser::LuaTokeniser() {}
208LuaTokeniser::~LuaTokeniser() {}
209
211{
212 return LuaTokeniserFunctions::readNextToken (source);
213}
214
216{
218 {
219 { "Error", Colour (0xffcc0000) },
220 { "Comment", Colour (0xff3c3c3c) },
221 { "Keyword", Colour (0xff0000cc) },
222 { "Operator", Colour (0xff225500) },
223 { "Identifier", Colour (0xff000000) },
224 { "Integer", Colour (0xff880000) },
225 { "Float", Colour (0xff885500) },
226 { "String", Colour (0xff990099) },
227 { "Bracket", Colour (0xff000055) },
228 { "Punctuation", Colour (0xff004400) }
229 };
230
232
233 for (auto& t : types)
234 cs.set (t.name, Colour (t.colour));
235
236 return cs;
237}
238
239} // namespace juce
Wraps a pointer to a null-terminated ASCII character string, and provides various methods to operate ...
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
Iterates the text in a CodeDocument.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
int readNextToken(CodeDocument::Iterator &) override
Reads the next token from the source and returns its token type.
CodeEditorComponent::ColourScheme getDefaultColourScheme() override
Returns a suggested syntax highlighting colour scheme.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
Defines a syntax highlighting colour scheme.