Commit 47a0ae42 authored by Sylvain Berfini's avatar Sylvain Berfini 🐮
Browse files

Added possibility to load grammar directly from a string

parent 09d8379a
...@@ -130,6 +130,17 @@ public: ...@@ -130,6 +130,17 @@ public:
* Initialize the builder. * Initialize the builder.
**/ **/
ABNFGrammarBuilder(); ABNFGrammarBuilder();
/**
* Create a grammar from an ABNF grammar defined in the string pointed by abnf.
* An optional Grammar argument corresponding to a grammar to include can be passed.
* Usually the belr::CoreRules grammar is required for most IETF text protocols.
* The returned grammar can be used to instanciate a belr::Parser object capable of parsing
* the protocol or language described in the grammar.
* @param abnf the string that contains the abnf grammar.
* @param grammar an optional grammar to include.
* @return the Grammar object corresponding to the text definition loaded, NULL if an error occured.
**/
shared_ptr<Grammar> createFromAbnf(const string &abnf, const shared_ptr<Grammar> &grammar=NULL);
/** /**
* Create a grammar from an ABNF grammar defined in the text file pointed by path. * Create a grammar from an ABNF grammar defined in the text file pointed by path.
* An optional Grammar argument corresponding to a grammar to include can be passed. * An optional Grammar argument corresponding to a grammar to include can be passed.
...@@ -140,7 +151,7 @@ public: ...@@ -140,7 +151,7 @@ public:
* @param grammar an optional grammar to include. * @param grammar an optional grammar to include.
* @return the Grammar object corresponding to the text definition loaded, NULL if an error occured. * @return the Grammar object corresponding to the text definition loaded, NULL if an error occured.
**/ **/
shared_ptr<Grammar> createFromAbnf(const string &path, const shared_ptr<Grammar> &grammar=NULL); shared_ptr<Grammar> createFromAbnfFile(const string &path, const shared_ptr<Grammar> &grammar=NULL);
private: private:
Parser<shared_ptr<ABNFBuilder>> mParser; Parser<shared_ptr<ABNFBuilder>> mParser;
}; };
......
...@@ -305,23 +305,16 @@ ABNFGrammarBuilder::ABNFGrammarBuilder() ...@@ -305,23 +305,16 @@ ABNFGrammarBuilder::ABNFGrammarBuilder()
->setCollector("dec-val", make_sfn(&ABNFNumval::setDecVal)); ->setCollector("dec-val", make_sfn(&ABNFNumval::setDecVal));
} }
shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &path, const shared_ptr<Grammar> &gram){ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &abnf, const shared_ptr<Grammar> &gram){
size_t parsed; size_t parsed;
ifstream istr(path); shared_ptr<ABNFBuilder> builder = mParser.parseInput("rulelist",abnf,&parsed);
if (!istr.is_open()){ if (parsed<(size_t)abnf.size()){
cerr<<"Could not open "<<path<<endl; cerr<<"ERROR: only "<<parsed<<" bytes parsed over a total of "<< abnf.size() <<endl;
return NULL;
}
stringstream sstr;
sstr<<istr.rdbuf();
shared_ptr<ABNFBuilder> builder = mParser.parseInput("rulelist",sstr.str(),&parsed);
if (parsed<(size_t)sstr.str().size()){
cerr<<"ERROR: only "<<parsed<<" bytes parsed over a total of "<< sstr.str().size() <<endl;
return NULL; return NULL;
} }
shared_ptr<Grammar> retGram; shared_ptr<Grammar> retGram;
if (gram==NULL) retGram=make_shared<Grammar>(path); if (gram==NULL) retGram=make_shared<Grammar>(abnf);
else retGram=gram; else retGram=gram;
builder->buildRecognizer(retGram); builder->buildRecognizer(retGram);
cout<<"Succesfully created grammar with "<<retGram->getNumRules()<<" rules."<<endl; cout<<"Succesfully created grammar with "<<retGram->getNumRules()<<" rules."<<endl;
...@@ -335,4 +328,15 @@ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &path, const ...@@ -335,4 +328,15 @@ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &path, const
return gram; return gram;
} }
shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnfFile(const string &path, const shared_ptr<Grammar> &gram){
ifstream istr(path);
if (!istr.is_open()){
cerr<<"Could not open "<<path<<endl;
return NULL;
}
stringstream sstr;
sstr<<istr.rdbuf();
return createFromAbnf(sstr.str(), gram);
}
}//end of namespace }//end of namespace
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