1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #include "skin_parser.h"
28 #include "skin_debug.h"
29 #include "tag_table.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 */
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
,
48 int skin_parse_tag(struct skin_element
* element
, char** document
);
49 int skin_parse_text(struct skin_element
* element
, char** document
,
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*/
67 while(*cursor
!= '\0')
73 to_write
= &(last
->next
);
76 *to_write
= skin_parse_viewport(&cursor
);
81 /* Making sure last is at the end */
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
);
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 */
136 while(*cursor
!= '\n' && *cursor
!= '\0'
137 && !(check_viewport(cursor
) && cursor
!= *document
))
139 if(*cursor
== MULTILINESYM
)
144 else if(*cursor
== TAGSYM
)
146 /* A ';' directly after a '%' doesn't count */
154 else if(*cursor
== COMMENTSYM
)
156 skip_comment(&cursor
);
160 /* Advancing the cursor as normal */
169 to_write
= &(last
->next
);
173 *to_write
= skin_parse_sublines(&cursor
);
181 *to_write
= skin_parse_line(&cursor
);
188 /* Making sure last is at the end */
201 retval
->children
[retval
->children_count
- 1] = root
;
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();
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
)
242 && !(check_viewport(cursor
) && cursor
!= *document
))
244 /* Allocating memory if necessary */
247 current
->next
= skin_alloc_element();
248 current
= current
->next
;
252 current
= skin_alloc_element();
256 /* Parsing the current element */
257 if(*cursor
== TAGSYM
&& cursor
[1] == CONDITIONSYM
)
259 if(!skin_parse_conditional(current
, &cursor
))
262 else if(*cursor
== TAGSYM
&& !find_escape_character(cursor
[1]))
264 if(!skin_parse_tag(current
, &cursor
))
267 else if(*cursor
== COMMENTSYM
)
269 if(!skin_parse_comment(current
, &cursor
))
274 if(!skin_parse_text(current
, &cursor
, conditional
))
279 /* Moving up the calling function's pointer */
282 retval
->children
[0] = root
;
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
,
294 struct skin_element
* retval
;
295 char* cursor
= *document
;
300 retval
= skin_alloc_element();
301 retval
->type
= SUBLINES
;
303 retval
->line
= skin_line
;
305 /* First we count the sublines */
306 while(*cursor
!= '\0' && *cursor
!= '\n'
307 && !((*cursor
== ARGLISTSEPERATESYM
308 || *cursor
== ARGLISTCLOSESYM
309 || *cursor
== ENUMLISTSEPERATESYM
310 || *cursor
== ENUMLISTCLOSESYM
)
312 && !(check_viewport(cursor
) && cursor
!= *document
))
314 if(*cursor
== COMMENTSYM
)
316 skip_comment(&cursor
);
320 if(*cursor
== ENUMLISTOPENSYM
&& conditional
)
326 if(*cursor
== ENUMLISTOPENSYM
)
328 if(*cursor
== ENUMLISTCLOSESYM
)
333 /* Accounting for escaped subline symbols */
334 if(*cursor
== TAGSYM
)
337 if(*cursor
== '\0' || *cursor
== '\n')
341 if(*cursor
== MULTILINESYM
)
349 /* ...and then we parse them */
350 retval
->children_count
= sublines
;
351 retval
->children
= skin_alloc_children(sublines
);
354 for(i
= 0; i
< sublines
; i
++)
356 retval
->children
[i
] = skin_parse_line_optional(&cursor
, conditional
);
357 skip_whitespace(&cursor
);
359 if(*cursor
!= MULTILINESYM
&& i
!= sublines
- 1)
361 skin_error(MULTILINE_EXPECTED
);
364 else if(i
!= sublines
- 1)
375 int skin_parse_tag(struct skin_element
* element
, char** document
)
378 char* cursor
= *document
+ 1;
383 struct tag_info
*tag
;
387 int star
= 0; /* Flag for the all-or-none option */
388 int req_args
; /* To mark when we enter optional arguments */
392 /* Checking the tag name */
393 tag_name
[0] = cursor
[0];
394 tag_name
[1] = cursor
[1];
397 /* First we check the two characters after the '%', then a single char */
398 tag
= find_tag(tag_name
);
403 tag
= find_tag(tag_name
);
413 skin_error(ILLEGAL_TAG
);
417 /* Copying basic tag info */
420 tag_args
= tag
->params
;
421 element
->line
= skin_line
;
423 /* Checking for the * flag */
424 if(tag_args
[0] == '*')
430 /* If this tag has no arguments, we can bail out now */
431 if(strlen(tag_args
) == 0
432 || (tag_args
[0] == '|' && *cursor
!= ARGLISTOPENSYM
)
433 || (star
&& *cursor
!= ARGLISTOPENSYM
))
439 /* Checking the number of arguments and allocating args */
440 if(*cursor
!= ARGLISTOPENSYM
&& tag_args
[0] != '|')
442 skin_error(ARGLIST_EXPECTED
);
450 for(bookmark
= cursor
; *cursor
!= '\n' && *cursor
!= '\0' &&
451 *cursor
!= ARGLISTCLOSESYM
; cursor
++)
453 /* Skipping over escaped characters */
454 if(*cursor
== TAGSYM
)
461 /* Skipping comments */
462 if(*cursor
== COMMENTSYM
)
463 skip_comment(&cursor
);
465 /* Commas inside of contained lists don't count */
466 if(*cursor
== ARGLISTOPENSYM
)
467 while(*cursor
!= ARGLISTCLOSESYM
&& *cursor
!= '\0')
470 if(*cursor
== ARGLISTSEPERATESYM
)
474 cursor
= bookmark
; /* Restoring the cursor */
475 element
->params_count
= num_args
;
476 element
->params
= skin_alloc_params(num_args
);
478 /* Now we have to actually parse each argument */
479 for(i
= 0; i
< num_args
; i
++)
481 /* Making sure we haven't run out of arguments */
482 if(*tag_args
== '\0')
484 skin_error(TOO_MANY_ARGS
);
488 /* Checking for the optional bar */
496 /* Scanning the arguments */
497 skip_whitespace(&cursor
);
500 /* Checking for comments */
501 if(*cursor
== COMMENTSYM
)
502 skip_comment(&cursor
);
504 /* Storing the type code */
505 element
->params
[i
].type_code
= *tag_args
;
507 /* Checking a nullable argument for null */
508 if(*cursor
== DEFAULTSYM
&& !isdigit(cursor
[1]))
510 if(islower(*tag_args
))
512 element
->params
[i
].type
= DEFAULT
;
517 skin_error(DEFAULT_NOT_ALLOWED
);
521 else if(tolower(*tag_args
) == 'i')
523 /* Scanning an int argument */
524 if(!isdigit(*cursor
) && *cursor
!= '-')
526 skin_error(INT_EXPECTED
);
530 element
->params
[i
].type
= NUMERIC
;
531 element
->params
[i
].data
.numeric
= scan_int(&cursor
);
533 else if(tolower(*tag_args
) == 's' || tolower(*tag_args
) == 'f')
535 /* Scanning a string argument */
536 element
->params
[i
].type
= STRING
;
537 element
->params
[i
].data
.text
= scan_string(&cursor
);
540 else if(tolower(*tag_args
) == 'c')
542 /* Recursively parsing a code argument */
543 element
->params
[i
].type
= CODE
;
544 element
->params
[i
].data
.code
= skin_parse_code_as_arg(&cursor
);
545 if(!element
->params
[i
].data
.code
)
549 skip_whitespace(&cursor
);
551 if(*cursor
!= ARGLISTSEPERATESYM
&& i
< num_args
- 1)
553 skin_error(SEPERATOR_EXPECTED
);
556 else if(*cursor
!= ARGLISTCLOSESYM
&& i
== num_args
- 1)
558 skin_error(CLOSE_EXPECTED
);
568 /* Checking for the optional bar */
578 /* Checking for a premature end */
579 if(*tag_args
!= '\0' && !optional
)
581 skin_error(INSUFFICIENT_ARGS
);
591 * If the conditional flag is set true, then parsing text will stop at an
592 * ARGLISTSEPERATESYM. Only set that flag when parsing within a conditional
594 int skin_parse_text(struct skin_element
* element
, char** document
,
597 char* cursor
= *document
;
603 /* First figure out how much text we're copying */
604 while(*cursor
!= '\0' && *cursor
!= '\n' && *cursor
!= MULTILINESYM
605 && *cursor
!= COMMENTSYM
606 && !((*cursor
== ARGLISTSEPERATESYM
607 || *cursor
== ARGLISTCLOSESYM
608 || *cursor
== ENUMLISTSEPERATESYM
609 || *cursor
== ENUMLISTCLOSESYM
)
612 /* Dealing with possibility of escaped characters */
613 if(*cursor
== TAGSYM
)
615 if(find_escape_character(cursor
[1]))
627 /* Copying the text into the element struct */
628 element
->type
= TEXT
;
629 element
->line
= skin_line
;
630 element
->next
= NULL
;
631 element
->text
= skin_alloc_string(length
);
633 for(dest
= 0; dest
< length
; dest
++)
635 /* Advancing cursor if we've encountered an escaped character */
636 if(*cursor
== TAGSYM
)
639 element
->text
[dest
] = *cursor
;
642 element
->text
[length
] = '\0';
649 int skin_parse_conditional(struct skin_element
* element
, char** document
)
652 char* cursor
= *document
+ 1; /* Starting past the "%" */
654 struct skin_element
* tag
= skin_alloc_element(); /* The tag to evaluate */
659 element
->type
= CONDITIONAL
;
660 element
->line
= skin_line
;
662 /* Parsing the tag first */
663 if(!skin_parse_tag(tag
, &cursor
))
666 /* Counting the children */
667 if(*(cursor
++) != ENUMLISTOPENSYM
)
669 skin_error(ARGLIST_EXPECTED
);
673 while(*cursor
!= ENUMLISTCLOSESYM
&& *cursor
!= '\n' && *cursor
!= '\0')
675 if(*cursor
== COMMENTSYM
)
677 skip_comment(&cursor
);
681 if(*cursor
== ENUMLISTOPENSYM
)
687 if(*cursor
== ENUMLISTOPENSYM
)
691 else if(*cursor
== ENUMLISTCLOSESYM
)
701 if(*cursor
== TAGSYM
)
704 if(*cursor
== '\0' || *cursor
== '\n')
710 if(*cursor
== ENUMLISTSEPERATESYM
)
717 /* Parsing the children */
718 element
->children_count
= children
+ 1; /* Make sure to include the tag */
719 element
->children
= skin_alloc_children(children
+ 1);
720 element
->children
[0] = tag
;
722 for(i
= 1; i
< children
+ 1; i
++)
724 element
->children
[i
] = skin_parse_code_as_arg(&cursor
);
725 skip_whitespace(&cursor
);
727 if(i
< children
&& *cursor
!= ENUMLISTSEPERATESYM
)
729 skin_error(SEPERATOR_EXPECTED
);
732 else if(i
== children
&& *cursor
!= ENUMLISTCLOSESYM
)
734 skin_error(CLOSE_EXPECTED
);
748 int skin_parse_comment(struct skin_element
* element
, char** document
)
750 char* cursor
= *document
;
754 * Finding the index of the ending newline or null-terminator
755 * The length of the string of interest doesn't include the leading #, the
756 * length we need to reserve is the same as the index of the last character
758 for(length
= 0; cursor
[length
] != '\n' && cursor
[length
] != '\0'; length
++);
760 element
->type
= COMMENT
;
761 element
->line
= skin_line
;
762 element
->text
= skin_alloc_string(length
);
763 /* We copy from one char past cursor to leave out the # */
764 memcpy((void*)(element
->text
), (void*)(cursor
+ 1),
765 sizeof(char) * (length
-1));
766 element
->text
[length
- 1] = '\0';
768 if(cursor
[length
] == '\n')
771 *document
+= (length
); /* Move cursor up past # and all text */
772 if(**document
== '\n')
778 struct skin_element
* skin_parse_code_as_arg(char** document
)
782 char* cursor
= *document
;
784 /* Checking for sublines */
785 while(*cursor
!= '\n' && *cursor
!= '\0')
787 if(*cursor
== MULTILINESYM
)
792 else if(*cursor
== TAGSYM
)
794 /* A ';' directly after a '%' doesn't count */
804 /* Advancing the cursor as normal */
810 return skin_parse_sublines_optional(document
, 1);
812 return skin_parse_line_optional(document
, 1);
816 /* Memory management */
817 struct skin_element
* skin_alloc_element()
822 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
824 int delta
= sizeof(struct skin_element
) / (sizeof(char) * 4);
826 /* If one block is partially filled, make sure to advance to the
827 * next one for the next allocation
829 if(sizeof(struct skin_element
) % (sizeof(char) * 4) != 0)
832 skin_current_block
+= delta
;
834 return (struct skin_element
*)retval
;
838 struct skin_element
* retval
= (struct skin_element
*)
839 malloc(sizeof(struct skin_element
));
841 retval
->params_count
= 0;
842 retval
->children_count
= 0;
848 struct skin_tag_parameter
* skin_alloc_params(int count
)
852 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
854 int delta
= sizeof(struct skin_tag_parameter
) / (sizeof(char) * 4);
857 /* Correcting uneven alignment */
858 if(count
* sizeof(struct skin_tag_parameter
) % (sizeof(char) * 4) != 0)
861 skin_current_block
+= delta
;
863 return (struct skin_tag_parameter
*) retval
;
867 return (struct skin_tag_parameter
*)malloc(sizeof(struct skin_tag_parameter
)
872 char* skin_alloc_string(int length
)
876 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
878 /* Checking alignment */
879 length
++; /* Accounting for the null terminator */
880 int delta
= length
/ 4;
884 skin_current_block
+= delta
;
886 if(skin_current_block
>= SKIN_MAX_MEMORY
/ 4)
887 skin_error(MEMORY_LIMIT_EXCEEDED
);
893 return (char*)malloc(sizeof(char) * (length
+ 1));
897 struct skin_element
** skin_alloc_children(int count
)
899 return (struct skin_element
**) malloc(sizeof(struct skin_element
*) * count
);
902 void skin_free_tree(struct skin_element
* root
)
906 /* First make the recursive call */
909 skin_free_tree(root
->next
);
912 if(root
->type
== TEXT
)
915 /* Then recursively free any children, before freeing their pointers */
916 for(i
= 0; i
< root
->children_count
; i
++)
917 skin_free_tree(root
->children
[i
]);
918 if(root
->children_count
> 0)
919 free(root
->children
);
921 /* Free any parameters, making sure to deallocate strings */
922 for(i
= 0; i
< root
->params_count
; i
++)
923 if(root
->params
[i
].type
== STRING
)
924 free(root
->params
[i
].data
.text
);
925 if(root
->params_count
> 0)
928 /* Finally, delete root's memory */