2 * Experimenta lMai Renderer -- MapCSS Lexer
4 * (c) 2014 Martin Mares <mj@ucw.cz>
8 #include <ucw/chartype.h>
9 #include <ucw/fastbuf.h>
10 #include <ucw/mempool.h>
17 #include "css-parse.h"
19 static struct fastbuf *fb;
22 void css_error(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)
43 struct mempool *mp = css_this->pool;
57 if (c == ' ' || c == '\t')
62 if (c >= '0' && c <= '9' || c == '-' && next >= '0' && next <= '9')
68 p = mp_append_char(mp, p, c);
71 while (c >= '0' && c <= '9')
73 p = mp_append_char(mp, p, c);
78 p = mp_append_char(mp, p, c);
80 while (c >= '0' && c <= '9')
82 p = mp_append_char(mp, p, c);
86 p = mp_end_string(mp, p);
93 if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_')
95 // Alphabetical identifier
96 // FIXME: Identifiers starting with "-" are not supported
97 // FIXME: Unquoted identifiers should be always case-insensitive
99 while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_')
101 p = mp_append_char(mp, p, c);
104 p = mp_end_string(mp, p);
120 css_error("Unterminated string literal");
124 if (c == '\\' || c == '"')
129 css_error("Unknown backslash sequence \"\\%c\" in string literal", c);
131 p = mp_append_char(mp, p, c);
134 css_lval.s = mp_end_string(mp, p);
146 css_error("Unterminated comment");
163 else if (next == '/')
168 while (c >= 0 && c != '\n');
176 while (next >= '0' && next <= '9' || next >= 'a' && next <= 'f' || next >= 'A' && next <= 'F')
178 p = mp_append_char(mp, p, bgetc(fb));
181 p = mp_end_string(mp, p);
183 if (len != 3 && len != 6)
184 css_error("Invalid RGB literal");
188 // One-character operators
201 // Two-character operators
232 css_error("Invalid character \"%c\"", c);
237 color_t css_rgb_to_color(const char *rgb)
240 ASSERT(n == 3 || n == 6);
242 for (uns i=0; i<n; i++)
244 uns j = Cxvalue(rgb[i]);
246 color = (color << 4) | j;
248 color = (color << 8) | (j * 17);