fc231ab2b8dcdd28f59ebe8ff05ab8d7477f72b2
[platform/upstream/libxkbcommon.git] / src / scanner-utils.h
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #ifndef XKBCOMP_SCANNER_UTILS_H
25 #define XKBCOMP_SCANNER_UTILS_H
26
27 /* Point to some substring in the file; used to avoid copying. */
28 struct sval {
29     const char *start;
30     unsigned int len;
31 };
32 typedef darray(struct sval) darray_sval;
33
34 static inline bool
35 svaleq(struct sval s1, struct sval s2)
36 {
37     return s1.len == s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
38 }
39
40 static inline bool
41 svaleq_prefix(struct sval s1, struct sval s2)
42 {
43     return s1.len <= s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
44 }
45
46 struct scanner {
47     const char *s;
48     size_t pos;
49     size_t len;
50     char buf[1024];
51     size_t buf_pos;
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;
57 };
58
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__)
64
65 #define scanner_err(scanner, fmt, ...) \
66     scanner_log(scanner, XKB_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
67
68 #define scanner_warn(scanner, fmt, ...) \
69     scanner_log(scanner, XKB_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
70
71 static inline void
72 scanner_init(struct scanner *s, struct xkb_context *ctx,
73              const char *string, size_t len, const char *file_name)
74 {
75     s->s = string;
76     s->len = len;
77     s->pos = 0;
78     s->line = s->column = 1;
79     s->token_line = s->token_column = 1;
80     s->file_name = file_name;
81     s->ctx = ctx;
82 }
83
84 static inline char
85 peek(struct scanner *s)
86 {
87     if (unlikely(s->pos >= s->len))
88         return '\0';
89     return s->s[s->pos];
90 }
91
92 static inline bool
93 eof(struct scanner *s)
94 {
95     return s->pos >= s->len;
96 }
97
98 static inline bool
99 eol(struct scanner *s)
100 {
101     return peek(s) == '\n';
102 }
103
104 static inline char
105 next(struct scanner *s)
106 {
107     if (unlikely(eof(s)))
108         return '\0';
109     if (unlikely(eol(s))) {
110         s->line++;
111         s->column = 1;
112     }
113     else {
114         s->column++;
115     }
116     return s->s[s->pos++];
117 }
118
119 static inline bool
120 chr(struct scanner *s, char ch)
121 {
122     if (likely(peek(s) != ch))
123         return false;
124     s->pos++; s->column++;
125     return true;
126 }
127
128 static inline bool
129 str(struct scanner *s, const char *string, size_t len)
130 {
131     if (s->len - s->pos < len)
132         return false;
133     if (memcmp(s->s + s->pos, string, len) != 0)
134         return false;
135     s->pos += len; s->column += len;
136     return true;
137 }
138
139 #define lit(s, literal) str(s, literal, sizeof(literal) - 1)
140
141 static inline bool
142 buf_append(struct scanner *s, char ch)
143 {
144     if (s->buf_pos + 1 >= sizeof(s->buf))
145         return false;
146     s->buf[s->buf_pos++] = ch;
147     return true;
148 }
149
150 static inline bool
151 oct(struct scanner *s, uint8_t *out)
152 {
153     int i;
154     for (i = 0, *out = 0; peek(s) >= '0' && peek(s) <= '7' && i < 3; i++)
155         *out = *out * 8 + next(s) - '0';
156     return i > 0;
157 }
158
159 #endif