Commit 47a0ae42 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Added possibility to load grammar directly from a string

parent 09d8379a
......@@ -130,6 +130,17 @@ public:
* Initialize the builder.
**/
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.
* An optional Grammar argument corresponding to a grammar to include can be passed.
......@@ -140,7 +151,7 @@ public:
* @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 &path, const shared_ptr<Grammar> &grammar=NULL);
shared_ptr<Grammar> createFromAbnfFile(const string &path, const shared_ptr<Grammar> &grammar=NULL);
private:
Parser<shared_ptr<ABNFBuilder>> mParser;
};
......
......@@ -305,23 +305,16 @@ ABNFGrammarBuilder::ABNFGrammarBuilder()
->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;
ifstream istr(path);
if (!istr.is_open()){
cerr<<"Could not open "<<path<<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;
shared_ptr<ABNFBuilder> builder = mParser.parseInput("rulelist",abnf,&parsed);
if (parsed<(size_t)abnf.size()){
cerr<<"ERROR: only "<<parsed<<" bytes parsed over a total of "<< abnf.size() <<endl;
return NULL;
}
shared_ptr<Grammar> retGram;
if (gram==NULL) retGram=make_shared<Grammar>(path);
if (gram==NULL) retGram=make_shared<Grammar>(abnf);
else retGram=gram;
builder->buildRecognizer(retGram);
cout<<"Succesfully created grammar with "<<retGram->getNumRules()<<" rules."<<endl;
......@@ -335,4 +328,15 @@ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &path, const
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
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