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, YYLTYPE *yylloc, struct scanner *s,
32 xkb_log_cond_level(s->ctx, level, "%s:%d:%d: %s\n", s->file_name,
33 yylloc->first_line, yylloc->first_column, msg);
37 scanner_error(YYLTYPE *yylloc, struct scanner *s, const char *msg)
39 scanner_log(XKB_LOG_LEVEL_ERROR, yylloc, s, msg);
44 scanner_warn(YYLTYPE *yylloc, struct scanner *s, const char *msg)
46 scanner_log(XKB_LOG_LEVEL_WARNING, yylloc, s, msg);
50 number(struct scanner *s, int64_t *out, enum yytokentype *out_tok)
52 bool is_float = false, is_hex = false;
53 const char *start = s->s + s->pos;
57 while (isxdigit(peek(s))) next(s);
61 while (isdigit(peek(s))) next(s);
62 is_float = chr(s, '.');
63 while (isdigit(peek(s))) next(s);
65 if (s->s + s->pos == start)
70 *out = strtoul(start, &end, 16);
72 *out = strtod(start, &end);
74 *out = strtoul(start, &end, 10);
75 if (errno != 0 || s->s + s->pos != end)
78 *out_tok = (is_float ? FLOAT : INTEGER);
83 _xkbcommon_lex(YYSTYPE *yylval, YYLTYPE *yylloc, struct scanner *s)
87 skip_more_whitespace_and_comments:
89 while (isspace(peek(s))) next(s);
92 if (lit(s, "//") || chr(s, '#')) {
93 while (!eof(s) && !eol(s)) next(s);
94 goto skip_more_whitespace_and_comments;
97 /* See if we're done. */
98 if (eof(s)) return END_OF_FILE;
101 yylloc->first_line = yylloc->last_line = s->line;
102 yylloc->first_column = yylloc->last_column = s->column;
105 /* String literal. */
107 while (!eof(s) && !eol(s) && peek(s) != '\"') {
110 if (chr(s, '\\')) buf_append(s, '\\');
111 else if (chr(s, 'n')) buf_append(s, '\n');
112 else if (chr(s, 't')) buf_append(s, '\t');
113 else if (chr(s, 'r')) buf_append(s, '\r');
114 else if (chr(s, 'b')) buf_append(s, '\b');
115 else if (chr(s, 'f')) buf_append(s, '\f');
116 else if (chr(s, 'v')) buf_append(s, '\v');
117 else if (chr(s, 'e')) buf_append(s, '\033');
118 else if (oct(s, &o)) buf_append(s, (char) o);
120 scanner_warn(yylloc, s,
121 "unknown escape sequence in string literal");
125 buf_append(s, next(s));
128 if (!buf_append(s, '\0') || !chr(s, '\"'))
129 return scanner_error(yylloc, s, "unterminated string literal");
130 yylval->str = strdup(s->buf);
132 return scanner_error(yylloc, s, "scanner out of memory");
136 /* Key name literal. */
138 while (isgraph(peek(s)) && peek(s) != '>')
139 buf_append(s, next(s));
140 if (!buf_append(s, '\0') || !chr(s, '>'))
141 return scanner_error(yylloc, s, "unterminated key name literal");
142 /* Empty key name literals are allowed. */
143 yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
147 /* Operators and punctuation. */
148 if (chr(s, ';')) return SEMI;
149 if (chr(s, '{')) return OBRACE;
150 if (chr(s, '}')) return CBRACE;
151 if (chr(s, '=')) return EQUALS;
152 if (chr(s, '[')) return OBRACKET;
153 if (chr(s, ']')) return CBRACKET;
154 if (chr(s, '(')) return OPAREN;
155 if (chr(s, ')')) return CPAREN;
156 if (chr(s, '.')) return DOT;
157 if (chr(s, ',')) return COMMA;
158 if (chr(s, '+')) return PLUS;
159 if (chr(s, '-')) return MINUS;
160 if (chr(s, '*')) return TIMES;
161 if (chr(s, '!')) return EXCLAM;
162 if (chr(s, '~')) return INVERT;
165 if (isalpha(peek(s)) || peek(s) == '_') {
167 while (isalnum(peek(s)) || peek(s) == '_')
168 buf_append(s, next(s));
169 if (!buf_append(s, '\0'))
170 return scanner_error(yylloc, s, "identifier too long");
173 tok = keyword_to_token(s->buf);
174 if ((int) tok != -1) return tok;
176 yylval->str = strdup(s->buf);
178 return scanner_error(yylloc, s, "scanner out of memory");
182 /* Number literal (hexadecimal / decimal / float). */
183 if (number(s, &yylval->num, &tok)) {
184 if (tok == ERROR_TOK)
185 return scanner_error(yylloc, s, "malformed number literal");
189 return scanner_error(yylloc, s, "unrecognized token");
193 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
194 const char *file_name, const char *map)
196 struct scanner scanner;
197 scanner_init(&scanner, ctx, string, len, file_name);
198 return parse(ctx, &scanner, map);
202 XkbParseFile(struct xkb_context *ctx, FILE *file,
203 const char *file_name, const char *map)
210 ok = map_file(file, &string, &size);
212 log_err(ctx, "Couldn't read XKB file %s: %s\n",
213 file_name, strerror(errno));
217 xkb_file = XkbParseString(ctx, string, size, file_name, map);
218 unmap_file(string, size);