-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchema.cc
119 lines (109 loc) · 3.23 KB
/
Schema.cc
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
#include <cstdio>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include "YangAst.hh"
#include "y.tab.hh"
#include "DbTree.hh"
#include "Schema.hh"
extern FILE* yyin;
extern Yang::Ast::ModuleBases* moduleBases;
//Modules
void
Modules::addModule(const std::string& name, Yang::Ast::ModuleBase* module)
{
std::cout << "Proccessing module: " << name << std::endl;
Mods::iterator i = _modules.find(name);
if (i != _modules.end()) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << ": duplicate module: " << name;
throw std::invalid_argument(ss.str());
}
_modules[name] = module;
}
Yang::Ast::ModuleBase*
Modules::findModule(const std::string& name)
{
Yang::Ast::ModuleBase* result = 0;
Mods::iterator i = _modules.find(name);
if (i != _modules.end()) {
result = i->second;
}
return result;
}
Yang::Ast::InteriorNode*
Modules::findInteriorNode(const std::string& name)
{
Yang::Ast::InteriorNode* result = 0;
for (Mods::iterator i = _modules.begin();
i != _modules.end() && result == 0;
++i) {
result = i->second->findInteriorNode(name);
}
return result;
}
// Schema
Schema::Schema(const std::list<std::string>& schemas)
{
if (schemas.size() == 0) {
throw std::invalid_argument("Db::Db: need at least one schema file.");
}
typedef std::list<std::string> Schemas;
for (Schemas::const_iterator i = schemas.begin();
i != schemas.end();
++i) {
addSchema(*i);
}
Mods& ms = _modules.getModules();
for (Mods::iterator i = ms.begin(); i != ms.end(); ++i) {
i->second->elaborate(this, &_modules);
}
for (Mods::iterator i = ms.begin(); i != ms.end(); ++i) {
i->second->eval(this, &_modules);
}
}
void
Schema::print()
{
}
extern int yydebug;
void
Schema::addSchema(const std::string& fileName)
{
size_t dotPos = fileName.find_first_of(".");
if (dotPos == std::string::npos) {
std::stringstream ss;
ss << "Db::addSchema, could not find '.' in: " << fileName;
throw std::invalid_argument(ss.str());
}
std::string extension = fileName.substr(dotPos + 1);
if (extension.compare("yang") != 0) {
std::stringstream ss;
ss << "Db::addSchema, wrong extension expecting '.yang' in: " << fileName;
throw std::invalid_argument(ss.str());
}
if (!(yyin = fopen(fileName.c_str(), "r"))) {
std::stringstream ss;
ss << "Db::addSchema, could not open : " << fileName << " for reading";
throw std::invalid_argument(ss.str());
}
//yydebug = 1;
moduleBases = 0;
int rc = yyparse();
if (rc != 0) {
std::cerr << "Db::addSchema, failed parsing: " << fileName
<< " skipping it." << std::endl;
return;
}
for (Yang::Ast::ModuleBases::iterator i = moduleBases->begin();
i != moduleBases->end();
++i) {
_modules.addModule((*i)->getString(), *i);
}
delete moduleBases;
}
Yang::Ast::InteriorNode*
Schema::findInteriorNode(const std::string& name)
{
return _modules.findInteriorNode(name);
}