Commit 21a8a58d authored by Simon Morlat's avatar Simon Morlat

set GPLv2 headers, and move all cerr/cout to use bctbx logging api.

parent 6557f1fc
/*
* abnf.h
* Copyright (C) 2017 Belledonne Communications SARL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _ABNF_H_
......
/*
* belr.h
* Copyright (C) 2017 Belledonne Communications SARL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _BELR_H_
......@@ -283,6 +283,6 @@ private:
}
}//end of namespace
#endif
/*
* parser.h
* Copyright (C) 2017 Belledonne Communications SARL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _PARSER_H_
......@@ -312,6 +312,8 @@ T universal_pointer_cast(U * p){
return static_cast<T>(p);
}
void belr_fatal(const char *message);
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
void ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_parserElementT obj, _valueT value){
mFunc(universal_pointer_cast<_derivedParserElementT>(obj),value);
......@@ -319,8 +321,7 @@ void ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_p
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
void ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invokeWithChild(_parserElementT obj, _parserElementT child){
std::cerr<<"We should never be called in ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invokeWithChild(_parserElementT obj, _parserElementT child)"<<std::endl;
abort();
belr_fatal("We should never be called in ParserCollector<_derivedParserElementT,_parserElementT, _valueT>::invokeWithChild(_parserElementT obj, _parserElementT child)");
}
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
......@@ -330,8 +331,7 @@ void ParserChildCollector<_derivedParserElementT,_parserElementT, _valueT>::invo
template <typename _derivedParserElementT, typename _parserElementT, typename _valueT>
void ParserChildCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_parserElementT obj, _valueT value){
std::cerr<<"We should never be called in ParserChildCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_parserElementT obj, _valueT value)"<<std::endl;
abort();
belr_fatal("We should never be called in ParserChildCollector<_derivedParserElementT,_parserElementT, _valueT>::invoke(_parserElementT obj, _valueT value)");
}
template <typename _parserElementT>
......@@ -425,7 +425,9 @@ template <typename _parserElementT>
void ParserHandlerBase<_parserElementT>::installCollector(const std::string &rulename, const std::shared_ptr<AbstractCollector<_parserElementT>> &collector){
std::shared_ptr<Recognizer> rec=mParser.mGrammar->findRule(rulename);
if (!rec){
std::cerr<<"There is no rule '"<<rulename<<"' in the grammar."<<std::endl;
std::ostringstream ostr;
ostr<<"There is no rule '"<<rulename<<"' in the grammar.";
belr_fatal(ostr.str().c_str());
return;
}
mCollectors[rec->getId()]=collector;
......@@ -524,7 +526,7 @@ _parserElementT ParserContext<_parserElementT>::createRootObject(const std::stri
template <typename _parserElementT>
std::shared_ptr<HandlerContext<_parserElementT>> ParserContext<_parserElementT>::_branch(){
if (mHandlerStack.empty()){
std::cerr<<"Cannot branch while stack is empty"<<std::endl;
belr_fatal("Cannot branch while stack is empty");
}
std::shared_ptr<HandlerContext<_parserElementT>> ret=mHandlerStack.back()->branch();
mHandlerStack.push_back(ret);
......@@ -534,8 +536,7 @@ std::shared_ptr<HandlerContext<_parserElementT>> ParserContext<_parserElementT>:
template <typename _parserElementT>
void ParserContext<_parserElementT>::_merge(const std::shared_ptr<HandlerContext<_parserElementT>> &other){
if (mHandlerStack.back()!=other){
std::cerr<<"The branch being merged is not the last one of the stack !"<<std::endl;
abort();
belr_fatal("The branch being merged is not the last one of the stack !");
}
mHandlerStack.pop_back();
mHandlerStack.back()->merge(other);
......@@ -546,8 +547,7 @@ template <typename _parserElementT>
void ParserContext<_parserElementT>::_removeBranch(const std::shared_ptr<HandlerContext<_parserElementT>> &other){
auto it=find(mHandlerStack.rbegin(), mHandlerStack.rend(),other);
if (it==mHandlerStack.rend()){
std::cerr<<"A branch could not be found in the stack while removing it !"<<std::endl;
abort();
belr_fatal("A branch could not be found in the stack while removing it !");
}else{
advance(it,1);
mHandlerStack.erase(it.base());
......@@ -587,7 +587,7 @@ void ParserContext<_parserElementT>::removeBranch(const std::shared_ptr<HandlerC
template <typename _parserElementT>
Parser<_parserElementT>::Parser(const std::shared_ptr<Grammar> &grammar) : mGrammar(grammar) {
if (!mGrammar->isComplete()){
std::cerr<<"Grammar not complete, aborting."<<std::endl;
belr_fatal("Grammar not complete, aborting.");
return;
}
}
......@@ -603,8 +603,9 @@ template <typename _parserElementT>
void Parser<_parserElementT>::installHandler(const std::shared_ptr<ParserHandlerBase<_parserElementT>> &handler){
std::shared_ptr<Recognizer> rec=mGrammar->findRule(handler->getRulename());
if (!rec){
std::cerr<<"There is no rule '"<<handler->getRulename()<<"' in the grammar."<<std::endl;
return;
std::ostringstream str;
str<<"There is no rule '"<<handler->getRulename()<<"' in the grammar.";
belr_fatal(str.str().c_str());
}
mHandlers[rec->getId()]=handler;
}
......
......@@ -32,6 +32,10 @@ set(BELR_SOURCE_FILES_CXX
bc_apply_compile_flags(BELR_SOURCE_FILES_C STRICT_OPTIONS_CPP STRICT_OPTIONS_C)
bc_apply_compile_flags(BELR_SOURCE_FILES_CXX STRICT_OPTIONS_CPP STRICT_OPTIONS_CXX)
add_definitions(
-DBCTBX_LOG_DOMAIN="belr"
)
if(ENABLE_STATIC)
add_library(belr-static STATIC ${BELR_HEADER_FILES} ${BELR_SOURCE_FILES_C} ${BELR_SOURCE_FILES_CXX})
set_target_properties(belr-static PROPERTIES OUTPUT_NAME belr)
......
/*
* abnf.cpp
* Copyright (C) 2017 Belledonne Communications SARL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "belr/abnf.h"
using namespace std;
......
/*
* belr.cpp
* Copyright (C) 2017 Belledonne Communications SARL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "belr/parser.h"
#include "common.h"
#include "belr/parser.h"
#include "belr/belr.h"
using namespace std;
using namespace belr;
namespace belr{;
void belr_fatal(const char *message){
bctbx_fatal("%s", message);
}
// =============================================================================
......@@ -68,9 +73,9 @@ const string &Recognizer::getName()const{
size_t Recognizer::feed(const shared_ptr<ParserContextBase> &ctx, const string &input, size_t pos){
size_t match;
#ifdef BELR_DEBUG
cout << "Trying to match: "<<mName<<endl;
#endif
#ifdef BELR_DEBUG
BCTBX_SLOGD<<<"Trying to match: "<<mName;
#endif
ParserLocalContext hctx;
if (ctx) ctx->beginParse(hctx, shared_from_this());
......@@ -79,7 +84,7 @@ size_t Recognizer::feed(const shared_ptr<ParserContextBase> &ctx, const string &
#ifdef BELR_DEBUG
if (mName.size()>0){
string matched=input.substr(pos,match);
cout<<"Matched recognizer '"<<mName<<"' with sequence '"<<matched<<"'."<<endl;
BCTBX_SLOGD<<"Matched recognizer '"<<mName<<"' with sequence '"<<matched<<"'.";
}
#endif
}
......@@ -91,11 +96,12 @@ size_t Recognizer::feed(const shared_ptr<ParserContextBase> &ctx, const string &
bool Recognizer::getTransitionMap(TransitionMap* mask){
bool ret=_getTransitionMap(mask);
if (0 /*!mName.empty()*/){
cout<<"TransitionMap after "<<mName<<endl;
BCTBX_SLOGD<<"TransitionMap after "<<mName;
ostringstream mapss;
for(int i=0;i<256;++i){
if (mask->mPossibleChars[i]) cout<<(char)i;
if (mask->mPossibleChars[i]) mapss<<(char)i;
}
cout<<endl;
BCTBX_SLOGD<<mapss.str();
}
return ret;
}
......@@ -213,7 +219,6 @@ void Selector::_optimize(int recursionLevel){
}
if (all) delete all;
if (!intersectionFound){
//cout<<"Selector '"<<getName()<<"' is exclusive."<<endl;
mIsExclusive=true;
}
}
......@@ -361,8 +366,7 @@ size_t RecognizerPointer::_feed(const shared_ptr<ParserContextBase> &ctx, const
if (mRecognizer){
return mRecognizer->feed(ctx, input, pos);
}else{
cerr<<"RecognizerPointer with name '"<<mName<<"' is undefined"<<endl;
abort();
bctbx_fatal("RecognizerPointer with name '%s' is undefined", mName.c_str());
}
return string::npos;
}
......@@ -397,8 +401,7 @@ void Grammar::assignRule(const string &argname, const shared_ptr<Recognizer> &ru
if (pointer){
pointer->setPointed(rule);
}else{
cerr<<"Error: rule '"<<name<<"' is being redefined !"<<endl;
abort();
bctbx_fatal("Rule '%s' is being redefined !", name.c_str());
}
}
/*in any case the map should contain real recognizers (not just pointers) */
......@@ -414,12 +417,10 @@ void Grammar::_extendRule(const string &argname, const shared_ptr<Recognizer> &r
if (sel){
sel->addRecognizer(rule);
}else{
cerr<<"Error: rule '"<<name<<"' cannot be extended because it was not defined with a Selector."<<endl;
abort();
bctbx_fatal("rule '%s' cannot be extended because it was not defined with a Selector.", name.c_str());
}
}else{
cerr<<"Error: rule '"<<name<<"' cannot be extended because it is not defined."<<endl;
abort();
bctbx_fatal("rule '%s' cannot be extended because it is not defined.", name.c_str());
}
}
......@@ -459,7 +460,7 @@ shared_ptr<Recognizer> Grammar::getRule(const string &argname){
void Grammar::include(const shared_ptr<Grammar>& grammar){
for(auto it=grammar->mRules.begin();it!=grammar->mRules.end();++it){
if (mRules.find((*it).first)!=mRules.end()){
cerr<<"Rule '"<<(*it).first<<"' is being redefined while including grammar '"<<grammar->mName<<"' into '"<<mName<<"'"<<endl;
BCTBX_SLOGE<<"Rule '"<<(*it).first<<"' is being redefined while including grammar '"<<grammar->mName<<"' into '"<<mName<<"'";
}
mRules[(*it).first]=(*it).second;
}
......@@ -470,7 +471,7 @@ bool Grammar::isComplete()const{
for(auto it=mRules.begin(); it!=mRules.end(); ++it){
shared_ptr<RecognizerPointer> pointer=dynamic_pointer_cast<RecognizerPointer>((*it).second);
if (pointer && !pointer->getPointed()){
cerr<<"Rule '"<<(*it).first<<"' is not defined."<<endl;
BCTBX_SLOGE<<"Rule '"<<(*it).first<<"' is not defined.";
ret=false;
}
}
......@@ -489,8 +490,11 @@ int Grammar::getNumRules() const{
}
string belr::tolower(const string &str){
string tolower(const string &str){
string ret(str);
transform(ret.begin(),ret.end(), ret.begin(), ::tolower);
return ret;
}
}//end of namespace
/*
* grammarbuilder.cpp
* Copyright (C) 2017 Belledonne Communications SARL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <fstream>
......@@ -118,8 +118,7 @@ shared_ptr< Recognizer > ABNFElement::buildRecognizer(const shared_ptr< Grammar
else
return Utils::literal(mCharVal);
}
bctbx_error("[belr] ABNFElement::buildRecognizer is empty, should not happen!");
abort();
bctbx_fatal("ABNFElement::buildRecognizer is empty, should not happen!");
return nullptr;
}
......@@ -141,8 +140,7 @@ void ABNFElement::setCharVal(const string& charval){
void ABNFElement::setProseVal(const string& prose){
if (!prose.empty()){
bctbx_error("[belr] prose-val is not supported.");
abort();
bctbx_fatal("prose-val is not supported.");
}
}
......@@ -172,7 +170,6 @@ void ABNFRepetition::setElement(const shared_ptr< ABNFElement >& e){
shared_ptr< Recognizer > ABNFRepetition::buildRecognizer(const shared_ptr< Grammar >& grammar){
if (mRepeat.empty()) return mElement->buildRecognizer(grammar);
//cout<<"Building repetition recognizer with count="<<mCount<<" min="<<mMin<<" max="<<mMax<<endl;
if (mCount!=-1){
return Foundation::loop()->setRecognizer(mElement->buildRecognizer(grammar), mCount, mCount);
}else{
......@@ -186,8 +183,7 @@ shared_ptr<ABNFConcatenation> ABNFConcatenation::create(){
shared_ptr<Recognizer> ABNFConcatenation::buildRecognizer(const shared_ptr<Grammar> &grammar){
if (mRepetitions.size()==0){
bctbx_error("[belr] No repetitions set !");
abort();
bctbx_fatal("No repetitions set !");
}
if (mRepetitions.size()==1){
return mRepetitions.front()->buildRecognizer(grammar);
......@@ -210,7 +206,6 @@ shared_ptr<ABNFAlternation> ABNFAlternation::create(){
}
void ABNFAlternation::addConcatenation(const shared_ptr<ABNFConcatenation> &c){
//cout<<"Concatenation "<<c<<" added to alternation "<<this<<endl;
mConcatenations.push_back(c);
}
......@@ -232,13 +227,11 @@ shared_ptr<ABNFRule> ABNFRule::create(){
}
void ABNFRule::setName(const string& name){
if (!mName.empty()) bctbx_error("[belr] Rule %s is renamed !!!!!", name.c_str());
//cout<<"Rule "<<this<<" is named "<<name<<endl;
if (!mName.empty()) bctbx_error("Rule %s is renamed !!!!!", name.c_str());
mName=name;
}
void ABNFRule::setAlternation(const shared_ptr<ABNFAlternation> &a){
//cout<<"Rule "<<this<<" is given alternation "<<a<<endl;
mAlternation=a;
}
......@@ -256,12 +249,10 @@ void ABNFRule::setDefinedAs(const string& defined_as){
shared_ptr<ABNFRuleList> ABNFRuleList::create(){
//cout<<"Rulelist created."<<endl;
return make_shared<ABNFRuleList>();
}
void ABNFRuleList::addRule(const shared_ptr<ABNFRule>& rule){
//cout<<"Rule "<<rule<<" added to rulelist "<<this<<endl;
mRules.push_back(rule);
}
......@@ -316,12 +307,12 @@ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &abnf, const
size_t parsed;
shared_ptr<ABNFBuilder> builder = mParser.parseInput("rulelist",abnf,&parsed);
if (!builder) {
bctbx_error("[belr] Failed to create builder.");
bctbx_error("Failed to create builder.");
return nullptr;
}
if (parsed<(size_t)abnf.size()){
bctbx_error("[belr] Only %llu bytes parsed over a total of %llu.", (unsigned long long)parsed, (unsigned long long) abnf.size());
bctbx_error("Only %llu bytes parsed over a total of %llu.", (unsigned long long)parsed, (unsigned long long) abnf.size());
return nullptr;
}
......@@ -330,13 +321,13 @@ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &abnf, const
else retGram=gram;
builder->buildRecognizer(retGram);
bctbx_message("[belr] Succesfully created grammar with %i rules.", retGram->getNumRules());
bctbx_message("Succesfully created grammar with %i rules.", retGram->getNumRules());
if (retGram->isComplete()){
bctbx_message("[belr] Grammar is complete.");
bctbx_message("Grammar is complete.");
retGram->optimize();
bctbx_message("[belr] Grammar has been optimized.");
bctbx_message("Grammar has been optimized.");
}else{
bctbx_warning("[belr] Grammar is not complete.");
bctbx_warning("Grammar is not complete.");
}
return gram;
}
......@@ -344,7 +335,7 @@ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &abnf, const
shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnfFile(const string &path, const shared_ptr<Grammar> &gram){
ifstream istr(path);
if (!istr.is_open()){
bctbx_error("[belr] Could not open %s", path.c_str());
bctbx_error("Could not open %s", path.c_str());
return nullptr;
}
stringstream sstr;
......
/*
* parser.cpp
* Copyright (C) 2017 Belledonne Communications SARL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "common.h"
#include "belr/parser.h"
using namespace std;
......
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