-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
169 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,171 @@ | ||
#include <srcSAXEventDispatcher.hpp> | ||
#include <srcSAXHandler.hpp> | ||
#include <exception> | ||
#include <DeclTypePolicy.hpp> | ||
#include <ExprPolicy.hpp> | ||
|
||
class DeclTypePolicy : public srcSAXEventDispatch::EventListener, public srcSAXEventDispatch::PolicyDispatcher, public srcSAXEventDispatch::PolicyListener { | ||
//Problem with variables with same name in different scopes (Use a stack, every time change of scope push onto stack) | ||
//Possibly add line numbers to names | ||
|
||
class SrcSlicePolicy : public srcSAXEventDispatch::EventListener, public srcSAXEventDispatch::PolicyDispatcher, public srcSAXEventDispatch::PolicyListener { | ||
public: | ||
struct DeclTypeData{ | ||
DeclTypeData(): linenumber{0}, isConst{false}, isReference{false}, isPointer{false}, isStatic{false} {} | ||
struct SrcSliceData{ | ||
SrcSliceData(){} | ||
SrcSliceData(std::string name, std::string type, std::set<unsigned int> lines){ | ||
identifierName = name; | ||
identifierType = type; | ||
lineNumbers = lines; | ||
} | ||
|
||
void clear(){ | ||
nameoftype.clear(); | ||
nameofidentifier.clear(); | ||
namespaces.clear(); | ||
linenumber = -1; | ||
isConst = false; | ||
isReference = false; | ||
isPointer = false; | ||
isStatic = false; | ||
identifierName.clear(); | ||
identifierType.clear(); | ||
lineNumbers.clear(); //Should I clear this??? | ||
} | ||
std::string nameoftype; | ||
std::string nameofidentifier; | ||
std::vector<std::string> namespaces; | ||
int linenumber; | ||
bool isConst; | ||
bool isReference; | ||
bool isPointer; | ||
bool isStatic; | ||
|
||
std::string identifierName; | ||
std::string identifierType; | ||
std::set<unsigned int> lineNumbers; | ||
}; | ||
DeclTypeData data; | ||
~DeclTypePolicy(){} | ||
DeclTypePolicy(std::initializer_list<srcSAXEventDispatch::PolicyListener *> listeners = {}): srcSAXEventDispatch::PolicyDispatcher(listeners){ | ||
|
||
struct SrcSliceSet{ | ||
SrcSliceSet() = default; | ||
SrcSliceSet(std::map<std::string, SrcSliceData> dat){ | ||
srcSliceMap = dat; | ||
} | ||
void clear(){ | ||
srcSliceMap.clear(); | ||
} | ||
std::map<std::string, SrcSliceData> srcSliceMap; | ||
}; | ||
|
||
std::map<std::string, SrcSliceData> srcSliceMap; | ||
SrcSliceSet data; | ||
~SrcSlicePolicy(){} | ||
SrcSlicePolicy(std::initializer_list<srcSAXEventDispatch::PolicyListener *> listeners = {}): srcSAXEventDispatch::PolicyDispatcher(listeners){ | ||
exprPolicy.AddListener(this); | ||
declTypePolicy.AddListener(this); | ||
InitializeEventHandlers(); | ||
} | ||
void Notify(const PolicyDispatcher * policy, const srcSAXEventDispatch::srcSAXEventContext & ctx) override {} //doesn't use other parsers | ||
|
||
void Notify(const PolicyDispatcher * policy, const srcSAXEventDispatch::srcSAXEventContext & ctx) override { | ||
using namespace srcSAXEventDispatch; | ||
|
||
|
||
if(ctx.IsOpen(ParserState::declstmt) && ctx.IsClosed(ParserState::exprstmt)){ | ||
declData = *policy->Data<DeclTypePolicy::DeclTypeData>(); | ||
|
||
|
||
auto it = srcSliceMap.find(declData.nameofidentifier); | ||
if(it == srcSliceMap.end()){ | ||
std::set<unsigned int> lines; | ||
lines.insert(declData.linenumber); | ||
srcSliceMap.insert(std::make_pair(declData.nameofidentifier, SrcSliceData(declData.nameofidentifier, declData.nameoftype, lines))); | ||
}else{ | ||
//What to do about multiple decl of same name | ||
} | ||
|
||
|
||
}else if (ctx.IsOpen(ParserState::exprstmt) && ctx.IsClosed(ParserState::declstmt)){ | ||
exprData = *policy->Data<ExprPolicy::ExprDataSet>(); | ||
|
||
//Needed? | ||
for(auto deal : exprData.dataset){ | ||
auto it = srcSliceMap.find(deal.second.nameofidentifier); | ||
if(it != srcSliceMap.end()){ | ||
|
||
|
||
std::set<unsigned int> temp1 = deal.second.def; | ||
std::set<unsigned int> temp2 = deal.second.use; | ||
std::set<unsigned int> final = temp2; | ||
bool match = false; | ||
|
||
|
||
//Union | ||
/* | ||
for(std::set<unsigned int>::iterator it1 = temp1.begin(); it1!= temp1.end(); ++it1){ | ||
for(std::set<unsigned int>::iterator it2 = temp2.begin(); it2!= temp2.end(); ++it2){ | ||
if((*it1) == (*it2)) | ||
match = true; | ||
} | ||
if(!match) | ||
final.insert(*it1); | ||
match = false; | ||
} | ||
temp1 = it->second.lineNumbers; | ||
temp2 = final; | ||
//Union with existing | ||
for(std::set<unsigned int>::iterator it1 = temp1.begin(); it1!= temp1.end(); ++it1){ | ||
for(std::set<unsigned int>::iterator it2 = temp2.begin(); it2!= temp2.end(); ++it2){ | ||
if((*it1) == (*it2)) | ||
match = true; | ||
} | ||
if(!match) | ||
final.insert(*it1); | ||
match = false; | ||
} | ||
*/ | ||
|
||
SrcSliceData sliceData = SrcSliceData(deal.second.nameofidentifier, it->second.identifierType, final); | ||
data.srcSliceMap.insert(std::make_pair(deal.second.nameofidentifier, sliceData)); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
protected: | ||
void * DataInner() const override { | ||
return new DeclTypeData(data); | ||
return new SrcSliceSet(data); | ||
} | ||
private: | ||
std::string currentTypeName, currentDeclName, currentModifier, currentSpecifier; | ||
ExprPolicy exprPolicy; | ||
ExprPolicy::ExprDataSet exprData; | ||
DeclTypePolicy declTypePolicy; | ||
DeclTypePolicy::DeclTypeData declData; | ||
|
||
//This correct? | ||
void InitializeEventHandlers(){ | ||
std::cerr<<"Enter"<<std::endl; | ||
using namespace srcSAXEventDispatch; | ||
openEventMap[ParserState::op] = [this](srcSAXEventContext& ctx){ | ||
if(ctx.And({ParserState::type, ParserState::declstmt}) && ctx.Nor({ParserState::specifier, ParserState::modifier, ParserState::genericargumentlist})){ | ||
data.namespaces.push_back(ctx.currentToken); | ||
} | ||
}; | ||
closeEventMap[ParserState::modifier] = [this](srcSAXEventContext& ctx){ | ||
if(ctx.IsOpen(ParserState::declstmt)){ | ||
if(currentModifier == "*"){ | ||
data.isPointer = true; | ||
} | ||
if(currentModifier == "&"){ | ||
data.isReference = true; | ||
} | ||
} | ||
}; | ||
|
||
closeEventMap[ParserState::decl] = [this](srcSAXEventContext& ctx){ | ||
if(ctx.And({ParserState::declstmt})){ | ||
data.linenumber = ctx.currentLineNumber; | ||
data.nameofidentifier = currentDeclName; | ||
} | ||
openEventMap[ParserState::declstmt] = [this](srcSAXEventContext& ctx) { | ||
ctx.dispatcher->AddListenerDispatch(&declTypePolicy); | ||
}; | ||
|
||
closeEventMap[ParserState::type] = [this](srcSAXEventContext& ctx){ | ||
if(ctx.And({ParserState::declstmt})){ | ||
data.nameoftype = currentTypeName; | ||
} | ||
openEventMap[ParserState::exprstmt] = [this](srcSAXEventContext& ctx) { | ||
ctx.dispatcher->AddListenerDispatch(&exprPolicy); | ||
}; | ||
|
||
closeEventMap[ParserState::tokenstring] = [this](srcSAXEventContext& ctx){ | ||
//TODO: possibly, this if-statement is suppressing more than just unmarked whitespace. Investigate. | ||
if(!(ctx.currentToken.empty() || ctx.currentToken[0] == ' ')){ | ||
if(ctx.And({ParserState::name, ParserState::type, ParserState::decl, ParserState::declstmt}) && ctx.Nor({ParserState::specifier, ParserState::modifier, ParserState::genericargumentlist})){ | ||
currentTypeName = ctx.currentToken; | ||
} | ||
if(ctx.And({ParserState::name, ParserState::decl, ParserState::declstmt}) && | ||
ctx.Nor({ParserState::type, ParserState::index/*skip array portion*/, ParserState::argumentlist/*skip init list portion*/, ParserState::init, ParserState::specifier, ParserState::modifier})){ | ||
currentDeclName = ctx.currentToken; | ||
} | ||
if(ctx.And({ParserState::specifier, ParserState::decl, ParserState::declstmt})){ | ||
currentSpecifier = ctx.currentToken; | ||
} | ||
if(ctx.And({ParserState::modifier, ParserState::type, ParserState::declstmt})){ | ||
currentModifier = ctx.currentToken; | ||
} | ||
} | ||
|
||
//Closing EventMap | ||
closeEventMap[ParserState::exprstmt] = [this](srcSAXEventContext& ctx){ | ||
ctx.dispatcher->RemoveListenerDispatch(&exprPolicy); | ||
NotifyAll(ctx); | ||
//data.clear(); | ||
}; | ||
closeEventMap[ParserState::declstmt] = [this](srcSAXEventContext& ctx){ | ||
ctx.dispatcher->RemoveListenerDispatch(&declTypePolicy); | ||
NotifyAll(ctx); | ||
data.clear(); | ||
}; | ||
closeEventMap[ParserState::specifier] = [this](srcSAXEventContext& ctx){ | ||
if(ctx.IsOpen(ParserState::declstmt)){ | ||
if(currentSpecifier == "const"){ | ||
data.isConst = true; | ||
} | ||
if(currentSpecifier == "static"){ | ||
data.isStatic = true; | ||
} | ||
} | ||
currentSpecifier.clear(); | ||
///data.clear(); //Do we clear? | ||
}; | ||
|
||
//Is this Needed?? | ||
//closeEventMap[ParserState::archive] = [this](srcSAXEventContext& ctx){ | ||
// NotifyAll(ctx); | ||
//}; | ||
std::cerr<<"Exit"<<std::endl; | ||
} | ||
}; | ||
|
||
}; |