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.
26 #include "xkbcomp-priv.h"
27 #include "parser-priv.h"
28 #include "scanner-utils.h"
31 number(struct scanner *s, int64_t *out, int *out_tok)
33 bool is_float = false, is_hex = false;
34 const char *start = s->s + s->pos;
38 while (is_xdigit(peek(s))) next(s);
42 while (is_digit(peek(s))) next(s);
43 is_float = chr(s, '.');
44 while (is_digit(peek(s))) next(s);
46 if (s->s + s->pos == start)
51 *out = strtoul(start, &end, 16);
53 *out = strtod(start, &end);
55 *out = strtoul(start, &end, 10);
56 if (errno != 0 || s->s + s->pos != end)
59 *out_tok = (is_float ? FLOAT : INTEGER);
64 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
68 skip_more_whitespace_and_comments:
70 while (is_space(peek(s))) next(s);
73 if (lit(s, "//") || chr(s, '#')) {
75 goto skip_more_whitespace_and_comments;
78 /* See if we're done. */
79 if (eof(s)) return END_OF_FILE;
82 s->token_line = s->line;
83 s->token_column = s->column;
88 while (!eof(s) && !eol(s) && peek(s) != '\"') {
91 if (chr(s, '\\')) buf_append(s, '\\');
92 else if (chr(s, 'n')) buf_append(s, '\n');
93 else if (chr(s, 't')) buf_append(s, '\t');
94 else if (chr(s, 'r')) buf_append(s, '\r');
95 else if (chr(s, 'b')) buf_append(s, '\b');
96 else if (chr(s, 'f')) buf_append(s, '\f');
97 else if (chr(s, 'v')) buf_append(s, '\v');
98 else if (chr(s, 'e')) buf_append(s, '\033');
99 else if (oct(s, &o)) buf_append(s, (char) o);
101 scanner_warn(s, "unknown escape sequence in string literal");
105 buf_append(s, next(s));
108 if (!buf_append(s, '\0') || !chr(s, '\"')) {
109 scanner_err(s, "unterminated string literal");
112 yylval->str = strdup(s->buf);
118 /* Key name literal. */
120 while (is_graph(peek(s)) && peek(s) != '>')
121 buf_append(s, next(s));
122 if (!buf_append(s, '\0') || !chr(s, '>')) {
123 scanner_err(s, "unterminated key name literal");
126 /* Empty key name literals are allowed. */
127 yylval->atom = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
131 /* Operators and punctuation. */
132 if (chr(s, ';')) return SEMI;
133 if (chr(s, '{')) return OBRACE;
134 if (chr(s, '}')) return CBRACE;
135 if (chr(s, '=')) return EQUALS;
136 if (chr(s, '[')) return OBRACKET;
137 if (chr(s, ']')) return CBRACKET;
138 if (chr(s, '(')) return OPAREN;
139 if (chr(s, ')')) return CPAREN;
140 if (chr(s, '.')) return DOT;
141 if (chr(s, ',')) return COMMA;
142 if (chr(s, '+')) return PLUS;
143 if (chr(s, '-')) return MINUS;
144 if (chr(s, '*')) return TIMES;
145 if (chr(s, '/')) return DIVIDE;
146 if (chr(s, '!')) return EXCLAM;
147 if (chr(s, '~')) return INVERT;
150 if (is_alpha(peek(s)) || peek(s) == '_') {
152 while (is_alnum(peek(s)) || peek(s) == '_')
153 buf_append(s, next(s));
154 if (!buf_append(s, '\0')) {
155 scanner_err(s, "identifier too long");
160 tok = keyword_to_token(s->buf, s->buf_pos - 1);
161 if (tok != -1) return tok;
163 yylval->str = strdup(s->buf);
169 /* Number literal (hexadecimal / decimal / float). */
170 if (number(s, &yylval->num, &tok)) {
171 if (tok == ERROR_TOK) {
172 scanner_err(s, "malformed number literal");
178 scanner_err(s, "unrecognized token");
183 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
184 const char *file_name, const char *map)
186 struct scanner scanner;
187 scanner_init(&scanner, ctx, string, len, file_name, NULL);
188 return parse(ctx, &scanner, map);
192 XkbParseFile(struct xkb_context *ctx, FILE *file,
193 const char *file_name, const char *map)
200 ok = map_file(file, &string, &size);
202 log_err(ctx, "Couldn't read XKB file %s: %s\n",
203 file_name, strerror(errno));
207 xkb_file = XkbParseString(ctx, string, size, file_name, map);
208 unmap_file(string, size);