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_cond_level(s->ctx, level, "%s:%d:%d: %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 (isxdigit(peek(s))) next(s);
60 while (isdigit(peek(s))) next(s);
61 is_float = chr(s, '.');
62 while (isdigit(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 (isspace(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 (isgraph(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 EXCLAM;
160 if (chr(s, '~')) return INVERT;
163 if (isalpha(peek(s)) || peek(s) == '_') {
165 while (isalnum(peek(s)) || peek(s) == '_')
166 buf_append(s, next(s));
167 if (!buf_append(s, '\0'))
168 return scanner_error(s, "identifier too long");
171 tok = keyword_to_token(s->buf);
172 if (tok != -1) return tok;
174 yylval->str = strdup(s->buf);
176 return scanner_error(s, "scanner out of memory");
180 /* Number literal (hexadecimal / decimal / float). */
181 if (number(s, &yylval->num, &tok)) {
182 if (tok == ERROR_TOK)
183 return scanner_error(s, "malformed number literal");
187 return scanner_error(s, "unrecognized token");
191 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
192 const char *file_name, const char *map)
194 struct scanner scanner;
195 scanner_init(&scanner, ctx, string, len, file_name);
196 return parse(ctx, &scanner, map);
200 XkbParseFile(struct xkb_context *ctx, FILE *file,
201 const char *file_name, const char *map)
208 ok = map_file(file, &string, &size);
210 log_err(ctx, "Couldn't read XKB file %s: %s\n",
211 file_name, strerror(errno));
215 xkb_file = XkbParseString(ctx, string, size, file_name, map);
216 unmap_file(string, size);