4be2c40482198ba15428374c81e3f1aeb54b29e4
[platform/upstream/libxkbcommon.git] / src / xkbcomp / scanner.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include "xkbcomp-priv.h"
25 #include "parser-priv.h"
26 #include "scanner-utils.h"
27
28 int
29 scanner_error(YYLTYPE *yylloc, struct scanner *s, const char *msg)
30 {
31     log_err(s->ctx, "%s:%d:%d: %s\n",
32             s->file_name, yylloc->first_line, yylloc->first_column, msg);
33     return ERROR_TOK;
34 }
35
36 static bool
37 number(struct scanner *s, int64_t *out, enum yytokentype *out_tok)
38 {
39     bool is_float = false, is_hex = false;
40     const char *start = s->s + s->pos;
41     char *end;
42
43     if (lit(s, "0x")) {
44         while (isxdigit(peek(s))) next(s);
45         is_hex = true;
46     }
47     else {
48         while (isdigit(peek(s))) next(s);
49         is_float = chr(s, '.');
50         while (isdigit(peek(s))) next(s);
51     }
52     if (s->s + s->pos == start)
53         return false;
54
55     errno = 0;
56     if (is_hex)
57         *out = strtoul(start, &end, 16);
58     else if (is_float)
59         *out = strtod(start, &end);
60     else
61         *out = strtoul(start, &end, 10);
62     if (errno != 0 || s->s + s->pos != end)
63         *out_tok = ERROR_TOK;
64     else
65         *out_tok = (is_float ? FLOAT : INTEGER);
66     return true;
67 }
68
69 int
70 _xkbcommon_lex(YYSTYPE *yylval, YYLTYPE *yylloc, struct scanner *s)
71 {
72     enum yytokentype tok;
73
74 skip_more_whitespace_and_comments:
75     /* Skip spaces. */
76     while (isspace(peek(s))) next(s);
77
78     /* Skip comments. */
79     if (lit(s, "//") || chr(s, '#')) {
80         while (!eof(s) && !eol(s)) next(s);
81         goto skip_more_whitespace_and_comments;
82     }
83
84     /* See if we're done. */
85     if (eof(s)) return END_OF_FILE;
86
87     /* New token. */
88     yylloc->first_line = yylloc->last_line = s->line;
89     yylloc->first_column = yylloc->last_column = s->column;
90     s->buf_pos = 0;
91
92     /* String literal. */
93     if (chr(s, '\"')) {
94         while (!eof(s) && !eol(s) && peek(s) != '\"') {
95             if (chr(s, '\\')) {
96                 uint8_t o;
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");
108             } else {
109                 buf_append(s, next(s));
110             }
111         }
112         if (!buf_append(s, '\0') || !chr(s, '\"'))
113             return scanner_error(yylloc, s, "unterminated string literal");
114         yylval->str = strdup(s->buf);
115         if (!yylval->str)
116             return scanner_error(yylloc, s, "scanner out of memory");
117         return STRING;
118     }
119
120     /* Key name literal. */
121     if (chr(s, '<')) {
122         while (isgraph(peek(s)) && peek(s) != '>')
123             buf_append(s, next(s));
124         if (s->buf_pos == 0)
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);
129         return KEYNAME;
130     }
131
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;
148
149     /* Identifier. */
150     if (isalpha(peek(s)) || peek(s) == '_') {
151         s->buf_pos = 0;
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");
156
157         /* Keyword. */
158         tok = keyword_to_token(s->buf);
159         if (tok != -1) return tok;
160
161         yylval->str = strdup(s->buf);
162         if (!yylval->str)
163             return scanner_error(yylloc, s, "scanner out of memory");
164         return IDENT;
165     }
166
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");
171         return tok;
172     }
173
174     return scanner_error(yylloc, s, "unrecognized token");
175 }
176
177 XkbFile *
178 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
179                const char *file_name, const char *map)
180 {
181     struct scanner scanner;
182     scanner_init(&scanner, ctx, string, len, file_name);
183     return parse(ctx, &scanner, map);
184 }
185
186 XkbFile *
187 XkbParseFile(struct xkb_context *ctx, FILE *file,
188              const char *file_name, const char *map)
189 {
190     bool ok;
191     XkbFile *xkb_file;
192     const char *string;
193     size_t size;
194
195     ok = map_file(file, &string, &size);
196     if (!ok) {
197         log_err(ctx, "Couldn't read XKB file %s: %s\n",
198                 file_name, strerror(errno));
199         return NULL;
200     }
201
202     xkb_file = XkbParseString(ctx, string, size, file_name, map);
203     unmap_file(string, size);
204     return xkb_file;
205 }