1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker * Copyright © 2012 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker *
4*2b949d04SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker *
11*2b949d04SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker * Software.
14*2b949d04SAndroid Build Coastguard Worker *
15*2b949d04SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker */
23*2b949d04SAndroid Build Coastguard Worker
24*2b949d04SAndroid Build Coastguard Worker #ifndef XKBCOMP_SCANNER_UTILS_H
25*2b949d04SAndroid Build Coastguard Worker #define XKBCOMP_SCANNER_UTILS_H
26*2b949d04SAndroid Build Coastguard Worker
27*2b949d04SAndroid Build Coastguard Worker /* Point to some substring in the file; used to avoid copying. */
28*2b949d04SAndroid Build Coastguard Worker struct sval {
29*2b949d04SAndroid Build Coastguard Worker const char *start;
30*2b949d04SAndroid Build Coastguard Worker unsigned int len;
31*2b949d04SAndroid Build Coastguard Worker };
32*2b949d04SAndroid Build Coastguard Worker typedef darray(struct sval) darray_sval;
33*2b949d04SAndroid Build Coastguard Worker
34*2b949d04SAndroid Build Coastguard Worker static inline bool
svaleq(struct sval s1,struct sval s2)35*2b949d04SAndroid Build Coastguard Worker svaleq(struct sval s1, struct sval s2)
36*2b949d04SAndroid Build Coastguard Worker {
37*2b949d04SAndroid Build Coastguard Worker return s1.len == s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
38*2b949d04SAndroid Build Coastguard Worker }
39*2b949d04SAndroid Build Coastguard Worker
40*2b949d04SAndroid Build Coastguard Worker static inline bool
svaleq_prefix(struct sval s1,struct sval s2)41*2b949d04SAndroid Build Coastguard Worker svaleq_prefix(struct sval s1, struct sval s2)
42*2b949d04SAndroid Build Coastguard Worker {
43*2b949d04SAndroid Build Coastguard Worker return s1.len <= s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
44*2b949d04SAndroid Build Coastguard Worker }
45*2b949d04SAndroid Build Coastguard Worker
46*2b949d04SAndroid Build Coastguard Worker struct scanner {
47*2b949d04SAndroid Build Coastguard Worker const char *s;
48*2b949d04SAndroid Build Coastguard Worker size_t pos;
49*2b949d04SAndroid Build Coastguard Worker size_t len;
50*2b949d04SAndroid Build Coastguard Worker char buf[1024];
51*2b949d04SAndroid Build Coastguard Worker size_t buf_pos;
52*2b949d04SAndroid Build Coastguard Worker size_t line, column;
53*2b949d04SAndroid Build Coastguard Worker /* The line/column of the start of the current token. */
54*2b949d04SAndroid Build Coastguard Worker size_t token_line, token_column;
55*2b949d04SAndroid Build Coastguard Worker const char *file_name;
56*2b949d04SAndroid Build Coastguard Worker struct xkb_context *ctx;
57*2b949d04SAndroid Build Coastguard Worker void *priv;
58*2b949d04SAndroid Build Coastguard Worker };
59*2b949d04SAndroid Build Coastguard Worker
60*2b949d04SAndroid Build Coastguard Worker #define scanner_log(scanner, level, fmt, ...) \
61*2b949d04SAndroid Build Coastguard Worker xkb_log((scanner)->ctx, (level), 0, \
62*2b949d04SAndroid Build Coastguard Worker "%s:%zu:%zu: " fmt "\n", \
63*2b949d04SAndroid Build Coastguard Worker (scanner)->file_name, \
64*2b949d04SAndroid Build Coastguard Worker (scanner)->token_line, (scanner)->token_column, ##__VA_ARGS__)
65*2b949d04SAndroid Build Coastguard Worker
66*2b949d04SAndroid Build Coastguard Worker #define scanner_err(scanner, fmt, ...) \
67*2b949d04SAndroid Build Coastguard Worker scanner_log(scanner, XKB_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
68*2b949d04SAndroid Build Coastguard Worker
69*2b949d04SAndroid Build Coastguard Worker #define scanner_warn(scanner, fmt, ...) \
70*2b949d04SAndroid Build Coastguard Worker scanner_log(scanner, XKB_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
71*2b949d04SAndroid Build Coastguard Worker
72*2b949d04SAndroid Build Coastguard Worker static inline void
scanner_init(struct scanner * s,struct xkb_context * ctx,const char * string,size_t len,const char * file_name,void * priv)73*2b949d04SAndroid Build Coastguard Worker scanner_init(struct scanner *s, struct xkb_context *ctx,
74*2b949d04SAndroid Build Coastguard Worker const char *string, size_t len, const char *file_name,
75*2b949d04SAndroid Build Coastguard Worker void *priv)
76*2b949d04SAndroid Build Coastguard Worker {
77*2b949d04SAndroid Build Coastguard Worker s->s = string;
78*2b949d04SAndroid Build Coastguard Worker s->len = len;
79*2b949d04SAndroid Build Coastguard Worker s->pos = 0;
80*2b949d04SAndroid Build Coastguard Worker s->line = s->column = 1;
81*2b949d04SAndroid Build Coastguard Worker s->token_line = s->token_column = 1;
82*2b949d04SAndroid Build Coastguard Worker s->file_name = file_name;
83*2b949d04SAndroid Build Coastguard Worker s->ctx = ctx;
84*2b949d04SAndroid Build Coastguard Worker s->priv = priv;
85*2b949d04SAndroid Build Coastguard Worker }
86*2b949d04SAndroid Build Coastguard Worker
87*2b949d04SAndroid Build Coastguard Worker static inline char
peek(struct scanner * s)88*2b949d04SAndroid Build Coastguard Worker peek(struct scanner *s)
89*2b949d04SAndroid Build Coastguard Worker {
90*2b949d04SAndroid Build Coastguard Worker if (unlikely(s->pos >= s->len))
91*2b949d04SAndroid Build Coastguard Worker return '\0';
92*2b949d04SAndroid Build Coastguard Worker return s->s[s->pos];
93*2b949d04SAndroid Build Coastguard Worker }
94*2b949d04SAndroid Build Coastguard Worker
95*2b949d04SAndroid Build Coastguard Worker static inline bool
eof(struct scanner * s)96*2b949d04SAndroid Build Coastguard Worker eof(struct scanner *s)
97*2b949d04SAndroid Build Coastguard Worker {
98*2b949d04SAndroid Build Coastguard Worker return s->pos >= s->len;
99*2b949d04SAndroid Build Coastguard Worker }
100*2b949d04SAndroid Build Coastguard Worker
101*2b949d04SAndroid Build Coastguard Worker static inline bool
eol(struct scanner * s)102*2b949d04SAndroid Build Coastguard Worker eol(struct scanner *s)
103*2b949d04SAndroid Build Coastguard Worker {
104*2b949d04SAndroid Build Coastguard Worker return peek(s) == '\n';
105*2b949d04SAndroid Build Coastguard Worker }
106*2b949d04SAndroid Build Coastguard Worker
107*2b949d04SAndroid Build Coastguard Worker static inline void
skip_to_eol(struct scanner * s)108*2b949d04SAndroid Build Coastguard Worker skip_to_eol(struct scanner *s)
109*2b949d04SAndroid Build Coastguard Worker {
110*2b949d04SAndroid Build Coastguard Worker const char *nl = memchr(s->s + s->pos, '\n', s->len - s->pos);
111*2b949d04SAndroid Build Coastguard Worker const size_t new_pos = nl ? (size_t) (nl - s->s) : s->len;
112*2b949d04SAndroid Build Coastguard Worker s->column += new_pos - s->pos;
113*2b949d04SAndroid Build Coastguard Worker s->pos = new_pos;
114*2b949d04SAndroid Build Coastguard Worker }
115*2b949d04SAndroid Build Coastguard Worker
116*2b949d04SAndroid Build Coastguard Worker static inline char
next(struct scanner * s)117*2b949d04SAndroid Build Coastguard Worker next(struct scanner *s)
118*2b949d04SAndroid Build Coastguard Worker {
119*2b949d04SAndroid Build Coastguard Worker if (unlikely(eof(s)))
120*2b949d04SAndroid Build Coastguard Worker return '\0';
121*2b949d04SAndroid Build Coastguard Worker if (unlikely(eol(s))) {
122*2b949d04SAndroid Build Coastguard Worker s->line++;
123*2b949d04SAndroid Build Coastguard Worker s->column = 1;
124*2b949d04SAndroid Build Coastguard Worker }
125*2b949d04SAndroid Build Coastguard Worker else {
126*2b949d04SAndroid Build Coastguard Worker s->column++;
127*2b949d04SAndroid Build Coastguard Worker }
128*2b949d04SAndroid Build Coastguard Worker return s->s[s->pos++];
129*2b949d04SAndroid Build Coastguard Worker }
130*2b949d04SAndroid Build Coastguard Worker
131*2b949d04SAndroid Build Coastguard Worker static inline bool
chr(struct scanner * s,char ch)132*2b949d04SAndroid Build Coastguard Worker chr(struct scanner *s, char ch)
133*2b949d04SAndroid Build Coastguard Worker {
134*2b949d04SAndroid Build Coastguard Worker if (likely(peek(s) != ch))
135*2b949d04SAndroid Build Coastguard Worker return false;
136*2b949d04SAndroid Build Coastguard Worker s->pos++; s->column++;
137*2b949d04SAndroid Build Coastguard Worker return true;
138*2b949d04SAndroid Build Coastguard Worker }
139*2b949d04SAndroid Build Coastguard Worker
140*2b949d04SAndroid Build Coastguard Worker static inline bool
str(struct scanner * s,const char * string,size_t len)141*2b949d04SAndroid Build Coastguard Worker str(struct scanner *s, const char *string, size_t len)
142*2b949d04SAndroid Build Coastguard Worker {
143*2b949d04SAndroid Build Coastguard Worker if (s->len - s->pos < len)
144*2b949d04SAndroid Build Coastguard Worker return false;
145*2b949d04SAndroid Build Coastguard Worker if (memcmp(s->s + s->pos, string, len) != 0)
146*2b949d04SAndroid Build Coastguard Worker return false;
147*2b949d04SAndroid Build Coastguard Worker s->pos += len; s->column += len;
148*2b949d04SAndroid Build Coastguard Worker return true;
149*2b949d04SAndroid Build Coastguard Worker }
150*2b949d04SAndroid Build Coastguard Worker
151*2b949d04SAndroid Build Coastguard Worker #define lit(s, literal) str(s, literal, sizeof(literal) - 1)
152*2b949d04SAndroid Build Coastguard Worker
153*2b949d04SAndroid Build Coastguard Worker static inline bool
buf_append(struct scanner * s,char ch)154*2b949d04SAndroid Build Coastguard Worker buf_append(struct scanner *s, char ch)
155*2b949d04SAndroid Build Coastguard Worker {
156*2b949d04SAndroid Build Coastguard Worker if (s->buf_pos + 1 >= sizeof(s->buf))
157*2b949d04SAndroid Build Coastguard Worker return false;
158*2b949d04SAndroid Build Coastguard Worker s->buf[s->buf_pos++] = ch;
159*2b949d04SAndroid Build Coastguard Worker return true;
160*2b949d04SAndroid Build Coastguard Worker }
161*2b949d04SAndroid Build Coastguard Worker
162*2b949d04SAndroid Build Coastguard Worker static inline bool
buf_appends(struct scanner * s,const char * str)163*2b949d04SAndroid Build Coastguard Worker buf_appends(struct scanner *s, const char *str)
164*2b949d04SAndroid Build Coastguard Worker {
165*2b949d04SAndroid Build Coastguard Worker int ret;
166*2b949d04SAndroid Build Coastguard Worker ret = snprintf(s->buf + s->buf_pos, sizeof(s->buf) - s->buf_pos, "%s", str);
167*2b949d04SAndroid Build Coastguard Worker if (ret < 0 || (size_t) ret >= sizeof(s->buf) - s->buf_pos)
168*2b949d04SAndroid Build Coastguard Worker return false;
169*2b949d04SAndroid Build Coastguard Worker s->buf_pos += ret;
170*2b949d04SAndroid Build Coastguard Worker return true;
171*2b949d04SAndroid Build Coastguard Worker }
172*2b949d04SAndroid Build Coastguard Worker
173*2b949d04SAndroid Build Coastguard Worker static inline bool
oct(struct scanner * s,uint8_t * out)174*2b949d04SAndroid Build Coastguard Worker oct(struct scanner *s, uint8_t *out)
175*2b949d04SAndroid Build Coastguard Worker {
176*2b949d04SAndroid Build Coastguard Worker int i;
177*2b949d04SAndroid Build Coastguard Worker for (i = 0, *out = 0; peek(s) >= '0' && peek(s) <= '7' && i < 3; i++)
178*2b949d04SAndroid Build Coastguard Worker *out = *out * 8 + next(s) - '0';
179*2b949d04SAndroid Build Coastguard Worker return i > 0;
180*2b949d04SAndroid Build Coastguard Worker }
181*2b949d04SAndroid Build Coastguard Worker
182*2b949d04SAndroid Build Coastguard Worker static inline bool
hex(struct scanner * s,uint8_t * out)183*2b949d04SAndroid Build Coastguard Worker hex(struct scanner *s, uint8_t *out)
184*2b949d04SAndroid Build Coastguard Worker {
185*2b949d04SAndroid Build Coastguard Worker int i;
186*2b949d04SAndroid Build Coastguard Worker for (i = 0, *out = 0; is_xdigit(peek(s)) && i < 2; i++) {
187*2b949d04SAndroid Build Coastguard Worker const char c = next(s);
188*2b949d04SAndroid Build Coastguard Worker const char offset = (c >= '0' && c <= '9' ? '0' :
189*2b949d04SAndroid Build Coastguard Worker c >= 'a' && c <= 'f' ? 'a' - 10 : 'A' - 10);
190*2b949d04SAndroid Build Coastguard Worker *out = *out * 16 + c - offset;
191*2b949d04SAndroid Build Coastguard Worker }
192*2b949d04SAndroid Build Coastguard Worker return i > 0;
193*2b949d04SAndroid Build Coastguard Worker }
194*2b949d04SAndroid Build Coastguard Worker
195*2b949d04SAndroid Build Coastguard Worker #endif
196