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)) {
102 if (is_valid_char((char) o)) {
103 scanner_buf_append(s, (char) o);
105 scanner_warn_with_code(s,
106 XKB_WARNING_INVALID_ESCAPE_SEQUENCE,
107 "invalid octal escape sequence: \\%o", o);
111 // TODO: display actual sequence! See: scanner_peek(s).
112 // require escaping any potential control character
113 scanner_warn_with_code(s, XKB_WARNING_UNKNOWN_CHAR_ESCAPE_SEQUENCE,
114 "unknown escape sequence in string literal");
118 scanner_buf_append(s, scanner_next(s));
121 if (!scanner_buf_append(s, '\0') || !scanner_chr(s, '\"')) {
122 scanner_err(s, "unterminated string literal");
125 yylval->str = strdup(s->buf);
131 /* Key name literal. */
132 if (scanner_chr(s, '<')) {
133 while (is_graph(scanner_peek(s)) && scanner_peek(s) != '>')
134 scanner_buf_append(s, scanner_next(s));
135 if (!scanner_buf_append(s, '\0') || !scanner_chr(s, '>')) {
136 scanner_err(s, "unterminated key name literal");
139 /* Empty key name literals are allowed. */
140 yylval->atom = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
144 /* Operators and punctuation. */
145 if (scanner_chr(s, ';')) return SEMI;
146 if (scanner_chr(s, '{')) return OBRACE;
147 if (scanner_chr(s, '}')) return CBRACE;
148 if (scanner_chr(s, '=')) return EQUALS;
149 if (scanner_chr(s, '[')) return OBRACKET;
150 if (scanner_chr(s, ']')) return CBRACKET;
151 if (scanner_chr(s, '(')) return OPAREN;
152 if (scanner_chr(s, ')')) return CPAREN;
153 if (scanner_chr(s, '.')) return DOT;
154 if (scanner_chr(s, ',')) return COMMA;
155 if (scanner_chr(s, '+')) return PLUS;
156 if (scanner_chr(s, '-')) return MINUS;
157 if (scanner_chr(s, '*')) return TIMES;
158 if (scanner_chr(s, '/')) return DIVIDE;
159 if (scanner_chr(s, '!')) return EXCLAM;
160 if (scanner_chr(s, '~')) return INVERT;
163 if (is_alpha(scanner_peek(s)) || scanner_peek(s) == '_') {
165 while (is_alnum(scanner_peek(s)) || scanner_peek(s) == '_')
166 scanner_buf_append(s, scanner_next(s));
167 if (!scanner_buf_append(s, '\0')) {
168 scanner_err(s, "identifier too long");
173 tok = keyword_to_token(s->buf, s->buf_pos - 1);
174 if (tok != -1) return tok;
176 yylval->str = strdup(s->buf);
182 /* Number literal (hexadecimal / decimal / float). */
183 if (number(s, &yylval->num, &tok)) {
184 if (tok == ERROR_TOK) {
185 scanner_err_with_code(s, XKB_ERROR_MALFORMED_NUMBER_LITERAL,
186 "malformed number literal");
192 scanner_err(s, "unrecognized token");
197 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
198 const char *file_name, const char *map)
200 struct scanner scanner;
201 scanner_init(&scanner, ctx, string, len, file_name, NULL);
202 return parse(ctx, &scanner, map);
206 XkbParseFile(struct xkb_context *ctx, FILE *file,
207 const char *file_name, const char *map)
214 ok = map_file(file, &string, &size);
216 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
217 "Couldn't read XKB file %s: %s\n",
218 file_name, strerror(errno));
222 xkb_file = XkbParseString(ctx, string, size, file_name, map);
223 unmap_file(string, size);