git-svn-id: svn://db.shs.com.ru/pip@553 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-11-17 11:54:54 +00:00
parent 606261c38d
commit 6cbe77c5b6
7 changed files with 121 additions and 36 deletions

View File

@@ -84,7 +84,16 @@ PICodeParser::PICodeParser() {
void PICodeParser::parseFile(const PIString & file, bool follow_includes) {
clear();
parseFileInternal(file, follow_includes);
/*piCout << "\n\nDefines:";
/*piForeachC (Entity * c, entities) {
piCout << "";
piCout << c->type << c->name << c->parent_scope << c->parents << c->children;
if (c->parent_scope)
piCout << "parent" << c->parent_scope->name;
piForeachC (Member & m, c->members) {
piCout << m.type << m.name;
}
}
piCout << "\n\nDefines:";
piForeachC (Define & m, defines)
piCout << "define" << m.first << m.second;
piCout << "\n\nMacros:";
@@ -171,6 +180,7 @@ void PICodeParser::clear() {
main_file.clear();
evaluator.clearCustomVariables();
cur_def_vis = Global;
anon_num = 0;
defines << Define("PICODE", "") << custom_defines;
}
@@ -277,19 +287,19 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
else pfc.takeSymbol();
continue;
}
if (pfc.left(5) == "class" || pfc.left(6) == "struct") {
if (pfc.left(5) == "class" || pfc.left(6) == "struct" || pfc.left(5) == "union") {
int dind = pfc.find("{", 0), find = pfc.find(";", 0);
if (dind < 0 && find < 0) {pfc.cutLeft(6); continue;}
if (dind < 0 || find < dind) {pfc.cutLeft(6); continue;}
ccmn = pfc.left(dind) + "{\n" + pfc.mid(dind).takeRange('{', '}') + "\n}\n";
pfc.remove(0, ccmn.size());
parseClass(ccmn);
parseClass(0, ccmn);
continue;
}
if (pfc.left(4) == "enum") {
pfc.cutLeft(4);
tmp = pfc.takeCWord();
parseEnum(cur_namespace + tmp, pfc.takeRange("{", "}"));
parseEnum(0, cur_namespace + tmp, pfc.takeRange("{", "}"));
pfc.takeSymbol();
continue;
}
@@ -337,13 +347,18 @@ PICodeParser::Entity * PICodeParser::parseClassDeclaration(const PIString & fc)
else parents << pe;
}
}
bool is_class = cd.left(5) == "class";
PIString typename_ = cd.left(6).trim();
bool is_class = typename_ == "class";
cur_def_vis = (is_class ? Private : Public);
PIString cn = cd.mid(6).trim();
bool has_name = !cn.isEmpty();
if (cn.isEmpty()) cn = "<unnamed_" + PIString::fromNumber(anon_num++) + ">";
//piCout << "found " << typename_ << cn;
if (cn.isEmpty()) return 0;
Entity * e = new Entity();
e->name = cur_namespace + cn;
e->type = (is_class ? "class" : "struct");
e->type = typename_;//(is_class ? "class" : "struct");
e->has_name = has_name;
e->parents = parents;
e->file = cur_file;
entities << e;
@@ -351,15 +366,18 @@ PICodeParser::Entity * PICodeParser::parseClassDeclaration(const PIString & fc)
}
PIString PICodeParser::parseClass(PIString & fc) {
PIString PICodeParser::parseClass(Entity * parent, PIString & fc) {
Visibility prev_vis = cur_def_vis;
int dind = fc.find("{"), find = fc.find(";"), end = 0;
if (dind < 0 && find < 0) return PIString();
if (dind < 0 || find < dind) return fc.left(find);
//piCout << "parse class <****\n" << fc.left(20) << "\n****>";
Entity * ce = parseClassDeclaration(fc.takeLeft(dind));
fc.trim().cutLeft(1).cutRight(1).trim();
//piCout << "found class <****\n" << fc << "\n****>";
if (!ce) return PIString();
if (parent) parent->children << ce;
ce->parent_scope = parent;
int ps = -1;
bool def = false;
PIString prev_namespace = cur_namespace, stmp;
@@ -369,12 +387,23 @@ PIString PICodeParser::parseClass(PIString & fc) {
while (!fc.isEmpty()) {
PIString cw = fc.takeCWord(), tmp;
//piCout << "\ntaked word" << cw;
if (cw == "public") {cur_def_vis = Public; fc.cutLeft(1); continue;}
if (cw == "public") {cur_def_vis = Public; fc.cutLeft(1); continue;}
if (cw == "protected") {cur_def_vis = Protected; fc.cutLeft(1); continue;}
if (cw == "private") {cur_def_vis = Private; fc.cutLeft(1); continue;}
if (cw == "class") {if (isDeclaration(fc, 0, &end)) {fc.cutLeft(end); fc.takeSymbol(); continue;} tmp = fc.takeLeft(fc.find("{")); stmp = fc.takeRange("{", "}"); fc.takeSymbol(); stmp = "class " + tmp + "{" + stmp + "}"; parseClass(stmp); continue;}
if (cw == "struct") {if (isDeclaration(fc, 0, &end)) {fc.cutLeft(end); fc.takeSymbol(); continue;} tmp = fc.takeLeft(fc.find("{")); stmp = fc.takeRange("{", "}"); fc.takeSymbol(); stmp = "struct " + tmp + "{" + stmp + "}"; parseClass(stmp); continue;}
if (cw == "enum") {tmp = fc.takeCWord(); parseEnum(cur_namespace + tmp, fc.takeRange("{", "}")); fc.takeSymbol(); continue;}
if (cw == "private") {cur_def_vis = Private; fc.cutLeft(1); continue;}
if (cw == "class" || cw == "struct" || cw == "union") {
if (isDeclaration(fc, 0, &end)) {
fc.cutLeft(end);
fc.takeSymbol();
continue;
}
tmp = fc.takeLeft(fc.find("{"));
stmp = fc.takeRange("{", "}");
fc.takeSymbol();
stmp = cw + " " + tmp + "{" + stmp + "}";
parseClass(ce, stmp);
continue;
}
if (cw == "enum") {tmp = fc.takeCWord(); parseEnum(ce, cur_namespace + tmp, fc.takeRange("{", "}")); fc.takeSymbol(); continue;}
if (cw == "friend") {fc.cutLeft(fc.find(";") + 1); continue;}
if (cw == "typedef") {ce->typedefs << parseTypedef(fc.takeLeft(fc.find(";"))); typedefs << ce->typedefs.back(); typedefs.back().first.insert(0, cur_namespace); if (ce->typedefs.back().first.isEmpty()) ce->typedefs.pop_back(); fc.takeSymbol(); continue;}
if (cw == "template") {
@@ -400,7 +429,7 @@ PIString PICodeParser::parseClass(PIString & fc) {
}
bool PICodeParser::parseEnum(const PIString & name, PIString fc) {
bool PICodeParser::parseEnum(Entity * parent, const PIString & name, PIString fc) {
//piCout << "enum" << name << fc;
Enum e(name);
PIStringList vl(fc.split(","));
@@ -511,8 +540,10 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
} else {
if (fc.endsWith(";")) fc.cutRight(1);
if (fc.startsWith("using") || !(fc.contains(' ') || fc.contains('\t') || fc.contains('\n'))) return true;
int bits = extractMemberBits(fc);
tl = fc.split(",");
//piCout << "\tmember" << fc;
//piCout << "member" << fc;
//piCout << "member after eb" << fc << ", bits =" << bits;
if (tl.isEmpty()) return true;
bool vn = true;
ctemp = tl.front();
@@ -562,7 +593,9 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
}
me.is_type_ptr = (me.type.right(1) == "]" || me.type.right(1) == "*");
me.type += crepl;
//piCout << "var" << me.type << me.name << me.is_const << me.is_static;
me.bits = bits;
//PICout(PICoutManipulators::AddAll) << "var" << me.type << me.name << me.bits;
//piCout << "var" << v;
parent->members << me;
}
}
@@ -571,6 +604,16 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
}
int PICodeParser::extractMemberBits(PIString & fc) {
int i = fc.findLast(":");
if (i <= 0) return -1;
if (fc[i - 1].toAscii() == ':') return -1;
PIString bs = fc.takeMid(i).mid(1).trim();
if (bs.isEmpty()) return -1;
return bs.toInt();
}
void PICodeParser::normalizeEntityNamespace(PIString & n) {
PIString suff, pref;
for (int i = n.size_s() - 1; i > 0; --i)
@@ -713,7 +756,7 @@ PICodeParser::Entity * PICodeParser::findEntityByName(const PIString & en) {
bool PICodeParser::isDeclaration(const PIString & fc, int start, int * end) {
int dind = fc.find("{", start), find = fc.find(";", start);
//piCout << "isDeclaration" << dind << find;
//piCout << "isDeclaration" << dind << find << fc.left(10);
if (dind < 0 && find < 0) {if (end) *end = -1; return true;}
if (dind < 0 || find < dind) {if (end) *end = find; return true;}
if (end) *end = dind;