build: include config.h manually
[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 "config.h"
25
26 #include "xkbcomp-priv.h"
27 #include "parser-priv.h"
28 #include "scanner-utils.h"
29
30 static bool
31 number(struct scanner *s, int64_t *out, int *out_tok)
32 {
33     bool is_float = false, is_hex = false;
34     const char *start = s->s + s->pos;
35     char *end;
36
37     if (lit(s, "0x")) {
38         while (is_xdigit(peek(s))) next(s);
39         is_hex = true;
40     }
41     else {
42         while (is_digit(peek(s))) next(s);
43         is_float = chr(s, '.');
44         while (is_digit(peek(s))) next(s);
45     }
46     if (s->s + s->pos == start)
47         return false;
48
49     errno = 0;
50     if (is_hex)
51         *out = strtoul(start, &end, 16);
52     else if (is_float)
53         *out = strtod(start, &end);
54     else
55         *out = strtoul(start, &end, 10);
56     if (errno != 0 || s->s + s->pos != end)
57         *out_tok = ERROR_TOK;
58     else
59         *out_tok = (is_float ? FLOAT : INTEGER);
60     return true;
61 }
62
63 int
64 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
65 {
66     int tok;
67
68 skip_more_whitespace_and_comments:
69     /* Skip spaces. */
70     while (is_space(peek(s))) next(s);
71
72     /* Skip comments. */
73     if (lit(s, "//") || chr(s, '#')) {
74         skip_to_eol(s);
75         goto skip_more_whitespace_and_comments;
76     }
77
78     /* See if we're done. */
79     if (eof(s)) return END_OF_FILE;
80
81     /* New token. */
82     s->token_line = s->line;
83     s->token_column = s->column;
84     s->buf_pos = 0;
85
86     /* String literal. */
87     if (chr(s, '\"')) {
88         while (!eof(s) && !eol(s) && peek(s) != '\"') {
89             if (chr(s, '\\')) {
90                 uint8_t o;
91                 if      (chr(s, '\\')) buf_append(s, '\\');
92                 else if (chr(s, 'n'))  buf_append(s, '\n');
93                 else if (chr(s, 't'))  buf_append(s, '\t');
94                 else if (chr(s, 'r'))  buf_append(s, '\r');
95                 else if (chr(s, 'b'))  buf_append(s, '\b');
96                 else if (chr(s, 'f'))  buf_append(s, '\f');
97                 else if (chr(s, 'v'))  buf_append(s, '\v');
98                 else if (chr(s, 'e'))  buf_append(s, '\033');
99                 else if (oct(s, &o))   buf_append(s, (char) o);
100                 else {
101                     scanner_warn(s, "unknown escape sequence in string literal");
102                     /* Ignore. */
103                 }
104             } else {
105                 buf_append(s, next(s));
106             }
107         }
108         if (!buf_append(s, '\0') || !chr(s, '\"')) {
109             scanner_err(s, "unterminated string literal");
110             return ERROR_TOK;
111         }
112         yylval->str = strdup(s->buf);
113         if (!yylval->str)
114             return ERROR_TOK;
115         return STRING;
116     }
117
118     /* Key name literal. */
119     if (chr(s, '<')) {
120         while (is_graph(peek(s)) && peek(s) != '>')
121             buf_append(s, next(s));
122         if (!buf_append(s, '\0') || !chr(s, '>')) {
123             scanner_err(s, "unterminated key name literal");
124             return ERROR_TOK;
125         }
126         /* Empty key name literals are allowed. */
127         yylval->atom = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
128         return KEYNAME;
129     }
130
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 DIVIDE;
146     if (chr(s, '!')) return EXCLAM;
147     if (chr(s, '~')) return INVERT;
148
149     /* Identifier. */
150     if (is_alpha(peek(s)) || peek(s) == '_') {
151         s->buf_pos = 0;
152         while (is_alnum(peek(s)) || peek(s) == '_')
153             buf_append(s, next(s));
154         if (!buf_append(s, '\0')) {
155             scanner_err(s, "identifier too long");
156             return ERROR_TOK;
157         }
158
159         /* Keyword. */
160         tok = keyword_to_token(s->buf, s->buf_pos - 1);
161         if (tok != -1) return tok;
162
163         yylval->str = strdup(s->buf);
164         if (!yylval->str)
165             return ERROR_TOK;
166         return IDENT;
167     }
168
169     /* Number literal (hexadecimal / decimal / float). */
170     if (number(s, &yylval->num, &tok)) {
171         if (tok == ERROR_TOK) {
172             scanner_err(s, "malformed number literal");
173             return ERROR_TOK;
174         }
175         return tok;
176     }
177
178     scanner_err(s, "unrecognized token");
179     return ERROR_TOK;
180 }
181
182 XkbFile *
183 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
184                const char *file_name, const char *map)
185 {
186     struct scanner scanner;
187     scanner_init(&scanner, ctx, string, len, file_name, NULL);
188     return parse(ctx, &scanner, map);
189 }
190
191 XkbFile *
192 XkbParseFile(struct xkb_context *ctx, FILE *file,
193              const char *file_name, const char *map)
194 {
195     bool ok;
196     XkbFile *xkb_file;
197     char *string;
198     size_t size;
199
200     ok = map_file(file, &string, &size);
201     if (!ok) {
202         log_err(ctx, "Couldn't read XKB file %s: %s\n",
203                 file_name, strerror(errno));
204         return NULL;
205     }
206
207     xkb_file = XkbParseString(ctx, string, size, file_name, map);
208     unmap_file(string, size);
209     return xkb_file;
210 }