Commit 28d5f7708cac3b78e62cc47768a4a9956300a6de

Ran Benita 2014-02-10T20:33:34

scanner: sort out scanner logging functions First, make the rules and xkb scanners/parsers use the same logging functions instead of rolling their own. Second, use the gcc ##__VA_ARGS extension instead of dealing with C99 stupidity. I hope all relevant compilers support it. Signed-off-by: Ran Benita <ran234@gmail.com>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
diff --git a/src/xkbcomp/parser-priv.h b/src/xkbcomp/parser-priv.h
index 08475a7..0675e55 100644
--- a/src/xkbcomp/parser-priv.h
+++ b/src/xkbcomp/parser-priv.h
@@ -27,22 +27,16 @@
 #ifndef XKBCOMP_PARSER_PRIV_H
 #define XKBCOMP_PARSER_PRIV_H
 
-struct scanner;
 struct parser_param;
 
+#include "scanner-utils.h"
 #include "parser.h"
 
 int
-scanner_error(struct scanner *scanner, const char *msg);
-
-void
-scanner_warn(struct scanner *s, const char *msg);
-
-int
 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *scanner);
 
 XkbFile *
-parse(struct xkb_context *ctx, void *scanner, const char *map);
+parse(struct xkb_context *ctx, struct scanner *scanner, const char *map);
 
 int
 keyword_to_token(const char *string, unsigned int len);
diff --git a/src/xkbcomp/parser.y b/src/xkbcomp/parser.y
index f640ada..d9f6cbc 100644
--- a/src/xkbcomp/parser.y
+++ b/src/xkbcomp/parser.y
@@ -37,27 +37,21 @@
 
 struct parser_param {
     struct xkb_context *ctx;
-    void *scanner;
+    struct scanner *scanner;
     XkbFile *rtrn;
     bool more_maps;
 };
 
-static void
-parser_error(struct parser_param *param, const char *msg)
-{
-    scanner_error(param->scanner, msg);
-}
+#define parser_err(param, fmt, ...) \
+    scanner_err((param)->scanner, fmt, ##__VA_ARGS__)
 
-static void
-parser_warn(struct parser_param *param, const char *msg)
-{
-    scanner_warn(param->scanner, msg);
-}
+#define parser_warn(param, fmt, ...) \
+    scanner_warn((param)->scanner, fmt, ##__VA_ARGS__)
 
 static void
 _xkbcommon_error(struct parser_param *param, const char *msg)
 {
-    parser_error(param, msg);
+    parser_err(param, "%s", msg);
 }
 
 static bool
@@ -84,11 +78,11 @@ resolve_keysym(const char *str, xkb_keysym_t *sym_rtrn)
     return false;
 }
 
-#define scanner param->scanner
+#define param_scanner param->scanner
 %}
 
 %pure-parser
-%lex-param      { struct scanner *scanner }
+%lex-param      { struct scanner *param_scanner }
 %parse-param    { struct parser_param *param }
 
 %token
@@ -761,10 +755,8 @@ MapName         :       STRING  { $$ = $1; }
 
 %%
 
-#undef scanner
-
 XkbFile *
-parse(struct xkb_context *ctx, void *scanner, const char *map)
+parse(struct xkb_context *ctx, struct scanner *scanner, const char *map)
 {
     int ret;
     XkbFile *first = NULL;
@@ -809,5 +801,3 @@ parse(struct xkb_context *ctx, void *scanner, const char *map)
 
     return first;
 }
-
-#define scanner param->scanner
diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c
index e99ba5d..61799e7 100644
--- a/src/xkbcomp/rules.c
+++ b/src/xkbcomp/rules.c
@@ -70,16 +70,6 @@ enum rules_token {
     TOK_ERROR
 };
 
