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 #include "xkbcomp-priv.h"
25 #include "parser-priv.h"
28 number(struct scanner *s, int64_t *out, int *out_tok)
30 bool is_float = false, is_hex = false;
31 const char *start = s->s + s->pos;
35 while (is_xdigit(peek(s))) next(s);
39 while (is_digit(peek(s))) next(s);
40 is_float = chr(s, '.');
41 while (is_digit(peek(s))) next(s);
43 if (s->s + s->pos == start)
48 *out = strtoul(start, &end, 16);
50 *out = strtod(start, &end);
52 *out = strtoul(start, &end, 10);
53 if (errno != 0 || s->s + s->pos != end)
56 *out_tok = (is_float ? FLOAT : INTEGER);
61 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
65 skip_more_whitespace_and_comments:
67 while (is_space(peek(s))) next(s);
70 if (lit(s, "//") || chr(s, '#')) {
71 while (!eof(s) && !eol(s)) next(s);
72 goto skip_more_whitespace_and_comments;
75 /* See if we're done. */
76 if (eof(s)) return END_OF_FILE;
79 s->token_line = s->line;
80 s->token_column = s->column;
85 while (!eof(s) && !eol(s) && peek(s) != '\"') {
88 if (chr(s, '\\')) buf_append(s, '\\');
89 else if (chr(s, 'n')) buf_append(s, '\n');
90 else if (chr(s, 't')) buf_append(s, '\t');
91 else if (chr(s, 'r')) buf_append(s, '\r');
92 else if (chr(s, 'b')) buf_append(s, '\b');
93 else if (chr(s, 'f')) buf_append(s, '\f');
94 else if (chr(s, 'v')) buf_append(s, '\v');
95 else if (chr(s, 'e')) buf_append(s, '\033');
96 else if (oct(s, &o)) buf_append(s, (char) o);
98 scanner_warn(s, "unknown escape sequence in string literal");
102 buf_append(s, next(s));
105 if (!buf_append(s, '\0') || !chr(s, '\"')) {
106 scanner_err(s, "unterminated string literal");
109 yylval->str = strdup(s->buf);
115 /* Key name literal. */
117 while (is_graph(peek(s)) && peek(s) != '>')
118 buf_append(s, next(s));
119 if (!buf_append(s, '\0') || !chr(s, '>')) {
120 scanner_err(s, "unterminated key name literal");
123 /* Empty key name literals are allowed. */
124 yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
128 /* Operators and punctuation. */
129 if (chr(s, ';')) return SEMI;
130 if (chr(s, '{')) return OBRACE;
131 if (chr(s, '}')) return CBRACE;
132 if (chr(s, '=')) return EQUALS;
133 if (chr(s, '[')) return OBRACKET;
134 if (chr(s, ']')) return CBRACKET;
135 if (chr(s, '(')) return OPAREN;
136 if (chr(s, ')')) return CPAREN;
137 if (chr(s, '.')) return DOT;
138 if (chr(s, ',')) return COMMA;
139 if (chr(s, '+')) return PLUS;
140 if (chr(s, '-')) return MINUS;
141 if (chr(s, '*')) return TIMES;
142 if (chr(s, '/')) return DIVIDE;
143 if (chr(s, '!')) return EXCLAM;
144 if (chr(s, '~')) return INVERT;
147 if (is_alpha(peek(s)) || peek(s) == '_') {
149 while (is_alnum(peek(s)) || peek(s) == '_')
150 buf_append(s, next(s));
151 if (!buf_append(s, '\0')) {
152 scanner_err(s, "identifier too long");
157 tok = keyword_to_token(s->buf, s->buf_pos - 1);
158 if (tok != -1) return tok;
160 yylval->str = strdup(s->buf);
166 /* Number literal (hexadecimal / decimal / float). */
167 if (number(s, &yylval->num, &tok)) {
168 if (tok == ERROR_TOK) {
169 scanner_err(s, "malformed number literal");
175 scanner_err(s, "unrecognized token");
180 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
181 const char *file_name, const char *map)
183 struct scanner scanner;
184 scanner_init(&scanner, ctx, string, len, file_name);
185 return parse(ctx, &scanner, map);
189 XkbParseFile(struct xkb_context *ctx, FILE *file,
190 const char *file_name, const char *map)
197 ok = map_file(file, &string, &size);
199 log_err(ctx, "Couldn't read XKB file %s: %s\n",
200 file_name, strerror(errno));
204 xkb_file = XkbParseString(ctx, string, size, file_name, map);
205 unmap_file(string, size);