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
);
160 else if(*cursor
== ARGLISTOPENSYM
)
162 skip_arglist(&cursor
);
164 else if(*cursor
== ENUMLISTOPENSYM
)
166 skip_arglist(&cursor
);
170 /* Advancing the cursor as normal */
179 to_write
= &(last
->next
);
183 *to_write
= skin_parse_sublines(&cursor
);
191 *to_write
= skin_parse_line(&cursor
);
198 /* Making sure last is at the end */
211 retval
->children
[retval
->children_count
- 1] = root
;
216 /* Auxiliary Parsing Functions */
218 struct skin_element
* skin_parse_line(char**document
)
221 return skin_parse_line_optional(document
, 0);
227 * If conditional is set to true, then this will break upon encountering
228 * SEPERATESYM. This should only be used when parsing a line inside a
229 * conditional, otherwise just use the wrapper function skin_parse_line()
231 struct skin_element
* skin_parse_line_optional(char** document
, int conditional
)
233 char* cursor
= *document
;
235 struct skin_element
* root
= NULL
;
236 struct skin_element
* current
= NULL
;
237 struct skin_element
* retval
= NULL
;
239 /* A wrapper for the line */
240 retval
= skin_alloc_element();
242 retval
->line
= skin_line
;
243 retval
->children_count
= 1;
244 retval
->children
= skin_alloc_children(1);
246 while(*cursor
!= '\n' && *cursor
!= '\0' && *cursor
!= MULTILINESYM
247 && !((*cursor
== ARGLISTSEPERATESYM
248 || *cursor
== ARGLISTCLOSESYM
249 || *cursor
== ENUMLISTSEPERATESYM
250 || *cursor
== ENUMLISTCLOSESYM
)
252 && !(check_viewport(cursor
) && cursor
!= *document
))
254 /* Allocating memory if necessary */
257 current
->next
= skin_alloc_element();
258 current
= current
->next
;
262 current
= skin_alloc_element();
266 /* Parsing the current element */
267 if(*cursor
== TAGSYM
&& cursor
[1] == CONDITIONSYM
)
269 if(!skin_parse_conditional(current
, &cursor
))
272 else if(*cursor
== TAGSYM
&& !find_escape_character(cursor
[1]))
274 if(!skin_parse_tag(current
, &cursor
))
277 else if(*cursor
== COMMENTSYM
)
279 if(!skin_parse_comment(current
, &cursor
))
284 if(!skin_parse_text(current
, &cursor
, conditional
))
289 /* Moving up the calling function's pointer */
292 retval
->children
[0] = root
;
296 struct skin_element
* skin_parse_sublines(char** document
)
298 return skin_parse_sublines_optional(document
, 0);
301 struct skin_element
* skin_parse_sublines_optional(char** document
,
304 struct skin_element
* retval
;
305 char* cursor
= *document
;
309 retval
= skin_alloc_element();
310 retval
->type
= SUBLINES
;
312 retval
->line
= skin_line
;
314 /* First we count the sublines */
315 while(*cursor
!= '\0' && *cursor
!= '\n'
316 && !((*cursor
== ARGLISTSEPERATESYM
317 || *cursor
== ARGLISTCLOSESYM
318 || *cursor
== ENUMLISTSEPERATESYM
319 || *cursor
== ENUMLISTCLOSESYM
)
321 && !(check_viewport(cursor
) && cursor
!= *document
))
323 if(*cursor
== COMMENTSYM
)
325 skip_comment(&cursor
);
327 else if(*cursor
== ENUMLISTOPENSYM
)
329 skip_enumlist(&cursor
);
331 else if(*cursor
== ARGLISTOPENSYM
)
333 skip_arglist(&cursor
);
335 else if(*cursor
== TAGSYM
)
338 if(*cursor
== '\0' || *cursor
== '\n')
342 else if(*cursor
== MULTILINESYM
)
353 /* ...and then we parse them */
354 retval
->children_count
= sublines
;
355 retval
->children
= skin_alloc_children(sublines
);
358 for(i
= 0; i
< sublines
; i
++)
360 retval
->children
[i
] = skin_parse_line_optional(&cursor
, conditional
);
361 skip_whitespace(&cursor
);
363 if(*cursor
!= MULTILINESYM
&& i
!= sublines
- 1)
365 skin_error(MULTILINE_EXPECTED
);
368 else if(i
!= sublines
- 1)
379 int skin_parse_tag(struct skin_element
* element
, char** document
)
382 char* cursor
= *document
+ 1;
387 struct tag_info
*tag
;
391 int star
= 0; /* Flag for the all-or-none option */
392 int req_args
; /* To mark when we enter optional arguments */
396 /* Checking the tag name */
397 tag_name
[0] = cursor
[0];
398 tag_name
[1] = cursor
[1];
401 /* First we check the two characters after the '%', then a single char */
402 tag
= find_tag(tag_name
);
407 tag
= find_tag(tag_name
);
417 skin_error(ILLEGAL_TAG
);
421 /* Copying basic tag info */
424 tag_args
= tag
->params
;
425 element
->line
= skin_line
;
427 /* Checking for the * flag */
428 if(tag_args
[0] == '*')
434 /* If this tag has no arguments, we can bail out now */
435 if(strlen(tag_args
) == 0
436 || (tag_args
[0] == '|' && *cursor
!= ARGLISTOPENSYM
)
437 || (star
&& *cursor
!= ARGLISTOPENSYM
))
443 /* Checking the number of arguments and allocating args */
444 if(*cursor
!= ARGLISTOPENSYM
&& tag_args
[0] != '|')
446 skin_error(ARGLIST_EXPECTED
);
455 while(*cursor
!= '\n' && *cursor
!= '\0' && *cursor
!= ARGLISTCLOSESYM
)
457 /* Skipping over escaped characters */
458 if(*cursor
== TAGSYM
)
465 else if(*cursor
== COMMENTSYM
)
467 skip_comment(&cursor
);
469 else if(*cursor
== ARGLISTOPENSYM
)
471 skip_arglist(&cursor
);
473 else if(*cursor
== ARGLISTSEPERATESYM
)
484 cursor
= bookmark
; /* Restoring the cursor */
485 element
->params_count
= num_args
;
486 element
->params
= skin_alloc_params(num_args
);
488 /* Now we have to actually parse each argument */
489 for(i
= 0; i
< num_args
; i
++)
491 /* Making sure we haven't run out of arguments */
492 if(*tag_args
== '\0')
494 skin_error(TOO_MANY_ARGS
);
498 /* Checking for the optional bar */
506 /* Scanning the arguments */
507 skip_whitespace(&cursor
);
510 /* Checking for comments */
511 if(*cursor
== COMMENTSYM
)
512 skip_comment(&cursor
);
514 /* Storing the type code */
515 element
->params
[i
].type_code
= *tag_args
;
517 /* Checking a nullable argument for null */
518 if(*cursor
== DEFAULTSYM
&& !isdigit(cursor
[1]))
520 if(islower(*tag_args
))
522 element
->params
[i
].type
= DEFAULT
;
527 skin_error(DEFAULT_NOT_ALLOWED
);
531 else if(tolower(*tag_args
) == 'i')
533 /* Scanning an int argument */
534 if(!isdigit(*cursor
) && *cursor
!= '-')
536 skin_error(INT_EXPECTED
);
540 element
->params
[i
].type
= NUMERIC
;
541 element
->params
[i
].data
.numeric
= scan_int(&cursor
);
543 else if(tolower(*tag_args
) == 's' || tolower(*tag_args
) == 'f')
545 /* Scanning a string argument */
546 element
->params
[i
].type
= STRING
;
547 element
->params
[i
].data
.text
= scan_string(&cursor
);
550 else if(tolower(*tag_args
) == 'c')
552 /* Recursively parsing a code argument */
553 element
->params
[i
].type
= CODE
;
554 element
->params
[i
].data
.code
= skin_parse_code_as_arg(&cursor
);
555 if(!element
->params
[i
].data
.code
)
559 skip_whitespace(&cursor
);
561 if(*cursor
!= ARGLISTSEPERATESYM
&& i
< num_args
- 1)
563 skin_error(SEPERATOR_EXPECTED
);
566 else if(*cursor
!= ARGLISTCLOSESYM
&& i
== num_args
- 1)
568 skin_error(CLOSE_EXPECTED
);
578 /* Checking for the optional bar */
588 /* Checking for a premature end */
589 if(*tag_args
!= '\0' && !optional
)
591 skin_error(INSUFFICIENT_ARGS
);
601 * If the conditional flag is set true, then parsing text will stop at an
602 * ARGLISTSEPERATESYM. Only set that flag when parsing within a conditional
604 int skin_parse_text(struct skin_element
* element
, char** document
,
607 char* cursor
= *document
;
613 /* First figure out how much text we're copying */
614 while(*cursor
!= '\0' && *cursor
!= '\n' && *cursor
!= MULTILINESYM
615 && *cursor
!= COMMENTSYM
616 && !((*cursor
== ARGLISTSEPERATESYM
617 || *cursor
== ARGLISTCLOSESYM
618 || *cursor
== ENUMLISTSEPERATESYM
619 || *cursor
== ENUMLISTCLOSESYM
)
622 /* Dealing with possibility of escaped characters */
623 if(*cursor
== TAGSYM
)
625 if(find_escape_character(cursor
[1]))
637 /* Copying the text into the element struct */
638 element
->type
= TEXT
;
639 element
->line
= skin_line
;
640 element
->next
= NULL
;
641 element
->text
= skin_alloc_string(length
);
643 for(dest
= 0; dest
< length
; dest
++)
645 /* Advancing cursor if we've encountered an escaped character */
646 if(*cursor
== TAGSYM
)
649 element
->text
[dest
] = *cursor
;
652 element
->text
[length
] = '\0';
659 int skin_parse_conditional(struct skin_element
* element
, char** document
)
662 char* cursor
= *document
+ 1; /* Starting past the "%" */
664 struct skin_element
* tag
= skin_alloc_element(); /* The tag to evaluate */
668 element
->type
= CONDITIONAL
;
669 element
->line
= skin_line
;
671 /* Parsing the tag first */
672 if(!skin_parse_tag(tag
, &cursor
))
675 /* Counting the children */
676 if(*(cursor
++) != ENUMLISTOPENSYM
)
678 skin_error(ARGLIST_EXPECTED
);
682 while(*cursor
!= ENUMLISTCLOSESYM
&& *cursor
!= '\n' && *cursor
!= '\0')
684 if(*cursor
== COMMENTSYM
)
686 skip_comment(&cursor
);
688 else if(*cursor
== ENUMLISTOPENSYM
)
690 skip_enumlist(&cursor
);
692 else if(*cursor
== TAGSYM
)
695 if(*cursor
== '\0' || *cursor
== '\n')
699 else if(*cursor
== ENUMLISTSEPERATESYM
)
711 /* Parsing the children */
712 element
->children_count
= children
+ 1; /* Make sure to include the tag */
713 element
->children
= skin_alloc_children(children
+ 1);
714 element
->children
[0] = tag
;
716 for(i
= 1; i
< children
+ 1; i
++)
718 element
->children
[i
] = skin_parse_code_as_arg(&cursor
);
719 skip_whitespace(&cursor
);
721 if(i
< children
&& *cursor
!= ENUMLISTSEPERATESYM
)
723 skin_error(SEPERATOR_EXPECTED
);
726 else if(i
== children
&& *cursor
!= ENUMLISTCLOSESYM
)
728 skin_error(CLOSE_EXPECTED
);
742 int skin_parse_comment(struct skin_element
* element
, char** document
)
744 char* cursor
= *document
;
748 * Finding the index of the ending newline or null-terminator
749 * The length of the string of interest doesn't include the leading #, the
750 * length we need to reserve is the same as the index of the last character
752 for(length
= 0; cursor
[length
] != '\n' && cursor
[length
] != '\0'; length
++);
754 element
->type
= COMMENT
;
755 element
->line
= skin_line
;
756 element
->text
= skin_alloc_string(length
);
757 /* We copy from one char past cursor to leave out the # */
758 memcpy((void*)(element
->text
), (void*)(cursor
+ 1),
759 sizeof(char) * (length
-1));
760 element
->text
[length
- 1] = '\0';
762 if(cursor
[length
] == '\n')
765 *document
+= (length
); /* Move cursor up past # and all text */
766 if(**document
== '\n')
772 struct skin_element
* skin_parse_code_as_arg(char** document
)
776 char* cursor
= *document
;
778 /* Checking for sublines */
779 while(*cursor
!= '\n' && *cursor
!= '\0')
781 if(*cursor
== MULTILINESYM
)
786 else if(*cursor
== TAGSYM
)
788 /* A ';' directly after a '%' doesn't count */
798 /* Advancing the cursor as normal */
804 return skin_parse_sublines_optional(document
, 1);
806 return skin_parse_line_optional(document
, 1);
810 /* Memory management */
811 struct skin_element
* skin_alloc_element()
816 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
818 int delta
= sizeof(struct skin_element
) / (sizeof(char) * 4);
820 /* If one block is partially filled, make sure to advance to the
821 * next one for the next allocation
823 if(sizeof(struct skin_element
) % (sizeof(char) * 4) != 0)
826 skin_current_block
+= delta
;
828 return (struct skin_element
*)retval
;
832 struct skin_element
* retval
= (struct skin_element
*)
833 malloc(sizeof(struct skin_element
));
835 retval
->params_count
= 0;
836 retval
->children_count
= 0;
842 struct skin_tag_parameter
* skin_alloc_params(int count
)
846 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
848 int delta
= sizeof(struct skin_tag_parameter
) / (sizeof(char) * 4);
851 /* Correcting uneven alignment */
852 if(count
* sizeof(struct skin_tag_parameter
) % (sizeof(char) * 4) != 0)
855 skin_current_block
+= delta
;
857 return (struct skin_tag_parameter
*) retval
;
861 return (struct skin_tag_parameter
*)malloc(sizeof(struct skin_tag_parameter
)
866 char* skin_alloc_string(int length
)
870 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
872 /* Checking alignment */
873 length
++; /* Accounting for the null terminator */
874 int delta
= length
/ 4;
878 skin_current_block
+= delta
;
880 if(skin_current_block
>= SKIN_MAX_MEMORY
/ 4)
881 skin_error(MEMORY_LIMIT_EXCEEDED
);
887 return (char*)malloc(sizeof(char) * (length
+ 1));
891 struct skin_element
** skin_alloc_children(int count
)
893 return (struct skin_element
**) malloc(sizeof(struct skin_element
*) * count
);
896 void skin_free_tree(struct skin_element
* root
)
900 /* First make the recursive call */
903 skin_free_tree(root
->next
);
906 if(root
->type
== TEXT
)
909 /* Then recursively free any children, before freeing their pointers */
910 for(i
= 0; i
< root
->children_count
; i
++)
911 skin_free_tree(root
->children
[i
]);
912 if(root
->children_count
> 0)
913 free(root
->children
);
915 /* Free any parameters, making sure to deallocate strings */
916 for(i
= 0; i
< root
->params_count
; i
++)
917 if(root
->params
[i
].type
== STRING
)
918 free(root
->params
[i
].data
.text
);
919 if(root
->params_count
> 0)
922 /* Finally, delete root's memory */