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*/
69 while(*cursor
!= '\0')
75 to_write
= &(last
->next
);
78 *to_write
= skin_parse_viewport(&cursor
);
83 /* Making sure last is at the end */
93 struct skin_element
* skin_parse_viewport(char** document
)
96 struct skin_element
* root
= NULL
;
97 struct skin_element
* last
= NULL
;
98 struct skin_element
* retval
= NULL
;
100 retval
= skin_alloc_element();
101 retval
->type
= VIEWPORT
;
102 retval
->children_count
= 1;
103 retval
->line
= skin_line
;
105 struct skin_element
** to_write
= 0;
107 char* cursor
= *document
; /* Keeps track of location in the document */
108 char* bookmark
; /* Used when we need to look ahead */
110 int sublines
= 0; /* Flag for parsing sublines */
112 /* Parsing out the viewport tag if there is one */
113 if(check_viewport(cursor
))
115 retval
->children_count
= 2;
116 retval
->children
= skin_alloc_children(2);
117 retval
->children
[0] = skin_alloc_element();
118 skin_parse_tag(retval
->children
[0], &cursor
);
127 retval
->children_count
= 1;
128 retval
->children
= skin_alloc_children(1);
132 while(*cursor
!= '\0' && !(check_viewport(cursor
) && cursor
!= *document
))
135 /* First, we check to see if this line will contain sublines */
138 while(*cursor
!= '\n' && *cursor
!= '\0'
139 && !(check_viewport(cursor
) && cursor
!= *document
))
141 if(*cursor
== MULTILINESYM
)
146 else if(*cursor
== TAGSYM
)
148 /* A ';' directly after a '%' doesn't count */
156 else if(*cursor
== COMMENTSYM
)
158 skip_comment(&cursor
);
162 /* Advancing the cursor as normal */
171 to_write
= &(last
->next
);
175 *to_write
= skin_parse_sublines(&cursor
);
183 *to_write
= skin_parse_line(&cursor
);
190 /* Making sure last is at the end */
203 retval
->children
[retval
->children_count
- 1] = root
;
208 /* Auxiliary Parsing Functions */
210 struct skin_element
* skin_parse_line(char**document
)
213 return skin_parse_line_optional(document
, 0);
219 * If conditional is set to true, then this will break upon encountering
220 * SEPERATESYM. This should only be used when parsing a line inside a
221 * conditional, otherwise just use the wrapper function skin_parse_line()
223 struct skin_element
* skin_parse_line_optional(char** document
, int conditional
)
225 char* cursor
= *document
;
227 struct skin_element
* root
= NULL
;
228 struct skin_element
* current
= NULL
;
229 struct skin_element
* retval
= NULL
;
231 /* A wrapper for the line */
232 retval
= skin_alloc_element();
234 retval
->line
= skin_line
;
235 retval
->children_count
= 1;
236 retval
->children
= skin_alloc_children(1);
238 while(*cursor
!= '\n' && *cursor
!= '\0' && *cursor
!= MULTILINESYM
239 && !((*cursor
== ARGLISTSEPERATESYM
240 || *cursor
== ARGLISTCLOSESYM
241 || *cursor
== ENUMLISTSEPERATESYM
242 || *cursor
== ENUMLISTCLOSESYM
)
244 && !(check_viewport(cursor
) && cursor
!= *document
))
246 /* Allocating memory if necessary */
249 current
->next
= skin_alloc_element();
250 current
= current
->next
;
254 current
= skin_alloc_element();
258 /* Parsing the current element */
259 if(*cursor
== TAGSYM
&& cursor
[1] == CONDITIONSYM
)
261 if(!skin_parse_conditional(current
, &cursor
))
264 else if(*cursor
== TAGSYM
&& !find_escape_character(cursor
[1]))
266 if(!skin_parse_tag(current
, &cursor
))
269 else if(*cursor
== COMMENTSYM
)
271 if(!skin_parse_comment(current
, &cursor
))
276 if(!skin_parse_text(current
, &cursor
, conditional
))
281 /* Moving up the calling function's pointer */
284 retval
->children
[0] = root
;
288 struct skin_element
* skin_parse_sublines(char** document
)
290 return skin_parse_sublines_optional(document
, 0);
293 struct skin_element
* skin_parse_sublines_optional(char** document
,
296 struct skin_element
* retval
;
297 char* cursor
= *document
;
302 retval
= skin_alloc_element();
303 retval
->type
= SUBLINES
;
305 retval
->line
= skin_line
;
307 /* First we count the sublines */
308 while(*cursor
!= '\0' && *cursor
!= '\n'
309 && !((*cursor
== ARGLISTSEPERATESYM
310 || *cursor
== ARGLISTCLOSESYM
311 || *cursor
== ENUMLISTSEPERATESYM
312 || *cursor
== ENUMLISTCLOSESYM
)
314 && !(check_viewport(cursor
) && cursor
!= *document
))
316 if(*cursor
== COMMENTSYM
)
318 skip_comment(&cursor
);
322 if(*cursor
== ENUMLISTOPENSYM
&& conditional
)
328 if(*cursor
== ENUMLISTOPENSYM
)
330 if(*cursor
== ENUMLISTCLOSESYM
)
335 /* Accounting for escaped subline symbols */
336 if(*cursor
== TAGSYM
)
339 if(*cursor
== '\0' || *cursor
== '\n')
343 if(*cursor
== MULTILINESYM
)
351 /* ...and then we parse them */
352 retval
->children_count
= sublines
;
353 retval
->children
= skin_alloc_children(sublines
);
356 for(i
= 0; i
< sublines
; i
++)
358 retval
->children
[i
] = skin_parse_line_optional(&cursor
, conditional
);
359 skip_whitespace(&cursor
);
361 if(*cursor
!= MULTILINESYM
&& i
!= sublines
- 1)
363 skin_error(MULTILINE_EXPECTED
);
366 else if(i
!= sublines
- 1)
377 int skin_parse_tag(struct skin_element
* element
, char** document
)
380 char* cursor
= *document
+ 1;
385 struct tag_info
*tag
;
389 int star
= 0; /* Flag for the all-or-none option */
390 int req_args
; /* To mark when we enter optional arguments */
394 /* Checking the tag name */
395 tag_name
[0] = cursor
[0];
396 tag_name
[1] = cursor
[1];
399 /* First we check the two characters after the '%', then a single char */
400 tag
= find_tag(tag_name
);
405 tag
= find_tag(tag_name
);
415 skin_error(ILLEGAL_TAG
);
419 /* Copying basic tag info */
422 tag_args
= tag
->params
;
423 element
->line
= skin_line
;
425 /* Checking for the * flag */
426 if(tag_args
[0] == '*')
432 /* If this tag has no arguments, we can bail out now */
433 if(strlen(tag_args
) == 0
434 || (tag_args
[0] == '|' && *cursor
!= ARGLISTOPENSYM
)
435 || (star
&& *cursor
!= ARGLISTOPENSYM
))
441 /* Checking the number of arguments and allocating args */
442 if(*cursor
!= ARGLISTOPENSYM
&& tag_args
[0] != '|')
444 skin_error(ARGLIST_EXPECTED
);
452 for(bookmark
= cursor
; *cursor
!= '\n' && *cursor
!= '\0' &&
453 *cursor
!= ARGLISTCLOSESYM
; cursor
++)
455 /* Skipping over escaped characters */
456 if(*cursor
== TAGSYM
)
463 /* Skipping comments */
464 if(*cursor
== COMMENTSYM
)
465 skip_comment(&cursor
);
467 /* Commas inside of contained lists don't count */
468 if(*cursor
== ARGLISTOPENSYM
)
469 while(*cursor
!= ARGLISTCLOSESYM
&& *cursor
!= '\0')
472 if(*cursor
== ARGLISTSEPERATESYM
)
476 cursor
= bookmark
; /* Restoring the cursor */
477 element
->params_count
= num_args
;
478 element
->params
= skin_alloc_params(num_args
);
480 /* Now we have to actually parse each argument */
481 for(i
= 0; i
< num_args
; i
++)
483 /* Making sure we haven't run out of arguments */
484 if(*tag_args
== '\0')
486 skin_error(TOO_MANY_ARGS
);
490 /* Checking for the optional bar */
498 /* Scanning the arguments */
499 skip_whitespace(&cursor
);
502 /* Checking for comments */
503 if(*cursor
== COMMENTSYM
)
504 skip_comment(&cursor
);
506 /* Storing the type code */
507 element
->params
[i
].type_code
= *tag_args
;
509 /* Checking a nullable argument for null */
510 if(*cursor
== DEFAULTSYM
&& !isdigit(cursor
[1]))
512 if(islower(*tag_args
))
514 element
->params
[i
].type
= DEFAULT
;
519 skin_error(DEFAULT_NOT_ALLOWED
);
523 else if(tolower(*tag_args
) == 'i')
525 /* Scanning an int argument */
526 if(!isdigit(*cursor
) && *cursor
!= '-')
528 skin_error(INT_EXPECTED
);
532 element
->params
[i
].type
= NUMERIC
;
533 element
->params
[i
].data
.numeric
= scan_int(&cursor
);
535 else if(tolower(*tag_args
) == 's' || tolower(*tag_args
) == 'f')
537 /* Scanning a string argument */
538 element
->params
[i
].type
= STRING
;
539 element
->params
[i
].data
.text
= scan_string(&cursor
);
542 else if(tolower(*tag_args
) == 'c')
544 /* Recursively parsing a code argument */
545 element
->params
[i
].type
= CODE
;
546 element
->params
[i
].data
.code
= skin_parse_code_as_arg(&cursor
);
547 if(!element
->params
[i
].data
.code
)
551 skip_whitespace(&cursor
);
553 if(*cursor
!= ARGLISTSEPERATESYM
&& i
< num_args
- 1)
555 skin_error(SEPERATOR_EXPECTED
);
558 else if(*cursor
!= ARGLISTCLOSESYM
&& i
== num_args
- 1)
560 skin_error(CLOSE_EXPECTED
);
570 /* Checking for the optional bar */
580 /* Checking for a premature end */
581 if(*tag_args
!= '\0' && !optional
)
583 skin_error(INSUFFICIENT_ARGS
);
593 * If the conditional flag is set true, then parsing text will stop at an
594 * ARGLISTSEPERATESYM. Only set that flag when parsing within a conditional
596 int skin_parse_text(struct skin_element
* element
, char** document
,
599 char* cursor
= *document
;
605 /* First figure out how much text we're copying */
606 while(*cursor
!= '\0' && *cursor
!= '\n' && *cursor
!= MULTILINESYM
607 && *cursor
!= COMMENTSYM
608 && !((*cursor
== ARGLISTSEPERATESYM
609 || *cursor
== ARGLISTCLOSESYM
610 || *cursor
== ENUMLISTSEPERATESYM
611 || *cursor
== ENUMLISTCLOSESYM
)
614 /* Dealing with possibility of escaped characters */
615 if(*cursor
== TAGSYM
)
617 if(find_escape_character(cursor
[1]))
629 /* Copying the text into the element struct */
630 element
->type
= TEXT
;
631 element
->line
= skin_line
;
632 element
->next
= NULL
;
633 element
->text
= skin_alloc_string(length
);
635 for(dest
= 0; dest
< length
; dest
++)
637 /* Advancing cursor if we've encountered an escaped character */
638 if(*cursor
== TAGSYM
)
641 element
->text
[dest
] = *cursor
;
644 element
->text
[length
] = '\0';
651 int skin_parse_conditional(struct skin_element
* element
, char** document
)
654 char* cursor
= *document
+ 1; /* Starting past the "%" */
656 struct skin_element
* tag
= skin_alloc_element(); /* The tag to evaluate */
661 element
->type
= CONDITIONAL
;
662 element
->line
= skin_line
;
664 /* Parsing the tag first */
665 if(!skin_parse_tag(tag
, &cursor
))
668 /* Counting the children */
669 if(*(cursor
++) != ENUMLISTOPENSYM
)
671 skin_error(ARGLIST_EXPECTED
);
675 while(*cursor
!= ENUMLISTCLOSESYM
&& *cursor
!= '\n' && *cursor
!= '\0')
677 if(*cursor
== COMMENTSYM
)
679 skip_comment(&cursor
);
683 if(*cursor
== ENUMLISTOPENSYM
)
689 if(*cursor
== ENUMLISTOPENSYM
)
693 else if(*cursor
== ENUMLISTCLOSESYM
)
703 if(*cursor
== TAGSYM
)
706 if(*cursor
== '\0' || *cursor
== '\n')
712 if(*cursor
== ENUMLISTSEPERATESYM
)
719 /* Parsing the children */
720 element
->children_count
= children
+ 1; /* Make sure to include the tag */
721 element
->children
= skin_alloc_children(children
+ 1);
722 element
->children
[0] = tag
;
724 for(i
= 1; i
< children
+ 1; i
++)
726 element
->children
[i
] = skin_parse_code_as_arg(&cursor
);
727 skip_whitespace(&cursor
);
729 if(i
< children
&& *cursor
!= ENUMLISTSEPERATESYM
)
731 skin_error(SEPERATOR_EXPECTED
);
734 else if(i
== children
&& *cursor
!= ENUMLISTCLOSESYM
)
736 skin_error(CLOSE_EXPECTED
);
750 int skin_parse_comment(struct skin_element
* element
, char** document
)
752 char* cursor
= *document
;
756 * Finding the index of the ending newline or null-terminator
757 * The length of the string of interest doesn't include the leading #, the
758 * length we need to reserve is the same as the index of the last character
760 for(length
= 0; cursor
[length
] != '\n' && cursor
[length
] != '\0'; length
++);
762 element
->type
= COMMENT
;
763 element
->line
= skin_line
;
764 element
->text
= skin_alloc_string(length
);
765 /* We copy from one char past cursor to leave out the # */
766 memcpy((void*)(element
->text
), (void*)(cursor
+ 1),
767 sizeof(char) * (length
-1));
768 element
->text
[length
- 1] = '\0';
770 if(cursor
[length
] == '\n')
773 *document
+= (length
); /* Move cursor up past # and all text */
774 if(**document
== '\n')
780 struct skin_element
* skin_parse_code_as_arg(char** document
)
784 char* cursor
= *document
;
786 /* Checking for sublines */
787 while(*cursor
!= '\n' && *cursor
!= '\0')
789 if(*cursor
== MULTILINESYM
)
794 else if(*cursor
== TAGSYM
)
796 /* A ';' directly after a '%' doesn't count */
806 /* Advancing the cursor as normal */
812 return skin_parse_sublines_optional(document
, 1);
814 return skin_parse_line_optional(document
, 1);
818 /* Memory management */
819 struct skin_element
* skin_alloc_element()
824 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
826 int delta
= sizeof(struct skin_element
) / (sizeof(char) * 4);
828 /* If one block is partially filled, make sure to advance to the
829 * next one for the next allocation
831 if(sizeof(struct skin_element
) % (sizeof(char) * 4) != 0)
834 skin_current_block
+= delta
;
836 return (struct skin_element
*)retval
;
840 struct skin_element
* retval
= (struct skin_element
*)
841 malloc(sizeof(struct skin_element
));
843 retval
->params_count
= 0;
844 retval
->children_count
= 0;
850 struct skin_tag_parameter
* skin_alloc_params(int count
)
854 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
856 int delta
= sizeof(struct skin_tag_parameter
) / (sizeof(char) * 4);
859 /* Correcting uneven alignment */
860 if(count
* sizeof(struct skin_tag_parameter
) % (sizeof(char) * 4) != 0)
863 skin_current_block
+= delta
;
865 return (struct skin_tag_parameter
*) retval
;
869 return (struct skin_tag_parameter
*)malloc(sizeof(struct skin_tag_parameter
)
874 char* skin_alloc_string(int length
)
878 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
880 /* Checking alignment */
881 length
++; /* Accounting for the null terminator */
882 int delta
= length
/ 4;
886 skin_current_block
+= delta
;
888 if(skin_current_block
>= SKIN_MAX_MEMORY
/ 4)
889 skin_error(MEMORY_LIMIT_EXCEEDED
);
895 return (char*)malloc(sizeof(char) * (length
+ 1));
899 struct skin_element
** skin_alloc_children(int count
)
901 return (struct skin_element
**) malloc(sizeof(struct skin_element
*) * count
);
904 void skin_free_tree(struct skin_element
* root
)
908 /* First make the recursive call */
911 skin_free_tree(root
->next
);
914 if(root
->type
== TEXT
)
917 /* Then recursively free any children, before freeing their pointers */
918 for(i
= 0; i
< root
->children_count
; i
++)
919 skin_free_tree(root
->children
[i
]);
920 if(root
->children_count
> 0)
921 free(root
->children
);
923 /* Free any parameters, making sure to deallocate strings */
924 for(i
= 0; i
< root
->params_count
; i
++)
925 if(root
->params
[i
].type
== STRING
)
926 free(root
->params
[i
].data
.text
);
927 if(root
->params_count
> 0)
930 /* Finally, delete root's memory */