-/* C99 is stupid. Just use the 1 variant when there are no args. */
-#define scanner_error1(scanner, msg) \
-    log_warn((scanner)->ctx, "%s:%u:%u: %s\n", \
-             (scanner)->file_name, \
-             (scanner)->token_line, (scanner)->token_column, msg)
-#define scanner_error(scanner, fmt, ...) \
-    log_err((scanner)->ctx, "%s:%u:%u: " fmt "\n", \
-             (scanner)->file_name, \
-             (scanner)->token_line, (scanner)->token_column, __VA_ARGS__)
-
 static inline bool
 is_ident(char ch)
 {
@@ -107,7 +97,7 @@ skip_more_whitespace_and_comments:
     /* Escaped line continuation. */
     if (chr(s, '\\')) {
         if (!eol(s)) {
-            scanner_error1(s, "illegal new line escape; must appear at end of line");
+            scanner_err(s, "illegal new line escape; must appear at end of line");
             return TOK_ERROR;
         }
         next(s);
@@ -135,7 +125,7 @@ skip_more_whitespace_and_comments:
             val->string.len++;
         }
         if (val->string.len == 0) {
-            scanner_error1(s, "unexpected character after \'$\'; expected name");
+            scanner_err(s, "unexpected character after \'$\'; expected name");
             return TOK_ERROR;
         }
         return TOK_GROUP_NAME;
@@ -152,7 +142,7 @@ skip_more_whitespace_and_comments:
         return TOK_IDENTIFIER;
     }
 
-    scanner_error1(s, "unrecognized token");
+    scanner_err(s, "unrecognized token");
     return TOK_ERROR;
 }
 
@@ -323,10 +313,8 @@ matcher_free(struct matcher *m)
     free(m);
 }
 
-#define matcher_error1(matcher, msg) \
-    scanner_error1(&(matcher)->scanner, msg)
-#define matcher_error(matcher, fmt, ...) \
-    scanner_error(&(matcher)->scanner, fmt, __VA_ARGS__)
+#define matcher_err(matcher, fmt, ...) \
+    scanner_err(&(matcher)->scanner, fmt, ## __VA_ARGS__)
 
 static void
 matcher_group_start_new(struct matcher *m, struct sval name)
@@ -387,19 +375,15 @@ matcher_mapping_set_mlvo(struct matcher *m, struct sval ident)
 
     /* Not found. */
     if (mlvo >= _MLVO_NUM_ENTRIES) {
-        matcher_error(m,
-                      "invalid mapping: %.*s is not a valid value here; "
-                      "ignoring rule set",
-                      ident.len, ident.start);
+        matcher_err(m, "invalid mapping: %.*s is not a valid value here; ignoring rule set",
+                    ident.len, ident.start);
         m->mapping.skip = true;
         return;
     }
 
     if (m->mapping.defined_mlvo_mask & (1u << mlvo)) {
-        matcher_error(m,
-                      "invalid mapping: %.*s appears twice on the same line; "
-                      "ignoring rule set",
-                      mlvo_sval.len, mlvo_sval.start);
+        matcher_err(m, "invalid mapping: %.*s appears twice on the same line; ignoring rule set",
+                    mlvo_sval.len, mlvo_sval.start);
         m->mapping.skip = true;
         return;
     }
@@ -410,10 +394,8 @@ matcher_mapping_set_mlvo(struct matcher *m, struct sval ident)
         int consumed = extract_layout_index(ident.start + mlvo_sval.len,
                                             ident.len - mlvo_sval.len, &idx);
         if ((int) (ident.len - mlvo_sval.len) != consumed) {
-            matcher_error(m,
-                          "invalid mapping:\" %.*s\" may only be followed by a valid group index; "
-                          "ignoring rule set",
-                          mlvo_sval.len, mlvo_sval.start);
+            matcher_err(m, "invalid mapping: \"%.*s\" may only be followed by a valid group index; ignoring rule set",
+                        mlvo_sval.len, mlvo_sval.start);
             m->mapping.skip = true;
             return;
         }
@@ -425,10 +407,8 @@ matcher_mapping_set_mlvo(struct matcher *m, struct sval ident)
             m->mapping.variant_idx = idx;
         }
         else {
-            matcher_error(m,
-                          "invalid mapping: \"%.*s\" cannot be followed by a group index; "
-                          "ignoring rule set",
-                          mlvo_sval.len, mlvo_sval.start);
+            matcher_err(m, "invalid mapping: \"%.*s\" cannot be followed by a group index; ignoring rule set",
+                        mlvo_sval.len, mlvo_sval.start);
             m->mapping.skip = true;
             return;
         }
