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;
37 if (scanner_lit(s, "0x")) {
38 while (is_xdigit(scanner_peek(s))) scanner_next(s);
42 while (is_digit(scanner_peek(s))) scanner_next(s);
43 is_float = scanner_chr(s, '.');
44 while (is_digit(scanner_peek(s))) scanner_next(s);
46 if (s->s + s->pos == start)
51 *out = strtoul(start, &end, 16);
53 /* The parser currently just ignores floats, so the cast is
54 * fine - the value doesn't matter. */
55 *out = strtod(start, &end);
57 *out = strtoul(start, &end, 10);
58 if (errno != 0 || s->s + s->pos != end)
61 *out_tok = (is_float ? FLOAT : INTEGER);
66 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
70 skip_more_whitespace_and_comments:
72 while (is_space(scanner_peek(s))) scanner_next(s);
75 if (scanner_lit(s, "//") || scanner_chr(s, '#')) {
76 scanner_skip_to_eol(s);
77 goto skip_more_whitespace_and_comments;
80 /* See if we're done. */
81 if (scanner_eof(s)) return END_OF_FILE;
84 s->token_line = s->line;
85 s->token_column = s->column;
89 if (scanner_chr(s, '\"')) {
90 while (!scanner_eof(s) && !scanner_eol(s) && scanner_peek(s) != '\"') {
91 if (scanner_chr(s, '\\')) {
93 if (scanner_chr(s, '\\')) scanner_buf_append(s, '\\');
94 else if (scanner_chr(s, 'n')) scanner_buf_append(s, '\n');
95 else if (scanner_chr(s, 't')) scanner_buf_append(s, '\t');
96 else if (scanner_chr(s, 'r')) scanner_buf_append(s, '\r');
97 else if (scanner_chr(s, 'b')) scanner_buf_append(s, '\b');
98 else if (scanner_chr(s, 'f')) scanner_buf_append(s, '\f');
99 else if (scanner_chr(s, 'v')) scanner_buf_append(s, '\v');
100 else if (scanner_chr(s, 'e')) scanner_buf_append(s, '\033');
101 else if (scanner_oct(s, &o)) scanner_buf_append(s, (char) o);
103 // TODO: display actual sequence! See: scanner_peek(s).
104 // require escaping any potential control character
105 scanner_warn_with_code(s, XKB_WARNING_UNKNOWN_CHAR_ESCAPE_SEQUENCE,
106 "unknown escape sequence in string literal");
110 scanner_buf_append(s, scanner_next(s));
113 if (!scanner_buf_append(s, '\0') || !scanner_chr(s, '\"')) {
114 scanner_err(s, "unterminated string literal");
117 yylval->str = strdup(s->buf);
123 /* Key name literal. */
124 if (scanner_chr(s, '<')) {
125 while (is_graph(scanner_peek(s)) && scanner_peek(s) != '>')
126 scanner_buf_append(s, scanner_next(s));
127 if (!scanner_buf_append(s, '\0') || !scanner_chr(s, '>')) {
128 scanner_err(s, "unterminated key name literal");
131 /* Empty key name literals are allowed. */
132 yylval->atom = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
136 /* Operators and punctuation. */
137 if (scanner_chr(s, ';')) return SEMI;
138 if (scanner_chr(s, '{')) return OBRACE;
139 if (scanner_chr(s, '}')) return CBRACE;
140 if (scanner_chr(s, '=')) return EQUALS;
141 if (scanner_chr(s, '[')) return OBRACKET;
142 if (scanner_chr(s, ']')) return CBRACKET;
143 if (scanner_chr(s, '(')) return OPAREN;
144 if (scanner_chr(s, ')')) return CPAREN;
145 if (scanner_chr(s, '.')) return DOT;
146 if (scanner_chr(s, ',')) return COMMA;
147 if (scanner_chr(s, '+')) return PLUS;
148 if (scanner_chr(s, '-')) return MINUS;
149 if (scanner_chr(s, '*')) return TIMES;
150 if (scanner_chr(s, '/')) return DIVIDE;
151 if (scanner_chr(s, '!')) return EXCLAM;
152 if (scanner_chr(s, '~')) return INVERT;
155 if (is_alpha(scanner_peek(s)) || scanner_peek(s) == '_') {
157 while (is_alnum(scanner_peek(s)) || scanner_peek(s) == '_')
158 scanner_buf_append(s, scanner_next(s));
159 if (!scanner_buf_append(s, '\0')) {
160 scanner_err(s, "identifier too long");
165 tok = keyword_to_token(s->buf, s->buf_pos - 1);
166 if (tok != -1) return tok;
168 yylval->str = strdup(s->buf);
174 /* Number literal (hexadecimal / decimal / float). */
175 if (number(s, &yylval->num, &tok)) {
176 if (tok == ERROR_TOK) {
177 scanner_err_with_code(s, XKB_ERROR_MALFORMED_NUMBER_LITERAL,
178 "malformed number literal");
184 scanner_err(s, "unrecognized token");
189 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
190 const char *file_name, const char *map)
192 struct scanner scanner;
193 scanner_init(&scanner, ctx, string, len, file_name, NULL);
194 return parse(ctx, &scanner, map);
198 XkbParseFile(struct xkb_context *ctx, FILE *file,
199 const char *file_name, const char *map)
206 ok = map_file(file, &string, &size);
208 log_err(ctx, "Couldn't read XKB file %s: %s\n",
209 file_name, strerror(errno));
213 xkb_file = XkbParseString(ctx, string, size, file_name, map);
214 unmap_file(string, size);