aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc/tree.h
blob: 6fd5bcaa7377dd39add8fda160c6ad197b20274e (plain) (blame)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/**
 * mds — A micro-display server
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef MDS_MDS_KBDC_TREE_H
#define MDS_MDS_KBDC_TREE_H


#include <stddef.h>
#include <stdio.h>
#include <unistd.h>



/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_information_t`
 */
#define MDS_KBDC_TREE_TYPE_INFORMATION  0

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_information_language_t`
 */
#define MDS_KBDC_TREE_TYPE_INFORMATION_LANGUAGE  1

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_information_country_t`
 */
#define MDS_KBDC_TREE_TYPE_INFORMATION_COUNTRY  2

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_information_variant_t`
 */
#define MDS_KBDC_TREE_TYPE_INFORMATION_VARIANT  3

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_include_t`
 */
#define MDS_KBDC_TREE_TYPE_INCLUDE  4

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_function_t`
 */
#define MDS_KBDC_TREE_TYPE_FUNCTION  5

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_macro_t`
 */
#define MDS_KBDC_TREE_TYPE_MACRO  6

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_assumption`
 */
#define MDS_KBDC_TREE_TYPE_ASSUMPTION  7

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_assumption_have_t`
 */
#define MDS_KBDC_TREE_TYPE_ASSUMPTION_HAVE  8

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_assumption_have_chars_t`
 */
#define MDS_KBDC_TREE_TYPE_ASSUMPTION_HAVE_CHARS  9

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_assumption_have_range_t`
 */
#define MDS_KBDC_TREE_TYPE_ASSUMPTION_HAVE_RANGE  10

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_for_t`
 */
#define MDS_KBDC_TREE_TYPE_FOR  11

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_if_t`
 */
#define MDS_KBDC_TREE_TYPE_IF  12

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_let_t`
 */
#define MDS_KBDC_TREE_TYPE_LET  13

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_map_t`
 */
#define MDS_KBDC_TREE_TYPE_MAP  14

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_array_t`
 */
#define MDS_KBDC_TREE_TYPE_ARRAY  15

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_keys_t`
 */
#define MDS_KBDC_TREE_TYPE_KEYS  16

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_string_t`
 */
#define MDS_KBDC_TREE_TYPE_STRING  17

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_nothing_t`
 */
#define MDS_KBDC_TREE_TYPE_NOTHING  18

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_alternation_t`
 */
#define MDS_KBDC_TREE_TYPE_ALTERNATION  19

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_unordered_t`
 */
#define MDS_KBDC_TREE_TYPE_UNORDERED  20

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_ordered_t`
 */
#define MDS_KBDC_TREE_TYPE_ORDERED  21

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_macro_call_t`
 */
#define MDS_KBDC_TREE_TYPE_MACRO_CALL  22

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_return_t`
 */
#define MDS_KBDC_TREE_TYPE_RETURN  23

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_break_t`
 */
#define MDS_KBDC_TREE_TYPE_BREAK  24

/**
 * Value of `mds_kbdc_tree_t.type` for `mds_kbdc_tree_continue_t`
 */
#define MDS_KBDC_TREE_TYPE_CONTINUE  25



/**
 * Keyboard layout syntax tree
 */
union mds_kbdc_tree __attribute__((transparent_union));

/**
 * Keyboard layout syntax tree
 */
typedef union mds_kbdc_tree mds_kbdc_tree_t;



/**
 * This macro is used in this header file, and is then
 * undefined. It defined the common members for these
 * tree structures.
 * 
 * It defines:
 * -  int type;                 -- Integer that specifies which structure is used
 * -  mds_kbdc_tree_t* next;    -- The next node in the tree, at the same level; a sibling
 * -  size_t loc_line;          -- The line in the source code where this is found
 * -  size_t loc_start;         -- The first byte in the source code where this is found, inclusive
 * -  size_t loc_end;           -- The last byte in the source code where this is found, exclusive
 * -  long processed;           -- The lasted step where the statement has already been processed once
 */
#define MDS_KBDC_TREE_COMMON  \
  int type;		      \
  mds_kbdc_tree_t* next;      \
  size_t loc_line;	      \
  size_t loc_start;	      \
  size_t loc_end;	      \
  long processed

