Commit 869290ac authored by Simon Morlat's avatar Simon Morlat

work in progress, good progresses

parent 38493581
INSTALL
aclocal.m4
*~
configure
Makefile.in
Makefile
build-aux
m4
autom4te.cache
*.o
*.lo
*.la
config.log
config.status
.deps
.libs
libtool
compile
depcomp
missing
install-sh
config.guess
config.h.in
config.sub
ltmain.sh
stamp-h1
.anjuta
belle-sip.anjuta
.kdev4/
belle-sip.kdev4
config.h
src/specs.c
belle-sip.pc
belle-sip.spec
html/
*.orig
tester/belle_sip_object_describe
tester/belle_sip_parse
tester/belle_sip_tester
tester/belle_http_get
tester/belle_sip_resolve
......@@ -228,6 +228,24 @@ void Grammar::assignRule(const string &argname, const shared_ptr<Recognizer> &ru
mRules[name]=rule;
}
void Grammar::_extendRule(const string &argname, const shared_ptr<Recognizer> &rule){
string name=tolower(argname);
rule->setName("");
auto it=mRules.find(name);
if (it!=mRules.end()){
shared_ptr<Selector> sel=dynamic_pointer_cast<Selector>((*it).second);
if (sel){
sel->addRecognizer(rule);
}else{
cerr<<"Error: rule '"<<name<<"' cannot be extended because it was not defined with a Selector."<<endl;
abort();
}
}else{
cerr<<"Error: rule '"<<name<<"' cannot be extended because it is not defined."<<endl;
abort();
}
}
shared_ptr<Recognizer> Grammar::getRule(const string &argname){
shared_ptr<Recognizer> ret;
string name=tolower(argname);
......
......@@ -116,10 +116,16 @@ public:
assignRule(name, rule);
return rule;
}
template <typename _recognizerT>
shared_ptr<_recognizerT> extendRule(const string & name, const shared_ptr<_recognizerT> &rule){
_extendRule(name, rule);
return rule;
}
shared_ptr<Recognizer> getRule(const string &name);
bool isComplete()const;
private:
void assignRule(const string &name, const shared_ptr<Recognizer> &rule);
void _extendRule(const string &name, const shared_ptr<Recognizer> &rule);
map<string,shared_ptr<Recognizer>> mRules;
string mName;
};
......
......@@ -16,32 +16,99 @@ namespace belr{
ABNFBuilder::~ABNFBuilder(){
}
ABNFRule *ABNFRule::create(){
cout<<"Rule created."<<endl;
return new ABNFRule();
shared_ptr<ABNFConcatenation> ABNFConcatenation::create(){
return make_shared<ABNFConcatenation>();
}
shared_ptr<Recognizer> ABNFRule::buildRecognizer(){
shared_ptr<Recognizer> ABNFConcatenation::buildRecognizer(const shared_ptr<Grammar> &grammar){
return NULL;
}
ABNFRuleList *ABNFRuleList::create(){
shared_ptr<ABNFAlternation> ABNFAlternation::create(){
return make_shared<ABNFAlternation>();
}
void ABNFAlternation::addConcatenation(const shared_ptr<ABNFConcatenation> &c){
cout<<"Concatenation "<<c<<"added to alternation "<<this<<endl;
mConcatenations.push_back(c);
}
shared_ptr<Recognizer> ABNFAlternation::buildRecognizer(const shared_ptr<Grammar> &grammar){
if (mConcatenations.size()==1) return mConcatenations.front()->buildRecognizer(grammar);
return buildRecognizerNoOptim(grammar);
}
shared_ptr< Recognizer > ABNFAlternation::buildRecognizerNoOptim(const shared_ptr< Grammar >& grammar){
auto sel=Foundation::selector();
for (auto it=mConcatenations.begin(); it!=mConcatenations.end(); ++it){
sel->addRecognizer((*it)->buildRecognizer(grammar));
}
return sel;
}
shared_ptr<ABNFRule> ABNFRule::create(){
cout<<"Rule created."<<endl;
return make_shared<ABNFRule>();
}
void ABNFRule::setName(const string& name){
if (!mName.empty())
cerr<<"Rule "<<this<<" is renamed !!!!!"<<endl;
cout<<"Rule "<<this<<" is named "<<name<<endl;
mName=name;
}
void ABNFRule::setAlternation(const shared_ptr<ABNFAlternation> &a){
cout<<"Rule "<<this<<" is given alternation "<<a<<endl;
mAlternation=a;
}
bool ABNFRule::isExtension()const{
return mDefinedAs.find('/')!=string::npos;
}
shared_ptr<Recognizer> ABNFRule::buildRecognizer(const shared_ptr<Grammar> &grammar){
return mAlternation->buildRecognizer(grammar);
}
void ABNFRule::setDefinedAs(const string& defined_as){
mDefinedAs=defined_as;
}
shared_ptr<ABNFRuleList> ABNFRuleList::create(){
cout<<"Rulelist created."<<endl;
return new ABNFRuleList();
return make_shared<ABNFRuleList>();
}
void ABNFRuleList::addRule(ABNFRule *rule){
void ABNFRuleList::addRule(const shared_ptr<ABNFRule>& rule){
cout<<"Rule "<<rule<<" added to rulelist "<<this<<endl;
mRules.push_back(rule);
}
shared_ptr<Recognizer> ABNFRuleList::buildRecognizer(){
shared_ptr<Recognizer> ABNFRuleList::buildRecognizer(const shared_ptr<Grammar> &grammar){
for (auto it=mRules.begin(); it!=mRules.end(); ++it){
shared_ptr<ABNFRule> rule=(*it);
if (rule->isExtension()){
grammar->extendRule(rule->getName(), rule->buildRecognizer(grammar));
}else{
grammar->addRule(rule->getName(), rule->buildRecognizer(grammar));
}
}
return NULL;
}
ABNFGrammarBuilder::ABNFGrammarBuilder()
: mParser(make_shared<ABNFGrammar>()){
mParser.setHandler("rulelist", make_ptrfn(&ABNFRuleList::create))
->setCollector("rule", make_memfn(&ABNFRuleList::addRule));
mParser.setHandler("rule", make_ptrfn(&ABNFRule::create));
mParser.setHandler("rulelist", make_fn(&ABNFRuleList::create))
->setCollector("rule", make_sfn(&ABNFRuleList::addRule));
mParser.setHandler("rule", make_fn(&ABNFRule::create))
->setCollector("rulename",make_sfn(&ABNFRule::setName))
->setCollector("defined-as",make_sfn(&ABNFRule::setDefinedAs))
->setCollector("alternation",make_sfn(&ABNFRule::setAlternation));
mParser.setHandler("alternation", make_fn(&ABNFAlternation::create))
->setCollector("concatenation",make_sfn(&ABNFAlternation::addConcatenation));
}
shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &path){
......
......@@ -6,24 +6,54 @@
#include "parser.hh"
namespace belr{
class ABNFAlternation;
class ABNFBuilder{
public:
virtual ~ABNFBuilder();
virtual shared_ptr<Recognizer> buildRecognizer()=0;
virtual shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar)=0;
};
class ABNFRule : public ABNFBuilder{
public:
static ABNFRule *create();
shared_ptr<Recognizer> buildRecognizer();
static shared_ptr<ABNFRule> create();
void setName(const string &name);
void setDefinedAs(const string &defined_as);
void setAlternation(const shared_ptr<ABNFAlternation> &a);
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
bool isExtension()const;
const string &getName()const{
return mName;
}
private:
shared_ptr<ABNFAlternation> mAlternation;
string mName;
string mDefinedAs;
};
class ABNFRuleList : public ABNFBuilder{
public:
static ABNFRuleList *create();
void addRule(ABNFRule *rule);
shared_ptr<Recognizer> buildRecognizer();
static shared_ptr<ABNFRuleList> create();
void addRule(const shared_ptr<ABNFRule> & rule);
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
private:
list<shared_ptr<ABNFRule>> mRules;
};
class ABNFConcatenation : public ABNFBuilder{
public:
shared_ptr<ABNFConcatenation> create();
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
};
class ABNFAlternation : public ABNFBuilder{
public:
static shared_ptr<ABNFAlternation> create();
void addConcatenation(const shared_ptr<ABNFConcatenation> &c);
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
shared_ptr<Recognizer> buildRecognizerNoOptim(const shared_ptr<Grammar> &grammar);
private:
list<shared_ptr<ABNFConcatenation>> mConcatenations;
};
class ABNFGrammarBuilder{
......@@ -31,7 +61,7 @@ public:
ABNFGrammarBuilder();
shared_ptr<Grammar> createFromAbnf(const string &path);
private:
Parser<ABNFBuilder*> mParser;
Parser<shared_ptr<ABNFBuilder>> mParser;
};
}
......
......@@ -5,18 +5,40 @@
namespace belr{
template <class T, class U>
T universal_pointer_cast(const shared_ptr<U>& sp){
return static_pointer_cast<typename T::element_type>(sp);
}
template <class T, class U>
T universal_pointer_cast(U * p){
return static_cast<T>(p);
}
template <typename _parserElementT>
AbstractCollector<_parserElementT>::~AbstractCollector(){
}
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
void ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_parserElementT obj, _valueT value){
mFunc(static_cast<_derivedParserElementT>(obj),value);
mFunc(universal_pointer_cast<_derivedParserElementT>(obj),value);
}
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
void ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invokeWithChild(_parserElementT obj, _parserElementT child){
cerr<<"We should never be called in ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invokeWithChild(_parserElementT obj, _parserElementT child)"<<endl;
abort();
}
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
void ParserChildCollector<_derivedParserElementT,_parserElementT, _valueT>::invokeWithChild(_parserElementT obj, _parserElementT value){
mFunc(universal_pointer_cast<_derivedParserElementT>(obj),universal_pointer_cast<typename decay<_valueT>::type>(value));
}
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
void ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invokeWithChild(_parserElementT obj, _parserElementT value){
mFunc(static_cast<_derivedParserElementT>(obj),static_cast<_valueT>(value));
void ParserChildCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_parserElementT obj, _valueT value){
cerr<<"We should never be called in ParserChildCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_parserElementT obj, _valueT value)"<<endl;
abort();
}
template <typename _parserElementT>
......@@ -43,6 +65,11 @@ void Assignment<_parserElementT>::invoke(_parserElementT parent, const string &i
}
}
template <typename _derivedParserElementT, typename _parserElementT>
_parserElementT ParserHandler<_derivedParserElementT,_parserElementT>::invoke(){
return universal_pointer_cast<_parserElementT>(mHandlerCreateFunc());
}
template <typename _parserElementT>
ParserContext<_parserElementT>::ParserContext(Parser<_parserElementT> &parser) : mParser(parser){
}
......
......@@ -31,6 +31,17 @@ private:
function<void (_derivedParserElementT, _valueT)> mFunc;
};
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
class ParserChildCollector : public CollectorBase<_parserElementT,_valueT>{
public:
ParserChildCollector(const function<void (_derivedParserElementT , _valueT)> &fn) : mFunc(fn){
}
virtual void invoke(_parserElementT obj, _valueT value);
virtual void invokeWithChild(_parserElementT obj, _parserElementT child);
private:
function<void (_derivedParserElementT, _valueT)> mFunc;
};
template <typename _parserElementT>
class HandlerContext;
......@@ -50,15 +61,16 @@ public:
ParserHandler(const function<_derivedParserElementT ()> &create)
: mHandlerCreateFunc(create){
}
shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setCollector(const string &child_rule_name, function<void (_derivedParserElementT , const string & )> fn){
this->mCollectors[child_rule_name]=make_shared<ParserCollector<_derivedParserElementT,_parserElementT,const string&>>(fn);
return static_pointer_cast<ParserHandler<_derivedParserElementT,_parserElementT>>(this->shared_from_this());
}
template <typename _valueT>
shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setCollector(const string &child_rule_name, function<void (_derivedParserElementT , _valueT)> fn){
this->mCollectors[child_rule_name]=make_shared<ParserCollector<_derivedParserElementT,_parserElementT,_valueT>>(fn);
this->mCollectors[child_rule_name]=make_shared<ParserChildCollector<_derivedParserElementT,_parserElementT,_valueT>>(fn);
return static_pointer_cast<ParserHandler<_derivedParserElementT,_parserElementT>>(this->shared_from_this());
}
_parserElementT invoke(){
return static_cast<_parserElementT>(mHandlerCreateFunc());
}
_parserElementT invoke();
private:
function<_derivedParserElementT ()> mHandlerCreateFunc;
};
......@@ -167,20 +179,25 @@ private:
template <typename _retT>
function< _retT ()> make_ptrfn(_retT (*arg)()){
function< _retT ()> make_fn(_retT (*arg)()){
return function<_retT ()>(arg);
}
template <typename _arg1T, typename _arg2T>
function< void (_arg1T,_arg2T)> make_ptrfn(void (*arg)(_arg1T,_arg2T)){
function< void (_arg1T,_arg2T)> make_fn(void (*arg)(_arg1T,_arg2T)){
return function< void (_arg1T,_arg2T)>(arg);
}
template <typename _klassT, typename _argT>
function< void (_klassT*,_argT)> make_memfn(void (_klassT::*arg)(_argT)){
function< void (_klassT*,_argT)> make_fn(void (_klassT::*arg)(_argT)){
return function< void (_klassT*,_argT)>(mem_fn(arg));
}
template <typename _klassT, typename _argT>
function< void (shared_ptr<_klassT>,_argT)> make_sfn(void (_klassT::*arg)(_argT)){
return function< void (shared_ptr<_klassT>,_argT)>(mem_fn(arg));
}
}
......
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