parser: drop %name-prefix, use -p yacc argument instead
[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 static void
29 scanner_log(enum xkb_log_level level, YYLTYPE *yylloc, struct scanner *s,
30             const char *msg)
31 {
32     xkb_log_cond_level(s->ctx, level, "%s:%d:%d: %s\n", s->file_name,
33                        yylloc->first_line, yylloc->first_column, msg);
34 }
35
36 int
37 scanner_error(YYLTYPE *yylloc, struct scanner *s, const char *msg)
38 {
39     scanner_log(XKB_LOG_LEVEL_ERROR, yylloc, s, msg);
40     return ERROR_TOK;
41 }
42
43 void
44 scanner_warn(YYLTYPE *yylloc, struct scanner *s, const char *msg)
45 {
46     scanner_log(XKB_LOG_LEVEL_WARNING, yylloc, s, msg);
47 }
48
49 static bool
50 number(struct scanner *s, int64_t *out, int *out_tok)
51 {
52     bool is_float = false, is_hex = false;
53     const char *start = s->s + s->pos;
54     char *end;
55
56     if (lit(s, "0x")) {
57         while (isxdigit(peek(s))) next(s);
58         is_hex = true;
59     }
60     else {
61         while (isdigit(peek(s))) next(s);
62         is_float = chr(s, '.');
63         while (isdigit(peek(s))) next(s);
64     }
65     if (s->s + s->pos == start)
66         return false;
67
68     errno = 0;
69     if (is_hex)
70         *out = strtoul(start, &end, 16);
71     else if (is_float)
72         *out = strtod(start, &end);
73     else
74         *out = strtoul(start, &end, 10);
75     if (errno != 0 || s->s + s->pos != end)
76         *out_tok = ERROR_TOK;
77     else
78         *out_tok = (is_float ? FLOAT : INTEGER);
79     return true;
80 }
81
82 int
83 _xkbcommon_lex(YYSTYPE *yylval, YYLTYPE *yylloc, struct scanner *s)
84 {
85     int tok;
86
87 skip_more_whitespace_and_comments:
88     /* Skip spaces. */
89     while (isspace(peek(s))) next(s);
90
91     /* Skip comments. */
92     if (lit(s, "//") || chr(s, '#')) {
93         while (!eof(s) && !eol(s)) next(s);
94         goto skip_more_whitespace_and_comments;
95     }
96
97     /* See if we're done. */
98     if (eof(s)) return END_OF_FILE;
99
100     /* New token. */
101     yylloc->first_line = yylloc->last_line = s->line;
102     yylloc->first_column = yylloc->last_column = s->column;
103     s->buf_pos = 0;
104
105     /* String literal. */
106     if (chr(s, '\"')) {
107         while (!eof(s) && !eol(s) && peek(s) != '\"') {
108             if (chr(s, '\\')) {
109                 uint8_t o;
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);
119                 else {
120                     scanner_warn(yylloc, s,
121                                  "unknown escape sequence in string literal");
122                     /* Ignore. */
123                 }
124             } else {
125                 buf_append(s, next(s));
126             }
127         }
128         if (!buf_append(s, '\0') || !chr(s, '\"'))
129             return scanner_error(yylloc, s, "unterminated string literal");
130         yylval->str = strdup(s->buf);
131         if (!yylval->str)
132             return scanner_error(yylloc, s, "scanner out of memory");
133         return STRING;
134     }
135
136     /* Key name literal. */
137     if (chr(s, '<')) {
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);
144         return KEYNAME;
145     }
146
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;
163
164     /* Identifier. */
165     if (isalpha(peek(s)) || peek(s) == '_') {
166         s->buf_pos = 0;
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");
171
172         /* Keyword. */
173         tok = keyword_to_token(s->buf);
174         if ((int) tok != -1) return tok;
175
176         yylval->str = strdup(s->buf);
177         if (!yylval->str)
178             return scanner_error(yylloc, s, "scanner out of memory");
179         return IDENT;
180     }
181
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");
186         return tok;
187     }
188
189     return scanner_error(yylloc, s, "unrecognized token");
190 }
191
192 XkbFile *
193 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
194                const char *file_name, const char *map)
195 {
196     struct scanner scanner;
197     scanner_init(&scanner, ctx, string, len, file_name);
198     return parse(ctx, &scanner, map);
199 }
200
201 XkbFile *
202 XkbParseFile(struct xkb_context *ctx, FILE *file,
203              const char *file_name, const char *map)
204 {
205     bool ok;
206     XkbFile *xkb_file;
207     const char *string;
208     size_t size;
209
210     ok = map_file(file, &string, &size);
211     if (!ok) {
212         log_err(ctx, "Couldn't read XKB file %s: %s\n",
213                 file_name, strerror(errno));
214         return NULL;
215     }
216
217     xkb_file = XkbParseString(ctx, string, size, file_name, map);
218     unmap_file(string, size);
219     return xkb_file;
220 }