summaryrefslogtreecommitdiff
path: root/src/cparse/cparse.ypp
blob: eb2ab12c4de95f36c4edc2a40a14e042163d456b (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
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
%{
#include <cstdio>

#include "cparse.hpp"
#include "cparse_def.h"

// stuff from lex
extern "C" {
extern int cparse_lineno;
extern char *cparse_text;
int cparse_lex();
void cparse_set_in(FILE *fin);
}

static void
cparse_error(const char *s)
{
  printf("Invalid config file (%s): error at line %d, text [%s]\n",
         s, cparse_lineno, cparse_text);
}

int level = 0;
int ndeact = 0;
char *ncomment = NULL;
char *nname = NULL;
char *nval = NULL;

static void
print_node()
{
  int i = 0;
  if (ncomment) {
    for (i = 0; i <= level; i++) {
      printf("  ");
    }
    printf(" /*%s*/\n", ncomment);
  }
  printf("%s", ndeact ? "!" : " ");
  for (i = 0; i <= level; i++) {
    printf("  ");
  }
  printf("%s", nname);
  if (nval) {
    printf(" '%s'", nval);
  }
  printf("\n");
}

%}

%token NODE
%token VALUE
%token COMMENT
%token LEFTB
%token RIGHTB
%token SYNTAX_ERROR

%%

input:      forest
          | forest comment
;

forest:     /* empty */
          | forest tree
;

tree:       node {
              print_node();
            }
          | node {
              print_node();
            } LEFTB { ++level; } forest RIGHTB { --level; }
;

node:       nodec {
              nval = NULL;
            }
          | nodec VALUE {
              nval = $2.str;
            }
;

nodec:      NODE {
              ncomment = NULL;
              nname = $1.str;
              ndeact = $1.deactivated;
            }
          | COMMENT NODE {
              ncomment = $1.str;
              nname = $2.str;
              ndeact = $2.deactivated;
            }
;

comment:    COMMENT
          | comment COMMENT
;

%%

int
cparse::parse_file(FILE *fin)
{
  cparse_set_in(fin);
  return cparse_parse();
}