Debian Codegraph @

modules/lsp-runtime-lib/include/private/fmt/xpm/Tokenizer.h

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 21 янв. 2026 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef PRIVATE_FMT_XPM_TOKENIZER_H_
23 #define PRIVATE_FMT_XPM_TOKENIZER_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26
27 #include <lsp-plug.in/io/IInStream.h>
28 #include <lsp-plug.in/common/alloc.h>
29 #include <lsp-plug.in/stdlib/string.h>
30
31 #include <private/fmt/xpm/utils.h>
32
33 namespace lsp
34 {
35 namespace xpm
36 {
37 enum token_type_t
38 {
39 TOK_INVALID,
40 TOK_DEFINE,
41 TOK_XPM2_SIG,
42 TOK_XPM3_SIG,
43 TOK_STRING,
44 TOK_ASTERISK,
45 TOK_LBRACE,
46 TOK_RBRACE,
47 TOK_LQBRACKET,
48 TOK_RQBRACKET,
49 TOK_SEMICOLON,
50 TOK_COMMA,
51 TOK_ASSIGN,
52 TOK_IDENTIFIER,
53 TOK_INTEGER,
54 TOK_COMMENT,
55 };
56
57 /**
58 * Parser for XPM1 built-in format
59 */
60 class LSP_HIDDEN_MODIFIER Tokenizer
61 {
62 private:
63 static constexpr size_t BUF_SIZE = 0x400;
64
65 private:
66 typedef struct token_t
67 {
68 token_type_t type;
69 char *value;
70 size_t length;
71 size_t capacity;
72 bool unread;
73 } token_t;
74
75 private:
76 io::IInStream *pIS;
77 size_t nWFlags;
78 char *pBuffer;
79 int nLastChar;
80 size_t nSize;
81 size_t nOffset;
82 token_t sToken;
83 size_t nLineNum;
84 bool bNewLine;
85
86 private:
87 bool append(char ch)
88 {
89 if (sToken.length >= sToken.capacity)
90 {
91 const size_t new_cap = lsp_max(sToken.capacity + (sToken.capacity >> 1), size_t(0x20));
92 char *ptr = static_cast<char *>(realloc(sToken.value, new_cap));
93 if (ptr == NULL)
94 return false;
95
96 sToken.value = ptr;
97 sToken.capacity = new_cap;
98 }
99
100 sToken.value[sToken.length++] = ch;
101 return true;
102 }
103
104 inline int getch()
105 {
106 // Is there a character in unget buffer?
107 const int ch = nLastChar;
108 if (ch >= 0)
109 {
110 nLastChar = -1;
111 return ch;
112 }
113
114 // Is buffer not empty?
115 if (nOffset >= nSize)
116 {
117 // Read data
118 const ssize_t nread = pIS->read(pBuffer, BUF_SIZE);
119 if (nread < 0)
120 return int(nread);
121
122 nOffset = 0;
123 nSize = nread;
124 }
125
126 return (nOffset < nSize) ? pBuffer[nOffset++] : -STATUS_EOF;
127 }
128
129 inline void ungetch(char ch)
130 {
131 nLastChar = ch;
132 }
133
134 status_t skip_whitespace()
135 {
136 int ch;
137 while ((ch = getch()) >= 0)
138 {
139 if (!is_space(ch))
140 {
141 ungetch(ch);
142 return STATUS_OK;
143 }
144 if (ch == '\n')
145 {
146 ++nLineNum;
147 bNewLine = true;
148 }
149 }
150
151 return (ch == -STATUS_EOF) ? STATUS_OK : status_t(-ch);
152 }
153
154 status_t read_alphadigit(size_t nread)
155 {
156 int ch;
157
158 while ((ch = getch()) >= 0)
159 {
160 if (!is_alphadigit(ch))
161 {
162 ungetch(ch);
163 if (nread <= 0)
164 return STATUS_CORRUPTED_FILE;
165 return (nread > 0) ? STATUS_OK : STATUS_CORRUPTED_FILE;
166 }
167
168 if (!append(ch))
169 return STATUS_NO_MEM;
170 }
171
172 return (nread > 0) ? STATUS_OK : status_t(-ch);
173 }
174
175 status_t read_identifier()
176 {
177 // Read first character
178 int ch = getch();
179 if (ch <= 0)
180 return status_t(-ch);
181 if (!is_alpha(ch))
182 return STATUS_CORRUPTED_FILE;
183 if (!append(ch))
184 return STATUS_NO_MEM;
185
186 status_t res = read_alphadigit(1);
187 return (res == STATUS_EOF) ? STATUS_OK : res;
188 }
189
190 status_t read_integer(size_t nread)
191 {
192 int ch;
193
194 while ((ch = getch()) >= 0)
195 {
196 if (!is_digit(ch))
197 {
198 ungetch(ch);
199 return (nread > 0) ? STATUS_OK : STATUS_CORRUPTED_FILE;
200 }
201
202 if (!append(ch))
203 return STATUS_NO_MEM;
204 }
205
206 return (nread > 0) ? STATUS_OK : status_t(-ch);
207 }
208
209 status_t expect_characters(const char *text)
210 {
211 // Match characters
212 int ch;
213 for ( ; *text != '\0'; ++text)
214 {
215 if ((ch = getch()) < 0)
216 return ch;
217 if (ch != (*text))
218 return STATUS_CORRUPTED_FILE;
219 if (!append(ch))
220 return STATUS_NO_MEM;
221 }
222
223 return STATUS_OK;
224 }
225
226 status_t expect_end_of_line()
227 {
228 int ch;
229
230 // Complete read until the end-of-line
231 while ((ch = getch()) >= 0)
232 {
233 switch (ch)
234 {
235 case '\n': // End of line
236 ++nLineNum;
237 bNewLine = true;
238 return STATUS_OK;
239
240 case '\r': // \r\n sequence
241 if ((ch = getch()) < 0)
242 return ch;
243 if (ch != '\n')
244 return STATUS_CORRUPTED_FILE;
245
246 ++nLineNum;
247 bNewLine = true;
248 return STATUS_OK;
249
250 default: // Only space characters allowed
251 if (!is_space(ch))
252 return STATUS_CORRUPTED_FILE;
253 break;
254 }
255 }
256 return ch;
257 }
258
259 status_t expect_signature(const char *text)
260 {
261 status_t res = expect_characters(text);
262 if (res == STATUS_OK)
263 res = expect_end_of_line();
264 return res;
265 }
266
267 status_t read_comment()
268 {
269 int ch;
270 while ((ch = getch()) >= 0)
271 {
272 if (!append(ch))
273 return STATUS_NO_MEM;
274
275 if (ch == '*') // Possible end of comment?
276 {
277 if ((ch = getch()) < 0)
278 return status_t(-ch);
279
280 if (!append(ch))
281 return STATUS_NO_MEM;
282
283 if (ch == '/')
284 return STATUS_OK;
285 }
286
287 }
288
289 return status_t(-ch);
290 }
291
292 status_t read_c_string()
293 {
294 int ch;
295 while ((ch = getch()) >= 0)
296 {
297 if (ch == '\"') // End of string found
298 return STATUS_OK;
299 else if (ch == '\\') // We do not allow any escape sequences in strings
300 return STATUS_CORRUPTED_FILE;
301
302 if (!append(ch))
303 return STATUS_NO_MEM;
304 }
305
306 return status_t(-ch);
307 }
308
309 public:
310 Tokenizer(io::IInStream *is, size_t flags)
311 {
312 pIS = is;
313 nWFlags = flags;
314 pBuffer = NULL;
315 nLastChar = -1;
316 nSize = 0;
317 nOffset = 0;
318
319 sToken.type = TOK_INVALID;
320 sToken.value = NULL;
321 sToken.length = 0;
322 sToken.capacity = 0;
323 sToken.unread = false;
324 nLineNum = 0;
325
326 bNewLine = true;
327 }
328
329 ~Tokenizer()
330 {
331 close();
332 }
333
334 public:
335 status_t init(size_t prefetch = 0x20)
336 {
337 // Allocate buffer
338 pBuffer = static_cast<char *>(malloc(BUF_SIZE));
339 if (pBuffer == NULL)
340 return STATUS_NO_MEM;
341 nOffset = 0;
342 nSize = 0;
343 nLineNum = 0;
344 nLastChar = -1;
345 bNewLine = true;
346
347 // Prefetch data
348 const size_t to_read = lsp_min(prefetch, BUF_SIZE);
349 if (to_read <= 0)
350 return STATUS_OK;
351
352 const ssize_t nread = pIS->read(pBuffer, to_read);
353 if (nread < 0)
354 return -status_t(nread);
355
356 nSize = nread;
357 return STATUS_OK;
358 }
359
360 status_t close()
361 {
362 // Drop buffer
363 if (pBuffer != NULL)
364 {
365 free(pBuffer);
366 pBuffer = NULL;
367 }
368
369 // Drop token buffer
370 if (sToken.value != NULL)
371 {
372 free(sToken.value);
373 sToken.value = NULL;
374 }
375
376 if (pIS == NULL)
377 return STATUS_OK;
378
379 status_t res = pIS->close();
380 if (nWFlags & WRAP_CLOSE)
381 res = update_status(res, pIS->close());
382 if (nWFlags & WRAP_DELETE)
383 delete pIS;
384
385 pIS = NULL;
386
387 return res;
388 }
389
390 status_t read_token(token_type_t & type, const char * & value)
391 {
392 if (sToken.unread)
393 {
394 type = sToken.type;
395 value = sToken.value;
396 sToken.unread = false;
397 return STATUS_OK;
398 }
399
400 sToken.type = TOK_INVALID;
401 sToken.length = 0;
402
403 int ch = getch();
404 if (ch < 0)
405 return status_t(-ch);
406
407 if (!append(ch))
408 return STATUS_NO_MEM;
409
410 status_t res;
411 switch (ch)
412 {
413 case '*': // Asterisk
414 sToken.type = TOK_ASTERISK;
415 break;
416 case '{': // Left brace
417 sToken.type = TOK_LBRACE;
418 break;
419 case '}': // Right brace
420 sToken.type = TOK_RBRACE;
421 break;
422 case '[': // Left square bracket
423 sToken.type = TOK_LQBRACKET;
424 break;
425 case ']': // Right square bracket
426 sToken.type = TOK_RQBRACKET;
427 break;
428 case ';': // Semicolon
429 sToken.type = TOK_SEMICOLON;
430 break;
431 case ',': // Comma
432 sToken.type = TOK_COMMA;
433 break;
434 case '=': // Assign
435 sToken.type = TOK_ASSIGN;
436 break;
437 case '#': // define
438 if (!bNewLine)
439 return STATUS_CORRUPTED_FILE;
440 if ((res = read_identifier()) != STATUS_OK)
441 return res;
442 if (memcmp(sToken.value, "#define", sToken.length) != 0)
443 return STATUS_CORRUPTED_FILE;
444 sToken.type = TOK_DEFINE;
445 break;
446 case '!': // XPM2 header
447 if (nLineNum != 0)
448 return STATUS_CORRUPTED_FILE;
449 if ((res = expect_signature(" XPM2")) != STATUS_OK)
450 return res;
451 sToken.type = TOK_XPM2_SIG;
452 break;
453 case '/': // XPM3 signature?
454 if ((res = expect_characters("* XPM")) == STATUS_OK)
455 {
456 if (nLineNum != 0)
457 return STATUS_CORRUPTED_FILE;
458
459 ch = getch();
460 if (ch < 0)
461 return status_t(-ch);
462 if (ch == '3')
463 {
464 if (!append(ch))
465 return STATUS_NO_MEM;
466 if ((res = expect_signature(" */")) != STATUS_OK)
467 return res;
468 }
469 else if (ch == ' ')
470 {
471 if ((res = expect_signature("*/")) != STATUS_OK)
472 return res;
473 }
474 sToken.type = TOK_XPM3_SIG;
475 }
476 else
477 {
478 if ((res = read_comment()) != STATUS_OK)
479 return res;
480 if ((res = expect_end_of_line()) != STATUS_OK)
481 return res;
482 sToken.type = TOK_COMMENT;
483 }
484
485 break;
486 case '\"': // C String
487 --sToken.length; // Remove '"' character
488 if ((res = read_c_string()) != STATUS_OK)
489 return res;
490 sToken.type = TOK_STRING;
491 break;
492
493 default: // Integer, identifier
494 if (is_alpha(ch))
495 {
496 sToken.type = TOK_IDENTIFIER;
497 res = read_alphadigit(1);
498 }
499 else if (is_digit(ch))
500 {
501 sToken.type = TOK_INTEGER;
502 res = read_integer(1);
503 }
504 else
505 return STATUS_CORRUPTED_FILE;
506
507 if ((res != STATUS_OK) && (res != STATUS_EOF))
508 return res;
509 break;
510 }
511
512 // Append terminating zero character
513 if (!append('\0'))
514 return STATUS_NO_MEM;
515
516 bNewLine = false;
517 if ((res = skip_whitespace()) != STATUS_OK)
518 return res;
519
520 type = sToken.type;
521 value = sToken.value;
522
523 return STATUS_OK;
524 }
525
526 void unread_token()
527 {
528 sToken.unread = true;
529 }
530
531 status_t read_line(const char * & value)
532 {
533 sToken.length = 0;
534
535 // Read file until we find the '\n' character
536 int ch;
537 while ((ch = getch()) >= 0)
538 {
539 if (ch == '\n')
540 {
541 bNewLine = true;
542 ++nLineNum;
543 break;
544 }
545 if (!append(ch))
546 return STATUS_NO_MEM;
547 }
548
549 // Analyze status
550 if (ch < 0)
551 {
552 // Check that we reached end of file
553 if (ch != -STATUS_EOF)
554 return status_t(-ch);
555 if (sToken.length <= 0)
556 return STATUS_EOF;
557 }
558 else
559 {
560 // Remove trailing '\r' character if present
561 if ((sToken.length > 0) && (sToken.value[sToken.length - 1] == '\r'))
562 --sToken.length;
563 }
564
565 // Append terminating zero character
566 if (!append('\0'))
567 return STATUS_NO_MEM;
568
569 value = sToken.value;
570
571 return STATUS_OK;
572 }
573 };
574
575 } /* namespace xpm */
576 } /* namespace lsp */
577
578
579
580 #endif /* PRIVATE_FMT_XPM_TOKENIZER_H_ */