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"
26 #include "scanner-utils.h"
29 scanner_log(enum xkb_log_level level, struct scanner *s, const char *msg)
31 xkb_log(s->ctx, level, 0, "%s:%u:%u: %s\n", s->file_name,
32 s->token_line, s->token_column, msg);
36 scanner_error(struct scanner *s, const char *msg)
38 scanner_log(XKB_LOG_LEVEL_ERROR, s, msg);
43 scanner_warn(struct scanner *s, const char *msg)
45 scanner_log(XKB_LOG_LEVEL_WARNING, s, msg);
49 number(struct scanner *s, int64_t *out, int *out_tok)
51 bool is_float = false, is_hex = false;
52 const char *start = s->s + s->pos;
56 while (is_xdigit(peek(s))) next(s);
60 while (is_digit(peek(s))) next(s);
61 is_float = chr(s, '.');
62 while (is_digit(peek(s))) next(s);
64 if (s->s + s->pos == start)
69 *out = strtoul(start, &end, 16);
71 *out = strtod(start, &end);
73 *out = strtoul(start, &end, 10);
74 if (errno != 0 || s->s + s->pos != end)
77 *out_tok = (is_float ? FLOAT : INTEGER);
82 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
86 skip_more_whitespace_and_comments:
88 while (is_space(peek(s))) next(s);
91 if (lit(s, "//") || chr(s, '#')) {
92 while (!eof(s) && !eol(s)) next(s);
93 goto skip_more_whitespace_and_comments;
96 /* See if we're done. */
97 if (eof(s)) return END_OF_FILE;
100 s->token_line = s->line;
101 s->token_column = s->column;
104 /* String literal. */
106 while (!eof(s) && !eol(s) && peek(s) != '\"') {
109 if (chr(s, '\\')) buf_append(s, '\\');
110 else if (chr(s, 'n')) buf_append(s, '\n');
111 else if (chr(s, 't')) buf_append(s, '\t');
112 else if (chr(s, 'r')) buf_append(s, '\r');
113 else if (chr(s, 'b')) buf_append(s, '\b');
114 else if (chr(s, 'f')) buf_append(s, '\f');
115 else if (chr(s, 'v')) buf_append(s, '\v');
116 else if (chr(s, 'e')) buf_append(s, '\033');
117 else if (oct(s, &o)) buf_append(s, (char) o);
119 scanner_warn(s, "unknown escape sequence in string literal");
123 buf_append(s, next(s));
126 if (!buf_append(s, '\0') || !chr(s, '\"'))
127 return scanner_error(s, "unterminated string literal");
128 yylval->str = strdup(s->buf);
130 return scanner_error(s, "scanner out of memory");
134 /* Key name literal. */
136 while (is_graph(peek(s)) && peek(s) != '>')
137 buf_append(s, next(s));
138 if (!buf_append(s, '\0') || !chr(s, '>'))
139 return scanner_error(s, "unterminated key name literal");
140 /* Empty key name literals are allowed. */
141 yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
145 /* Operators and punctuation. */
146 if (chr(s, ';')) return SEMI;
147 if (chr(s, '{')) return OBRACE;
148 if (chr(s, '}')) return CBRACE;
149 if (chr(s, '=')) return EQUALS;
150 if (chr(s, '[')) return OBRACKET;
151 if (chr(s, ']')) return CBRACKET;
152 if (chr(s, '(')) return OPAREN;
153 if (chr(s, ')')) return CPAREN;
154 if (chr(s, '.')) return DOT;
155 if (chr(s, ',')) return COMMA;
156 if (chr(s, '+')) return PLUS;
157 if (chr(s, '-')) return MINUS;
158 if (chr(s, '*')) return TIMES;
159 if (chr(s, '/')) return DIVIDE;
160 if (chr(s, '!')) return EXCLAM;
161 if (chr(s, '~')) return INVERT;
164 if (is_alpha(peek(s)) || peek(s) == '_') {
166 while (is_alnum(peek(s)) || peek(s) == '_')
167 buf_append(s, next(s));
168 if (!buf_append(s, '\0'))
169 return scanner_error(s, "identifier too long");
172 tok = keyword_to_token(s->buf, s->buf_pos - 1);
173 if (tok != -1) return tok;
175 yylval->str = strdup(s->buf);
177 return scanner_error(s, "scanner out of memory");
181 /* Number literal (hexadecimal / decimal / float). */
182 if (number(s, &yylval->num, &tok)) {
183 if (tok == ERROR_TOK)
184 return scanner_error(s, "malformed number literal");
188 return scanner_error(s, "unrecognized token");
192 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
193 const char *file_name, const char *map)
195 struct scanner scanner;
196 scanner_init(&scanner, ctx, string, len, file_name);
197 return parse(ctx, &scanner, map);
201 XkbParseFile(struct xkb_context *ctx, FILE *file,
202 const char *file_name, const char *map)
209 ok = map_file(file, &string, &size);
211 log_err(ctx, "Couldn't read XKB file %s: %s\n",
212 file_name, strerror(errno));
216 xkb_file = XkbParseString(ctx, string, size, file_name, map);
217 unmap_file(string, size);