Blender V5.3
text_format_py.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
8
9#include <cstring>
10
11#include "BLI_string_utf8.hh"
12
13#include "DNA_space_types.h"
14#include "DNA_text_types.h"
15
16#include "BKE_text.h"
17
18#include "text_format.hh"
19
20namespace blender {
21
22/* -------------------------------------------------------------------- */
25
54 /* Force single column, sorted list. */
55 /* clang-format off */
56 "and",
57 "as",
58 "assert",
59 "async",
60 "await",
61 "break",
62 "case",
63 "continue",
64 "del",
65 "elif",
66 "else",
67 "except",
68 "finally",
69 "for",
70 "from",
71 "global",
72 "if",
73 "import",
74 "in",
75 "is",
76 "lambda",
77 "match",
78 "nonlocal",
79 "not",
80 "or",
81 "pass",
82 "raise",
83 "return",
84 "try",
85 "while",
86 "with",
87 "yield",
88 /* clang-format on */
89};
93
96 /* Force single column, sorted list. */
97 /* clang-format off */
98 "class",
99 "def",
100 /* clang-format on */
101};
104
106static const char *text_format_py_literals_bool_data[] = {
107 /* Force single column, sorted list. */
108 /* clang-format off */
109 "False",
110 "None",
111 "True",
112 /* clang-format on */
113};
116
118
119/* -------------------------------------------------------------------- */
122
123static int txtfmt_py_find_builtinfunc(const char *string)
124{
126
127 /* If next source char is an identifier (eg. `i` in "definite") no match. */
128 if (i == 0 || text_check_identifier(string[i])) {
129 return -1;
130 }
131 return i;
132}
133
134static int txtfmt_py_find_specialvar(const char *string)
135{
137
138 /* If next source char is an identifier (eg. `i` in "definite") no match. */
139 if (i == 0 || text_check_identifier(string[i])) {
140 return -1;
141 }
142 return i;
143}
144
145static int txtfmt_py_find_decorator(const char *string)
146{
147 if (string[0] != '@') {
148 return -1;
149 }
150 if (!text_check_identifier(string[1])) {
151 return -1;
152 }
153 /* Interpret as matrix multiplication when followed by whitespace. */
154 if (text_check_whitespace(string[1])) {
155 return -1;
156 }
157
158 int i = 1;
159 while (text_check_identifier(string[i])) {
160 i++;
161 }
162 return i;
163}
164
165static int txtfmt_py_find_bool(const char *string)
166{
168
169 /* If next source char is an identifier (eg. `i` in "Nonetheless") no match. */
170 if (i == 0 || text_check_identifier(string[i])) {
171 return -1;
172 }
173 return i;
174}
175
176/* Numeral character matching. */
177#define TXTFMT_PY_NUMERAL_STRING_COUNT_IMPL(txtfmt_py_numeral_char_is_fn) \
178 { \
179 uint count = 0; \
180 for (; txtfmt_py_numeral_char_is_fn(*string); string += 1) { \
181 count += 1; \
182 } \
183 return count; \
184 } \
185 ((void)0)
186
187/* Binary. */
188static bool txtfmt_py_numeral_char_is_binary(const char c)
189{
190 return ELEM(c, '0', '1') || (c == '_');
191}
196
197/* Octal. */
198static bool txtfmt_py_numeral_char_is_octal(const char c)
199{
200 return (c >= '0' && c <= '7') || (c == '_');
201}
206
207/* Decimal. */
208static bool txtfmt_py_numeral_char_is_decimal(const char c)
209{
210 return (c >= '0' && c <= '9') || (c == '_');
211}
216
217/* Hexadecimal. */
219{
220 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c == '_');
221}
226
227/* Zeros. */
228static bool txtfmt_py_numeral_char_is_zero(const char c)
229{
230 return ELEM(c, '0', '_');
231}
236
237#undef TXTFMT_PY_NUMERAL_STRING_COUNT_IMPL
238
239static int txtfmt_py_find_numeral_inner(const char *string)
240{
241 if (string == nullptr || *string == '\0') {
242 return -1;
243 }
244
245 const char first = *string, second = *(string + 1);
246
247 /* Decimal dot must be followed by a digit, any decimal digit.
248 * Note that there can be any number of leading zeros after the decimal point
249 * (leading zeros are not allowed in integers). */
250 if (first == '.') {
251 if (text_check_digit(second)) {
252 return 1 + txtfmt_py_numeral_string_count_decimal(string + 1);
253 }
254 }
255 else if (first == '0') {
256 /* Numerals starting with `0x` or `0X` is followed by hexadecimal digits. */
257 if (ELEM(second, 'x', 'X')) {
258 return 2 + txtfmt_py_numeral_string_count_hexadecimal(string + 2);
259 }
260 /* Numerals starting with `0o` or `0O` is followed by octal digits. */
261 if (ELEM(second, 'o', 'O')) {
262 return 2 + txtfmt_py_numeral_string_count_octal(string + 2);
263 }
264 /* Numerals starting with `0b` or `0B` is followed by binary digits. */
265 if (ELEM(second, 'b', 'B')) {
266 return 2 + txtfmt_py_numeral_string_count_binary(string + 2);
267 }
268 /* Other numerals starting with `0` can be followed by any number of `0` characters. */
269 if (ELEM(second, '0', '_')) {
270 return 2 + txtfmt_py_numeral_string_count_zeros(string + 2);
271 }
272 }
273 /* Any non-zero digit is the start of a decimal number. */
274 else if (first > '0' && first <= '9') {
275 return 1 + txtfmt_py_numeral_string_count_decimal(string + 1);
276 }
277 /* A single zero is also allowed. */
278 return (first == '0') ? 1 : 0;
279}
280
281static int txtfmt_py_literal_numeral(const char *string, char prev_fmt)
282{
283 if (string == nullptr || *string == '\0') {
284 return -1;
285 }
286
287 const char first = *string, second = *(string + 1);
288
289 if (prev_fmt == FMT_TYPE_NUMERAL) {
290 /* Previous was a number; if immediately followed by `e` or `E` and a digit,
291 * it's a base 10 exponent (scientific notation). */
292 if (ELEM(first, 'e', 'E') && (text_check_digit(second) || second == '-')) {
293 return 1 + txtfmt_py_find_numeral_inner(string + 1);
294 }
295 /* Previous was a number; if immediately followed by `.` it's a floating point decimal number.
296 * NOTE: keep the decimal point, it's needed to allow leading zeros. */
297 if (first == '.') {
298 return txtfmt_py_find_numeral_inner(string);
299 }
300 /* "Imaginary" part of a complex number ends with `j`. */
301 if (ELEM(first, 'j', 'J') && !text_check_digit(second)) {
302 return 1;
303 }
304 }
305 else if ((prev_fmt != FMT_TYPE_DEFAULT) &&
306 (text_check_digit(first) || (first == '.' && text_check_digit(second))))
307 {
308 /* New numeral, starting with a digit or a decimal point followed by a digit. */
309 return txtfmt_py_find_numeral_inner(string);
310 }
311 /* Not a literal numeral. */
312 return 0;
313}
314
315static char txtfmt_py_format_identifier(const char *str)
316{
317 char fmt;
318
319 /* Keep aligned args for readability. */
320 /* clang-format off */
321
323 } else if (txtfmt_py_find_builtinfunc(str) != -1) { fmt = FMT_TYPE_KEYWORD;
324 } else if (txtfmt_py_find_decorator(str) != -1) { fmt = FMT_TYPE_RESERVED;
325 } else { fmt = FMT_TYPE_DEFAULT;
326 }
327
328 /* clang-format on */
329 return fmt;
330}
331
333
334/* -------------------------------------------------------------------- */
337
338static void txtfmt_py_format_line(SpaceText *st, TextLine *line, const bool do_next)
339{
340 FlattenString fs;
341 const char *str;
342 char *fmt;
343 char cont_orig, cont, find, prev = ' ';
344 int len, i;
345
346 /* Get continuation from previous line. */
347 if (line->prev && line->prev->format != nullptr) {
348 fmt = line->prev->format;
349 cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator. */
350 BLI_assert((FMT_CONT_ALL & cont) == cont);
351 /* So slashes beginning on continuation display properly, see: #118767. */
353 prev = FMT_TYPE_STRING;
354 }
355 }
356 else {
357 cont = FMT_CONT_NOP;
358 }
359
360 /* Get original continuation from this line. */
361 if (line->format != nullptr) {
362 fmt = line->format;
363 cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator. */
364 BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig);
365 }
366 else {
367 cont_orig = 0xFF;
368 }
369
370 len = flatten_string(st, &fs, line->line);
371 str = fs.buf;
372 if (!text_check_format_len(line, len)) {
374 return;
375 }
376 fmt = line->format;
377
378 while (*str) {
379 /* Handle escape sequences by skipping both \ and next char. */
380 if (*str == '\\') {
381 *fmt = prev;
382 fmt++;
383 str++;
384 if (*str == '\0') {
385 break;
386 }
387 *fmt = prev;
388 fmt++;
390 continue;
391 }
392 /* Handle continuations. */
393 if (cont) {
394 /* Triple strings (`"""..."""` or `'''...'''`). */
395 if (cont & FMT_CONT_TRIPLE) {
396 find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\'';
397 if (*str == find && *(str + 1) == find && *(str + 2) == find) {
398 *fmt = FMT_TYPE_STRING;
399 fmt++;
400 str++;
401 *fmt = FMT_TYPE_STRING;
402 fmt++;
403 str++;
404 cont = FMT_CONT_NOP;
405 }
406 /* Handle other strings. */
407 }
408 else {
409 find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\'';
410 if (*str == find) {
411 cont = FMT_CONT_NOP;
412 }
413 }
414
415 *fmt = FMT_TYPE_STRING;
417 }
418 /* Not in a string... */
419 else {
420 /* Deal with comments first. */
421 if (*str == '#') {
422 /* Fill the remaining line. */
423 text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - int(fmt - line->format));
424 }
425 else if (ELEM(*str, '"', '\'')) {
426 /* Strings. */
427 find = *str;
428 cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE;
429 if (*(str + 1) == find && *(str + 2) == find) {
430 *fmt = FMT_TYPE_STRING;
431 fmt++;
432 str++;
433 *fmt = FMT_TYPE_STRING;
434 fmt++;
435 str++;
436 cont |= FMT_CONT_TRIPLE;
437 }
438 *fmt = FMT_TYPE_STRING;
439 }
440 else if (ELEM(*str, 'f', 'F', 'r', 'R', 'u', 'U') && ELEM(*(str + 1), '"', '\'')) {
441 /* Strings with single letter prefixes (f-strings, raw strings, and unicode strings).
442 * Format the prefix as part of the string. */
443 *fmt = FMT_TYPE_STRING;
444 fmt++;
445 str++;
446 find = *str;
447 cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE;
448 if (*(str + 1) == find && *(str + 2) == find) {
449 *fmt = FMT_TYPE_STRING;
450 fmt++;
451 str++;
452 *fmt = FMT_TYPE_STRING;
453 fmt++;
454 str++;
455 cont |= FMT_CONT_TRIPLE;
456 }
457 *fmt = FMT_TYPE_STRING;
458 }
459 else if (((ELEM(*str, 'f', 'F') && ELEM(*(str + 1), 'r', 'R')) ||
460 (ELEM(*str, 'r', 'R') && ELEM(*(str + 1), 'f', 'F'))) &&
461 ELEM(*(str + 2), '"', '\''))
462 {
463 /* Strings with two letter prefixes (raw f-strings).
464 * Format the prefix as part of the string. */
465 *fmt = FMT_TYPE_STRING;
466 fmt++;
467 str++;
468 *fmt = FMT_TYPE_STRING;
469 fmt++;
470 str++;
471 find = *str;
472 cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE;
473 if (*(str + 1) == find && *(str + 2) == find) {
474 *fmt = FMT_TYPE_STRING;
475 fmt++;
476 str++;
477 *fmt = FMT_TYPE_STRING;
478 fmt++;
479 str++;
480 cont |= FMT_CONT_TRIPLE;
481 }
482 *fmt = FMT_TYPE_STRING;
483 }
484 /* White-space (all white-space has been converted to spaces). */
485 else if (*str == ' ') {
486 *fmt = FMT_TYPE_WHITESPACE;
487 }
488 /* Literal numerals, "numbers". */
489 else if ((i = txtfmt_py_literal_numeral(str, prev)) > 0) {
491 }
492 /* Booleans. */
493 else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_py_find_bool(str)) != -1) {
494 if (i > 0) {
496 }
497 else {
499 *fmt = FMT_TYPE_DEFAULT;
500 }
501 }
502 /* Punctuation. */
503 else if ((*str != '@') && text_check_delim(*str)) {
504 *fmt = FMT_TYPE_SYMBOL;
505 }
506 /* Identifiers and other text (no previous white-space/delimiters so text continues). */
507 else if (prev == FMT_TYPE_DEFAULT) {
509 *fmt = FMT_TYPE_DEFAULT;
510 }
511 /* Not white-space, a digit, punctuation, or continuing text.
512 * Must be new, check for special words. */
513 else {
514 /* Keep aligned arguments for readability. */
515 /* clang-format off */
516
517 /* Special vars(v) or built-in keywords(b). */
518 /* keep in sync with `txtfmt_py_format_identifier()`. */
519 if ((i = txtfmt_py_find_specialvar(str)) != -1) { prev = FMT_TYPE_SPECIAL;
520 } else if ((i = txtfmt_py_find_builtinfunc(str)) != -1) { prev = FMT_TYPE_KEYWORD;
521 } else if ((i = txtfmt_py_find_decorator(str)) != -1) { prev = FMT_TYPE_DIRECTIVE;
522 }
523
524 /* clang-format on */
525
526 if (i > 0) {
527 if (prev == FMT_TYPE_DIRECTIVE) { /* May contain UTF8. */
528 text_format_fill(&str, &fmt, prev, i);
529 }
530 else {
531 text_format_fill_ascii(&str, &fmt, prev, i);
532 }
533 }
534 else {
536 *fmt = FMT_TYPE_DEFAULT;
537 }
538 }
539 }
540 prev = *fmt;
541 fmt++;
542 str++;
543 }
544
545 /* Terminate and add continuation char. */
546 *fmt = '\0';
547 fmt++;
548 *fmt = cont;
549
550 /* If continuation has changed and we're allowed, process the next line. */
551 if (cont != cont_orig && do_next && line->next) {
552 txtfmt_py_format_line(st, line->next, do_next);
553 }
554
556}
557
559
560/* -------------------------------------------------------------------- */
563
580
582
583} // 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.
static int txtfmt_py_find_decorator(const char *string)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static const char * text_format_py_literals_bool_data[]
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 bool txtfmt_py_numeral_char_is_zero(const char c)
unsigned int uint
Definition BKE_text.h:55
void ED_text_format_register_py()
static int txtfmt_py_find_builtinfunc(const char *string)
static bool txtfmt_py_numeral_char_is_decimal(const char c)
bool text_format_string_literals_check_sorted_array(const Span< const char * > string_literals)
static bool txtfmt_py_numeral_char_is_hexadecimal(const char c)
static const Span< const char * > text_format_py_literals_builtinfunc(text_format_py_literals_builtinfunc_data, ARRAY_SIZE(text_format_py_literals_builtinfunc_data))
static uint txtfmt_py_numeral_string_count_octal(const char *string)
static uint txtfmt_py_numeral_string_count_binary(const char *string)
int text_format_string_literal_find(const Span< const char * > string_literals, const char *text)
static bool txtfmt_py_numeral_char_is_octal(const char c)
bool text_check_delim(char ch)
Definition text.cc:2296
static const Span< const char * > text_format_py_literals_specialvar(text_format_py_literals_specialvar_data, ARRAY_SIZE(text_format_py_literals_specialvar_data))
void flatten_string_free(FlattenString *fs)
static char txtfmt_py_format_identifier(const char *str)
static uint txtfmt_py_numeral_string_count_zeros(const char *string)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static bool txtfmt_py_numeral_char_is_binary(const char c)
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 const Span< const char * > text_format_py_literals_bool(text_format_py_literals_bool_data, ARRAY_SIZE(text_format_py_literals_bool_data))
static int txtfmt_py_literal_numeral(const char *string, char prev_fmt)
static const char * text_format_py_literals_builtinfunc_data[]
@ FMT_CONT_QUOTEDOUBLE
@ FMT_CONT_QUOTESINGLE
@ FMT_CONT_TRIPLE
static const char * text_format_py_literals_specialvar_data[]
void ED_text_format_register(TextFormatType *tft)
static int txtfmt_py_find_numeral_inner(const char *string)
static int txtfmt_py_find_bool(const char *string)
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
static int txtfmt_py_find_specialvar(const char *string)
static uint txtfmt_py_numeral_string_count_hexadecimal(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)
static void txtfmt_py_format_line(SpaceText *st, TextLine *line, const bool do_next)
static uint txtfmt_py_numeral_string_count_decimal(const char *string)
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
#define TXTFMT_PY_NUMERAL_STRING_COUNT_IMPL(txtfmt_py_numeral_char_is_fn)