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