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
);
120 retval
->children_count
= 1;
121 retval
->children
= skin_alloc_children(1);
125 while(*cursor
!= '\0' && !(check_viewport(cursor
) && cursor
!= *document
))
128 /* First, we check to see if this line will contain sublines */
131 while(*cursor
!= '\n' && *cursor
!= '\0'
132 && !(check_viewport(cursor
) && cursor
!= *document
))
134 if(*cursor
== MULTILINESYM
)
139 else if(*cursor
== TAGSYM
)
141 /* A ';' directly after a '%' doesn't count */
149 else if(*cursor
== COMMENTSYM
)
151 skip_comment(&cursor
);
155 /* Advancing the cursor as normal */
164 to_write
= &(last
->next
);
168 *to_write
= skin_parse_sublines(&cursor
);
176 *to_write
= skin_parse_line(&cursor
);
183 /* Making sure last is at the end */
194 retval
->children
[retval
->children_count
- 1] = root
;
199 /* Auxiliary Parsing Functions */
201 struct skin_element
* skin_parse_line(char**document
)
204 return skin_parse_line_optional(document
, 0);
210 * If conditional is set to true, then this will break upon encountering
211 * SEPERATESYM. This should only be used when parsing a line inside a
212 * conditional, otherwise just use the wrapper function skin_parse_line()
214 struct skin_element
* skin_parse_line_optional(char** document
, int conditional
)
216 char* cursor
= *document
;
218 struct skin_element
* root
= NULL
;
219 struct skin_element
* current
= NULL
;
220 struct skin_element
* retval
= NULL
;
222 /* A wrapper for the line */
223 retval
= skin_alloc_element();
225 retval
->line
= skin_line
;
226 retval
->children_count
= 1;
227 retval
->children
= skin_alloc_children(1);
229 while(*cursor
!= '\n' && *cursor
!= '\0' && *cursor
!= MULTILINESYM
230 && !((*cursor
== ARGLISTSEPERATESYM
231 || *cursor
== ARGLISTCLOSESYM
232 || *cursor
== ENUMLISTSEPERATESYM
233 || *cursor
== ENUMLISTCLOSESYM
)
235 && !(check_viewport(cursor
) && cursor
!= *document
))
237 /* Allocating memory if necessary */
240 current
->next
= skin_alloc_element();
241 current
= current
->next
;
245 current
= skin_alloc_element();
249 /* Parsing the current element */
250 if(*cursor
== TAGSYM
&& cursor
[1] == CONDITIONSYM
)
252 if(!skin_parse_conditional(current
, &cursor
))
255 else if(*cursor
== TAGSYM
&& !find_escape_character(cursor
[1]))
257 if(!skin_parse_tag(current
, &cursor
))
260 else if(*cursor
== COMMENTSYM
)
262 if(!skin_parse_comment(current
, &cursor
))
267 if(!skin_parse_text(current
, &cursor
, conditional
))
272 /* Moving up the calling function's pointer */
275 retval
->children
[0] = root
;
279 struct skin_element
* skin_parse_sublines(char** document
)
281 return skin_parse_sublines_optional(document
, 0);
284 struct skin_element
* skin_parse_sublines_optional(char** document
,
287 struct skin_element
* retval
;
288 char* cursor
= *document
;
292 retval
= skin_alloc_element();
293 retval
->type
= SUBLINES
;
295 retval
->line
= skin_line
;
297 /* First we count the sublines */
298 while(*cursor
!= '\0' && *cursor
!= '\n'
299 && !((*cursor
== ARGLISTSEPERATESYM
300 || *cursor
== ARGLISTCLOSESYM
301 || *cursor
== ENUMLISTSEPERATESYM
302 || *cursor
== ENUMLISTCLOSESYM
)
304 && !(check_viewport(cursor
) && cursor
!= *document
))
306 if(*cursor
== COMMENTSYM
)
307 skip_comment(&cursor
);
309 /* Accounting for escaped subline symbols */
310 if(*cursor
== TAGSYM
)
313 if(*cursor
== '\0' || *cursor
== '\n')
317 if(*cursor
== MULTILINESYM
)
325 /* ...and then we parse them */
326 retval
->children_count
= sublines
;
327 retval
->children
= skin_alloc_children(sublines
);
330 for(i
= 0; i
< sublines
; i
++)
332 retval
->children
[i
] = skin_parse_line_optional(&cursor
, conditional
);
333 skip_whitespace(&cursor
);
335 if(*cursor
!= MULTILINESYM
&& i
!= sublines
- 1)
337 skin_error(MULTILINE_EXPECTED
);
340 else if(i
!= sublines
- 1)
351 int skin_parse_tag(struct skin_element
* element
, char** document
)
354 char* cursor
= *document
+ 1;
359 struct tag_info
*tag
;
363 int star
= 0; /* Flag for the all-or-none option */
364 int req_args
; /* To mark when we enter optional arguments */
368 /* Checking the tag name */
369 tag_name
[0] = cursor
[0];
370 tag_name
[1] = cursor
[1];
373 /* First we check the two characters after the '%', then a single char */
374 tag
= find_tag(tag_name
);
379 tag
= find_tag(tag_name
);
389 skin_error(ILLEGAL_TAG
);
393 /* Copying basic tag info */
396 tag_args
= tag
->params
;
397 element
->line
= skin_line
;
399 /* Checking for the * flag */
400 if(tag_args
[0] == '*')
406 /* If this tag has no arguments, we can bail out now */
407 if(strlen(tag_args
) == 0
408 || (tag_args
[0] == '|' && *cursor
!= ARGLISTOPENSYM
)
409 || (star
&& *cursor
!= ARGLISTOPENSYM
))
415 /* Checking the number of arguments and allocating args */
416 if(*cursor
!= ARGLISTOPENSYM
&& tag_args
[0] != '|')
418 skin_error(ARGLIST_EXPECTED
);
426 for(bookmark
= cursor
; *cursor
!= '\n' && *cursor
!= '\0' &&
427 *cursor
!= ARGLISTCLOSESYM
; cursor
++)
429 /* Skipping over escaped characters */
430 if(*cursor
== TAGSYM
)
437 /* Skipping comments */
438 if(*cursor
== COMMENTSYM
)
439 skip_comment(&cursor
);
441 /* Commas inside of contained lists don't count */
442 if(*cursor
== ARGLISTOPENSYM
)
443 while(*cursor
!= ARGLISTCLOSESYM
&& *cursor
!= '\0')
446 if(*cursor
== ARGLISTSEPERATESYM
)
450 cursor
= bookmark
; /* Restoring the cursor */
451 element
->params_count
= num_args
;
452 element
->params
= skin_alloc_params(num_args
);
454 /* Now we have to actually parse each argument */
455 for(i
= 0; i
< num_args
; i
++)
457 /* Making sure we haven't run out of arguments */
458 if(*tag_args
== '\0')
460 skin_error(TOO_MANY_ARGS
);
464 /* Checking for the optional bar */
472 /* Scanning the arguments */
473 skip_whitespace(&cursor
);
476 /* Checking for comments */
477 if(*cursor
== COMMENTSYM
)
478 skip_comment(&cursor
);
480 /* Storing the type code */
481 element
->params
[i
].type_code
= *tag_args
;
483 /* Checking a nullable argument for null */
484 if(*cursor
== DEFAULTSYM
)
486 if(islower(*tag_args
))
488 element
->params
[i
].type
= DEFAULT
;
493 skin_error(DEFAULT_NOT_ALLOWED
);
497 else if(tolower(*tag_args
) == 'i')
499 /* Scanning an int argument */
500 if(!isdigit(*cursor
))
502 skin_error(INT_EXPECTED
);
506 element
->params
[i
].type
= NUMERIC
;
507 element
->params
[i
].data
.numeric
= scan_int(&cursor
);
509 else if(tolower(*tag_args
) == 's' || tolower(*tag_args
) == 'f')
511 /* Scanning a string argument */
512 element
->params
[i
].type
= STRING
;
513 element
->params
[i
].data
.text
= scan_string(&cursor
);
516 else if(tolower(*tag_args
) == 'c')
518 /* Recursively parsing a code argument */
519 element
->params
[i
].type
= CODE
;
520 element
->params
[i
].data
.code
= skin_parse_code_as_arg(&cursor
);
521 if(!element
->params
[i
].data
.code
)
525 skip_whitespace(&cursor
);
527 if(*cursor
!= ARGLISTSEPERATESYM
&& i
< num_args
- 1)
529 skin_error(SEPERATOR_EXPECTED
);
532 else if(*cursor
!= ARGLISTCLOSESYM
&& i
== num_args
- 1)
534 skin_error(CLOSE_EXPECTED
);
544 /* Checking for the optional bar */
554 /* Checking for a premature end */
555 if(*tag_args
!= '\0' && !optional
)
557 skin_error(INSUFFICIENT_ARGS
);
567 * If the conditional flag is set true, then parsing text will stop at an
568 * ARGLISTSEPERATESYM. Only set that flag when parsing within a conditional
570 int skin_parse_text(struct skin_element
* element
, char** document
,
573 char* cursor
= *document
;
579 /* First figure out how much text we're copying */
580 while(*cursor
!= '\0' && *cursor
!= '\n' && *cursor
!= MULTILINESYM
581 && *cursor
!= COMMENTSYM
582 && !((*cursor
== ARGLISTSEPERATESYM
583 || *cursor
== ARGLISTCLOSESYM
584 || *cursor
== ENUMLISTSEPERATESYM
585 || *cursor
== ENUMLISTCLOSESYM
)
588 /* Dealing with possibility of escaped characters */
589 if(*cursor
== TAGSYM
)
591 if(find_escape_character(cursor
[1]))
603 /* Copying the text into the element struct */
604 element
->type
= TEXT
;
605 element
->line
= skin_line
;
606 element
->next
= NULL
;
607 element
->text
= skin_alloc_string(length
);
609 for(dest
= 0; dest
< length
; dest
++)
611 /* Advancing cursor if we've encountered an escaped character */
612 if(*cursor
== TAGSYM
)
615 element
->text
[dest
] = *cursor
;
618 element
->text
[length
] = '\0';
625 int skin_parse_conditional(struct skin_element
* element
, char** document
)
628 char* cursor
= *document
+ 1; /* Starting past the "%" */
630 struct skin_element
* tag
= skin_alloc_element(); /* The tag to evaluate */
634 element
->type
= CONDITIONAL
;
635 element
->line
= skin_line
;
637 /* Parsing the tag first */
638 if(!skin_parse_tag(tag
, &cursor
))
641 /* Counting the children */
642 if(*(cursor
++) != ENUMLISTOPENSYM
)
644 skin_error(ARGLIST_EXPECTED
);
648 while(*cursor
!= ENUMLISTCLOSESYM
&& *cursor
!= '\n' && *cursor
!= '\0')
650 if(*cursor
== COMMENTSYM
)
652 skip_comment(&cursor
);
656 if(*cursor
== TAGSYM
)
659 if(*cursor
== '\0' || *cursor
== '\n')
665 if(*cursor
== ENUMLISTSEPERATESYM
)
672 /* Parsing the children */
673 element
->children_count
= children
+ 1; /* Make sure to include the tag */
674 element
->children
= skin_alloc_children(children
+ 1);
675 element
->children
[0] = tag
;
677 for(i
= 1; i
< children
+ 1; i
++)
679 element
->children
[i
] = skin_parse_code_as_arg(&cursor
);
680 skip_whitespace(&cursor
);
682 if(i
< children
&& *cursor
!= ENUMLISTSEPERATESYM
)
684 skin_error(SEPERATOR_EXPECTED
);
687 else if(i
== children
&& *cursor
!= ENUMLISTCLOSESYM
)
689 skin_error(CLOSE_EXPECTED
);
703 int skin_parse_comment(struct skin_element
* element
, char** document
)
705 char* cursor
= *document
;
709 * Finding the index of the ending newline or null-terminator
710 * The length of the string of interest doesn't include the leading #, the
711 * length we need to reserve is the same as the index of the last character
713 for(length
= 0; cursor
[length
] != '\n' && cursor
[length
] != '\0'; length
++);
715 element
->type
= COMMENT
;
716 element
->line
= skin_line
;
717 element
->text
= skin_alloc_string(length
);
718 /* We copy from one char past cursor to leave out the # */
719 memcpy((void*)(element
->text
), (void*)(cursor
+ 1),
720 sizeof(char) * (length
-1));
721 element
->text
[length
- 1] = '\0';
723 if(cursor
[length
] == '\n')
726 *document
+= (length
); /* Move cursor up past # and all text */
727 if(**document
== '\n')
733 struct skin_element
* skin_parse_code_as_arg(char** document
)
737 char* cursor
= *document
;
739 /* Checking for sublines */
740 while(*cursor
!= '\n' && *cursor
!= '\0')
742 if(*cursor
== MULTILINESYM
)
747 else if(*cursor
== TAGSYM
)
749 /* A ';' directly after a '%' doesn't count */
759 /* Advancing the cursor as normal */
765 return skin_parse_sublines_optional(document
, 1);
767 return skin_parse_line_optional(document
, 1);
771 /* Memory management */
772 struct skin_element
* skin_alloc_element()
777 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
779 int delta
= sizeof(struct skin_element
) / (sizeof(char) * 4);
781 /* If one block is partially filled, make sure to advance to the
782 * next one for the next allocation
784 if(sizeof(struct skin_element
) % (sizeof(char) * 4) != 0)
787 skin_current_block
+= delta
;
789 return (struct skin_element
*)retval
;
793 struct skin_element
* retval
= (struct skin_element
*)
794 malloc(sizeof(struct skin_element
));
796 retval
->params_count
= 0;
797 retval
->children_count
= 0;
803 struct skin_tag_parameter
* skin_alloc_params(int count
)
807 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
809 int delta
= sizeof(struct skin_tag_parameter
) / (sizeof(char) * 4);
812 /* Correcting uneven alignment */
813 if(count
* sizeof(struct skin_tag_parameter
) % (sizeof(char) * 4) != 0)
816 skin_current_block
+= delta
;
818 return (struct skin_tag_parameter
*) retval
;
822 return (struct skin_tag_parameter
*)malloc(sizeof(struct skin_tag_parameter
)
827 char* skin_alloc_string(int length
)
831 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
833 /* Checking alignment */
834 length
++; /* Accounting for the null terminator */
835 int delta
= length
/ 4;
839 skin_current_block
+= delta
;
841 if(skin_current_block
>= SKIN_MAX_MEMORY
/ 4)
842 skin_error(MEMORY_LIMIT_EXCEEDED
);
848 return (char*)malloc(sizeof(char) * (length
+ 1));
852 struct skin_element
** skin_alloc_children(int count
)
854 return (struct skin_element
**) malloc(sizeof(struct skin_element
*) * count
);
857 void skin_free_tree(struct skin_element
* root
)
861 /* First make the recursive call */
864 skin_free_tree(root
->next
);
867 if(root
->type
== TEXT
)
870 /* Then recursively free any children, before freeing their pointers */
871 for(i
= 0; i
< root
->children_count
; i
++)
872 skin_free_tree(root
->children
[i
]);
873 if(root
->children_count
> 0)
874 free(root
->children
);
876 /* Free any parameters, making sure to deallocate strings */
877 for(i
= 0; i
< root
->params_count
; i
++)
878 if(root
->params
[i
].type
== STRING
)
879 free(root
->params
[i
].data
.text
);
880 if(root
->params_count
> 0)
883 /* Finally, delete root's memory */