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 && strncmp(s1.start, s2.start, s1.len) == 0;
41 svaleq_prefix(struct sval s1, struct sval s2)
43 return s1.len <= s2.len && strncmp(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;
59 #define scanner_log(scanner, level, fmt, ...) \
60 xkb_log((scanner)->ctx, (level), 0, \
61 "%s:%u:%u: " fmt "\n", \
62 (scanner)->file_name, \
63 (scanner)->token_line, (scanner)->token_column, ##__VA_ARGS__)
65 #define scanner_err(scanner, fmt, ...) \
66 scanner_log(scanner, XKB_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
68 #define scanner_warn(scanner, fmt, ...) \
69 scanner_log(scanner, XKB_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
72 scanner_init(struct scanner *s, struct xkb_context *ctx,
73 const char *string, size_t len, const char *file_name)
78 s->line = s->column = 1;
79 s->token_line = s->token_column = 1;
80 s->file_name = file_name;
85 peek(struct scanner *s)
87 if (unlikely(s->pos >= s->len))
93 eof(struct scanner *s)
95 return s->pos >= s->len;
99 eol(struct scanner *s)
101 return peek(s) == '\n';
105 next(struct scanner *s)
107 if (unlikely(eof(s)))
109 if (unlikely(eol(s))) {
116 return s->s[s->pos++];
120 chr(struct scanner *s, char ch)
122 if (likely(peek(s) != ch))
124 s->pos++; s->column++;
129 str(struct scanner *s, const char *string, size_t len)
131 if (s->len - s->pos < len)
133 if (memcmp(s->s + s->pos, string, len) != 0)
135 s->pos += len; s->column += len;
139 #define lit(s, literal) str(s, literal, sizeof(literal) - 1)
142 buf_append(struct scanner *s, char ch)
144 if (s->buf_pos + 1 >= sizeof(s->buf))
146 s->buf[s->buf_pos++] = ch;
151 oct(struct scanner *s, uint8_t *out)
154 for (i = 0, *out = 0; peek(s) >= '0' && peek(s) <= '7' && i < 3; i++)
155 *out = *out * 8 + next(s) - '0';