Commit a45ed80d authored by Simon Morlat's avatar Simon Morlat

cleanups again

parent e6b532c1
......@@ -11,7 +11,10 @@ Recognizer::Recognizer(){
}
void Recognizer::setName(const string& name){
static unsigned int id_base=0;
mName=name;
/* every recognizer that is given a name is given also a unique id*/
mId=++id_base;
}
const string &Recognizer::getName()const{
......@@ -173,6 +176,9 @@ shared_ptr<Loop> Foundation::loop(){
return make_shared<Loop>();
}
/*
* TODO: could be optimized() with strcmp with a specialized Recognizer !
*/
shared_ptr<Recognizer> Utils::literal(const string & lt){
shared_ptr<Sequence> seq=Foundation::sequence();
size_t i;
......
......@@ -19,10 +19,14 @@ public:
void setName(const string &name);
const string &getName()const;
size_t feed(const shared_ptr<ParserContextBase> &ctx, const string &input, size_t pos);
unsigned int getId()const{
return mId;
}
protected:
Recognizer();
virtual size_t _feed(const shared_ptr<ParserContextBase> &ctx, const string &input, size_t pos)=0;
string mName;
unsigned int mId;
};
class CharRecognizer : public Recognizer{
......
......@@ -69,7 +69,7 @@ void Assignment<_parserElementT>::invoke(_parserElementT parent, const string &i
//
// HandlerContext template implementation
// HandlerContext template class implementation
//
template <typename _parserElementT>
......@@ -79,9 +79,9 @@ HandlerContext<_parserElementT>::HandlerContext(const shared_ptr<ParserHandlerBa
template <typename _parserElementT>
void HandlerContext<_parserElementT>::setChild(const string &subrule_name, size_t begin, size_t count, const shared_ptr<HandlerContext<_parserElementT>> &child){
auto it=mHandler->mCollectors.find(subrule_name);
if (it!=mHandler->mCollectors.end()){
mAssignments.push_back(Assignment<_parserElementT>((*it).second, begin, count, child));
auto collector=mHandler->getCollector(subrule_name);
if (collector){
mAssignments.push_back(Assignment<_parserElementT>(collector, begin, count, child));
}
}
......@@ -121,7 +121,7 @@ void HandlerContext<_parserElementT>::undoAssignments(size_t pos){
//
template <typename _parserElementT>
ParserHandlerBase<_parserElementT>::ParserHandlerBase(const string &name) : mRulename(tolower(name)){
ParserHandlerBase<_parserElementT>::ParserHandlerBase(const Parser<_parserElementT> &parser, const string &name) : mParser(parser), mRulename(tolower(name)){
}
template <typename _parserElementT>
......@@ -133,7 +133,7 @@ template <typename _parserElementT>
const shared_ptr<AbstractCollector<_parserElementT>> & ParserHandlerBase<_parserElementT>::getCollector(const string &rulename)const{
auto it=mCollectors.find(rulename);
if (it!=mCollectors.end()) return (*it).second;
return NULL;
return mParser.mNullCollector;
}
//
......@@ -157,9 +157,9 @@ template <typename _parserElementT>
ParserLocalContext ParserContext<_parserElementT>::_beginParse(const shared_ptr<Recognizer> &rec){
shared_ptr<HandlerContextBase> ctx;
auto it=mParser.mHandlers.find(rec->getName());
if (it!=mParser.mHandlers.end()){
ctx=(*it).second->createContext();
auto h=mParser.getHandler(rec->getName());
if (h){
ctx=h->createContext();
mHandlerStack.push_back(static_pointer_cast<HandlerContext<_parserElementT>>(ctx));
}
return ParserLocalContext(ctx,rec,mHandlerStack.back()->getLastIterator());
......@@ -272,7 +272,7 @@ Parser<_parserElementT>::Parser(const shared_ptr<Grammar> &grammar) : mGrammar(g
template <typename _parserElementT>
shared_ptr<ParserHandlerBase<_parserElementT>> &Parser<_parserElementT>::getHandler(const string &rulename){
auto it=mHandlers.find(rulename);
if (it=mHandlers.end()) return NULL;
if (it==mHandlers.end()) return mNullHandler;
return (*it).second;
}
......
......@@ -47,11 +47,14 @@ private:
template <typename _parserElementT>
class HandlerContext;
template <typename _parserElementT>
class Parser;
template <typename _parserElementT>
class ParserHandlerBase : public enable_shared_from_this<ParserHandlerBase<_parserElementT>>{
friend class HandlerContext<_parserElementT>;
public:
ParserHandlerBase(const string &name);
ParserHandlerBase(const Parser<_parserElementT> &parser, const string &name);
virtual _parserElementT invoke(const string &input, size_t begin, size_t count)=0;
shared_ptr<HandlerContext<_parserElementT>> createContext();
const string &getRulename()const{
......@@ -62,17 +65,18 @@ protected:
const shared_ptr<AbstractCollector<_parserElementT>> &getCollector(const string &rulename)const;
private:
map<string, shared_ptr<AbstractCollector<_parserElementT>> > mCollectors;
const Parser<_parserElementT> &mParser;
string mRulename;
};
template <typename _derivedParserElementT, typename _parserElementT>
class ParserHandler : public ParserHandlerBase<_parserElementT>{
public:
ParserHandler(const string &rulename, const function<_derivedParserElementT ()> &create)
: ParserHandlerBase<_parserElementT>(rulename), mHandlerCreateFunc(create){
ParserHandler(const Parser<_parserElementT> &parser, const string &rulename, const function<_derivedParserElementT ()> &create)
: ParserHandlerBase<_parserElementT>(parser, rulename), mHandlerCreateFunc(create){
}
ParserHandler(const string &rulename, const function<_derivedParserElementT (const string &, const string &)> &create)
: ParserHandlerBase<_parserElementT>(rulename), mHandlerCreateDebugFunc(create){
ParserHandler(const Parser<_parserElementT> &parser, const string &rulename, const function<_derivedParserElementT (const string &, const string &)> &create)
: ParserHandlerBase<_parserElementT>(parser, rulename), mHandlerCreateDebugFunc(create){
}
shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setCollector(const string &child_rule_name, function<void (_derivedParserElementT , const string & )> fn){
this->installCollector(child_rule_name, make_shared<ParserCollector<_derivedParserElementT,_parserElementT,const string&>>(fn));
......@@ -128,9 +132,6 @@ private:
vector<Assignment<_parserElementT>> mAssignments;
};
template <typename _parserElementT>
class Parser;
struct ParserLocalContext{
ParserLocalContext(const shared_ptr<HandlerContextBase>& hc, const shared_ptr<Recognizer>& rec, size_t pos)
: mHandlerContext(hc), mRecognizer(rec), mAssignmentPos(pos){
......@@ -174,11 +175,12 @@ private:
template <typename _parserElementT>
class Parser{
friend class ParserContext<_parserElementT>;
friend class ParserHandlerBase<_parserElementT>;
public:
Parser(const shared_ptr<Grammar> &grammar);
template <typename _derivedParserElementT>
shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setHandler(const string &rulename,const function<_derivedParserElementT ()> & handler){
auto ret=make_shared<ParserHandler<_derivedParserElementT,_parserElementT>>(rulename,handler);
auto ret=make_shared<ParserHandler<_derivedParserElementT,_parserElementT>>(*this, rulename,handler);
installHandler(ret);
return ret;
......@@ -186,7 +188,7 @@ public:
template <typename _derivedParserElementT>
shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setHandler(const string &rulename,
const function<_derivedParserElementT (const string &, const string &)> & handler){
auto ret=make_shared<ParserHandler<_derivedParserElementT,_parserElementT>>(rulename,handler);
auto ret=make_shared<ParserHandler<_derivedParserElementT,_parserElementT>>(*this, rulename,handler);
installHandler(ret);
return ret;
......@@ -197,6 +199,8 @@ private:
void installHandler(const shared_ptr<ParserHandlerBase<_parserElementT>> &handler);
shared_ptr<Grammar> mGrammar;
map<string, shared_ptr<ParserHandlerBase<_parserElementT>>> mHandlers;
shared_ptr<ParserHandlerBase<_parserElementT>> mNullHandler;
shared_ptr<AbstractCollector<_parserElementT>> mNullCollector;
};
class DebugElement{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment