summaryrefslogtreecommitdiff
path: root/src/helper.c
blob: 5d7e9f88b35b54bd7037048442aa99a79a04c643 (plain)
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
#include "helper.h"

struct Label
read_label(unsigned char *ptr)
{
  struct Label label = { 0 };

  switch (*ptr) {
  case Plain:
    label.status = Plain;
    break;
  case Packed:
    label.status = Packed;
    break;
  default:
    label.status = Clone;
    label.clone_index = from_big_endian(ptr+1);
    break;
  }

  label.start = from_big_endian(ptr+9);
  label.end = from_big_endian(ptr+17);

  switch (*(ptr+25)) {
  case Terminal:
    label.variant = Terminal;
    break;
  case Nonterminal:
    label.variant = Nonterminal;
    break;
  default:
    label.variant = Rule;
    break;
  }

  label.content = from_big_endian(ptr+26);

  return label;
}

void
print_label(struct Label label)
{
  switch (label.status) {
  case Plain:
    printf("a plain node ");
    break;
  case Packed:
    printf("a packed node ");
    break;
  default:
    printf("a cloned node with index %llu ", label.clone_index);
    break;
  }

  printf("spanning (%llu, %llu) ", label.start, label.end);

  printf("labelled as a ");

  switch (label.variant) {
  case Terminal:
    printf("terminal ");
    break;
  case Nonterminal:
    printf("non-terminal ");
    break;
  default:
    printf("rule ");
    break;
  }

  printf("%llu\n", label.content);
}