skin_engine: rework the parser to be closer to the langauge grammar.
[maemo-rb.git] / lib / skin_parser / skin_scan.c
blobf93606d54d8fa4713b6d906dcb1e2cfa6002b0f0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "skin_scan.h"
28 #include "skin_debug.h"
29 #include "symbols.h"
30 #include "skin_parser.h"
31 #include "tag_table.h"
33 /* Scanning Functions */
35 /* Simple function to advance a char* past a comment */
36 void skip_comment(const char** document)
38 while(**document != '\n' && **document != '\0')
39 (*document)++;
40 if(**document == '\n')
41 (*document)++;
44 void skip_tag(const char** document)
46 char tag_name[MAX_TAG_LENGTH];
47 int i;
48 bool qmark;
49 const struct tag_info *tag;
50 const char *cursor;
52 if(**document == TAGSYM)
53 (*document)++;
54 qmark = (**document == CONDITIONSYM);
55 if (qmark)
56 (*document)++;
58 if (!qmark && find_escape_character(**document))
60 (*document)++;
62 else
64 cursor = *document;
66 /* Checking the tag name */
67 for (i=0; cursor[i] && i<MAX_TAG_LENGTH; i++)
68 tag_name[i] = cursor[i];
70 /* First we check the two characters after the '%', then a single char */
71 tag = NULL;
72 i = MAX_TAG_LENGTH;
73 while (!tag && i > 1)
75 tag_name[i-1] = '\0';
76 tag = find_tag(tag_name);
77 i--;
80 if (tag)
82 *document += strlen(tag->name);
85 if (**document == ARGLISTOPENSYM)
86 skip_arglist(document);
88 if (**document == ENUMLISTOPENSYM)
89 skip_enumlist(document);
92 void skip_arglist(const char** document)
94 if(**document == ARGLISTOPENSYM)
95 (*document)++;
96 while(**document && **document != ARGLISTCLOSESYM)
98 if(**document == TAGSYM)
99 skip_tag(document);
100 else if(**document == COMMENTSYM)
101 skip_comment(document);
102 else
103 (*document)++;
105 if(**document == ARGLISTCLOSESYM)
106 (*document)++;
109 void skip_enumlist(const char** document)
111 if(**document == ENUMLISTOPENSYM)
112 (*document)++;
113 while(**document && **document != ENUMLISTCLOSESYM)
115 if(**document == TAGSYM)
116 skip_tag(document);
117 else if(**document == COMMENTSYM)
118 skip_comment(document);
119 else
120 (*document)++;
123 if(**document == ENUMLISTCLOSESYM)
124 (*document)++;
127 char* scan_string(const char** document)
130 const char* cursor = *document;
131 int length = 0;
132 char* buffer = NULL;
133 int i;
135 while(*cursor != ARGLISTSEPARATESYM && *cursor != ARGLISTCLOSESYM &&
136 *cursor != '\0')
138 if(*cursor == COMMENTSYM)
140 skip_comment(&cursor);
141 continue;
144 if(*cursor == TAGSYM)
145 cursor++;
147 if(*cursor == '\n')
149 skin_error(UNEXPECTED_NEWLINE, cursor);
150 return NULL;
153 length++;
154 cursor++;
157 /* Copying the string */
158 cursor = *document;
159 buffer = skin_alloc_string(length);
160 if (!buffer)
161 return NULL;
162 buffer[length] = '\0';
163 for(i = 0; i < length; i++)
165 if(*cursor == TAGSYM)
166 cursor++;
168 if(*cursor == COMMENTSYM)
170 skip_comment(&cursor);
171 i--;
172 continue;
175 buffer[i] = *cursor;
176 cursor++;
179 *document = cursor;
180 return buffer;
183 int scan_int(const char** document)
186 const char *cursor = *document, *end;
187 int length = 0;
188 char buffer[16];
189 int retval;
190 int i;
192 while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-')
194 if(*cursor == COMMENTSYM)
196 skip_comment(&cursor);
197 continue;
200 length++;
201 cursor++;
203 if (length > 15)
204 length = 15;
205 end = cursor;
206 /* Copying to the buffer while avoiding comments */
207 cursor = *document;
208 buffer[length] = '\0';
209 for(i = 0; i < length; i++)
211 if(*cursor == COMMENTSYM)
213 skip_comment(&cursor);
214 i--;
215 continue;
218 buffer[i] = *cursor;
219 cursor++;
222 retval = atoi(buffer);
224 *document = end;
225 return retval;
228 int check_viewport(const char* document)
230 if(strlen(document) < 3)
231 return 0;
233 if(document[0] != TAGSYM)
234 return 0;
236 if(document[1] != 'V')
237 return 0;
239 if(document[2] != ARGLISTOPENSYM
240 && document[2] != 'l'
241 && document[2] != 'i')
242 return 0;
244 return 1;