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

#include <cstore/cstore.hpp>
#include <cnode/cnode.hpp>
#include "cparse.hpp"
#include "cparse_def.h"

using namespace std;
using namespace cnode;

// 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);
}

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

// XXX optimize: use unordered_map with non-vector
static map<vector<string>, CfgNode *> node_map;
static Cstore *cstore = NULL;
static CfgNode *cur_node = NULL;
static CfgNode *cur_parent = NULL;
static vector<CfgNode *> cur_path;
static vector<string> pcomps;
static vector<bool> pcomp_is_value;

static void
add_node()
{
  pcomps.push_back(nname);
  CfgNode *onode = NULL;
  if (node_map.find(pcomps) != node_map.end()) {
    onode = node_map[pcomps];
  }
  pcomps.pop_back();
  if (onode) {
    if (nval) {
      if (onode->isMulti()) {
        // a new value for a "multi node"
        onode->addMultiValue(nval);
        cur_node = onode;
      } else if (onode->isTag()) {
        // a new value for a "tag node"
        cur_node = new CfgNode(pcomps, nname, nval, ncomment, ndeact, cstore);
        onode->addChildNode(cur_node);
      } else {
        /* a new value for a single-value node => invalid?
         * for now, use the newer value.
         */
        cur_node = onode;
        cur_node->setValue(nval);
      }
    } else {
      // existing intermediate node => move current node pointer
      cur_node = onode;
    }
  } else {
    // new node
    cur_node = new CfgNode(pcomps, nname, nval, ncomment, ndeact, cstore);
    CfgNode *mapped_node = cur_node;
    if (cur_node->isTag() && cur_node->isValue()) {
      // tag value => need to add the "tag node" on top
      CfgNode *p = new CfgNode(pcomps, nname, NULL, NULL, ndeact, cstore);
      p->addChildNode(cur_node);
      mapped_node = p;
    }
    cur_parent->addChildNode(mapped_node);
    pcomps.push_back(nname);
    node_map[pcomps] = mapped_node;
    pcomps.pop_back();
  }
}

static void
go_down()
{
  cur_path.push_back(cur_parent);
  cur_parent = cur_node;

  pcomps.push_back(nname);
  pcomp_is_value.push_back(false);
  if (nval) {
    pcomps.push_back(nval);
    pcomp_is_value.push_back(true);
  }
}

static void
go_up()
{
  cur_parent = cur_path.back();
  cur_path.pop_back();

  if (pcomp_is_value.back()) {
    pcomps.pop_back();
    pcomp_is_value.pop_back();
  }
  pcomps.pop_back();
  pcomp_is_value.pop_back();
}

%}

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

%%

input:      forest
          | forest comment
;

forest:     /* empty */
          | forest {
              if (!cur_parent) {
                // root node
                cur_parent = new CfgNode(pcomps, nname, nval, ncomment,
                                         ndeact, cstore);
              }
            } tree
;

tree:       node {
              add_node();
            }
          | node {
              add_node();
            } LEFTB {
              go_down();
            } forest RIGHTB {
              go_up();
            }
;

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
;

%%

CfgNode *
cparse::parse_file(FILE *fin, Cstore& cs)
{
  cparse_set_in(fin);
  cstore = &cs;
  if (cparse_parse() != 0) {
    // parsing failed
    return NULL;
  }
  if (cur_path.size() > 0) {
    // didn't return to top-level => invalid
    return NULL;
  }
  return cur_parent;
}