/**
 * This macro is used in this header file, and is then
 * undefined. It defined padding of a tree structure.
 * 
 * @param  S:size_t  The size of the data structure excluding this padding and
 *                   the members defined by the macro `MDS_KBDC_TREE_COMMON`
 */
#define MDS_KBDC_TREE_PADDING_(S)  char _padding[(5 * sizeof(void*) - (S)) / sizeof(char)]

/**
 * This macro is used in this header file, and is then
 * undefined. It defined padding of a tree structure.
 * 
 * @param  N  The number of sizeof(void*) sized memebers defined data
 *            structure excluding this padding and the members defined
 *            by the macro `MDS_KBDC_TREE_COMMON`
 */
#define MDS_KBDC_TREE_PADDING(N)  MDS_KBDC_TREE_PADDING_((N) * sizeof(void*))



/**
 * This datastructure is not meant to be
 * used directly, it is a common definition
 * for tree structurs that only have, beside
 * the common members, a pointer to the first
 * node on next level in the tree
 */
struct mds_kbdc_tree_nesting
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The first child node, `.inner.next`
   * is used to access the second child node.
   */
  mds_kbdc_tree_t* inner;
  
  MDS_KBDC_TREE_PADDING(1);
};


/**
 * Tree structure for the "information"-keyword
 */
typedef struct mds_kbdc_tree_nesting mds_kbdc_tree_information_t;


/**
 * This datastructure is not meant to be
 * used directly, it is a common definition
 * for the tree structurs for the information
 * entries: the children of `mds_kbdc_tree_information_t`
 */
struct mds_kbdc_tree_information_data
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The value of the information entry
   */
  char* data;
  
  MDS_KBDC_TREE_PADDING(1);
};


/**
 * Declaration of one of the languages for which the layout is designed
 */
typedef struct mds_kbdc_tree_information_data mds_kbdc_tree_information_language_t;

/**
 * Declaration of one of the country for which the layout is designed
 */
typedef struct mds_kbdc_tree_information_data mds_kbdc_tree_information_country_t;

/**
 * Declaration of which variant of the language–country the layout is
 */
typedef struct mds_kbdc_tree_information_data mds_kbdc_tree_information_variant_t;


/**
 * Leaf structure for inclusion of a file
 */
typedef struct mds_kbdc_tree_include
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The filename of the file to include
   */
  char* filename;
  
  MDS_KBDC_TREE_PADDING(1);
  
} mds_kbdc_tree_include_t;


/**
 * This datastructure is not meant to be
 * used directly, it is a common definition
 * for tree structurs that define a callable
 * element
 */
struct mds_kbdc_tree_callable
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The name of the callable
   */
  char* name;
  
  /**
   * The first child node, `.inner.next`
   * is used to access the second child node
   */
  mds_kbdc_tree_t* inner;
  
  MDS_KBDC_TREE_PADDING(2);
};


/**
 * Tree structure for a function
 */
typedef struct mds_kbdc_tree_callable mds_kbdc_tree_function_t;

/**
 * Tree structure for a macro
 */
typedef struct mds_kbdc_tree_callable mds_kbdc_tree_macro_t;


/**
 * Tree structure for the "assumption"-keyword
 */
typedef struct mds_kbdc_tree_nesting mds_kbdc_tree_assumption_t;


/**
 * Tree structure for making the assumption
 * that there is a mapping to a key or string
 */
typedef struct mds_kbdc_tree_assumption_have
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The key or string
   */
  mds_kbdc_tree_t* data;
  
  MDS_KBDC_TREE_PADDING(1);
  
} mds_kbdc_tree_assumption_have_t;


/**
 * Leaf structure for making the assumption
 * that there are mappings to a set of characters
 */
typedef struct mds_kbdc_tree_assumption_have_chars
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The characters
   */
  char* chars;
  
  MDS_KBDC_TREE_PADDING(1);
  
} mds_kbdc_tree_assumption_have_chars_t;


/**
 * Leaf structure for making the assumption
 * that there are mappings to a range of characters
 */
typedef struct mds_kbdc_tree_assumption_have_range
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The first character, inclusive
   */
  char* first;
  
  /**
   * The last character, inclusive
   */
  char* last;
  
  MDS_KBDC_TREE_PADDING(2);
  
} mds_kbdc_tree_assumption_have_range_t;


