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));
125 return scanner_error(yylloc, s, "empty key name literal");
126 if (!buf_append(s, '\0') || !chr(s, '>'))
127 return scanner_error(yylloc, s, "unterminated key name literal");
128 yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
132 /* Operators and punctuation. */
133 if (chr(s, ';')) return SEMI;
134 if (chr(s, '{')) return OBRACE;
135 if (chr(s, '}')) return CBRACE;
136 if (chr(s, '=')) return EQUALS;
137 if (chr(s, '[')) return OBRACKET;
138 if (chr(s, ']')) return CBRACKET;
139 if (chr(s, '(')) return OPAREN;
140 if (chr(s, ')')) return CPAREN;
141 if (chr(s, '.')) return DOT;
142 if (chr(s, ',')) return COMMA;
143 if (chr(s, '+')) return PLUS;
144 if (chr(s, '-')) return MINUS;
145 if (chr(s, '*')) return TIMES;
146 if (chr(s, '!')) return EXCLAM;
147 if (chr(s, '~')) return INVERT;
150 if (isalpha(peek(s)) || peek(s) == '_') {
152 while (isalnum(peek(s)) || peek(s) == '_')
153 buf_append(s, next(s));
154 if (!buf_append(s, '\0'))
155 return scanner_error(yylloc, s, "identifier too long");
158 tok = keyword_to_token(s->buf);
159 if (tok != -1) return tok;
161 yylval->str = strdup(s->buf);
163 return scanner_error(yylloc, s, "scanner out of memory");
167 /* Number literal (hexadecimal / decimal / float). */
168 if (number(s, &yylval->num, &tok)) {
169 if (tok == ERROR_TOK)
170 return scanner_error(yylloc, s, "malformed number literal");
174 return scanner_error(yylloc, s, "unrecognized token");
178 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
179 const char *file_name, const char *map)
181 struct scanner scanner;
182 scanner_init(&scanner, ctx, string, len, file_name);
183 return parse(ctx, &scanner, map);
187 XkbParseFile(struct xkb_context *ctx, FILE *file,
188 const char *file_name, const char *map)
195 ok = map_file(file, &string, &size);
197 log_err(ctx, "Couldn't read XKB file %s: %s\n",
198 file_name, strerror(errno));
202 xkb_file = XkbParseString(ctx, string, size, file_name, map);
203 unmap_file(string, size);