2 * Experimenta lMai Renderer -- MapCSS Lexer
4 * (c) 2014--2015 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;
20 static struct fastbuf fbbuf;
23 void css_error(char *err, ...)
27 char *msg = mp_vprintf(css_this->pool, err, args);
28 die("Error in %s, line %d: %s", css_this->filename, lino, msg);
31 void css_lex_open(void)
33 fb = bopen_file(css_this->filename, O_RDONLY, NULL);
37 void css_lex_close(void)
42 void css_lex_string(const char *str)
44 fbbuf_init_read(&fbbuf, (char *) str, strlen(str), 0);
51 struct mempool *mp = css_this->pool;
53 int c, next, len, tok;
55 if (tok = css_this->pushed_token)
57 css_this->pushed_token = 0;
71 if (c == ' ' || c == '\t')
76 if (c >= '0' && c <= '9' || c == '-' && next >= '0' && next <= '9')
82 p = mp_append_char(mp, p, c);
85 while (c >= '0' && c <= '9')
87 p = mp_append_char(mp, p, c);
92 p = mp_append_char(mp, p, c);
94 while (c >= '0' && c <= '9')
96 p = mp_append_char(mp, p, c);
100 p = mp_end_string(mp, p);
107 if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_')
109 // Alphabetical identifier
110 // FIXME: Identifiers starting with "-" are not supported
111 // FIXME: Unquoted identifiers should be always case-insensitive
113 while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_')
115 p = mp_append_char(mp, p, c);
118 p = mp_end_string(mp, p);
134 css_error("Unterminated string literal");
138 if (c == '\\' || c == '"')
143 css_error("Unknown backslash sequence \"\\%c\" in string literal", c);
145 p = mp_append_char(mp, p, c);
148 css_lval.s = mp_end_string(mp, p);
160 css_error("Unterminated comment");
177 else if (next == '/')
182 while (c >= 0 && c != '\n');
190 while (next >= '0' && next <= '9' || next >= 'a' && next <= 'f' || next >= 'A' && next <= 'F')
192 p = mp_append_char(mp, p, bgetc(fb));
195 p = mp_end_string(mp, p);
197 if (len != 3 && len != 6)
198 css_error("Invalid RGB literal");
202 // One-character operators
215 // Two-character operators
246 css_error("Invalid character \"%c\"", c);
251 color_t css_rgb_to_color(const char *rgb)
254 ASSERT(n == 3 || n == 6);
256 for (uns i=0; i<n; i++)
258 uns j = Cxvalue(rgb[i]);
260 color = (color << 4) | j;
262 color = (color << 8) | (j * 17);