/**
 * Tree structure for a "for"-loop
 */
typedef struct mds_kbdc_tree_for
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The first value to variable should take, inclusive
   */
  char* first;
  
  /**
   * The last value the variable should take, inclusive
   */
  char* last;
  
  /**
   * The variable
   */
  char* variable;
  
  /**
   * The first child node, `.inner.next` is
   * used to access the second child node.
   * This is what should be done inside the loop.
   */
  mds_kbdc_tree_t* inner;
  
  MDS_KBDC_TREE_PADDING(4);
  
} mds_kbdc_tree_for_t;


/**
 * Tree structure for a "if"-statement
 */
typedef struct mds_kbdc_tree_if
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The condition
   */
  char* condition;
  
  /**
   * This is what should be done inside
   * if the condition is satisfied
   */
  mds_kbdc_tree_t* inner;
  
  /**
   * This is what should be done inside
   * if the condition is not satisfied
   */
  mds_kbdc_tree_t* otherwise;
  
  MDS_KBDC_TREE_PADDING(3);
  
} mds_kbdc_tree_if_t;


/**
 * Tree structure for assigning a value to a variable,
 * possibly declaring the variable in the process
 */
typedef struct mds_kbdc_tree_let
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The variable
   */
  char* variable;
  
  /**
   * The value to assign to the variable
   */
  mds_kbdc_tree_t* value;
  
  MDS_KBDC_TREE_PADDING(2);
  
} mds_kbdc_tree_let_t;


/**
 * Tree structure for mapping a (possible single element)
 * sequence of key combinations or strings to another
 * combination or string or sequence thereof
 * 
 * Inside functions this can be used for the return value,
 * in such case `sequence` should not be `NULL` but
 * `sequence.next` and `result` should be `NULL`
 */
typedef struct mds_kbdc_tree_map
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The input sequence
   */
  mds_kbdc_tree_t* sequence;
  
  /**
   * The output sequence
   */
  mds_kbdc_tree_t* result;
  
  /* 
   * These are ordered so that `mds_kbdc_tree_t.macro_call.arguments`
   * and `mds_kbdc_tree_t.map.sequence` have the same address.
   */
  
  MDS_KBDC_TREE_PADDING(2);
  
} mds_kbdc_tree_map_t;


/**
 * Tree structure for an array of values
 */
typedef struct mds_kbdc_tree_array
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The first value, `.elements.next`
   * is used to access the second value.
   */
  mds_kbdc_tree_t* elements;
  
  MDS_KBDC_TREE_PADDING(1);
  
} mds_kbdc_tree_array_t;


/**
 * Leaf structure for a key combination
 */
typedef struct mds_kbdc_tree_keys
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The key combination
   */
  char* keys;
  
  MDS_KBDC_TREE_PADDING(1);
  
} mds_kbdc_tree_keys_t;


/**
 * Leaf structure for a string
 */
typedef struct mds_kbdc_tree_string
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The string
   */
  char* string;
  
  MDS_KBDC_TREE_PADDING(1);
  
} mds_kbdc_tree_string_t;


/**
 * Leaf structure for nothing (a dot in the layout code)
 * 
 * Other leaf structures without any content may `typedef`
 * this structure
 */
typedef struct mds_kbdc_tree_nothing
{
  MDS_KBDC_TREE_COMMON;
  MDS_KBDC_TREE_PADDING(0);
  
} mds_kbdc_tree_nothing_t;


/**
 * Tree structure for an alternation
 */
typedef struct mds_kbdc_tree_nesting mds_kbdc_tree_alternation_t;

/**
 * Tree structure for an unordered sequence
 */
typedef struct mds_kbdc_tree_nesting mds_kbdc_tree_unordered_t;

/**
 * Tree structure for an ordered sequence
 * 
 * This is intended has an auxiliary type for
 * simplifying trees
 */
typedef struct mds_kbdc_tree_nesting mds_kbdc_tree_ordered_t;


/**
 * Tree structure for a macro call
 */
typedef struct mds_kbdc_tree_macro_call
{
  MDS_KBDC_TREE_COMMON;
  
  /**
   * The first input argument for the
   * macro call, the second is accessed
   * using `.arguments.next`
   */
  mds_kbdc_tree_t* arguments;
  
  /**
   * The name of the macro
   */
  char* name;
  
  /* 
   * These are ordered so that `mds_kbdc_tree_t.macro_call.arguments`
   * and `mds_kbdc_tree_t.map.sequence` have the same address.
   */
  
  MDS_KBDC_TREE_PADDING(2);
  
} mds_kbdc_tree_macro_call_t;


