2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #ifndef XKBCOMP_SCANNER_UTILS_H
25 #define XKBCOMP_SCANNER_UTILS_H
27 /* Point to some substring in the file; used to avoid copying. */
32 typedef darray(struct sval) darray_sval;
35 svaleq(struct sval s1, struct sval s2)
37 return s1.len == s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
41 svaleq_prefix(struct sval s1, struct sval s2)
43 return s1.len <= s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
52 unsigned line, column;
53 /* The line/column of the start of the current token. */
54 unsigned token_line, token_column;
55 const char *file_name;
56 struct xkb_context *ctx;
60 #define scanner_log(scanner, level, fmt, ...) \
61 xkb_log((scanner)->ctx, (level), 0, \
62 "%s:%u:%u: " fmt "\n", \
63 (scanner)->file_name, \
64 (scanner)->token_line, (scanner)->token_column, ##__VA_ARGS__)
66 #define scanner_err(scanner, fmt, ...) \
67 scanner_log(scanner, XKB_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
69 #define scanner_warn(scanner, fmt, ...) \
70 scanner_log(scanner, XKB_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
73 scanner_init(struct scanner *s, struct xkb_context *ctx,
74 const char *string, size_t len, const char *file_name,
80 s->line = s->column = 1;
81 s->token_line = s->token_column = 1;
82 s->file_name = file_name;
88 peek(struct scanner *s)
90 if (unlikely(s->pos >= s->len))
96 eof(struct scanner *s)
98 return s->pos >= s->len;
102 eol(struct scanner *s)
104 return peek(s) == '\n';
108 skip_to_eol(struct scanner *s)
110 const char *nl = memchr(s->s + s->pos, '\n', s->len - s->pos);
111 const size_t new_pos = nl ? (size_t) (nl - s->s) : s->len;
112 s->column += new_pos - s->pos;
117 next(struct scanner *s)
119 if (unlikely(eof(s)))
121 if (unlikely(eol(s))) {
128 return s->s[s->pos++];
132 chr(struct scanner *s, char ch)
134 if (likely(peek(s) != ch))
136 s->pos++; s->column++;
141 str(struct scanner *s, const char *string, size_t len)
143 if (s->len - s->pos < len)
145 if (memcmp(s->s + s->pos, string, len) != 0)
147 s->pos += len; s->column += len;
151 #define lit(s, literal) str(s, literal, sizeof(literal) - 1)
154 buf_append(struct scanner *s, char ch)
156 if (s->buf_pos + 1 >= sizeof(s->buf))
158 s->buf[s->buf_pos++] = ch;
163 buf_appends(struct scanner *s, const char *str)
166 ret = snprintf(s->buf + s->buf_pos, sizeof(s->buf) - s->buf_pos, "%s", str);
167 if (ret < 0 || (size_t) ret >= sizeof(s->buf) - s->buf_pos)
174 oct(struct scanner *s, uint8_t *out)
177 for (i = 0, *out = 0; peek(s) >= '0' && peek(s) <= '7' && i < 3; i++)
178 *out = *out * 8 + next(s) - '0';
183 hex(struct scanner *s, uint8_t *out)
186 for (i = 0, *out = 0; is_xdigit(peek(s)) && i < 2; i++) {
187 const char c = next(s);
188 const char offset = (c >= '0' && c <= '9' ? '0' :
189 c >= 'a' && c <= 'f' ? 'a' - 10 : 'A' - 10);
190 *out = *out * 16 + c - offset;