Debian Codegraph @

src/xkbcomp/parser.y

raw
1 /*
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3 * SPDX-License-Identifier: HPND
4 */
5
6 /*
7 * The parser should work with reasonably recent versions of bison.
8 *
9 * byacc support was dropped, because it does not support the following features:
10 * - `%define parse.error detailed`
11 */
12
13 /* The following goes in the header */
14 %code requires {
15 #include "config.h"
16
17 #include "scanner-utils.h"
18 #include "xkbcomp/ast.h"
19 }
20
21 %{
22 #include "config.h"
23
24 #include <assert.h>
25
26 #include "keysym.h"
27 #include "xkbcomp/xkbcomp-priv.h"
28 #include "xkbcomp/parser-priv.h"
29 #include "xkbcomp/ast-build.h"
30
31 struct parser_param {
32 struct xkb_context *ctx;
33 struct scanner *scanner;
34 XkbFile *rtrn;
35 bool more_maps;
36 };
37
38 #define parser_err(param, error_id, fmt, ...) \
39 scanner_err((param)->scanner, error_id, fmt, ##__VA_ARGS__)
40
41 #define parser_warn(param, warning_id, fmt, ...) \
42 scanner_warn((param)->scanner, warning_id, fmt, ##__VA_ARGS__)
43
44 #define parser_vrb(param, verbosity, warning_id, fmt, ...) \
45 scanner_vrb((param)->scanner, verbosity, warning_id, fmt, ##__VA_ARGS__)
46
47 static void
48 _xkbcommon_error(struct parser_param *param, const char *msg)
49 {
50 parser_err(param, XKB_ERROR_INVALID_XKB_SYNTAX, "%s", msg);
51 }
52
53 static bool
54 resolve_keysym(struct parser_param *param, struct sval name, xkb_keysym_t *sym_rtrn)
55 {
56 xkb_keysym_t sym;
57
58 if (isvaleq(name, SVAL_LIT("any")) || isvaleq(name, SVAL_LIT("nosymbol"))) {
59 *sym_rtrn = XKB_KEY_NoSymbol;
60 return true;
61 }
62
63 if (isvaleq(name, SVAL_LIT("none")) || isvaleq(name, SVAL_LIT("voidsymbol"))) {
64 *sym_rtrn = XKB_KEY_VoidSymbol;
65 return true;
66 }
67
68 /* xkb_keysym_from_name needs a C string. */
69 char buf[XKB_KEYSYM_NAME_MAX_SIZE];
70 if (name.len >= sizeof(buf)) {
71 return false;
72 }
73 memcpy(buf, name.start, name.len);
74 buf[name.len] = '\0';
75
76 sym = xkb_keysym_from_name(buf, XKB_KEYSYM_NO_FLAGS);
77 if (sym != XKB_KEY_NoSymbol) {
78 *sym_rtrn = sym;
79 check_deprecated_keysyms(parser_warn, param, param->ctx,
80 sym, buf, buf, "%s", "");
81 return true;
82 }
83
84 return false;
85 }
86
87 #define param_scanner param->scanner
88 %}
89
90 %define api.pure
91 %lex-param { struct scanner *param_scanner }
92 %parse-param { struct parser_param *param }
93
94 %define parse.error detailed
95
96 %token
97 END_OF_FILE 0 "end of file"
98 ERROR_TOK 255 "invalid token"
99 XKB_KEYMAP 1 "xkb_keymap"
100 XKB_KEYCODES 2 "xkb_keycodes"
101 XKB_TYPES 3 "xkb_types"
102 XKB_SYMBOLS 4 "xkb_symbols"
103 XKB_COMPATMAP 5 "xkb_compatibility"
104 XKB_GEOMETRY 6 "xkb_geometry"
105 XKB_SEMANTICS 7 "xkb_semantics"
106 XKB_LAYOUT 8 "xkb_layout"
107 INCLUDE 10 "include"
108 OVERRIDE 11 "override"
109 AUGMENT 12 "augment"
110 REPLACE 13 "replace"
111 ALTERNATE 14 "alternate"
112 VIRTUAL_MODS 20 "virtual_modifiers"
113 TYPE 21 "type"
114 INTERPRET 22 "interpret"
115 ACTION_TOK 23 "action"
116 KEY 24 "key"
117 ALIAS 25 "alias"
118 GROUP 26 "group"
119 MODIFIER_MAP 27 "modifier_map"
120 INDICATOR 28 "indicator"
121 SHAPE 29 "shape"
122 KEYS 30 "keys"
123 ROW 31 "row"
124 SECTION 32 "section"
125 OVERLAY 33 "overlay"
126 TEXT 34 "text"
127 OUTLINE 35 "outline"
128 SOLID 36 "solid"
129 LOGO 37 "logo"
130 VIRTUAL 38 "virtual"
131 EQUALS 40 "="
132 PLUS 41 "+"
133 MINUS 42 "-"
134 DIVIDE 43 "/"
135 TIMES 44 "*"
136 OBRACE 45 "{"
137 CBRACE 46 "}"
138 OPAREN 47 "("
139 CPAREN 48 ")"
140 OBRACKET 49 "["
141 CBRACKET 50 "]"
142 DOT 51 "."
143 COMMA 52 ","
144 SEMI 53 ";"
145 EXCLAM 54 "!"
146 INVERT 55 "~"
147 STRING 60 "string literal"
148 DECIMAL_DIGIT 61 "decimal digit"
149 INTEGER 62 "integer literal"
150 FLOAT 63 "float literal"
151 IDENT 64 "identifier"
152 KEYNAME 65 "key name"
153 PARTIAL 70 "partial"
154 DEFAULT 71 "default"
155 HIDDEN 72 "hidden"
156 ALPHANUMERIC_KEYS 73 "alphanumeric_keys"
157 MODIFIER_KEYS 74 "modifier_keys"
158 KEYPAD_KEYS 75 "keypad_keys"
159 FUNCTION_KEYS 76 "function_keys"
160 ALTERNATE_GROUP 77 "alternate_group"
161
162 %right EQUALS
163 %left PLUS MINUS
164 %left TIMES DIVIDE
165 %left EXCLAM INVERT
166 %left OPAREN
167
168 %start XkbFile
169
170 %union {
171 int64_t num;
172 enum xkb_file_type file_type;
173 char *str;
174 struct sval sval;
175 xkb_atom_t atom;
176 enum merge_mode merge;
177 enum xkb_map_flags mapFlags;
178 xkb_keysym_t keysym;
179 ParseCommon *any;
180 struct { ParseCommon *head; ParseCommon *last; } anyList;
181 uint32_t noSymbolOrActionList;
182 ExprDef *expr;
183 struct { ExprDef *head; ExprDef *last; } exprList;
184 VarDef *var;
185 struct { VarDef *head; VarDef *last; } varList;
186 VModDef *vmod;
187 struct { VModDef *head; VModDef *last; } vmodList;
188 InterpDef *interp;
189 KeyTypeDef *keyType;
190 SymbolsDef *syms;
191 ModMapDef *modMask;
192 GroupCompatDef *groupCompat;
193 LedMapDef *ledMap;
194 LedNameDef *ledName;
195 KeycodeDef *keyCode;
196 KeyAliasDef *keyAlias;
197 void *geom;
198 XkbFile *file;
199 struct { XkbFile *head; XkbFile *last; } fileList;
200 }
201
202 %type <num> DECIMAL_DIGIT INTEGER FLOAT
203 %type <str> STRING
204 %type <sval> IDENT
205 %type <atom> KEYNAME
206 %type <num> KeyCode Number Integer Float SignedNumber DoodadType
207 %type <merge> MergeMode OptMergeMode
208 %type <file_type> XkbCompositeType FileType
209 %type <mapFlags> Flag Flags OptFlags
210 %type <str> MapName OptMapName
211 %type <atom> FieldSpec Ident Element String
212 %type <keysym> KeySym KeySymLit
213 %type <noSymbolOrActionList> NoSymbolOrActionList
214 %type <any> Decl
215 %type <anyList> DeclList
216 %type <expr> Expr Term Lhs Terminal Coord CoordList
217 %type <expr> MultiKeySymOrActionList NonEmptyActions Actions Action
218 %type <expr> KeySyms NonEmptyKeySyms KeySymList KeyOrKeySym
219 %type <exprList> ExprList KeyOrKeySymList
220 %type <exprList> ActionList MultiActionList MultiKeySymList
221 %type <var> VarDecl SymbolsVarDecl
222 %type <varList> VarDeclList SymbolsBody OptSymbolsBody
223 %type <vmod> VModDef
224 %type <vmodList> VModDefList VModDecl
225 %type <interp> InterpretDecl InterpretMatch
226 %type <keyType> KeyTypeDecl
227 %type <syms> SymbolsDecl
228 %type <modMask> ModMapDecl
229 %type <groupCompat> GroupCompatDecl
230 %type <ledMap> LedMapDecl
231 %type <ledName> LedNameDecl
232 %type <keyCode> KeyNameDecl
233 %type <keyAlias> KeyAliasDecl
234 %type <geom> ShapeDecl SectionDecl SectionBody SectionBodyItem RowBody RowBodyItem
235 %type <geom> Keys Key OverlayDecl OverlayKeyList OverlayKey OutlineList OutlineInList
236 %type <geom> DoodadDecl
237 %type <file> XkbFile XkbMapConfig
238 %type <fileList> XkbMapConfigList
239 %type <file> XkbCompositeMap
240
241 %destructor { FreeStmt((ParseCommon *) $$); }
242 <any> <expr> <var> <vmod> <interp> <keyType> <syms> <modMask> <groupCompat>
243 <ledMap> <ledName> <keyCode> <keyAlias>
244 %destructor { FreeStmt((ParseCommon *) $$.head); }
245 <anyList> <exprList> <varList> <vmodList>
246 /* The destructor also runs on the start symbol when the parser *succeeds*.
247 * The `if` here catches this case. */
248 %destructor { if (!param->rtrn) FreeXkbFile($$); } <file>
249 %destructor { FreeXkbFile($$.head); } <fileList>
250 %destructor { free($$); } <str>
251
252 %%
253
254 /*
255 * An actual file may contain more than one map. However, if we do things
256 * in the normal yacc way, i.e. aggregate all of the maps into a list and
257 * let the caller find the map it wants, we end up scanning and parsing a
258 * lot of unneeded maps (in the end we always just need one).
259 * Instead of doing that, we make yyparse return one map at a time, and
260 * then call it repeatedly until we find the map we need. Once we find it,
261 * we don't need to parse everything that follows in the file.
262 * This does mean that if we e.g. always use the first map, the file may
263 * contain complete garbage after that. But it's worth it.
264 */
265
266 XkbFile : XkbCompositeMap
267 { $$ = param->rtrn = $1; param->more_maps = !!param->rtrn; (void) yynerrs; }
268 | XkbMapConfig
269 { $$ = param->rtrn = $1; param->more_maps = !!param->rtrn; YYACCEPT; }
270 | END_OF_FILE
271 { $$ = param->rtrn = NULL; param->more_maps = false; }
272 ;
273
274 XkbCompositeMap : OptFlags XkbCompositeType OptMapName OBRACE
275 XkbMapConfigList
276 CBRACE SEMI
277 { $$ = XkbFileCreate($2, $3, (ParseCommon *) $5.head, $1); }
278 ;
279
280 XkbCompositeType: XKB_KEYMAP { $$ = FILE_TYPE_KEYMAP; }
281 | XKB_SEMANTICS { $$ = FILE_TYPE_KEYMAP; }
282 | XKB_LAYOUT { $$ = FILE_TYPE_KEYMAP; }
283 ;
284
285 /*
286 * NOTE: Any component is optional
287 */
288 XkbMapConfigList : XkbMapConfigList XkbMapConfig
289 {
290 if ($2) {
291 if ($1.head) {
292 $$.head = $1.head;
293 $$.last->common.next = &$2->common;
294 $$.last = $2;
295 } else {
296 $$.head = $$.last = $2;
297 }
298 }
299 }
300 | { $$.head = $$.last = NULL; }
301 ;
302
303 XkbMapConfig : OptFlags FileType OptMapName OBRACE
304 DeclList
305 CBRACE SEMI
306 {
307 $$ = XkbFileCreate($2, $3, $5.head, $1);
308 }
309 ;
310
311 FileType : XKB_KEYCODES { $$ = FILE_TYPE_KEYCODES; }
312 | XKB_TYPES { $$ = FILE_TYPE_TYPES; }
313 | XKB_COMPATMAP { $$ = FILE_TYPE_COMPAT; }
314 | XKB_SYMBOLS { $$ = FILE_TYPE_SYMBOLS; }
315 | XKB_GEOMETRY { $$ = FILE_TYPE_GEOMETRY; }
316 ;
317
318 OptFlags : Flags { $$ = $1; }
319 | { $$ = 0; }
320 ;
321
322 Flags : Flags Flag { $$ = ($1 | $2); }
323 | Flag { $$ = $1; }
324 ;
325
326 Flag : PARTIAL { $$ = MAP_IS_PARTIAL; }
327 | DEFAULT { $$ = MAP_IS_DEFAULT; }
328 | HIDDEN { $$ = MAP_IS_HIDDEN; }
329 | ALPHANUMERIC_KEYS { $$ = MAP_HAS_ALPHANUMERIC; }
330 | MODIFIER_KEYS { $$ = MAP_HAS_MODIFIER; }
331 | KEYPAD_KEYS { $$ = MAP_HAS_KEYPAD; }
332 | FUNCTION_KEYS { $$ = MAP_HAS_FN; }
333 | ALTERNATE_GROUP { $$ = MAP_IS_ALTGR; }
334 ;
335
336 DeclList : DeclList Decl
337 {
338 if ($2) {
339 if ($1.head) {
340 $$.head = $1.head; $1.last->next = $2; $$.last = $2;
341 } else {
342 $$.head = $$.last = $2;
343 }
344 }
345 }
346 /*
347 * VModDecl is "inlined" directly into DeclList, i.e.
348 * each VModDef in the VModDecl is a separate Decl in
349 * the File.
350 */
351 | DeclList OptMergeMode VModDecl
352 {
353 for (VModDef *vmod = $3.head; vmod; vmod = (VModDef *) vmod->common.next)
354 vmod->merge = $2;
355 if ($1.head) {
356 $$.head = $1.head; $1.last->next = &$3.head->common; $$.last = &$3.last->common;
357 } else {
358 $$.head = &$3.head->common; $$.last = &$3.last->common;
359 }
360 }
361 | { $$.head = $$.last = NULL; }
362 ;
363
364 Decl : OptMergeMode VarDecl
365 {
366 $2->merge = $1;
367 $$ = (ParseCommon *) $2;
368 }
369 /* OptMergeMode VModDecl - see above. */
370 | OptMergeMode InterpretDecl
371 {
372 $2->merge = $1;
373 $$ = (ParseCommon *) $2;
374 }
375 | OptMergeMode KeyNameDecl
376 {
377 $2->merge = $1;
378 $$ = (ParseCommon *) $2;
379 }
380 | OptMergeMode KeyAliasDecl
381 {
382 $2->merge = $1;
383 $$ = (ParseCommon *) $2;
384 }
385 | OptMergeMode KeyTypeDecl
386 {
387 $2->merge = $1;
388 $$ = (ParseCommon *) $2;
389 }
390 | OptMergeMode SymbolsDecl
391 {
392 $2->merge = $1;
393 $$ = (ParseCommon *) $2;
394 }
395 | OptMergeMode ModMapDecl
396 {
397 $2->merge = $1;
398 $$ = (ParseCommon *) $2;
399 }
400 | OptMergeMode GroupCompatDecl
401 {
402 $2->merge = $1;
403 $$ = (ParseCommon *) $2;
404 }
405 | OptMergeMode LedMapDecl
406 {
407 $2->merge = $1;
408 $$ = (ParseCommon *) $2;
409 }
410 | OptMergeMode LedNameDecl
411 {
412 $2->merge = $1;
413 $$ = (ParseCommon *) $2;
414 }
415 | OptMergeMode ShapeDecl { $$ = NULL; }
416 | OptMergeMode SectionDecl { $$ = NULL; }
417 | OptMergeMode DoodadDecl { $$ = NULL; }
418 | MergeMode STRING
419 {
420 $$ = (ParseCommon *) IncludeCreate(param->ctx, $2, $1);
421 free($2);
422 }
423 ;
424
425 VarDecl : Lhs EQUALS Expr SEMI
426 { $$ = VarCreate($1, $3); }
427 | Ident SEMI
428 { $$ = BoolVarCreate($1, true); }
429 | EXCLAM Ident SEMI
430 { $$ = BoolVarCreate($2, false); }
431 ;
432
433 KeyNameDecl : KEYNAME EQUALS KeyCode SEMI
434 { $$ = KeycodeCreate($1, $3); }
435 ;
436
437 KeyAliasDecl : ALIAS KEYNAME EQUALS KEYNAME SEMI
438 { $$ = KeyAliasCreate($2, $4); }
439 ;
440
441 VModDecl : VIRTUAL_MODS VModDefList SEMI
442 { $$ = $2; }
443 ;
444
445 VModDefList : VModDefList COMMA VModDef
446 { $$.head = $1.head; $$.last->common.next = &$3->common; $$.last = $3; }
447 | VModDef
448 { $$.head = $$.last = $1; }
449 ;
450
451 VModDef : Ident
452 { $$ = VModCreate($1, NULL); }
453 | Ident EQUALS Expr
454 { $$ = VModCreate($1, $3); }
455 ;
456
457 InterpretDecl : INTERPRET InterpretMatch OBRACE
458 VarDeclList
459 CBRACE SEMI
460 { $2->def = $4.head; $$ = $2; }
461 ;
462
463 InterpretMatch : KeySym PLUS Expr
464 { $$ = InterpCreate($1, $3); }
465 | KeySym
466 { $$ = InterpCreate($1, NULL); }
467 ;
468
469 VarDeclList : VarDeclList VarDecl
470 {
471 if ($2) {
472 if ($1.head) {
473 $$.head = $1.head;
474 $$.last->common.next = &$2->common;
475 $$.last = $2;
476 } else {
477 $$.head = $$.last = $2;
478 }
479 }
480 }
481 | { $$.head = $$.last = NULL; }
482 ;
483
484 KeyTypeDecl : TYPE String OBRACE
485 VarDeclList
486 CBRACE SEMI
487 { $$ = KeyTypeCreate($2, $4.head); }
488 ;
489
490 SymbolsDecl : KEY KEYNAME OBRACE
491 OptSymbolsBody
492 CBRACE SEMI
493 { $$ = SymbolsCreate($2, $4.head); }
494 ;
495
496 OptSymbolsBody : SymbolsBody { $$ = $1; }
497 | { $$.head = $$.last = NULL; }
498 ;
499
500 SymbolsBody : SymbolsBody COMMA SymbolsVarDecl
501 { $$.head = $1.head; $$.last->common.next = &$3->common; $$.last = $3; }
502 | SymbolsVarDecl
503 { $$.head = $$.last = $1; }
504 ;
505
506 SymbolsVarDecl : Lhs EQUALS Expr { $$ = VarCreate($1, $3); }
507 | Lhs EQUALS MultiKeySymOrActionList { $$ = VarCreate($1, $3); }
508 | Ident { $$ = BoolVarCreate($1, true); }
509 | EXCLAM Ident { $$ = BoolVarCreate($2, false); }
510 | MultiKeySymOrActionList { $$ = VarCreate(NULL, $1); }
511 ;
512
513 /*
514 * A list of keysym/action lists
515 *
516 * There is some ambiguity because we use `{}` to denote both an empty list of
517 * keysyms and an empty list of actions. But as soon as we get a keysym or an
518 * action, we know whether it is a `MultiKeySymList` or a `MultiActionList`.
519 * So we just count the `{}` at the *beginning* using `NoSymbolOrActionList`,
520 * then replace it by the relevant count of `NoSymbol` or `NoAction()` once the
521 * ambiguity is solved. If not, this is a list of empties of *some* type: we
522 * drop those empties and delegate the type resolution using `ExprEmptyList()`.
523 */
524 MultiKeySymOrActionList
525 : OBRACKET MultiKeySymList CBRACKET
526 { $$ = $2.head; }
527 | OBRACKET NoSymbolOrActionList COMMA MultiKeySymList CBRACKET
528 {
529 /* Prepend n times NoSymbol */
530 struct {ExprDef *head; ExprDef *last;} list = {
531 .head = $4.head, .last = $4.last
532 };
533 for (uint32_t k = 0; k < $2; k++) {
534 ExprDef* const syms =
535 ExprCreateKeySymList(XKB_KEY_NoSymbol);
536 if (!syms) {
537 /* TODO: Use Bison’s more appropriate YYNOMEM */
538 YYABORT;
539 }
540 syms->common.next = &list.head->common;
541 list.head = syms;
542 }
543 $$ = list.head;
544 }
545 | OBRACKET MultiActionList CBRACKET
546 { $$ = $2.head; }
547 | OBRACKET NoSymbolOrActionList COMMA MultiActionList CBRACKET
548 {
549 /* Prepend n times NoAction() */
550 struct {ExprDef *head; ExprDef *last;} list = {
551 .head = $4.head, .last = $4.last
552 };
553 for (uint32_t k = 0; k < $2; k++) {
554 ExprDef* const acts = ExprCreateActionList(NULL);
555 if (!acts) {
556 /* TODO: Use Bison’s more appropriate YYNOMEM */
557 YYABORT;
558 }
559 acts->common.next = &list.head->common;
560 list.head = acts;
561 }
562 $$ = list.head;
563 }
564 /*
565 * A list of *empty* keysym/action lists `{}`.
566 * There is not enough context here to decide if it is
567 * a `MultiKeySymList` or a `MultiActionList`.
568 */
569 | OBRACKET NoSymbolOrActionList CBRACKET
570 { $$ = ExprEmptyList(); }
571 ;
572
573 /* A list of `{}`, which remains ambiguous until reaching a keysym or action list */
574 NoSymbolOrActionList
575 : NoSymbolOrActionList COMMA OBRACE CBRACE
576 { $$ = $1 + 1; }
577 | OBRACE CBRACE
578 { $$ = 1; }
579 | { $$ = 0; }
580 ;
581
582 GroupCompatDecl : GROUP Integer EQUALS Expr SEMI
583 { $$ = GroupCompatCreate($2, $4); }
584 ;
585
586 ModMapDecl : MODIFIER_MAP Ident OBRACE KeyOrKeySymList CBRACE SEMI
587 { $$ = ModMapCreate($2, $4.head); }
588 ;
589
590 KeyOrKeySymList : KeyOrKeySymList COMMA KeyOrKeySym
591 { $$.head = $1.head; $$.last->common.next = &$3->common; $$.last = $3; }
592 | KeyOrKeySym
593 { $$.head = $$.last = $1; }
594 ;
595
596 KeyOrKeySym : KEYNAME
597 { $$ = ExprCreateKeyName($1); }
598 | KeySym
599 { $$ = ExprCreateKeySym($1); }
600 ;
601
602 LedMapDecl: INDICATOR String OBRACE VarDeclList CBRACE SEMI
603 { $$ = LedMapCreate($2, $4.head); }
604 ;
605
606 LedNameDecl: INDICATOR Integer EQUALS Expr SEMI
607 { $$ = LedNameCreate($2, $4, false); }
608 | VIRTUAL INDICATOR Integer EQUALS Expr SEMI
609 { $$ = LedNameCreate($3, $5, true); }
610 ;
611
612 ShapeDecl : SHAPE String OBRACE OutlineList CBRACE SEMI
613 { $$ = NULL; }
614 | SHAPE String OBRACE CoordList CBRACE SEMI
615 { (void) $4; $$ = NULL; }
616 ;
617
618 SectionDecl : SECTION String OBRACE SectionBody CBRACE SEMI
619 { $$ = NULL; }
620 ;
621
622 SectionBody : SectionBody SectionBodyItem { $$ = NULL;}
623 | SectionBodyItem { $$ = NULL; }
624 ;
625
626 SectionBodyItem : ROW OBRACE RowBody CBRACE SEMI
627 { $$ = NULL; }
628 | VarDecl
629 { FreeStmt((ParseCommon *) $1); $$ = NULL; }
630 | DoodadDecl
631 { $$ = NULL; }
632 | LedMapDecl
633 { FreeStmt((ParseCommon *) $1); $$ = NULL; }
634 | OverlayDecl
635 { $$ = NULL; }
636 ;
637
638 RowBody : RowBody RowBodyItem { $$ = NULL;}
639 | RowBodyItem { $$ = NULL; }
640 ;
641
642 RowBodyItem : KEYS OBRACE Keys CBRACE SEMI { $$ = NULL; }
643 | VarDecl
644 { FreeStmt((ParseCommon *) $1); $$ = NULL; }
645 ;
646
647 Keys : Keys COMMA Key { $$ = NULL; }
648 | Key { $$ = NULL; }
649 ;
650
651 Key : KEYNAME
652 { $$ = NULL; }
653 | OBRACE ExprList CBRACE
654 { FreeStmt((ParseCommon *) $2.head); $$ = NULL; }
655 ;
656
657 OverlayDecl : OVERLAY String OBRACE OverlayKeyList CBRACE SEMI
658 { $$ = NULL; }
659 ;
660
661 OverlayKeyList : OverlayKeyList COMMA OverlayKey { $$ = NULL; }
662 | OverlayKey { $$ = NULL; }
663 ;
664
665 OverlayKey : KEYNAME EQUALS KEYNAME { $$ = NULL; }
666 ;
667
668 OutlineList : OutlineList COMMA OutlineInList
669 { $$ = NULL;}
670 | OutlineInList
671 { $$ = NULL; }
672 ;
673
674 OutlineInList : OBRACE CoordList CBRACE
675 { (void) $2; $$ = NULL; }
676 | Ident EQUALS OBRACE CoordList CBRACE
677 { (void) $4; $$ = NULL; }
678 | Ident EQUALS Expr
679 { FreeStmt((ParseCommon *) $3); $$ = NULL; }
680 ;
681
682 CoordList : CoordList COMMA Coord
683 { (void) $1; (void) $3; $$ = NULL; }
684 | Coord
685 { (void) $1; $$ = NULL; }
686 ;
687
688 Coord : OBRACKET SignedNumber COMMA SignedNumber CBRACKET
689 { $$ = NULL; }
690 ;
691
692 DoodadDecl : DoodadType String OBRACE VarDeclList CBRACE SEMI
693 { FreeStmt((ParseCommon *) $4.head); $$ = NULL; }
694 ;
695
696 DoodadType : TEXT { $$ = 0; }
697 | OUTLINE { $$ = 0; }
698 | SOLID { $$ = 0; }
699 | LOGO { $$ = 0; }
700 ;
701
702 FieldSpec : Ident { $$ = $1; }
703 | Element { $$ = $1; }
704 ;
705
706 Element : ACTION_TOK
707 { $$ = xkb_atom_intern_literal(param->ctx, "action"); }
708 | INTERPRET
709 { $$ = xkb_atom_intern_literal(param->ctx, "interpret"); }
710 | TYPE
711 { $$ = xkb_atom_intern_literal(param->ctx, "type"); }
712 | KEY
713 { $$ = xkb_atom_intern_literal(param->ctx, "key"); }
714 | GROUP
715 { $$ = xkb_atom_intern_literal(param->ctx, "group"); }
716 | MODIFIER_MAP
717 {$$ = xkb_atom_intern_literal(param->ctx, "modifier_map");}
718 | INDICATOR
719 { $$ = xkb_atom_intern_literal(param->ctx, "indicator"); }
720 | SHAPE
721 { $$ = xkb_atom_intern_literal(param->ctx, "shape"); }
722 | ROW
723 { $$ = xkb_atom_intern_literal(param->ctx, "row"); }
724 | SECTION
725 { $$ = xkb_atom_intern_literal(param->ctx, "section"); }
726 | TEXT
727 { $$ = xkb_atom_intern_literal(param->ctx, "text"); }
728 ;
729
730 OptMergeMode : MergeMode { $$ = $1; }
731 | { $$ = MERGE_DEFAULT; }
732 ;
733
734 MergeMode : INCLUDE { $$ = MERGE_DEFAULT; }
735 | AUGMENT { $$ = MERGE_AUGMENT; }
736 | OVERRIDE { $$ = MERGE_OVERRIDE; }
737 | REPLACE { $$ = MERGE_REPLACE; }
738 | ALTERNATE
739 {
740 /*
741 * This used to be MERGE_ALT_FORM. This functionality was
742 * unused and has been removed.
743 */
744 parser_warn(param, XKB_LOG_MESSAGE_NO_ID,
745 "ignored unsupported legacy merge mode \"alternate\"");
746 $$ = MERGE_DEFAULT;
747 }
748 ;
749
750 ExprList : ExprList COMMA Expr
751 {
752 if ($3) {
753 if ($1.head) {
754 $$.head = $1.head;
755 $$.last->common.next = &$3->common;
756 $$.last = $3;
757 } else {
758 $$.head = $$.last = $3;
759 }
760 }
761 }
762 | Expr
763 { $$.head = $$.last = $1; }
764 | { $$.head = $$.last = NULL; }
765 ;
766
767 Expr : Expr DIVIDE Expr
768 { $$ = ExprCreateBinary(STMT_EXPR_DIVIDE, $1, $3); }
769 | Expr PLUS Expr
770 { $$ = ExprCreateBinary(STMT_EXPR_ADD, $1, $3); }
771 | Expr MINUS Expr
772 { $$ = ExprCreateBinary(STMT_EXPR_SUBTRACT, $1, $3); }
773 | Expr TIMES Expr
774 { $$ = ExprCreateBinary(STMT_EXPR_MULTIPLY, $1, $3); }
775 | Lhs EQUALS Expr
776 { $$ = ExprCreateBinary(STMT_EXPR_ASSIGN, $1, $3); }
777 | Term
778 { $$ = $1; }
779 ;
780
781 Term : MINUS Term
782 { $$ = ExprCreateUnary(STMT_EXPR_NEGATE, $2); }
783 | PLUS Term
784 { $$ = ExprCreateUnary(STMT_EXPR_UNARY_PLUS, $2); }
785 | EXCLAM Term
786 { $$ = ExprCreateUnary(STMT_EXPR_NOT, $2); }
787 | INVERT Term
788 { $$ = ExprCreateUnary(STMT_EXPR_INVERT, $2); }
789 | Lhs
790 { $$ = $1; }
791 | FieldSpec OPAREN ExprList CPAREN %prec OPAREN
792 { $$ = ExprCreateAction($1, $3.head); }
793 | Actions
794 { $$ = $1; }
795 | Terminal
796 { $$ = $1; }
797 | OPAREN Expr CPAREN
798 { $$ = $2; }
799 ;
800
801 MultiActionList : MultiActionList COMMA Action
802 {
803 ExprDef *expr = ExprCreateActionList($3);
804 $$ = $1;
805 $$.last->common.next = &expr->common; $$.last = expr;
806 }
807 | MultiActionList COMMA Actions
808 { $$ = $1; $$.last->common.next = &$3->common; $$.last = $3; }
809 | Action
810 { $$.head = $$.last = ExprCreateActionList($1); }
811 | NonEmptyActions
812 { $$.head = $$.last = $1; }
813 ;
814
815 ActionList : ActionList COMMA Action
816 { $$ = $1; $$.last->common.next = &$3->common; $$.last = $3; }
817 | Action
818 { $$.head = $$.last = $1; }
819 ;
820
821 NonEmptyActions : OBRACE ActionList CBRACE
822 { $$ = ExprCreateActionList($2.head); }
823 ;
824
825 Actions : NonEmptyActions
826 { $$ = $1; }
827 | OBRACE CBRACE
828 { $$ = ExprCreateActionList(NULL); }
829 ;
830
831 Action : FieldSpec OPAREN ExprList CPAREN
832 { $$ = ExprCreateAction($1, $3.head); }
833 ;
834
835 Lhs : FieldSpec
836 { $$ = ExprCreateIdent($1); }
837 | FieldSpec DOT FieldSpec
838 { $$ = ExprCreateFieldRef($1, $3); }
839 | FieldSpec OBRACKET Expr CBRACKET
840 { $$ = ExprCreateArrayRef(XKB_ATOM_NONE, $1, $3); }
841 | FieldSpec DOT FieldSpec OBRACKET Expr CBRACKET
842 { $$ = ExprCreateArrayRef($1, $3, $5); }
843 ;
844
845 Terminal : String
846 { $$ = ExprCreateString($1); }
847 | Integer
848 { $$ = ExprCreateInteger($1); }
849 | Float
850 { $$ = ExprCreateFloat(/* Discard $1 */); }
851 | KEYNAME
852 { $$ = ExprCreateKeyName($1); }
853 ;
854
855 MultiKeySymList : MultiKeySymList COMMA KeySymLit
856 {
857 ExprDef *expr = ExprCreateKeySymList($3);
858 $$ = $1;
859 $$.last->common.next = &expr->common; $$.last = expr;
860 }
861 | MultiKeySymList COMMA KeySyms
862 { $$ = $1; $$.last->common.next = &$3->common; $$.last = $3; }
863 | KeySymLit
864 { $$.head = $$.last = ExprCreateKeySymList($1); }
865 | NonEmptyKeySyms
866 { $$.head = $$.last = $1; }
867 ;
868
869 KeySymList : KeySymList COMMA KeySymLit
870 { $$ = ExprAppendKeySymList($1, $3); }
871 | KeySymList COMMA STRING
872 {
873 $$ = ExprKeySymListAppendString(param->scanner, $1, $3);
874 free($3);
875 if (!$$)
876 YYERROR;
877 }
878 | KeySymLit
879 {
880 $$ = ExprCreateKeySymList($1);
881 if (!$$)
882 YYERROR;
883 }
884 | STRING
885 {
886 $$ = ExprCreateKeySymList(XKB_KEY_NoSymbol);
887 if (!$$)
888 YYERROR;
889 $$ = ExprKeySymListAppendString(param->scanner, $$, $1);
890 free($1);
891 if (!$$)
892 YYERROR;
893 }
894 ;
895
896 NonEmptyKeySyms : OBRACE KeySymList CBRACE
897 { $$ = $2; }
898 | STRING
899 {
900 $$ = ExprCreateKeySymList(XKB_KEY_NoSymbol);
901 if (!$$)
902 YYERROR;
903 $$ = ExprKeySymListAppendString(param->scanner, $$, $1);
904 free($1);
905 if (!$$)
906 YYERROR;
907 }
908 ;
909
910 KeySyms : NonEmptyKeySyms
911 { $$ = $1; }
912 | OBRACE CBRACE
913 { $$ = ExprCreateKeySymList(XKB_KEY_NoSymbol); }
914 ;
915
916 KeySym : KeySymLit
917 { $$ = $1; }
918 | STRING
919 {
920 $$ = KeysymParseString(param->scanner, $1);
921 free($1);
922 if ($$ == XKB_KEY_NoSymbol)
923 YYERROR;
924 }
925 ;
926
927 KeySymLit : IDENT
928 {
929 if (!resolve_keysym(param, $1, &$$)) {
930 parser_warn(
931 param,
932 XKB_WARNING_UNRECOGNIZED_KEYSYM,
933 "unrecognized keysym \"%.*s\"",
934 (unsigned int) $1.len, $1.start
935 );
936 $$ = XKB_KEY_NoSymbol;
937 }
938 }
939 /* Handle keysym that is also a keyword */
940 | SECTION { $$ = XKB_KEY_section; }
941 | DECIMAL_DIGIT
942 {
943 /*
944 * Special case for digits 0..9:
945 * map to XKB_KEY_0 .. XKB_KEY_9, consistent with
946 * other keysym names: <name> → XKB_KEY_<name>.
947 */
948 $$ = XKB_KEY_0 + (xkb_keysym_t) $1;
949 }
950 | INTEGER
951 {
952 if ($1 < XKB_KEYSYM_MIN) {
953 /* Negative value */
954 static_assert(XKB_KEYSYM_MIN == 0,
955 "Keysyms are positive");
956 parser_warn(
957 param,
958 XKB_ERROR_INVALID_NUMERIC_KEYSYM,
959 "unrecognized keysym \"-%#06"PRIx64"\""
960 " (%"PRId64")",
961 -$1, $1
962 );
963 $$ = XKB_KEY_NoSymbol;
964 }
965 /*
966 * Integers 0..9 are handled with DECIMAL_DIGIT if
967 * they were formatted as single characters '0'..'9'.
968 * Otherwise they are handled here as raw keysyms
969 * values. E.g. `01` and `0x1` are interpreted as
970 * the keysym 0x0001, while `1` is interpreted as
971 * XKB_KEY_1.
972 */
973 else {
974 /* Any other numeric value */
975 if ($1 <= XKB_KEYSYM_MAX) {
976 /*
977 * Valid keysym
978 * No normalization is performed and value
979 * is used as is.
980 */
981 $$ = (xkb_keysym_t) $1;
982 check_deprecated_keysyms(
983 parser_warn, param, param->ctx,
984 $$, NULL, $$, "%#06"PRIx32, "");
985 } else {
986 /* Invalid keysym */
987 parser_warn(
988 param, XKB_ERROR_INVALID_NUMERIC_KEYSYM,
989 "unrecognized keysym \"%#06"PRIx64"\" "
990 "(%"PRId64")", $1, $1
991 );
992 $$ = XKB_KEY_NoSymbol;
993 }
994 parser_vrb(
995 /*
996 * Require an extra high verbosity, because
997 * keysyms are formatted as number unless
998 * enabling pretty-pretting for the
999 * serialization.
1000 */
1001 param, XKB_LOG_VERBOSITY_COMPREHENSIVE,
1002 XKB_WARNING_NUMERIC_KEYSYM,
1003 "numeric keysym \"%#06"PRIx64"\" (%"PRId64")",
1004 $1, $1
1005 );
1006 }
1007 }
1008 ;
1009
1010 SignedNumber : MINUS Number { $$ = -$2; }
1011 | Number { $$ = $1; }
1012 ;
1013
1014 Number : FLOAT { $$ = $1; }
1015 | DECIMAL_DIGIT { $$ = $1; }
1016 | INTEGER { $$ = $1; }
1017 ;
1018
1019 Float : FLOAT { $$ = 0; }
1020 ;
1021
1022 Integer : INTEGER { $$ = $1; }
1023 | DECIMAL_DIGIT { $$ = $1; }
1024 ;
1025
1026 KeyCode : INTEGER { $$ = $1; }
1027 | DECIMAL_DIGIT { $$ = $1; }
1028 ;
1029
1030 Ident : IDENT { $$ = xkb_atom_intern(param->ctx, $1.start, $1.len); }
1031 | DEFAULT { $$ = xkb_atom_intern_literal(param->ctx, "default"); }
1032 ;
1033
1034 String : STRING { $$ = xkb_atom_intern(param->ctx, $1, strlen($1)); free($1); }
1035 ;
1036
1037 OptMapName : MapName { $$ = $1; }
1038 | { $$ = NULL; }
1039 ;
1040
1041 MapName : STRING { $$ = $1; }
1042 ;
1043
1044 %%
1045
1046 /* Parse a specific section */
1047 XkbFile *
1048 parse(struct xkb_context *ctx, struct scanner *scanner, const char *map)
1049 {
1050 int ret;
1051 XkbFile *first = NULL;
1052 struct parser_param param = {
1053 .scanner = scanner,
1054 .ctx = ctx,
1055 .rtrn = NULL,
1056 .more_maps = false,
1057 };
1058
1059 /*
1060 * If we got a specific map, we look for it exclusively and return
1061 * immediately upon finding it. Otherwise, we need to get the
1062 * default map. If we find a map marked as default, we return it
1063 * immediately. If there are no maps marked as default, we return
1064 * the first map in the file.
1065 */
1066
1067 while ((ret = yyparse(&param)) == 0 && param.more_maps) {
1068 if (map) {
1069 if (streq_not_null(map, param.rtrn->name))
1070 return param.rtrn;
1071 else
1072 FreeXkbFile(param.rtrn);
1073 }
1074 else {
1075 if (param.rtrn->flags & MAP_IS_DEFAULT) {
1076 FreeXkbFile(first);
1077 return param.rtrn;
1078 }
1079 else if (!first) {
1080 first = param.rtrn;
1081 }
1082 else {
1083 FreeXkbFile(param.rtrn);
1084 }
1085 }
1086 param.rtrn = NULL;
1087 }
1088
1089 if (ret != 0) {
1090 /* Some error happend; clear the Xkbfiles parsed so far */
1091 FreeXkbFile(first);
1092 FreeXkbFile(param.rtrn);
1093 return NULL;
1094 }
1095
1096 if (first)
1097 log_vrb(ctx, XKB_LOG_VERBOSITY_DETAILED,
1098 XKB_WARNING_MISSING_DEFAULT_SECTION,
1099 "No map in include statement, but \"%s\" contains several; "
1100 "Using first defined map, \"%s\"\n",
1101 scanner->file_name, safe_map_name(first));
1102
1103 return first;
1104 }
1105
1106 /* Parse the next section */
1107 bool
1108 parse_next(struct xkb_context *ctx, struct scanner *scanner, XkbFile **xkb_file)
1109 {
1110 int ret;
1111 struct parser_param param = {
1112 .scanner = scanner,
1113 .ctx = ctx,
1114 .rtrn = NULL,
1115 .more_maps = false,
1116 };
1117
1118 if ((ret = yyparse(&param)) == 0 && param.more_maps) {
1119 *xkb_file = param.rtrn;
1120 return true;
1121 } else {
1122 FreeXkbFile(param.rtrn);
1123 *xkb_file = NULL;
1124 return (ret == 0);
1125 }
1126 }