Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarlyn committed Dec 13, 2016
1 parent 1bbefd9 commit 264468c
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 94 deletions.
38 changes: 24 additions & 14 deletions src/cpp/TestsrcSlicePolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,48 @@
#include <unordered_map>
#include <unordered_set>
#include <srcSAXHandler.hpp>
#include <DeclTypePolicy.hpp>
#include <srcSlicePolicy.hpp>
#include <cassert>

class TestDeclType : public srcSAXEventDispatch::PolicyDispatcher, public srcSAXEventDispatch::PolicyListener{
class TestSrcSlice : public srcSAXEventDispatch::PolicyDispatcher, public srcSAXEventDispatch::PolicyListener{
public:
~TestDeclType(){}
TestDeclType(std::initializer_list<srcSAXEventDispatch::PolicyListener *> listeners = {}) : srcSAXEventDispatch::PolicyDispatcher(listeners){}
~TestSrcSlice(){}
TestSrcSlice(std::initializer_list<srcSAXEventDispatch::PolicyListener *> listeners = {}) : srcSAXEventDispatch::PolicyDispatcher(listeners){}
void Notify(const PolicyDispatcher * policy, const srcSAXEventDispatch::srcSAXEventContext & ctx) override {
decltypedata = *policy->Data<DeclTypePolicy::DeclTypeData>();
datatotest.push_back(decltypedata);
srcslicedata = *policy->Data<SrcSlicePolicy::SrcSliceData>();
datatotest.push_back(srcslicedata);
}
void RunTest(){
std::cout << datatotest.size() << std::endl;

for(int i = 0; i < 1/*datatotest.size()*/; i++)
{
std::cout << "Test works" << std::endl;
//std::cout << "Type: " << datatotest[i].nameoftype << "\nName: " << datatotest[i].nameofidentifier << std::endl;
}


}
protected:
void * DataInner() const override {
return (void*)0; //To silence the warning
}
private:

DeclTypePolicy declpolicy;
DeclTypePolicy::DeclTypeData decltypedata;
std::vector<DeclTypePolicy::DeclTypeData> datatotest;
SrcSlicePolicy srcslicepolicy;
SrcSlicePolicy::SrcSliceData srcslicedata;
std::vector<SrcSlicePolicy::SrcSliceData> datatotest;
};

int main(int argc, char** filename){
std::string codestr = "void foo(){int& abc; Object<int> onetwothree; static Object* DoReiMe; const Object* aybeecee;\n nlp::std::vector<std::string> spaces;}";
std::string srcmlstr = codestr;
std::string srcmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><unit xmlns=\"http://www.srcML.org/srcML/src\" xmlns:cpp=\"http://www.srcML.org/srcML/cpp\" revision=\"0.9.5\" language=\"C++\" filename=\"testFile.hpp\"><function><type><name>void</name></type> <name>foo</name><parameter_list>()</parameter_list><block>{<decl_stmt><decl><type><name>int</name><modifier>&amp;</modifier></type> <name>abc</name></decl>;</decl_stmt> <decl_stmt><decl><type><name><name>Object</name><argument_list type=\"generic\">&lt;<argument><expr><name>int</name></expr></argument>&gt;</argument_list></name></type> <name>onetwothree</name></decl>;</decl_stmt> <decl_stmt><decl><specifier>static</specifier> <type><name>Object</name><modifier>*</modifier></type> <name>DoReiMe</name></decl>;</decl_stmt> <decl_stmt><decl><type><specifier>const</specifier> <name>Object</name><modifier>*</modifier></type> <name>aybeecee</name></decl>;</decl_stmt><decl_stmt><decl><type><name><name>nlp</name><operator>::</operator><name>std</name><operator>::</operator><name><name>vector</name><argument_list type=\"generic\">&lt;<argument><expr><name><name>std</name><operator>::</operator><name>string</name></name></expr></argument>&gt;</argument_list></name></name></type> <name>spaces</name></decl>;</decl_stmt>}</block></function></unit>";
std::cerr<<"out"<<std::endl;
TestDeclType decltypedata;
TestSrcSlice srcslicedata;
srcSAXController control(srcmlstr);
srcSAXEventDispatch::srcSAXEventDispatcher<DeclTypePolicy> handler {&decltypedata};
srcSAXEventDispatch::srcSAXEventDispatcher<SrcSlicePolicy> handler {&srcslicedata};
std::cerr<<"Before"<<std::endl;
control.parse(&handler); //Start parsing
decltypedata.RunTest();
}
std::cout << "HERE HERE HERE HERE" << std::endl;
srcslicedata.RunTest();
}
225 changes: 145 additions & 80 deletions src/headers/srcSlicePolicy.hpp
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;
}
};

};

0 comments on commit 264468c

Please sign in to comment.