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;
53 /* The line/column of the start of the current token. */
54 size_t token_line, token_column;
55 const char *file_name;
56 struct xkb_context *ctx;
60 #define scanner_log_with_code(scanner, level, log_msg_id, fmt, ...) \
61 xkb_log_with_code((scanner)->ctx, (level), 0, log_msg_id, \
62 "%s:%zu:%zu: " fmt "\n", \
63 (scanner)->file_name, \
64 (scanner)->token_line, \
65 (scanner)->token_column, ##__VA_ARGS__)
67 #define scanner_log(scanner, level, fmt, ...) \
68 xkb_log((scanner)->ctx, (level), 0, \
69 "%s:%zu:%zu: " fmt "\n", \
70 (scanner)->file_name, \
71 (scanner)->token_line, (scanner)->token_column, ##__VA_ARGS__)
73 #define scanner_err_with_code(scanner, id, fmt, ...) \
74 scanner_log_with_code(scanner, XKB_LOG_LEVEL_ERROR, id, fmt, ##__VA_ARGS__)
76 #define scanner_err(scanner, fmt, ...) \
77 scanner_log(scanner, XKB_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
79 #define scanner_warn_with_code(scanner, id, fmt, ...) \
80 scanner_log_with_code(scanner, XKB_LOG_LEVEL_WARNING, id, fmt, ##__VA_ARGS__)
82 #define scanner_warn(scanner, fmt, ...) \
83 scanner_log(scanner, XKB_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
86 scanner_init(struct scanner *s, struct xkb_context *ctx,
87 const char *string, size_t len, const char *file_name,
93 s->line = s->column = 1;
94 s->token_line = s->token_column = 1;
95 s->file_name = file_name;
101 scanner_peek(struct scanner *s)
103 if (unlikely(s->pos >= s->len))
109 scanner_eof(struct scanner *s)
111 return s->pos >= s->len;
115 scanner_eol(struct scanner *s)
117 return scanner_peek(s) == '\n';
121 scanner_skip_to_eol(struct scanner *s)
123 const char *nl = memchr(s->s + s->pos, '\n', s->len - s->pos);
124 const size_t new_pos = nl ? (size_t) (nl - s->s) : s->len;
125 s->column += new_pos - s->pos;
130 scanner_next(struct scanner *s)
132 if (unlikely(scanner_eof(s)))
134 if (unlikely(scanner_eol(s))) {
141 return s->s[s->pos++];
145 scanner_chr(struct scanner *s, char ch)
147 if (likely(scanner_peek(s) != ch))
149 s->pos++; s->column++;
154 scanner_str(struct scanner *s, const char *string, size_t len)
156 if (s->len - s->pos < len)
158 if (memcmp(s->s + s->pos, string, len) != 0)
160 s->pos += len; s->column += len;
164 #define scanner_lit(s, literal) scanner_str(s, literal, sizeof(literal) - 1)
167 scanner_buf_append(struct scanner *s, char ch)
169 if (s->buf_pos + 1 >= sizeof(s->buf))
171 s->buf[s->buf_pos++] = ch;
176 scanner_buf_appends(struct scanner *s, const char *str)
179 ret = snprintf(s->buf + s->buf_pos, sizeof(s->buf) - s->buf_pos, "%s", str);
180 if (ret < 0 || (size_t) ret >= sizeof(s->buf) - s->buf_pos)
187 scanner_oct(struct scanner *s, uint8_t *out)
190 for (i = 0, *out = 0; scanner_peek(s) >= '0' && scanner_peek(s) <= '7' && i < 3; i++)
193 *out = *out * 8 + scanner_next(s) - '0';
195 /* Consume valid digit, but mark result as invalid */
203 scanner_hex(struct scanner *s, uint8_t *out)
206 unsigned int result = 0;
207 for (i = 0, *out = 0; is_xdigit(scanner_peek(s)) && i < 2; i++) {
208 const char c = scanner_next(s);
209 const char offset = (c >= '0' && c <= '9' ? '0' :
210 c >= 'a' && c <= 'f' ? 'a' - 10 : 'A' - 10);
212 result = *out * 16 + c - offset;
213 *out = (uint8_t)result;