@@ -454,19 +434,15 @@ matcher_mapping_set_kccgst(struct matcher *m, struct sval ident)
 
     /* Not found. */
     if (kccgst >= _KCCGST_NUM_ENTRIES) {
-        matcher_error(m,
-                      "invalid mapping: %.*s is not a valid value here; "
-                      "ignoring rule set",
-                      ident.len, ident.start);
+        matcher_err(m, "invalid mapping: %.*s is not a valid value here; ignoring rule set",
+                    ident.len, ident.start);
         m->mapping.skip = true;
         return;
     }
 
     if (m->mapping.defined_kccgst_mask & (1u << kccgst)) {
-        matcher_error(m,
-                      "invalid mapping: %.*s appears twice on the same line; "
-                      "ignoring rule set",
-                      kccgst_sval.len, kccgst_sval.start);
+        matcher_err(m, "invalid mapping: %.*s appears twice on the same line; ignoring rule set",
+                    kccgst_sval.len, kccgst_sval.start);
         m->mapping.skip = true;
         return;
     }
@@ -480,16 +456,12 @@ static void
 matcher_mapping_verify(struct matcher *m)
 {
     if (m->mapping.num_mlvo == 0) {
-        matcher_error1(m,
-                       "invalid mapping: must have at least one value on the left hand side; "
-                       "ignoring rule set");
+        matcher_err(m, "invalid mapping: must have at least one value on the left hand side; ignoring rule set");
         goto skip;
     }
 
     if (m->mapping.num_kccgst == 0) {
-        matcher_error1(m,
-                       "invalid mapping: must have at least one value on the right hand side; "
-                       "ignoring rule set");
+        matcher_err(m, "invalid mapping: must have at least one value on the right hand side; ignoring rule set");
         goto skip;
     }
 
@@ -540,9 +512,7 @@ matcher_rule_set_mlvo_common(struct matcher *m, struct sval ident,
                              enum mlvo_match_type match_type)
 {
     if (m->rule.num_mlvo_values + 1 > m->mapping.num_mlvo) {
-        matcher_error1(m,
-                       "invalid rule: has more values than the mapping line; "
-                       "ignoring rule");
+        matcher_err(m, "invalid rule: has more values than the mapping line; ignoring rule");
         m->rule.skip = true;
         return;
     }
@@ -574,9 +544,7 @@ static void
 matcher_rule_set_kccgst(struct matcher *m, struct sval ident)
 {
     if (m->rule.num_kccgst_values + 1 > m->mapping.num_kccgst) {
-        matcher_error1(m,
-                       "invalid rule: has more values than the mapping line; "
-                       "ignoring rule");
+        matcher_err(m, "invalid rule: has more values than the mapping line; ignoring rule");
         m->rule.skip = true;
         return;
     }
@@ -680,9 +648,7 @@ append_expanded_kccgst_value(struct matcher *m, darray_char *to,
             int consumed;
 
             if (mlv != MLVO_LAYOUT && mlv != MLVO_VARIANT) {
-                matcher_error1(m,
-                                "invalid index in %%-expansion; "
-                                "may only index layout or variant");
+                matcher_err(m, "invalid index in %%-expansion; may only index layout or variant");
                 goto error;
             }
 
@@ -756,7 +722,7 @@ append_expanded_kccgst_value(struct matcher *m, darray_char *to,
 
 error:
     darray_free(expanded);
-    matcher_error1(m, "invalid %%-expansion in value; not used");
+    matcher_err(m, "invalid %%-expansion in value; not used");
     return false;
 }
 
@@ -765,9 +731,7 @@ matcher_rule_verify(struct matcher *m)
 {
     if (m->rule.num_mlvo_values != m->mapping.num_mlvo ||
         m->rule.num_kccgst_values != m->mapping.num_kccgst) {
-        matcher_error1(m,
-                       "invalid rule: must have same number of values as mapping line;"
-                       "ignoring rule");
+        matcher_err(m, "invalid rule: must have same number of values as mapping line; ignoring rule");
         m->rule.skip = true;
     }
 }
@@ -990,7 +954,7 @@ finish:
     return true;
 
 state_error:
-    matcher_error1(m, "unexpected token");
+    matcher_err(m, "unexpected token");
 error:
     return false;
 }
diff --git a/src/xkbcomp/scanner-utils.h b/src/xkbcomp/scanner-utils.h
index 2341d33..e4e90eb 100644
--- a/src/xkbcomp/scanner-utils.h
+++ b/src/xkbcomp/scanner-utils.h
@@ -56,6 +56,18 @@ struct scanner {
     struct xkb_context *ctx;
 };
 
+#define scanner_log(scanner, level, fmt, ...) \
+    xkb_log((scanner)->ctx, (level), 0, \
+            "%s:%u:%u: " fmt "\n", \
+             (scanner)->file_name, \
+             (scanner)->token_line, (scanner)->token_column, ##__VA_ARGS__)
+
+#define scanner_err(scanner, fmt, ...) \
+    scanner_log(scanner, XKB_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
+
+#define scanner_warn(scanner, fmt, ...) \
+    scanner_log(scanner, XKB_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
+
 static inline void
 scanner_init(struct scanner *s, struct xkb_context *ctx,
              const char *string, size_t len, const char *file_name)
diff --git a/src/xkbcomp/scanner.c b/src/xkbcomp/scanner.c
index 67e8e3f..9f200bb 100644
--- a/src/xkbcomp/scanner.c
+++ b/src/xkbcomp/scanner.c
@@ -23,27 +23,6 @@
 
 #include "xkbcomp-priv.h"
 #include "parser-priv.h"
-#include "scanner-utils.h"
-
-static void
-scanner_log(enum xkb_log_level level, struct scanner *s, const char *msg)
-{
-    xkb_log(s->ctx, level, 0,  "%s:%u:%u: %s\n", s->file_name,
-            s->token_line, s->token_column, msg);
-}
-
-int
-scanner_error(struct scanner *s, const char *msg)
-{
-    scanner_log(XKB_LOG_LEVEL_ERROR, s, msg);
-    return ERROR_TOK;
-}
-
-void
-scanner_warn(struct scanner *s, const char *msg)
-{
-    scanner_log(XKB_LOG_LEVEL_WARNING, s, msg);
-}
 
 static bool
 number(struct scanner *s, int64_t *out, int *out_tok)
@@ -123,11 +102,13 @@ skip_more_whitespace_and_comments:
                 buf_append(s, next(s));
             }
         }
-        if (!buf_append(s, '\0') || !chr(s, '\"'))
-            return scanner_error(s, "unterminated string literal");
+        if (!buf_append(s, '\0') || !chr(s, '\"')) {
+            scanner_err(s, "unterminated string literal");
+            return ERROR_TOK;
+        }
         yylval->str = strdup(s->buf);
         if (!yylval->str)
-            return scanner_error(s, "scanner out of memory");
+            return ERROR_TOK;
         return STRING;
     }
 
@@ -135,8 +116,10 @@ skip_more_whitespace_and_comments:
     if (chr(s, '<')) {
         while (is_graph(peek(s)) && peek(s) != '>')
             buf_append(s, next(s));
-        if (!buf_append(s, '\0') || !chr(s, '>'))
-            return scanner_error(s, "unterminated key name literal");
+        if (!buf_append(s, '\0') || !chr(s, '>')) {
+            scanner_err(s, "unterminated key name literal");
+            return ERROR_TOK;
+        }
         /* Empty key name literals are allowed. */
         yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
         return KEYNAME;
@@ -165,8 +148,10 @@ skip_more_whitespace_and_comments:
         s->buf_pos = 0;
         while (is_alnum(peek(s)) || peek(s) == '_')
             buf_append(s, next(s));
-        if (!buf_append(s, '\0'))
-            return scanner_error(s, "identifier too long");
+        if (!buf_append(s, '\0')) {
+            scanner_err(s, "identifier too long");
+            return ERROR_TOK;
+        }
 
         /* Keyword. */
         tok = keyword_to_token(s->buf, s->buf_pos - 1);
@@ -174,18 +159,21 @@ skip_more_whitespace_and_comments:
 
         yylval->str = strdup(s->buf);
         if (!yylval->str)
-            return scanner_error(s, "scanner out of memory");
+            return ERROR_TOK;
         return IDENT;
     }
 
     /* Number literal (hexadecimal / decimal / float). */
     if (number(s, &yylval->num, &tok)) {
-        if (tok == ERROR_TOK)
-            return scanner_error(s, "malformed number literal");
+        if (tok == ERROR_TOK) {
+            scanner_err(s, "malformed number literal");
+            return ERROR_TOK;
+        }
         return tok;
     }
 
-    return scanner_error(s, "unrecognized token");
+    scanner_err(s, "unrecognized token");
+    return ERROR_TOK;
 }
 
 XkbFile *