parser: drop %name-prefix, use -p yacc argument instead
[platform/upstream/libxkbcommon.git] / src / xkbcomp / 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 #include <ctype.h>
28
29 /* Point to some substring in the file; used to avoid copying. */
30 struct sval {
31     const char *start;
32     unsigned int len;
33 };
34 typedef darray(struct sval) darray_sval;
35
36 static inline bool
37 svaleq(struct sval s1, struct sval s2)
38 {
39     return s1.len == s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
40 }
41
42 static inline bool
43 svaleq_prefix(struct sval s1, struct sval s2)
44 {
45     return s1.len <= s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
46 }
47
48 struct scanner {
49     const char *s;
50     size_t pos;
51     size_t len;
52     char buf[1024];
53     size_t buf_pos;
54     int line, column;
55     const char *file_name;
56     struct xkb_context *ctx;
57 };
58
59 static inline void
60 scanner_init(struct scanner *s, struct xkb_context *ctx,
61              const char *string, size_t len, const char *file_name)
62 {
63     s->s = string;
64     s->len = len;
65     s->pos = 0;
66     s->line = s->column = 1;
67     s->file_name = file_name;
68     s->ctx = ctx;
69 }
70
71 static inline char
72 peek(struct scanner *s)
73 {
74     return s->pos < s->len ? s->s[s->pos] : '\0';
75 }
76
77 static inline bool
78 eof(struct scanner *s)
79 {
80     return peek(s) == '\0';
81 }
82
83 static inline bool
84 eol(struct scanner *s)
85 {
86     return peek(s) == '\n';
87 }
88
89 /*
90  * Use the check_nl variant when the current char might be a new line;
91  * just an optimization.
92  */
93 static inline char
94 next(struct scanner *s)
95 {
96     if (eof(s))
97         return '\0';
98     if (eol(s)) {
99         s->line++;
100         s->column = 1;
101     }
102     else {
103         s->column++;
104     }
105     return s->s[s->pos++];
106 }
107
108 static inline bool
109 chr(struct scanner *s, char ch)
110 {
111     if (peek(s) != ch)
112         return false;
113     s->pos++; s->column++;
114     return true;
115 }
116
117 static inline bool
118 str(struct scanner *s, const char *string, size_t len)
119 {
120     if (s->len - s->pos < len)
121         return false;
122     if (strncasecmp(s->s + s->pos, string, len) != 0)
123         return false;
124     s->pos += len; s->column += len;
125     return true;
126 }
127
128 #define lit(s, literal) str(s, literal, sizeof(literal) - 1)
129
130 static inline bool
131 buf_append(struct scanner *s, char ch)
132 {
133     if (s->buf_pos + 1 >= sizeof(s->buf))
134         return false;
135     s->buf[s->buf_pos++] = ch;
136     return true;
137 }
138
139 static inline bool
140 oct(struct scanner *s, uint8_t *out)
141 {
142     int i;
143     for (i = 0, *out = 0; peek(s) >= '0' && peek(s) <= '7' && i < 3; i++)
144         *out = *out * 8 + next(s) - '0';
145     return i > 0;
146 }
147
148 #endif