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
29 /* Point to some substring in the file; used to avoid copying. */
34 typedef darray(struct sval) darray_sval;
37 svaleq(struct sval s1, struct sval s2)
39 return s1.len == s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
43 svaleq_prefix(struct sval s1, struct sval s2)
45 return s1.len <= s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
55 const char *file_name;
56 struct xkb_context *ctx;
60 scanner_init(struct scanner *s, struct xkb_context *ctx,
61 const char *string, size_t len, const char *file_name)
66 s->line = s->column = 1;
67 s->file_name = file_name;
72 peek(struct scanner *s)
74 return s->pos < s->len ? s->s[s->pos] : '\0';
78 eof(struct scanner *s)
80 return peek(s) == '\0';
84 eol(struct scanner *s)
86 return peek(s) == '\n';
90 * Use the check_nl variant when the current char might be a new line;
91 * just an optimization.
94 next(struct scanner *s)
105 return s->s[s->pos++];
109 chr(struct scanner *s, char ch)
113 s->pos++; s->column++;
118 str(struct scanner *s, const char *string, size_t len)
120 if (s->len - s->pos < len)
122 if (strncasecmp(s->s + s->pos, string, len) != 0)
124 s->pos += len; s->column += len;
128 #define lit(s, literal) str(s, literal, sizeof(literal) - 1)
131 buf_append(struct scanner *s, char ch)
133 if (s->buf_pos + 1 >= sizeof(s->buf))
135 s->buf[s->buf_pos++] = ch;
140 oct(struct scanner *s, uint8_t *out)
143 for (i = 0, *out = 0; peek(s) >= '0' && peek(s) <= '7' && i < 3; i++)
144 *out = *out * 8 + next(s) - '0';