Blender V5.3
text_format_osl.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cstring>
13
14#include "BLI_string_utf8.hh"
15
16#include "DNA_space_types.h"
17#include "DNA_text_types.h"
18
19#include "BKE_text.h"
20
21#include "text_format.hh"
22
23namespace blender {
24
25/* -------------------------------------------------------------------- */
28
35 /* Force single column, sorted list. */
36 /* clang-format off */
37 "break",
38 "closure",
39 "color",
40 "continue",
41 "do",
42 "else",
43 "emit",
44 "float",
45 "for",
46 "if",
47 "illuminance",
48 "illuminate",
49 "int",
50 "matrix",
51 "normal",
52 "output",
53 "point",
54 "public",
55 "return",
56 "string",
57 "struct",
58 "vector",
59 "void",
60 "while",
61 /* clang-format on */
62};
66
73 /* Force single column, sorted list. */
74 /* clang-format off */
75 "bool",
76 "case",
77 "catch",
78 "char",
79 "const",
80 "default",
81 "delete",
82 "double",
83 "enum",
84 "extern",
85 "false",
86 "friend",
87 "goto",
88 "inline",
89 "long",
90 "new",
91 "operator",
92 "private",
93 "protected",
94 "short",
95 "signed",
96 "sizeof",
97 "static",
98 "switch",
99 "template",
100 "this",
101 "throw",
102 "true",
103 "try",
104 "typedef",
105 "uniform",
106 "union",
107 "unsigned",
108 "varying",
109 "virtual",
110 "volatile",
111 /* clang-format on */
112};
115
118 /* Force single column, sorted list. */
119 /* clang-format off */
120 "displacement",
121 "shader",
122 "surface",
123 "volume",
124 /* clang-format on */
125};
129
131
132/* -------------------------------------------------------------------- */
135
136static int txtfmt_osl_find_builtinfunc(const char *string)
137{
139
140 /* If next source char is an identifier (eg. `i` in "definite") no match. */
141 if (i == 0 || text_check_identifier(string[i])) {
142 return -1;
143 }
144 return i;
145}
146
147static int txtfmt_osl_find_reserved(const char *string)
148{
150
151 /* If next source char is an identifier (eg. `i` in "definite") no match. */
152 if (i == 0 || text_check_identifier(string[i])) {
153 return -1;
154 }
155 return i;
156}
157
158static int txtfmt_osl_find_specialvar(const char *string)
159{
161
162 /* If next source char is an identifier (eg. `i` in "definite") no match. */
163 if (i == 0 || text_check_identifier(string[i])) {
164 return -1;
165 }
166 return i;
167}
168
169/* Matches Python's #txtfmt_osl_find_decorator. */
170static int txtfmt_osl_find_preprocessor(const char *string)
171{
172 if (string[0] == '#') {
173 int i = 1;
174 /* White-space is ok `# foo`. */
175 while (text_check_whitespace(string[i])) {
176 i++;
177 }
178 while (text_check_identifier(string[i])) {
179 i++;
180 }
181 return i;
182 }
183 return -1;
184}
185
186static char txtfmt_osl_format_identifier(const char *str)
187{
188 char fmt;
189
190 /* Keep aligned args for readability. */
191 /* clang-format off */
192
194 } else if (txtfmt_osl_find_builtinfunc(str) != -1) { fmt = FMT_TYPE_KEYWORD;
195 } else if (txtfmt_osl_find_reserved(str) != -1) { fmt = FMT_TYPE_RESERVED;
196 } else if (txtfmt_osl_find_preprocessor(str) != -1) { fmt = FMT_TYPE_DIRECTIVE;
197 } else { fmt = FMT_TYPE_DEFAULT;
198 }
199
200 /* clang-format on */
201
202 return fmt;
203}
204
206
207/* -------------------------------------------------------------------- */
210
211static void txtfmt_osl_format_line(SpaceText *st, TextLine *line, const bool do_next)
212{
213 FlattenString fs;
214 const char *str;
215 char *fmt;
216 char cont_orig, cont, find, prev = ' ';
217 int len, i;
218
219 /* Get continuation from previous line. */
220 if (line->prev && line->prev->format != nullptr) {
221 fmt = line->prev->format;
222 cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator. */
223 BLI_assert((FMT_CONT_ALL & cont) == cont);
224 }
225 else {
226 cont = FMT_CONT_NOP;
227 }
228
229 /* Get original continuation from this line. */
230 if (line->format != nullptr) {
231 fmt = line->format;
232 cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator. */
233 BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig);
234 }
235 else {
236 cont_orig = 0xFF;
237 }
238
239 len = flatten_string(st, &fs, line->line);
240 str = fs.buf;
241 if (!text_check_format_len(line, len)) {
243 return;
244 }
245 fmt = line->format;
246
247 while (*str) {
248 /* Handle escape sequences by skipping both \ and next char. */
249 if (*str == '\\') {
250 *fmt = prev;
251 fmt++;
252 str++;
253 if (*str == '\0') {
254 break;
255 }
256 *fmt = prev;
257 fmt++;
259 continue;
260 }
261 /* Handle continuations. */
262 if (cont) {
263 /* C-Style comments. */
264 if (cont & FMT_CONT_COMMENT_C) {
265 if (*str == '*' && *(str + 1) == '/') {
266 *fmt = FMT_TYPE_COMMENT;
267 fmt++;
268 str++;
269 *fmt = FMT_TYPE_COMMENT;
270 cont = FMT_CONT_NOP;
271 }
272 else {
273 *fmt = FMT_TYPE_COMMENT;
274 }
275 /* Handle other comments. */
276 }
277 else {
278 find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\'';
279 if (*str == find) {
280 cont = 0;
281 }
282 *fmt = FMT_TYPE_STRING;
283 }
284
286 }
287 /* Not in a string... */
288 else {
289 /* Deal with comments first. */
290 if (*str == '/' && *(str + 1) == '/') {
291 /* Fill the remaining line. */
292 text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - int(fmt - line->format));
293 }
294 /* C-Style (multi-line) comments. */
295 else if (*str == '/' && *(str + 1) == '*') {
296 cont = FMT_CONT_COMMENT_C;
297 *fmt = FMT_TYPE_COMMENT;
298 fmt++;
299 str++;
300 *fmt = FMT_TYPE_COMMENT;
301 }
302 else if (ELEM(*str, '"', '\'')) {
303 /* Strings. */
304 find = *str;
305 cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE;
306 *fmt = FMT_TYPE_STRING;
307 }
308 /* White-space (all white-space has been converted to spaces). */
309 else if (*str == ' ') {
310 *fmt = FMT_TYPE_WHITESPACE;
311 }
312 /* Numbers (digits not part of an identifier and periods followed by digits). */
313 else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) ||
314 (*str == '.' && text_check_digit(*(str + 1))))
315 {
316 *fmt = FMT_TYPE_NUMERAL;
317 }
318 /* Punctuation. */
319 else if ((*str != '#') && text_check_delim(*str)) {
320 *fmt = FMT_TYPE_SYMBOL;
321 }
322 /* Identifiers and other text (no previous white-space or delimiters. so text continues). */
323 else if (prev == FMT_TYPE_DEFAULT) {
325 *fmt = FMT_TYPE_DEFAULT;
326 }
327 /* Not white-space, a digit, punctuation, or continuing text.
328 * Must be new, check for special words. */
329 else {
330 /* Keep aligned arguments for readability. */
331 /* clang-format off */
332
333 /* Special vars(v) or built-in keywords(b). */
334 /* keep in sync with `txtfmt_osl_format_identifier()`. */
335 if ((i = txtfmt_osl_find_specialvar(str)) != -1) { prev = FMT_TYPE_SPECIAL;
336 } else if ((i = txtfmt_osl_find_builtinfunc(str)) != -1) { prev = FMT_TYPE_KEYWORD;
337 } else if ((i = txtfmt_osl_find_reserved(str)) != -1) { prev = FMT_TYPE_RESERVED;
338 } else if ((i = txtfmt_osl_find_preprocessor(str)) != -1) { prev = FMT_TYPE_DIRECTIVE;
339 }
340 /* clang-format on */
341
342 if (i > 0) {
343 if (prev == FMT_TYPE_DIRECTIVE) { /* May contain UTF8. */
344 text_format_fill(&str, &fmt, prev, i);
345 }
346 else {
347 text_format_fill_ascii(&str, &fmt, prev, i);
348 }
349 }
350 else {
352 *fmt = FMT_TYPE_DEFAULT;
353 }
354 }
355 }
356 prev = *fmt;
357 fmt++;
358 str++;
359 }
360
361 /* Terminate and add continuation char. */
362 *fmt = '\0';
363 fmt++;
364 *fmt = cont;
365
366 /* If continuation has changed and we're allowed, process the next line. */
367 if (cont != cont_orig && do_next && line->next) {
368 txtfmt_osl_format_line(st, line->next, do_next);
369 }
370
372}
373
375
376/* -------------------------------------------------------------------- */
379
396
398
399} // namespace blender
#define ELEM(...)
#define str(s)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void ED_text_format_register_osl()
int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
bool text_check_identifier(char ch)
Definition text.cc:2323
bool text_check_digit(char ch)
Definition text.cc:2312
int text_check_format_len(TextLine *line, uint len)
static const Span< const char * > text_format_osl_literals_builtinfunc(text_format_osl_literals_builtinfunc_data, ARRAY_SIZE(text_format_osl_literals_builtinfunc_data))
static void txtfmt_osl_format_line(SpaceText *st, TextLine *line, const bool do_next)
bool text_format_string_literals_check_sorted_array(const Span< const char * > string_literals)
static const Span< const char * > text_format_osl_literals_specialvar(text_format_osl_literals_specialvar_data, ARRAY_SIZE(text_format_osl_literals_specialvar_data))
static char txtfmt_osl_format_identifier(const char *str)
int text_format_string_literal_find(const Span< const char * > string_literals, const char *text)
bool text_check_delim(char ch)
Definition text.cc:2296
void flatten_string_free(FlattenString *fs)
static const char * text_format_osl_literals_reserved_data[]
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static const char * text_format_osl_literals_builtinfunc_data[]
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool text_check_whitespace(char ch)
Definition text.cc:2378
static int txtfmt_osl_find_preprocessor(const char *string)
static int txtfmt_osl_find_builtinfunc(const char *string)
@ FMT_CONT_COMMENT_C
@ FMT_CONT_QUOTEDOUBLE
@ FMT_CONT_QUOTESINGLE
static int txtfmt_osl_find_reserved(const char *string)
void ED_text_format_register(TextFormatType *tft)
static const char * text_format_osl_literals_specialvar_data[]
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
static const Span< const char * > text_format_osl_literals_reserved(text_format_osl_literals_reserved_data, ARRAY_SIZE(text_format_osl_literals_reserved_data))
static int txtfmt_osl_find_specialvar(const char *string)
@ FMT_TYPE_KEYWORD
@ FMT_TYPE_RESERVED
@ FMT_TYPE_WHITESPACE
@ FMT_TYPE_COMMENT
@ FMT_TYPE_SPECIAL
@ FMT_TYPE_DIRECTIVE
@ FMT_TYPE_STRING
@ FMT_TYPE_NUMERAL
@ FMT_TYPE_SYMBOL
@ FMT_TYPE_DEFAULT
void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
const char * comment_line
char(*) format_identifier(const char *string)
void(*) format_line(SpaceText *st, TextLine *line, bool do_next)
struct TextLine * prev
struct TextLine * next
#define FMT_CONT_ALL