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_newline(struct skin_element
* element
, char** document
);
53 int skin_parse_comment(struct skin_element
* element
, char** document
);
54 struct skin_element
* skin_parse_code_as_arg(char** document
);
56 struct skin_element
* skin_parse(char* document
)
59 struct skin_element
* root
= NULL
;
60 struct skin_element
* last
= NULL
;
62 struct skin_element
** to_write
= 0;
64 char* cursor
= document
; /* Keeps track of location in the document */
68 while(*cursor
!= '\0')
74 to_write
= &(last
->next
);
77 *to_write
= skin_parse_viewport(&cursor
);
82 /* Making sure last is at the end */
92 struct skin_element
* skin_parse_viewport(char** document
)
95 struct skin_element
* root
= NULL
;
96 struct skin_element
* last
= NULL
;
97 struct skin_element
* retval
= NULL
;
99 retval
= skin_alloc_element();
100 retval
->type
= VIEWPORT
;
101 retval
->children
= skin_alloc_children(1);
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 while(*cursor
!= '\0' && !(check_viewport(cursor
) && cursor
!= *document
))
115 /* First, we check to see if this line will contain sublines */
118 while(*cursor
!= '\n' && *cursor
!= '\0'
119 && !(check_viewport(cursor
) && cursor
!= *document
))
121 if(*cursor
== MULTILINESYM
)
126 else if(*cursor
== TAGSYM
)
128 /* A ';' directly after a '%' doesn't count */
136 else if(*cursor
== COMMENTSYM
)
138 skip_comment(&cursor
);
142 /* Advancing the cursor as normal */
151 to_write
= &(last
->next
);
155 *to_write
= skin_alloc_element();
156 skin_parse_newline(*to_write
, &cursor
);
162 *to_write
= skin_parse_sublines(&cursor
);
170 *to_write
= skin_parse_line(&cursor
);
177 /* Making sure last is at the end */
185 retval
->children
[0] = root
;
190 /* Auxiliary Parsing Functions */
192 struct skin_element
* skin_parse_line(char**document
)
195 return skin_parse_line_optional(document
, 0);
201 * If conditional is set to true, then this will break upon encountering
202 * SEPERATESYM. This should only be used when parsing a line inside a
203 * conditional, otherwise just use the wrapper function skin_parse_line()
205 struct skin_element
* skin_parse_line_optional(char** document
, int conditional
)
207 char* cursor
= *document
;
209 struct skin_element
* root
= NULL
;
210 struct skin_element
* current
= NULL
;
211 struct skin_element
* retval
= NULL
;
213 /* A wrapper for the line */
214 retval
= skin_alloc_element();
216 retval
->line
= skin_line
;
217 retval
->children_count
= 1;
218 retval
->children
= skin_alloc_children(1);
220 while(*cursor
!= '\n' && *cursor
!= '\0' && *cursor
!= MULTILINESYM
221 && !((*cursor
== ARGLISTSEPERATESYM
222 || *cursor
== ARGLISTCLOSESYM
223 || *cursor
== ENUMLISTSEPERATESYM
224 || *cursor
== ENUMLISTCLOSESYM
)
226 && !(check_viewport(cursor
) && cursor
!= *document
))
228 /* Allocating memory if necessary */
231 current
->next
= skin_alloc_element();
232 current
= current
->next
;
236 current
= skin_alloc_element();
240 /* Parsing the current element */
241 if(*cursor
== TAGSYM
&& cursor
[1] == CONDITIONSYM
)
243 if(!skin_parse_conditional(current
, &cursor
))
246 else if(*cursor
== TAGSYM
)
248 if(!skin_parse_tag(current
, &cursor
))
251 else if(*cursor
== COMMENTSYM
)
253 if(!skin_parse_comment(current
, &cursor
))
258 if(!skin_parse_text(current
, &cursor
, conditional
))
263 /* Moving up the calling function's pointer */
266 retval
->children
[0] = root
;
270 struct skin_element
* skin_parse_sublines(char** document
)
272 return skin_parse_sublines_optional(document
, 0);
275 struct skin_element
* skin_parse_sublines_optional(char** document
,
278 struct skin_element
* retval
;
279 char* cursor
= *document
;
283 retval
= skin_alloc_element();
284 retval
->type
= SUBLINES
;
286 retval
->line
= skin_line
;
288 /* First we count the sublines */
289 while(*cursor
!= '\0' && *cursor
!= '\n'
290 && !((*cursor
== ARGLISTSEPERATESYM
291 || *cursor
== ARGLISTCLOSESYM
292 || *cursor
== ENUMLISTSEPERATESYM
293 || *cursor
== ENUMLISTCLOSESYM
)
295 && !(check_viewport(cursor
) && cursor
!= *document
))
297 if(*cursor
== COMMENTSYM
)
298 skip_comment(&cursor
);
300 /* Accounting for escaped subline symbols */
301 if(*cursor
== TAGSYM
)
304 if(*cursor
== '\0' || *cursor
== '\n')
308 if(*cursor
== MULTILINESYM
)
316 /* ...and then we parse them */
317 retval
->children_count
= sublines
;
318 retval
->children
= skin_alloc_children(sublines
);
321 for(i
= 0; i
< sublines
; i
++)
323 retval
->children
[i
] = skin_parse_line_optional(&cursor
, conditional
);
324 skip_whitespace(&cursor
);
326 if(*cursor
!= MULTILINESYM
&& i
!= sublines
- 1)
328 skin_error(MULTILINE_EXPECTED
);
331 else if(i
!= sublines
- 1)
342 int skin_parse_tag(struct skin_element
* element
, char** document
)
345 char* cursor
= *document
+ 1;
353 int star
= 0; /* Flag for the all-or-none option */
354 int req_args
; /* To mark when we enter optional arguments */
358 /* Checking the tag name */
359 tag_name
[0] = cursor
[0];
360 tag_name
[1] = cursor
[1];
363 /* First we check the two characters after the '%', then a single char */
364 tag_args
= find_tag(tag_name
);
369 tag_args
= find_tag(tag_name
);
379 skin_error(ILLEGAL_TAG
);
383 /* Copying basic tag info */
385 strcpy(element
->name
, tag_name
);
386 element
->line
= skin_line
;
388 /* Checking for the * flag */
389 if(tag_args
[0] == '*')
395 /* If this tag has no arguments, we can bail out now */
396 if(strlen(tag_args
) == 0
397 || (tag_args
[0] == '|' && *cursor
!= ARGLISTOPENSYM
))
403 /* Checking the number of arguments and allocating args */
404 if(*cursor
!= ARGLISTOPENSYM
&& tag_args
[0] != '|')
406 skin_error(ARGLIST_EXPECTED
);
414 for(bookmark
= cursor
; *cursor
!= '\n' && *cursor
!= '\0' &&
415 *cursor
!= ARGLISTCLOSESYM
; cursor
++)
417 /* Skipping over escaped characters */
418 if(*cursor
== TAGSYM
)
425 /* Skipping comments */
426 if(*cursor
== COMMENTSYM
)
427 skip_comment(&cursor
);
429 /* Commas inside of contained lists don't count */
430 if(*cursor
== ARGLISTOPENSYM
)
431 while(*cursor
!= ARGLISTCLOSESYM
&& *cursor
!= '\0')
434 if(*cursor
== ARGLISTSEPERATESYM
)
438 cursor
= bookmark
; /* Restoring the cursor */
439 element
->params_count
= num_args
;
440 element
->params
= skin_alloc_params(num_args
);
442 /* Now we have to actually parse each argument */
443 for(i
= 0; i
< num_args
; i
++)
445 /* Making sure we haven't run out of arguments */
446 if(*tag_args
== '\0')
448 skin_error(TOO_MANY_ARGS
);
452 /* Checking for the optional bar */
460 /* Scanning the arguments */
461 skip_whitespace(&cursor
);
464 /* Checking for comments */
465 if(*cursor
== COMMENTSYM
)
466 skip_comment(&cursor
);
468 /* Checking a nullable argument for null */
469 if(*cursor
== DEFAULTSYM
)
471 if(islower(*tag_args
))
473 element
->params
[i
].type
= DEFAULT
;
478 skin_error(DEFAULT_NOT_ALLOWED
);
482 else if(tolower(*tag_args
) == 'i')
484 /* Scanning an int argument */
485 if(!isdigit(*cursor
))
487 skin_error(INT_EXPECTED
);
491 element
->params
[i
].type
= NUMERIC
;
492 element
->params
[i
].data
.numeric
= scan_int(&cursor
);
494 else if(tolower(*tag_args
) == 's' || tolower(*tag_args
) == 'f')
496 /* Scanning a string argument */
497 element
->params
[i
].type
= STRING
;
498 element
->params
[i
].data
.text
= scan_string(&cursor
);
501 else if(tolower(*tag_args
) == 'c')
503 /* Recursively parsing a code argument */
504 element
->params
[i
].type
= CODE
;
505 element
->params
[i
].data
.code
= skin_parse_code_as_arg(&cursor
);
506 if(!element
->params
[i
].data
.code
)
510 skip_whitespace(&cursor
);
512 if(*cursor
!= ARGLISTSEPERATESYM
&& i
< num_args
- 1)
514 skin_error(SEPERATOR_EXPECTED
);
517 else if(*cursor
!= ARGLISTCLOSESYM
&& i
== num_args
- 1)
519 skin_error(CLOSE_EXPECTED
);
529 /* Checking for the optional bar */
539 /* Checking for a premature end */
540 if(*tag_args
!= '\0' && !(optional
&& (!star
|| num_args
== req_args
)))
542 skin_error(INSUFFICIENT_ARGS
);
552 * If the conditional flag is set true, then parsing text will stop at an
553 * ARGLISTSEPERATESYM. Only set that flag when parsing within a conditional
555 int skin_parse_text(struct skin_element
* element
, char** document
,
558 char* cursor
= *document
;
564 /* First figure out how much text we're copying */
565 while(*cursor
!= '\0' && *cursor
!= '\n' && *cursor
!= MULTILINESYM
566 && *cursor
!= COMMENTSYM
567 && !((*cursor
== ARGLISTSEPERATESYM
568 || *cursor
== ARGLISTCLOSESYM
569 || *cursor
== ENUMLISTSEPERATESYM
570 || *cursor
== ENUMLISTCLOSESYM
)
573 /* Dealing with possibility of escaped characters */
574 if(*cursor
== TAGSYM
)
576 if(find_escape_character(cursor
[1]))
588 /* Copying the text into the element struct */
589 element
->type
= TEXT
;
590 element
->line
= skin_line
;
591 element
->next
= NULL
;
592 element
->text
= skin_alloc_string(length
);
594 for(dest
= 0; dest
< length
; dest
++)
596 /* Advancing cursor if we've encountered an escaped character */
597 if(*cursor
== TAGSYM
)
600 element
->text
[dest
] = *cursor
;
603 element
->text
[length
] = '\0';
610 int skin_parse_conditional(struct skin_element
* element
, char** document
)
613 char* cursor
= *document
+ 1; /* Starting past the "%" */
615 struct skin_element
* tag
= skin_alloc_element(); /* The tag to evaluate */
619 element
->type
= CONDITIONAL
;
620 element
->line
= skin_line
;
622 /* Parsing the tag first */
623 if(!skin_parse_tag(tag
, &cursor
))
626 /* Counting the children */
627 if(*(cursor
++) != ENUMLISTOPENSYM
)
629 skin_error(ARGLIST_EXPECTED
);
633 while(*cursor
!= ENUMLISTCLOSESYM
&& *cursor
!= '\n' && *cursor
!= '\0')
635 if(*cursor
== COMMENTSYM
)
637 skip_comment(&cursor
);
641 if(*cursor
== TAGSYM
)
644 if(*cursor
== '\0' || *cursor
== '\n')
650 if(*cursor
== ENUMLISTSEPERATESYM
)
657 /* Parsing the children */
658 element
->children_count
= children
+ 1; /* Make sure to include the tag */
659 element
->children
= skin_alloc_children(children
+ 1);
660 element
->children
[0] = tag
;
662 for(i
= 1; i
< children
+ 1; i
++)
664 element
->children
[i
] = skin_parse_code_as_arg(&cursor
);
665 skip_whitespace(&cursor
);
667 if(i
< children
&& *cursor
!= ENUMLISTSEPERATESYM
)
669 skin_error(SEPERATOR_EXPECTED
);
672 else if(i
== children
&& *cursor
!= ENUMLISTCLOSESYM
)
674 skin_error(CLOSE_EXPECTED
);
688 int skin_parse_newline(struct skin_element
* element
, char** document
)
691 char* cursor
= *document
;
694 skin_error(NEWLINE_EXPECTED
);
699 /* Assembling a skin_element struct for a newline */
700 element
->type
= NEWLINE
;
701 element
->line
= skin_line
;
703 element
->next
= NULL
;
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), sizeof(char) * length
);
727 element
->text
[length
] = '\0';
729 if(cursor
[length
] == '\n')
732 *document
+= (length
+ 1); /* Move cursor up past # and all text */
737 struct skin_element
* skin_parse_code_as_arg(char** document
)
741 char* cursor
= *document
;
743 /* Checking for sublines */
744 while(*cursor
!= '\n' && *cursor
!= '\0')
746 if(*cursor
== MULTILINESYM
)
751 else if(*cursor
== TAGSYM
)
753 /* A ';' directly after a '%' doesn't count */
763 /* Advancing the cursor as normal */
769 return skin_parse_sublines_optional(document
, 1);
771 return skin_parse_line_optional(document
, 1);
775 /* Memory management */
776 struct skin_element
* skin_alloc_element()
781 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
783 int delta
= sizeof(struct skin_element
) / (sizeof(char) * 4);
785 /* If one block is partially filled, make sure to advance to the
786 * next one for the next allocation
788 if(sizeof(struct skin_element
) % (sizeof(char) * 4) != 0)
791 skin_current_block
+= delta
;
793 return (struct skin_element
*)retval
;
797 struct skin_element
* retval
= (struct skin_element
*)
798 malloc(sizeof(struct skin_element
));
800 retval
->params_count
= 0;
801 retval
->children_count
= 0;
807 struct skin_tag_parameter
* skin_alloc_params(int count
)
811 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
813 int delta
= sizeof(struct skin_tag_parameter
) / (sizeof(char) * 4);
816 /* Correcting uneven alignment */
817 if(count
* sizeof(struct skin_tag_parameter
) % (sizeof(char) * 4) != 0)
820 skin_current_block
+= delta
;
822 return (struct skin_tag_parameter
*) retval
;
826 return (struct skin_tag_parameter
*)malloc(sizeof(struct skin_tag_parameter
)
831 char* skin_alloc_string(int length
)
835 char* retval
= &skin_parse_tree
[skin_current_block
* 4];
837 /* Checking alignment */
838 length
++; /* Accounting for the null terminator */
839 int delta
= length
/ 4;
843 skin_current_block
+= delta
;
845 if(skin_current_block
>= SKIN_MAX_MEMORY
/ 4)
846 skin_error(MEMORY_LIMIT_EXCEEDED
);
852 return (char*)malloc(sizeof(char) * (length
+ 1));
856 struct skin_element
** skin_alloc_children(int count
)
858 return (struct skin_element
**) malloc(sizeof(struct skin_element
*) * count
);
861 void skin_free_tree(struct skin_element
* root
)
865 /* First make the recursive call */
868 skin_free_tree(root
->next
);
871 if(root
->type
== TEXT
)
874 /* Then recursively free any children, before freeing their pointers */
875 for(i
= 0; i
< root
->children_count
; i
++)
876 skin_free_tree(root
->children
[i
]);
877 if(root
->children_count
> 0)
878 free(root
->children
);
880 /* Free any parameters, making sure to deallocate strings */
881 for(i
= 0; i
< root
->params_count
; i
++)
882 if(root
->params
[i
].type
== STRING
)
883 free(root
->params
[i
].data
.text
);
884 if(root
->params_count
> 0)
887 /* Finally, delete root's memory */