2 * Hic Est Leo -- MapCSS Lexer
4 * (c) 2014--2015 Martin Mares <mj@ucw.cz>
10 #include "css-parse.h"
12 #include <ucw/chartype.h>
13 #include <ucw/fastbuf.h>
14 #include <ucw/mempool.h>
18 static struct fastbuf *fb;
19 static struct fastbuf fbbuf;
22 void css_error(const char *err, ...)
26 char *msg = mp_vprintf(css_this->pool, err, args);
27 die("Error in %s, line %d: %s", css_this->filename, lino, msg);
30 void css_lex_open(void)
32 fb = bopen_file(css_this->filename, O_RDONLY, NULL);
36 void css_lex_close(void)
41 void css_lex_string(const char *str)
43 fbbuf_init_read(&fbbuf, (char *) str, strlen(str), 0);
50 struct mempool *mp = css_this->pool;
52 int c, next, len, tok, nesting;
54 if (tok = css_this->pushed_token)
56 css_this->pushed_token = 0;
70 if (c == ' ' || c == '\t')
75 if (c >= '0' && c <= '9' || c == '-' && next >= '0' && next <= '9')
81 p = mp_append_char(mp, p, c);
84 while (c >= '0' && c <= '9')
86 p = mp_append_char(mp, p, c);
91 p = mp_append_char(mp, p, c);
93 while (c >= '0' && c <= '9')
95 p = mp_append_char(mp, p, c);
99 p = mp_end_string(mp, p);
106 if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_')
108 // Alphabetical identifier
109 // FIXME: Identifiers starting with "-" are not supported
110 // FIXME: Unquoted identifiers should be always case-insensitive
112 while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_')
114 p = mp_append_char(mp, p, c);
117 p = mp_end_string(mp, p);
133 css_error("Unterminated string literal");
137 if (c == '\\' || c == '"')
142 css_error("Unknown backslash sequence \"\\%c\" in string literal", c);
144 p = mp_append_char(mp, p, c);
147 css_lval.s = mp_end_string(mp, p);
159 css_error("Unterminated comment");
176 else if (next == '/')
181 while (c >= 0 && c != '\n');
189 while (next >= '0' && next <= '9' || next >= 'a' && next <= 'f' || next >= 'A' && next <= 'F')
191 p = mp_append_char(mp, p, bgetc(fb));
194 p = mp_end_string(mp, p);
196 if (len != 3 && len != 6)
197 css_error("Invalid RGB literal");
206 // FIXME p = mp_append_string(mp, p, "function () ");
213 css_error("Unterminated Lua block");
222 p = mp_append_char(mp, p, c);
224 css_lval.s = mp_end_string(mp, p);
227 // One-character operators
240 // Two-character operators
271 css_error("Invalid character \"%c\"", c);
276 color_t css_rgb_to_color(const char *rgb)
279 ASSERT(n == 3 || n == 6);
281 for (uns i=0; i<n; i++)
283 uns j = Cxvalue(rgb[i]);
285 color = (color << 4) | j;
287 color = (color << 8) | (j * 17);