/**
 * Leaf structure for "return"-keyword
 */
typedef struct mds_kbdc_tree_nothing mds_kbdc_tree_return_t;

/**
 * Leaf structure for "break"-keyword
 */
typedef struct mds_kbdc_tree_nothing mds_kbdc_tree_break_t;

/**
 * Leaf structure for "continue"-keyword
 */
typedef struct mds_kbdc_tree_nothing mds_kbdc_tree_continue_t;



/**
 * Keyboard layout syntax tree
 */
union mds_kbdc_tree
{
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpedantic" /* unnamed struct */
  struct
  {
    MDS_KBDC_TREE_COMMON;
    MDS_KBDC_TREE_PADDING(0);
  };
# pragma GCC diagnostic pop
  
  mds_kbdc_tree_information_t information;
  mds_kbdc_tree_information_language_t language;
  mds_kbdc_tree_information_country_t country;
  mds_kbdc_tree_information_variant_t variant;
  mds_kbdc_tree_include_t include;
  mds_kbdc_tree_function_t function;
  mds_kbdc_tree_macro_t macro;
  mds_kbdc_tree_assumption_t assumption;
  mds_kbdc_tree_assumption_have_t have;
  mds_kbdc_tree_assumption_have_chars_t have_chars;
  mds_kbdc_tree_assumption_have_range_t have_range;
  mds_kbdc_tree_for_t for_;
  mds_kbdc_tree_if_t if_;
  mds_kbdc_tree_let_t let;
  mds_kbdc_tree_map_t map;
  mds_kbdc_tree_array_t array;
  mds_kbdc_tree_keys_t keys;
  mds_kbdc_tree_string_t string;
  mds_kbdc_tree_nothing_t nothing;
  mds_kbdc_tree_alternation_t alternation;
  mds_kbdc_tree_unordered_t unordered;
  mds_kbdc_tree_ordered_t ordered;
  mds_kbdc_tree_macro_call_t macro_call;
  mds_kbdc_tree_return_t return_;
  mds_kbdc_tree_break_t break_;
  mds_kbdc_tree_continue_t continue_;
};



/**
 * Initialise a tree node
 * 
 * @param  this  The memory slot for the tree node
 * @param  type  The type of the node
 */
void mds_kbdc_tree_initialise(mds_kbdc_tree_t* restrict this, int type);

/**
 * Create a tree node
 * 
 * @param   type  The type of the node
 * @return        The tree node, `NULL` on error
 */
mds_kbdc_tree_t* mds_kbdc_tree_create(int type);

/**
 * Release all resources stored in a tree node,
 * without freeing the node itself or freeing
 * or destroying inner `mds_kbdc_tree_t*`:s
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_destroy_nonrecursive(mds_kbdc_tree_t* restrict this);

/**
 * Release all resources stored in a tree node,
 * without freeing or destroying inner
 * `mds_kbdc_tree_t*`:s, but free this node's
 * allocation
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_free_nonrecursive(mds_kbdc_tree_t* restrict this);

/**
 * Release all resources stored in a tree node
 * recursively, but do not free the allocation
 * of the tree node
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_destroy(mds_kbdc_tree_t* restrict this);

/**
 * Release all resources stored in a tree node
 * recursively, and free the allocation
 * of the tree node
 * 
 * @param  this  The tree node
 */
void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this);


/**
 * Create a duplicate of a tree node and
 * its children
 * 
 * @param   this  The tree node
 * @return        A duplicate of `this`, `NULL` on error
 */
mds_kbdc_tree_t* mds_kbdc_tree_dup(mds_kbdc_tree_t* restrict this);


/**
 * Print a tree into a file
 * 
 * @param  this    The tree node
 * @param  output  The output file
 */
void mds_kbdc_tree_print(mds_kbdc_tree_t* restrict this, FILE* output);



#undef MDS_KBDC_TREE_PADDING
#undef MDS_KBDC_TREE_PADDING_
#undef MDS_KBDC_TREE_COMMON

#endif