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
;
299 retval
= skin_alloc_element();
300 retval
->type
= SUBLINES
;
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
)
311 && !(check_viewport(cursor
) && cursor
!= *document
))
313 if(*cursor
== COMMENTSYM
)
314 skip_comment(&cursor
);
316 /* Accounting for escaped subline symbols */
317 if(*cursor
== TAGSYM
)
320 if(*cursor
== '\0' || *cursor
== '\n')
324 if(*cursor
== MULTILINESYM
)
332 /* ...and then we parse them */
333 retval
->children_count
= sublines
;
334 retval
->children
= skin_alloc_children(sublines
);
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
);
347 else if(i
!= sublines
- 1)
358 int skin_parse_tag(struct skin_element
* element
, char** document
)
361 char* cursor
= *document
+ 1;
366 struct tag_info
*tag
;
370 int star
= 0; /* Flag for the all-or-none option */
371 int req_args
; /* To mark when we enter optional arguments */
375 /* Checking the tag name */
376 tag_name
[0] = cursor
[0];
377 tag_name
[1] = cursor
[1];
380 /* First we check the two characters after the '%', then a single char */
381 tag
= find_tag(tag_name
);
386 tag
= find_tag(tag_name
);
396 skin_error(ILLEGAL_TAG
);
400 /* Copying basic tag info */
403 tag_args
= tag
->params
;
404 element
->line
= skin_line
;
406 /* Checking for the * flag */
407 if(tag_args
[0] == '*')
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
))
422 /* Checking the number of arguments and allocating args */
423 if(*cursor
!= ARGLISTOPENSYM
&& tag_args
[0] != '|')
425 skin_error(ARGLIST_EXPECTED
);
433 for(bookmark
= cursor
; *cursor
!= '\n' && *cursor
!= '\0' &&
434 *cursor
!= ARGLISTCLOSESYM
; cursor
++)
436 /* Skipping over escaped characters */
437 if(*cursor
== TAGSYM
)
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')
453 if(*cursor
== ARGLISTSEPERATESYM
)
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
);
471 /* Checking for the optional bar */
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
;
500 skin_error(DEFAULT_NOT_ALLOWED
);
504 else if(tolower(*tag_args
) == 'i')
506 /* Scanning an int argument */
507 if(!isdigit(*cursor
) && *cursor
!= '-')
509 skin_error(INT_EXPECTED
);
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
)
532 skip_whitespace(&cursor
);
534 if(*cursor
!= ARGLISTSEPERATESYM
&& i
< num_args
- 1)
536 skin_error(SEPERATOR_EXPECTED
);
539 else if(*cursor
!= ARGLISTCLOSESYM
&& i
== num_args
- 1)
541 skin_error(CLOSE_EXPECTED
);
551 /* Checking for the optional bar */
561 /* Checking for a premature end */
562 if(*tag_args
!= '\0' && !optional
)
564 skin_error(INSUFFICIENT_ARGS
);
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
,
580 char* cursor
= *document
;
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
)
595 /* Dealing with possibility of escaped characters */
596 if(*cursor
== TAGSYM
)
598 if(find_escape_character(cursor
[1]))
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
)
622 element
->text
[dest
] = *cursor
;
625 element
->text
[length
] = '\0';
632 int skin_parse_conditional(struct skin_element
* element
, char** document
)
635 char* cursor
= *document
+ 1; /* Starting past the "%" */
637 struct skin_element
* tag
= skin_alloc_element(); /* The tag to evaluate */
641 element
->type
= CONDITIONAL
;
642 element
->line
= skin_line
;
644 /* Parsing the tag first */
645 if(!skin_parse_tag(tag
, &cursor
))
648 /* Counting the children */
649 if(*(cursor
++) != ENUMLISTOPENSYM
)
651 skin_error(ARGLIST_EXPECTED
);
655 while(*cursor
!= ENUMLISTCLOSESYM
&& *cursor
!= '\n' && *cursor
!= '\0')
657 if(*cursor
== COMMENTSYM
)
659 skip_comment(&cursor
);
663 if(*cursor
== TAGSYM
)
666 if(*cursor
== '\0' || *cursor
== '\n')
672 if(*cursor
== ENUMLISTSEPERATESYM
)
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
);
694 else if(i
== children
&& *cursor
!= ENUMLISTCLOSESYM
)
696 skin_error(CLOSE_EXPECTED
);
710 int skin_parse_comment(struct skin_element
* element
, char** document
)
712 char* cursor
= *document
;
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')
733 *document
+= (length
); /* Move cursor up past # and all text */
734 if(**document
== '\n')
740 struct skin_element
* skin_parse_code_as_arg(char** document
)
744 char* cursor
= *document
;
746 /* Checking for sublines */
747 while(*cursor
!= '\n' && *cursor
!= '\0')
749 if(*cursor
== MULTILINESYM
)
754 else if(*cursor
== TAGSYM
)
756 /* A ';' directly after a '%' doesn't count */
766 /* Advancing the cursor as normal */
772 return skin_parse_sublines_optional(document
, 1);
774 return skin_parse_line_optional(document
, 1);
778 /* Memory management */
779 struct skin_element
* skin_alloc_element()
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)
794 skin_current_block
+= delta
;
796 return (struct skin_element
*)retval
;
800 struct skin_element
* retval
= (struct skin_element
*)
801 malloc(sizeof(struct skin_element
));
803 retval
->params_count
= 0;
804 retval
->children_count
= 0;
810 struct skin_tag_parameter
* skin_alloc_params(int count
)
814 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
816 int delta
= sizeof(struct skin_tag_parameter
) / (sizeof(char) * 4);
819 /* Correcting uneven alignment */
820 if(count
* sizeof(struct skin_tag_parameter
) % (sizeof(char) * 4) != 0)
823 skin_current_block
+= delta
;
825 return (struct skin_tag_parameter
*) retval
;
829 return (struct skin_tag_parameter
*)malloc(sizeof(struct skin_tag_parameter
)
834 char* skin_alloc_string(int length
)
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;
846 skin_current_block
+= delta
;
848 if(skin_current_block
>= SKIN_MAX_MEMORY
/ 4)
849 skin_error(MEMORY_LIMIT_EXCEEDED
);
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
)
868 /* First make the recursive call */
871 skin_free_tree(root
->next
);
874 if(root
->type
== 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)
890 /* Finally, delete root's memory */