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_error(YYLTYPE *yylloc, struct scanner *s, const char *msg)
31 log_err(s->ctx, "%s:%d:%d: %s\n",
32 s->file_name, yylloc->first_line, yylloc->first_column, msg);
37 number(struct scanner *s, int64_t *out, enum yytokentype *out_tok)
39 bool is_float = false, is_hex = false;
40 const char *start = s->s + s->pos;
44 while (isxdigit(peek(s))) next(s);
48 while (isdigit(peek(s))) next(s);
49 is_float = chr(s, '.');
50 while (isdigit(peek(s))) next(s);
52 if (s->s + s->pos == start)
57 *out = strtoul(start, &end, 16);
59 *out = strtod(start, &end);
61 *out = strtoul(start, &end, 10);
62 if (errno != 0 || s->s + s->pos != end)
65 *out_tok = (is_float ? FLOAT : INTEGER);
70 _xkbcommon_lex(YYSTYPE *yylval, YYLTYPE *yylloc, struct scanner *s)
74 skip_more_whitespace_and_comments:
76 while (isspace(peek(s))) next(s);
79 if (lit(s, "//") || chr(s, '#')) {
80 while (!eof(s) && !eol(s)) next(s);
81 goto skip_more_whitespace_and_comments;
84 /* See if we're done. */
85 if (eof(s)) return END_OF_FILE;
88 yylloc->first_line = yylloc->last_line = s->line;
89 yylloc->first_column = yylloc->last_column = s->column;
94 while (!eof(s) && !eol(s) && peek(s) != '\"') {
97 if (chr(s, '\\')) buf_append(s, '\\');
98 else if (chr(s, 'n')) buf_append(s, '\n');
99 else if (chr(s, 't')) buf_append(s, '\t');
100 else if (chr(s, 'r')) buf_append(s, '\r');
101 else if (chr(s, 'b')) buf_append(s, '\b');
102 else if (chr(s, 'f')) buf_append(s, '\f');
103 else if (chr(s, 'v')) buf_append(s, '\v');
104 else if (chr(s, 'e')) buf_append(s, '\033');
105 else if (oct(s, &o)) buf_append(s, (char) o);
106 else return scanner_error(yylloc, s,
107 "illegal escape sequence in string literal");
109 buf_append(s, next(s));
112 if (!buf_append(s, '\0') || !chr(s, '\"'))
113 return scanner_error(yylloc, s, "unterminated string literal");
114 yylval->str = strdup(s->buf);
116 return scanner_error(yylloc, s, "scanner out of memory");
120 /* Key name literal. */
122 while (isgraph(peek(s)) && peek(s) != '>')
123 buf_append(s, next(s));
124 if (!buf_append(s, '\0') || !chr(s, '>'))
125 return scanner_error(yylloc, s, "unterminated key name literal");
126 /* Empty key name literals are allowed. */
127 yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
131 /* Operators and punctuation. */
132 if (chr(s, ';')) return SEMI;
133 if (chr(s, '{')) return OBRACE;
134 if (chr(s, '}')) return CBRACE;
135 if (chr(s, '=')) return EQUALS;
136 if (chr(s, '[')) return OBRACKET;
137 if (chr(s, ']')) return CBRACKET;
138 if (chr(s, '(')) return OPAREN;
139 if (chr(s, ')')) return CPAREN;
140 if (chr(s, '.')) return DOT;
141 if (chr(s, ',')) return COMMA;
142 if (chr(s, '+')) return PLUS;
143 if (chr(s, '-')) return MINUS;
144 if (chr(s, '*')) return TIMES;
145 if (chr(s, '!')) return EXCLAM;
146 if (chr(s, '~')) return INVERT;
149 if (isalpha(peek(s)) || peek(s) == '_') {
151 while (isalnum(peek(s)) || peek(s) == '_')
152 buf_append(s, next(s));
153 if (!buf_append(s, '\0'))
154 return scanner_error(yylloc, s, "identifier too long");
157 tok = keyword_to_token(s->buf);
158 if (tok != -1) return tok;
160 yylval->str = strdup(s->buf);
162 return scanner_error(yylloc, s, "scanner out of memory");
166 /* Number literal (hexadecimal / decimal / float). */
167 if (number(s, &yylval->num, &tok)) {
168 if (tok == ERROR_TOK)
169 return scanner_error(yylloc, s, "malformed number literal");
173 return scanner_error(yylloc, s, "unrecognized token");
177 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
178 const char *file_name, const char *map)
180 struct scanner scanner;
181 scanner_init(&scanner, ctx, string, len, file_name);
182 return parse(ctx, &scanner, map);
186 XkbParseFile(struct xkb_context *ctx, FILE *file,
187 const char *file_name, const char *map)
194 ok = map_file(file, &string, &size);
196 log_err(ctx, "Couldn't read XKB file %s: %s\n",
197 file_name, strerror(errno));
201 xkb_file = XkbParseString(ctx, string, size, file_name, map);
202 unmap_file(string, size);