Theme Editor: Fixed some more code generation bugs
[kugel-rb.git] / utils / themeeditor / skin_parser.c
blob94d059bfcc3f87826fb69bac1375d253f8ed7c0f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
27 #include "skin_parser.h"
28 #include "skin_debug.h"
29 #include "tag_table.h"
30 #include "symbols.h"
31 #include "skin_scan.h"
33 /* Declaration of parse tree buffer */
34 char skin_parse_tree[SKIN_MAX_MEMORY];
35 int skin_current_block = 0;
37 /* Global variables for the parser */
38 int skin_line = 0;
40 /* Auxiliary parsing functions (not visible at global scope) */
41 struct skin_element* skin_parse_viewport(char** document);
42 struct skin_element* skin_parse_line(char** document);
43 struct skin_element* skin_parse_line_optional(char** document, int conditional);
44 struct skin_element* skin_parse_sublines(char** document);
45 struct skin_element* skin_parse_sublines_optional(char** document,
46 int conditional);
48 int skin_parse_tag(struct skin_element* element, char** document);
49 int skin_parse_text(struct skin_element* element, char** document,
50 int conditional);
51 int skin_parse_conditional(struct skin_element* element, char** document);
52 int skin_parse_comment(struct skin_element* element, char** document);
53 struct skin_element* skin_parse_code_as_arg(char** document);
55 struct skin_element* skin_parse(const char* document)
58 struct skin_element* root = NULL;
59 struct skin_element* last = NULL;
61 struct skin_element** to_write = 0;
63 char* cursor = (char*)document; /*Keeps track of location in the document*/
65 skin_line = 1;
67 while(*cursor != '\0')
70 if(!root)
71 to_write = &root;
72 else
73 to_write = &(last->next);
76 *to_write = skin_parse_viewport(&cursor);
77 last = *to_write;
78 if(!last)
79 return NULL;
81 /* Making sure last is at the end */
82 while(last->next)
83 last = last->next;
87 return root;
91 struct skin_element* skin_parse_viewport(char** document)
94 struct skin_element* root = NULL;
95 struct skin_element* last = NULL;
96 struct skin_element* retval = NULL;
98 retval = skin_alloc_element();
99 retval->type = VIEWPORT;
100 retval->children_count = 1;
101 retval->line = skin_line;
103 struct skin_element** to_write = 0;
105 char* cursor = *document; /* Keeps track of location in the document */
106 char* bookmark; /* Used when we need to look ahead */
108 int sublines = 0; /* Flag for parsing sublines */
110 /* Parsing out the viewport tag if there is one */
111 if(check_viewport(cursor))
113 retval->children_count = 2;
114 retval->children = skin_alloc_children(2);
115 retval->children[0] = skin_alloc_element();
116 skin_parse_tag(retval->children[0], &cursor);
117 if(*cursor == '\n')
119 cursor++;
120 skin_line++;
123 else
125 retval->children_count = 1;
126 retval->children = skin_alloc_children(1);
130 while(*cursor != '\0' && !(check_viewport(cursor) && cursor != *document))
133 /* First, we check to see if this line will contain sublines */
134 bookmark = cursor;
135 sublines = 0;
136 while(*cursor != '\n' && *cursor != '\0'
137 && !(check_viewport(cursor) && cursor != *document))
139 if(*cursor == MULTILINESYM)
141 sublines = 1;
142 break;
144 else if(*cursor == TAGSYM)
146 /* A ';' directly after a '%' doesn't count */
147 cursor ++;
149 if(*cursor == '\0')
150 break;
152 cursor++;
154 else if(*cursor == COMMENTSYM)
156 skip_comment(&cursor);
158 else
160 /* Advancing the cursor as normal */
161 cursor++;
164 cursor = bookmark;
166 if(!root)
167 to_write = &root;
168 else
169 to_write = &(last->next);
171 if(sublines)
173 *to_write = skin_parse_sublines(&cursor);
174 last = *to_write;
175 if(!last)
176 return NULL;
178 else
181 *to_write = skin_parse_line(&cursor);
182 last = *to_write;
183 if(!last)
184 return NULL;
188 /* Making sure last is at the end */
189 while(last->next)
190 last = last->next;
192 if(*cursor == '\n')
194 cursor++;
195 skin_line++;
199 *document = cursor;
201 retval->children[retval->children_count - 1] = root;
202 return retval;
206 /* Auxiliary Parsing Functions */
208 struct skin_element* skin_parse_line(char**document)
211 return skin_parse_line_optional(document, 0);
217 * If conditional is set to true, then this will break upon encountering
218 * SEPERATESYM. This should only be used when parsing a line inside a
219 * conditional, otherwise just use the wrapper function skin_parse_line()
221 struct skin_element* skin_parse_line_optional(char** document, int conditional)
223 char* cursor = *document;
225 struct skin_element* root = NULL;
226 struct skin_element* current = NULL;
227 struct skin_element* retval = NULL;
229 /* A wrapper for the line */
230 retval = skin_alloc_element();
231 retval->type = LINE;
232 retval->line = skin_line;
233 retval->children_count = 1;
234 retval->children = skin_alloc_children(1);
236 while(*cursor != '\n' && *cursor != '\0' && *cursor != MULTILINESYM
237 && !((*cursor == ARGLISTSEPERATESYM
238 || *cursor == ARGLISTCLOSESYM
239 || *cursor == ENUMLISTSEPERATESYM
240 || *cursor == ENUMLISTCLOSESYM)
241 && conditional)
242 && !(check_viewport(cursor) && cursor != *document))
244 /* Allocating memory if necessary */
245 if(root)
247 current->next = skin_alloc_element();
248 current = current->next;
250 else
252 current = skin_alloc_element();
253 root = current;
256 /* Parsing the current element */
257 if(*cursor == TAGSYM && cursor[1] == CONDITIONSYM)
259 if(!skin_parse_conditional(current, &cursor))
260 return NULL;
262 else if(*cursor == TAGSYM && !find_escape_character(cursor[1]))
264 if(!skin_parse_tag(current, &cursor))
265 return NULL;
267 else if(*cursor == COMMENTSYM)
269 if(!skin_parse_comment(current, &cursor))
270 return NULL;
272 else
274 if(!skin_parse_text(current, &cursor, conditional))
275 return NULL;
279 /* Moving up the calling function's pointer */
280 *document = cursor;
282 retval->children[0] = root;
283 return retval;
286 struct skin_element* skin_parse_sublines(char** document)
288 return skin_parse_sublines_optional(document, 0);
291 struct skin_element* skin_parse_sublines_optional(char** document,
292 int conditional)
294 struct skin_element* retval;
295 char* cursor = *document;
296 int sublines = 1;
297 int i;
299 retval = skin_alloc_element();
300 retval->type = SUBLINES;
301 retval->next = NULL;
302 retval->line = skin_line;
304 /* First we count the sublines */
305 while(*cursor != '\0' && *cursor != '\n'
306 && !((*cursor == ARGLISTSEPERATESYM
307 || *cursor == ARGLISTCLOSESYM
308 || *cursor == ENUMLISTSEPERATESYM
309 || *cursor == ENUMLISTCLOSESYM)
310 && conditional)
311 && !(check_viewport(cursor) && cursor != *document))
313 if(*cursor == COMMENTSYM)
314 skip_comment(&cursor);
316 /* Accounting for escaped subline symbols */
317 if(*cursor == TAGSYM)
319 cursor++;
320 if(*cursor == '\0' || *cursor == '\n')
321 break;
324 if(*cursor == MULTILINESYM)
326 sublines++;
329 cursor++;
332 /* ...and then we parse them */
333 retval->children_count = sublines;
334 retval->children = skin_alloc_children(sublines);
336 cursor = *document;
337 for(i = 0; i < sublines; i++)
339 retval->children[i] = skin_parse_line_optional(&cursor, conditional);
340 skip_whitespace(&cursor);
342 if(*cursor != MULTILINESYM && i != sublines - 1)
344 skin_error(MULTILINE_EXPECTED);
345 return NULL;
347 else if(i != sublines - 1)
349 cursor++;
353 *document = cursor;
355 return retval;
358 int skin_parse_tag(struct skin_element* element, char** document)
361 char* cursor = *document + 1;
362 char* bookmark;
364 char tag_name[3];
365 char* tag_args;
366 struct tag_info *tag;
368 int num_args = 1;
369 int i;
370 int star = 0; /* Flag for the all-or-none option */
371 int req_args; /* To mark when we enter optional arguments */
373 int optional = 0;
375 /* Checking the tag name */
376 tag_name[0] = cursor[0];
377 tag_name[1] = cursor[1];
378 tag_name[2] = '\0';
380 /* First we check the two characters after the '%', then a single char */
381 tag = find_tag(tag_name);
383 if(!tag)
385 tag_name[1] = '\0';
386 tag = find_tag(tag_name);
387 cursor++;
389 else
391 cursor += 2;
394 if(!tag)
396 skin_error(ILLEGAL_TAG);
397 return 0;
400 /* Copying basic tag info */
401 element->type = TAG;
402 element->tag = tag;
403 tag_args = tag->params;
404 element->line = skin_line;
406 /* Checking for the * flag */
407 if(tag_args[0] == '*')
409 star = 1;
410 tag_args++;
413 /* If this tag has no arguments, we can bail out now */
414 if(strlen(tag_args) == 0
415 || (tag_args[0] == '|' && *cursor != ARGLISTOPENSYM)
416 || (star && *cursor != ARGLISTOPENSYM))
418 *document = cursor;
419 return 1;
422 /* Checking the number of arguments and allocating args */
423 if(*cursor != ARGLISTOPENSYM && tag_args[0] != '|')
425 skin_error(ARGLIST_EXPECTED);
426 return 0;
428 else
430 cursor++;
433 for(bookmark = cursor; *cursor != '\n' && *cursor != '\0' &&
434 *cursor != ARGLISTCLOSESYM; cursor++)
436 /* Skipping over escaped characters */
437 if(*cursor == TAGSYM)
439 cursor++;
440 if(*cursor == '\0')
441 break;
444 /* Skipping comments */
445 if(*cursor == COMMENTSYM)
446 skip_comment(&cursor);
448 /* Commas inside of contained lists don't count */
449 if(*cursor == ARGLISTOPENSYM)
450 while(*cursor != ARGLISTCLOSESYM && *cursor != '\0')
451 cursor++;
453 if(*cursor == ARGLISTSEPERATESYM)
454 num_args++;
457 cursor = bookmark; /* Restoring the cursor */
458 element->params_count = num_args;
459 element->params = skin_alloc_params(num_args);
461 /* Now we have to actually parse each argument */
462 for(i = 0; i < num_args; i++)
464 /* Making sure we haven't run out of arguments */
465 if(*tag_args == '\0')
467 skin_error(TOO_MANY_ARGS);
468 return 0;
471 /* Checking for the optional bar */
472 if(*tag_args == '|')
474 optional = 1;
475 req_args = i;
476 tag_args++;
479 /* Scanning the arguments */
480 skip_whitespace(&cursor);
483 /* Checking for comments */
484 if(*cursor == COMMENTSYM)
485 skip_comment(&cursor);
487 /* Storing the type code */
488 element->params[i].type_code = *tag_args;
490 /* Checking a nullable argument for null */
491 if(*cursor == DEFAULTSYM && !isdigit(cursor[1]))
493 if(islower(*tag_args))
495 element->params[i].type = DEFAULT;
496 cursor++;
498 else
500 skin_error(DEFAULT_NOT_ALLOWED);
501 return 0;
504 else if(tolower(*tag_args) == 'i')
506 /* Scanning an int argument */
507 if(!isdigit(*cursor) && *cursor != '-')
509 skin_error(INT_EXPECTED);
510 return 0;
513 element->params[i].type = NUMERIC;
514 element->params[i].data.numeric = scan_int(&cursor);
516 else if(tolower(*tag_args) == 's' || tolower(*tag_args) == 'f')
518 /* Scanning a string argument */
519 element->params[i].type = STRING;
520 element->params[i].data.text = scan_string(&cursor);
523 else if(tolower(*tag_args) == 'c')
525 /* Recursively parsing a code argument */
526 element->params[i].type = CODE;
527 element->params[i].data.code = skin_parse_code_as_arg(&cursor);
528 if(!element->params[i].data.code)
529 return 0;
532 skip_whitespace(&cursor);
534 if(*cursor != ARGLISTSEPERATESYM && i < num_args - 1)
536 skin_error(SEPERATOR_EXPECTED);
537 return 0;
539 else if(*cursor != ARGLISTCLOSESYM && i == num_args - 1)
541 skin_error(CLOSE_EXPECTED);
542 return 0;
544 else
546 cursor++;
549 tag_args++;
551 /* Checking for the optional bar */
552 if(*tag_args == '|')
554 optional = 1;
555 req_args = i + 1;
556 tag_args++;
561 /* Checking for a premature end */
562 if(*tag_args != '\0' && !optional)
564 skin_error(INSUFFICIENT_ARGS);
565 return 0;
568 *document = cursor;
570 return 1;
574 * If the conditional flag is set true, then parsing text will stop at an
575 * ARGLISTSEPERATESYM. Only set that flag when parsing within a conditional
577 int skin_parse_text(struct skin_element* element, char** document,
578 int conditional)
580 char* cursor = *document;
582 int length = 0;
584 int dest;
586 /* First figure out how much text we're copying */
587 while(*cursor != '\0' && *cursor != '\n' && *cursor != MULTILINESYM
588 && *cursor != COMMENTSYM
589 && !((*cursor == ARGLISTSEPERATESYM
590 || *cursor == ARGLISTCLOSESYM
591 || *cursor == ENUMLISTSEPERATESYM
592 || *cursor == ENUMLISTCLOSESYM)
593 && conditional))
595 /* Dealing with possibility of escaped characters */
596 if(*cursor == TAGSYM)
598 if(find_escape_character(cursor[1]))
599 cursor++;
600 else
601 break;
604 length++;
605 cursor++;
608 cursor = *document;
610 /* Copying the text into the element struct */
611 element->type = TEXT;
612 element->line = skin_line;
613 element->next = NULL;
614 element->text = skin_alloc_string(length);
616 for(dest = 0; dest < length; dest++)
618 /* Advancing cursor if we've encountered an escaped character */
619 if(*cursor == TAGSYM)
620 cursor++;
622 element->text[dest] = *cursor;
623 cursor++;
625 element->text[length] = '\0';
627 *document = cursor;
629 return 1;
632 int skin_parse_conditional(struct skin_element* element, char** document)
635 char* cursor = *document + 1; /* Starting past the "%" */
636 char* bookmark;
637 struct skin_element* tag = skin_alloc_element(); /* The tag to evaluate */
638 int children = 1;
639 int i;
641 element->type = CONDITIONAL;
642 element->line = skin_line;
644 /* Parsing the tag first */
645 if(!skin_parse_tag(tag, &cursor))
646 return 0;
648 /* Counting the children */
649 if(*(cursor++) != ENUMLISTOPENSYM)
651 skin_error(ARGLIST_EXPECTED);
652 return 0;
654 bookmark = cursor;
655 while(*cursor != ENUMLISTCLOSESYM && *cursor != '\n' && *cursor != '\0')
657 if(*cursor == COMMENTSYM)
659 skip_comment(&cursor);
660 continue;
663 if(*cursor == TAGSYM)
665 cursor++;
666 if(*cursor == '\0' || *cursor == '\n')
667 break;
668 cursor++;
669 continue;
672 if(*cursor == ENUMLISTSEPERATESYM)
673 children++;
675 cursor++;
677 cursor = bookmark;
679 /* Parsing the children */
680 element->children_count = children + 1; /* Make sure to include the tag */
681 element->children = skin_alloc_children(children + 1);
682 element->children[0] = tag;
684 for(i = 1; i < children + 1; i++)
686 element->children[i] = skin_parse_code_as_arg(&cursor);
687 skip_whitespace(&cursor);
689 if(i < children && *cursor != ENUMLISTSEPERATESYM)
691 skin_error(SEPERATOR_EXPECTED);
692 return 0;
694 else if(i == children && *cursor != ENUMLISTCLOSESYM)
696 skin_error(CLOSE_EXPECTED);
697 return 0;
699 else
701 cursor++;
705 *document = cursor;
707 return 1;
710 int skin_parse_comment(struct skin_element* element, char** document)
712 char* cursor = *document;
714 int length;
716 * Finding the index of the ending newline or null-terminator
717 * The length of the string of interest doesn't include the leading #, the
718 * length we need to reserve is the same as the index of the last character
720 for(length = 0; cursor[length] != '\n' && cursor[length] != '\0'; length++);
722 element->type = COMMENT;
723 element->line = skin_line;
724 element->text = skin_alloc_string(length);
725 /* We copy from one char past cursor to leave out the # */
726 memcpy((void*)(element->text), (void*)(cursor + 1),
727 sizeof(char) * (length-1));
728 element->text[length - 1] = '\0';
730 if(cursor[length] == '\n')
731 skin_line++;
733 *document += (length); /* Move cursor up past # and all text */
734 if(**document == '\n')
735 (*document)++;
737 return 1;
740 struct skin_element* skin_parse_code_as_arg(char** document)
743 int sublines = 0;
744 char* cursor = *document;
746 /* Checking for sublines */
747 while(*cursor != '\n' && *cursor != '\0')
749 if(*cursor == MULTILINESYM)
751 sublines = 1;
752 break;
754 else if(*cursor == TAGSYM)
756 /* A ';' directly after a '%' doesn't count */
757 cursor ++;
759 if(*cursor == '\0')
760 break;
762 cursor++;
764 else
766 /* Advancing the cursor as normal */
767 cursor++;
771 if(sublines)
772 return skin_parse_sublines_optional(document, 1);
773 else
774 return skin_parse_line_optional(document, 1);
778 /* Memory management */
779 struct skin_element* skin_alloc_element()
782 #if 0
784 char* retval = &skin_parse_tree[skin_current_block * 4];
786 int delta = sizeof(struct skin_element) / (sizeof(char) * 4);
788 /* If one block is partially filled, make sure to advance to the
789 * next one for the next allocation
791 if(sizeof(struct skin_element) % (sizeof(char) * 4) != 0)
792 delta++;
794 skin_current_block += delta;
796 return (struct skin_element*)retval;
798 #endif
800 struct skin_element* retval = (struct skin_element*)
801 malloc(sizeof(struct skin_element));
802 retval->next = NULL;
803 retval->params_count = 0;
804 retval->children_count = 0;
806 return retval;
810 struct skin_tag_parameter* skin_alloc_params(int count)
812 #if 0
814 char* retval = &skin_parse_tree[skin_current_block * 4];
816 int delta = sizeof(struct skin_tag_parameter) / (sizeof(char) * 4);
817 delta *= count;
819 /* Correcting uneven alignment */
820 if(count * sizeof(struct skin_tag_parameter) % (sizeof(char) * 4) != 0)
821 delta++;
823 skin_current_block += delta;
825 return (struct skin_tag_parameter*) retval;
827 #endif
829 return (struct skin_tag_parameter*)malloc(sizeof(struct skin_tag_parameter)
830 * count);
834 char* skin_alloc_string(int length)
837 #if 0
838 char* retval = &skin_parse_tree[skin_current_block * 4];
840 /* Checking alignment */
841 length++; /* Accounting for the null terminator */
842 int delta = length / 4;
843 if(length % 4 != 0)
844 delta++;
846 skin_current_block += delta;
848 if(skin_current_block >= SKIN_MAX_MEMORY / 4)
849 skin_error(MEMORY_LIMIT_EXCEEDED);
851 return retval;
853 #endif
855 return (char*)malloc(sizeof(char) * (length + 1));
859 struct skin_element** skin_alloc_children(int count)
861 return (struct skin_element**) malloc(sizeof(struct skin_element*) * count);
864 void skin_free_tree(struct skin_element* root)
866 int i;
868 /* First make the recursive call */
869 if(!root)
870 return;
871 skin_free_tree(root->next);
873 /* Free any text */
874 if(root->type == TEXT)
875 free(root->text);
877 /* Then recursively free any children, before freeing their pointers */
878 for(i = 0; i < root->children_count; i++)
879 skin_free_tree(root->children[i]);
880 if(root->children_count > 0)
881 free(root->children);
883 /* Free any parameters, making sure to deallocate strings */
884 for(i = 0; i < root->params_count; i++)
885 if(root->params[i].type == STRING)
886 free(root->params[i].data.text);
887 if(root->params_count > 0)
888 free(root->params);
890 /* Finally, delete root's memory */
891 free(root);