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

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

using namespace cparse;

/* to enable tracing, define ENABLE_PARSER_TRACE. may also want to invoke
 * bison with "-v" (by changing Makefile.am).
 */
#undef ENABLE_PARSER_TRACE
#ifdef ENABLE_PARSER_TRACE
#define YYDEBUG 1
#endif // ENABLE_PARSER_TRACE

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

typedef Cstore::MapT<Cpath, CfgNode *, CpathHash> NmapT;
static NmapT node_map;
static Cstore *cstore_ = NULL;
static CfgNode *cur_node = NULL;
static CfgNode *cur_parent = NULL;
static vector<CfgNode *> cur_path;
static Cpath pcomps;
static vector<bool> pcomp_is_value;

static void
add_node()
{
  pcomps.push(nname);
  CfgNode *onode = NULL;
  NmapT::iterator it = node_map.find(pcomps);
  if (it != node_map.end()) {
    onode = it->second;
  }
  pcomps.pop();
  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
      // (need to force "tag" if the node is invalid => tag_if_invalid)
      CfgNode *p = new CfgNode(pcomps, nname, NULL, NULL, ndeact, cstore_,
                               true);
      p->addChildNode(cur_node);
      mapped_node = p;
    }
    cur_parent->addChildNode(mapped_node);
    pcomps.push(nname);
    node_map[pcomps] = mapped_node;
    pcomps.pop();
  }
}

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

  pcomps.push(nname);
  pcomp_is_value.push_back(false);
  if (nval) {
    pcomps.push(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();
    pcomp_is_value.pop_back();
  }
  pcomps.pop();
  pcomp_is_value.pop_back();
}

%}

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

%%

input:    forest comment
;

forest:     /* empty */
          | forest tree
;

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

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

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

comment:    /* empty */
          | COMMENT comment
;

%%

CfgNode *
cparse::parse_file(FILE *fin, Cstore& cs)
{
  // for debug (see prologue)
#ifdef ENABLE_PARSER_TRACE
  cparse_debug = 1;
#endif // ENABLE_PARSER_TRACE

  // initial state
  cparse_set_in(fin);
  cstore_ = &cs;
  ndeact = 0;
  ncomment = NULL;
  nname = NULL;
  nval = NULL;
  node_map.clear();
  pcomps.clear();
  pcomp_is_value.clear();
  cur_path.clear();
  cur_node = NULL;
  cur_parent = new CfgNode(pcomps, nname, nval, ncomment, ndeact, cstore_);

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

CfgNode *
cparse::parse_file(const char *fname, Cstore& cs)
{
  FILE *fin = fopen(fname, "r");
  if (!fin) {
    return NULL;
  }
  return parse_file(fin, cs);
}