Commit 4f32b9c2 authored by Ronan's avatar Ronan

feat(belr): use suggest override warning

parent 9364d69f
......@@ -41,6 +41,7 @@ endif()
include(GNUInstallDirs)
include(CheckSymbolExists)
include(CMakePushCheckState)
include(CheckCXXCompilerFlag)
# find_package should be invoked here to check for libraries - however do NOT
# call include_directories here (see below)
......@@ -87,6 +88,11 @@ else()
endif()
if(ENABLE_STRICT)
list(APPEND STRICT_OPTIONS_CPP "-Werror" "-Wextra" "-Wno-unused-parameter" "-fno-strict-aliasing")
CHECK_CXX_COMPILER_FLAG("-Wsuggest-override" SUGGEST_OVERRIDE)
if(SUGGEST_OVERRIDE)
list(APPEND STRICT_OPTIONS_CPP "-Wsuggest-override -Werror=suggest-override")
endif()
endif()
endif()
if(STRICT_OPTIONS_CPP)
......
......@@ -51,6 +51,7 @@ struct TransitionMap{
bool intersect(const TransitionMap *other);
bool intersect(const TransitionMap *other, TransitionMap *result); //performs a AND operation
void merge(const TransitionMap *other); //Performs an OR operation
bool mPossibleChars[256];
};
......@@ -67,13 +68,15 @@ public:
bool getTransitionMap(TransitionMap *mask);
void optimize();
void optimize(int recursionLevel);
protected:
Recognizer() = default;
/*returns true if the transition map is complete, false otherwise*/
virtual bool _getTransitionMap(TransitionMap *mask);
virtual void _optimize(int recursionLevel)=0;
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos)=0;
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) = 0;
std::string mName;
unsigned int mId = 0;
};
......@@ -81,9 +84,11 @@ protected:
class CharRecognizer : public Recognizer{
public:
CharRecognizer(int to_recognize, bool caseSensitive=false);
private:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
void _optimize(int recursionLevel) override;
int mToRecognize;
bool mCaseSensitive;
};
......@@ -91,11 +96,14 @@ private:
class Selector : public Recognizer{
public:
std::shared_ptr<Selector> addRecognizer(const std::shared_ptr<Recognizer> &element);
protected:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
void _optimize(int recursionLevel) override;
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
bool _getTransitionMap(TransitionMap *mask) override;
size_t _feedExclusive(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
virtual bool _getTransitionMap(TransitionMap *mask);
std::list<std::shared_ptr<Recognizer>> mElements;
bool mIsExclusive = false;
};
......@@ -103,28 +111,36 @@ protected:
/**This is an optimization of the first one for the case where there can be only a single match*/
class ExclusiveSelector : public Selector{
private:
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
};
class Sequence : public Recognizer{
public:
bool _getTransitionMap(TransitionMap *mask) override;
std::shared_ptr<Sequence> addRecognizer(const std::shared_ptr<Recognizer> &element);
virtual bool _getTransitionMap(TransitionMap *mask);
protected:
virtual void _optimize(int recursionLevel);
void _optimize(int recursionLevel) override;
private:
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
std::list<std::shared_ptr<Recognizer>> mElements;
};
class Loop : public Recognizer{
public:
bool _getTransitionMap(TransitionMap *mask) override;
std::shared_ptr<Loop> setRecognizer(const std::shared_ptr<Recognizer> &element, int min=0, int max=-1);
virtual bool _getTransitionMap(TransitionMap *mask);
protected:
virtual void _optimize(int recursionLevel);
void _optimize(int recursionLevel) override;
private:
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
std::shared_ptr<Recognizer> mRecognizer;
int mMin = 0;
int mMax = -1;
......@@ -143,19 +159,25 @@ public:
class CharRange : public Recognizer{
public:
CharRange(int begin, int end);
private:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
int mBegin,mEnd;
void _optimize(int recursionLevel) override;
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
int mBegin;
int mEnd;
};
class Literal : public Recognizer{
public:
Literal(const std::string &lit);
virtual bool _getTransitionMap(TransitionMap *mask);
bool _getTransitionMap(TransitionMap *mask) override;
private:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
void _optimize(int recursionLevel) override;
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
std::string mLiteral;
size_t mLiteralSize;
};
......@@ -170,9 +192,11 @@ class RecognizerPointer : public Recognizer{
public:
std::shared_ptr<Recognizer> getPointed();
void setPointed(const std::shared_ptr<Recognizer> &r);
private:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
void _optimize(int recursionLevel) override;
size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos) override;
std::shared_ptr<Recognizer> mRecognizer;
};
......
......@@ -35,15 +35,18 @@ public:
class ABNFRule : public ABNFBuilder{
public:
static std::shared_ptr<ABNFRule> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void setName(const std::string &name);
void setDefinedAs(const std::string &defined_as);
void setAlternation(const std::shared_ptr<ABNFAlternation> &a);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
bool isExtension()const;
const std::string &getName()const{
return mName;
}
static std::shared_ptr<ABNFRule> create();
private:
std::shared_ptr<ABNFAlternation> mAlternation;
std::string mName;
......@@ -52,20 +55,26 @@ private:
class ABNFRuleList : public ABNFBuilder{
public:
static std::shared_ptr<ABNFRuleList> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void addRule(const std::shared_ptr<ABNFRule> & rule);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
static std::shared_ptr<ABNFRuleList> create();
private:
std::list<std::shared_ptr<ABNFRule>> mRules;
};
class ABNFNumval : public ABNFBuilder{
public:
static std::shared_ptr<ABNFNumval> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void setDecVal(const std::string &decval);
void setHexVal(const std::string &hexval);
void setBinVal(const std::string &binval);
static std::shared_ptr<ABNFNumval> create();
private:
void parseValues(const std::string &val, int base);
std::vector<int> mValues;
......@@ -74,12 +83,15 @@ private:
class ABNFElement : public ABNFBuilder{
public:
static std::shared_ptr<ABNFElement> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void setElement(const std::shared_ptr<ABNFBuilder> &e);
void setRulename(const std::string &rulename);
void setCharVal(const std::string &charval);
void setProseVal(const std::string &prose);
static std::shared_ptr<ABNFElement> create();
private:
std::shared_ptr<ABNFBuilder> mElement;
std::string mRulename;
......@@ -88,22 +100,28 @@ private:
class ABNFGroup : public ABNFBuilder{
public:
static std::shared_ptr<ABNFGroup> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void setAlternation(const std::shared_ptr<ABNFAlternation> &a);
static std::shared_ptr<ABNFGroup> create();
private:
std::shared_ptr<ABNFAlternation> mAlternation;
};
class ABNFRepetition : public ABNFBuilder{
public:
static std::shared_ptr<ABNFRepetition> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void setRepeat(const std::string &r);
void setMin(int min);
void setMax(int max);
void setCount(int count);
void setElement(const std::shared_ptr<ABNFElement> &e);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
static std::shared_ptr<ABNFRepetition> create();
private:
int mMin = 0;
int mMax = -1;
......@@ -114,28 +132,37 @@ private:
class ABNFOption : public ABNFBuilder{
public:
static std::shared_ptr<ABNFOption> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void setAlternation(const std::shared_ptr<ABNFAlternation> &a);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
static std::shared_ptr<ABNFOption> create();
private:
std::shared_ptr<ABNFAlternation> mAlternation;
};
class ABNFConcatenation : public ABNFBuilder{
public:
static std::shared_ptr<ABNFConcatenation> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void addRepetition(const std::shared_ptr<ABNFRepetition> &r);
static std::shared_ptr<ABNFConcatenation> create();
private:
std::list<std::shared_ptr<ABNFRepetition>> mRepetitions;
};
class ABNFAlternation : public ABNFBuilder{
public:
static std::shared_ptr<ABNFAlternation> create();
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar) override;
void addConcatenation(const std::shared_ptr<ABNFConcatenation> &c);
std::shared_ptr<Recognizer> buildRecognizer(const std::shared_ptr<Grammar> &grammar);
std::shared_ptr<Recognizer> buildRecognizerNoOptim(const std::shared_ptr<Grammar> &grammar);
static std::shared_ptr<ABNFAlternation> create();
private:
std::list<std::shared_ptr<ABNFConcatenation>> mConcatenations;
};
......@@ -171,10 +198,10 @@ public:
* @return the Grammar object corresponding to the text definition loaded, nullptr if an error occured.
**/
BELR_PUBLIC std::shared_ptr<Grammar> createFromAbnfFile(const std::string &path, const std::shared_ptr<Grammar> &grammar=nullptr);
private:
Parser<std::shared_ptr<ABNFBuilder>> mParser;
};
}
#endif
......@@ -33,8 +33,9 @@ namespace belr {
template<typename _parserElementT>
class AbstractCollector{
public:
virtual void invokeWithChild(_parserElementT obj, _parserElementT child)=0;
virtual ~AbstractCollector() = default;
virtual void invokeWithChild(_parserElementT obj, _parserElementT child)=0;
};
template<typename _parserElementT, typename _valueT>
......@@ -48,8 +49,8 @@ class ParserCollector : public CollectorBase<_parserElementT,_valueT>{
public:
ParserCollector(const std::function<void (_derivedParserElementT , _valueT)> &fn) : mFunc(fn) {}
virtual void invoke(_parserElementT obj, _valueT value);
void invokeWithChild(_parserElementT obj, _parserElementT child);
void invoke(_parserElementT obj, _valueT value) override;
void invokeWithChild(_parserElementT obj, _parserElementT child) override;
private:
std::function<void (_derivedParserElementT, _valueT)> mFunc;
......@@ -60,8 +61,8 @@ class ParserChildCollector : public CollectorBase<_parserElementT,_valueT>{
public:
ParserChildCollector(const std::function<void (_derivedParserElementT , _valueT)> &fn) : mFunc(fn){}
virtual void invoke(_parserElementT obj, _valueT value);
virtual void invokeWithChild(_parserElementT obj, _parserElementT child);
void invoke(_parserElementT obj, _valueT value) override;
void invokeWithChild(_parserElementT obj, _parserElementT child) override;
private:
std::function<void (_derivedParserElementT, _valueT)> mFunc;
......@@ -81,8 +82,9 @@ class ParserHandlerBase : public std::enable_shared_from_this<ParserHandlerBase<
public:
virtual _parserElementT invoke(const std::string &input, size_t begin, size_t count)=0;
std::shared_ptr<HandlerContext<_parserElementT>> createContext();
const std::string &getRulename()const{
const std::string &getRulename() const {
return mRulename;
}
......@@ -107,6 +109,8 @@ public:
ParserHandler(const Parser<_parserElementT> &parser, const std::string &rulename, const std::function<_derivedParserElementT (const std::string &, const std::string &)> &create)
: ParserHandlerBase<_parserElementT>(parser, rulename), mHandlerCreateDebugFunc(create){}
_parserElementT invoke(const std::string &input, size_t begin, size_t count) override;
template <typename _derivedParserElementTChild>
std::shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setCollector(const std::string &child_rule_name, std::function<void (_derivedParserElementTChild , const std::string & )> fn){
this->installCollector(child_rule_name, std::make_shared<ParserCollector<_derivedParserElementT,_parserElementT,const std::string&>>(fn));
......@@ -122,7 +126,6 @@ public:
this->installCollector(child_rule_name, std::make_shared<ParserChildCollector<_derivedParserElementT,_parserElementT,_valueT>>(fn));
return std::static_pointer_cast<ParserHandler<_derivedParserElementT,_parserElementT>>(this->shared_from_this());
}
_parserElementT invoke(const std::string &input, size_t begin, size_t count);
private:
std::function<_derivedParserElementT ()> mHandlerCreateFunc;
......@@ -194,11 +197,12 @@ public:
_parserElementT createRootObject(const std::string &input);
protected:
virtual void beginParse(ParserLocalContext &ctx, const std::shared_ptr<Recognizer> &rec);
virtual void endParse(const ParserLocalContext &ctx, const std::string &input, size_t begin, size_t count);
virtual std::shared_ptr<HandlerContextBase> branch();
virtual void merge(const std::shared_ptr<HandlerContextBase> &other);
virtual void removeBranch(const std::shared_ptr<HandlerContextBase> &other);
void beginParse(ParserLocalContext &ctx, const std::shared_ptr<Recognizer> &rec) override;
void endParse(const ParserLocalContext &ctx, const std::string &input, size_t begin, size_t count) override;
std::shared_ptr<HandlerContextBase> branch() override;
void merge(const std::shared_ptr<HandlerContextBase> &other) override;
void removeBranch(const std::shared_ptr<HandlerContextBase> &other) override;
void _beginParse(ParserLocalContext &ctx, const std::shared_ptr<Recognizer> &rec);
void _endParse(const ParserLocalContext &ctx, const std::string &input, size_t begin, size_t count);
std::shared_ptr<HandlerContext<_parserElementT>> _branch();
......
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