estyle/estyle/Generator.hpp

145 lines
5.6 KiB
C++

/**
* @author Edouard DUPIN
* @copyright 2017, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <estyle/lexer/Lexer.hpp>
#include <ememory/memory.hpp>
#include <eproperty/Value.hpp>
#include <eproperty/List.hpp>
#include <eproperty/Interface.hpp>
namespace estyle {
class Generator;
class BraceProperty {
public:
BraceProperty();
BraceProperty(estyle::Generator* _generator, const etk::String& _typeName, bool _allowSingle=true);
eproperty::Value<bool> inNewLineBefore;
eproperty::Value<bool> inNewLineAfter;
eproperty::Value<bool> inSpaceBefore;
eproperty::Value<bool> inSpaceAfter;
eproperty::Value<bool> outNewLineBefore;
eproperty::Value<bool> outNewLineAfter;
eproperty::Value<bool> outSpaceBefore;
eproperty::Value<bool> outSpaceAfter;
eproperty::Value<int32_t> oneLineIfSingleMaxSize;
eproperty::Value<bool> single;
eproperty::Value<bool> singleInNewLine;
eproperty::Value<bool> singleInSpace;
eproperty::Value<bool> singleOutNewLine;
eproperty::Value<bool> singleOutSpace;
BraceProperty(const BraceProperty& _obj) = delete;
BraceProperty(BraceProperty&& _obj);
~BraceProperty() = default;
BraceProperty& operator=(BraceProperty&& _obj);
BraceProperty& operator=(const BraceProperty& _obj) = delete;
};
class ParentheseProperty {
public:
ParentheseProperty();
ParentheseProperty(estyle::Generator* _generator, const etk::String& _typeName);
eproperty::Value<bool> inSpaceBefore;
eproperty::Value<bool> inSpaceAfter;
eproperty::Value<bool> outSpaceBefore;
eproperty::Value<bool> outSpaceAfter;
eproperty::Value<int32_t> oneLineMaxSize;
ParentheseProperty(const ParentheseProperty& _obj) = delete;
ParentheseProperty(ParentheseProperty&& _obj);
~ParentheseProperty() = default;
ParentheseProperty& operator=(ParentheseProperty&& _obj);
ParentheseProperty& operator=(const ParentheseProperty& _obj) = delete;
};
class Generator : public eproperty::Interface {
public:
Generator();
~Generator();
public:
eproperty::Value<bool> propertyEndOfLine;
eproperty::Value<bool> propertyIndentWithTabulation;
eproperty::Value<int8_t> propertyIndentSize;
eproperty::Value<bool> propertyDoxygenOneLine;
eproperty::List<int32_t> propertyDoxygenMultipleLine;
eproperty::Value<bool> propertySemiColonReturnBetweenAction; // add a \n if we have a ";" elemeent
// Brace section
// brace for "if"
etk::Map<etk::String, estyle::BraceProperty> propertyBrace;
etk::Map<etk::String, estyle::ParentheseProperty> propertyParenthese;
private:
void clear();
estyle::Lexer m_lexer;
int32_t m_indentation = 12345; //!< number of indentation needed in the current line.
void indentationPush();
void indentationPop();
int32_t m_offset = 12345; //!< number od space needed after the indentation (used for the if / else if / while / functions ....)
etk::Vector<int32_t> m_offsetStack;
void offsetPush(int32_t _count);
void offsetPushAuto();
void offsetPop();
etk::String m_type = "ZZZZZZZZZZZZZZZZZZZZ"; // current stack type ...
etk::Vector<etk::String> m_typeStack;
void typePush(etk::String _count);
void typePop();
etk::String m_output = "ZZZZZZZZZZZZZZZZZZZZ";
public:
etk::String process(const etk::String& _code);
int32_t process(int32_t _startId,
int32_t _stopId,
enum estyle::lexer::tocken _endTocken1 = estyle::lexer::END_OF_FILE,
enum estyle::lexer::tocken _endTocken2 = estyle::lexer::END_OF_FILE,
enum estyle::lexer::tocken _endTocken3 = estyle::lexer::END_OF_FILE,
enum estyle::lexer::tocken _endTocken4 = estyle::lexer::END_OF_FILE);
private:
/**
* @brief Add space " " if no space or '\t' is set before and add the indentation if needed (last char is a "\n")
*/
void addSpace(bool _force=false);
void addSpaceIfNeeded();
/**
* @brief Add Indentation "\t\t\t " if start of line
*/
void addIndent();
/**
* @brief Add newline if no newLine is set before
*/
void addNewLine();
void addNewLineIfSemiColon();
/// check if the previous cheracter is a newline or not...
bool onNewLine();
etk::String getEndOfLine();
etk::String getDoxygenOneLine();
etk::String getDoxygenNLine(const etk::String& _data);
int64_t endOfSection(int64_t _pos);
int64_t endOfAction(int64_t _pos);
int32_t countCurrent(int64_t _pos,
int64_t _posEnd,
enum estyle::lexer::tocken _type1,
enum estyle::lexer::tocken _type2 = estyle::lexer::END_OF_FILE,
enum estyle::lexer::tocken _type3 = estyle::lexer::END_OF_FILE,
enum estyle::lexer::tocken _type4 = estyle::lexer::END_OF_FILE);
int32_t countCurrentLevelCondition(int64_t _pos);
int32_t countCurrentParameters(int64_t _pos);
int32_t countCurrentAction(int64_t _pos);
int32_t countCurrentAction(int64_t _pos, int64_t _posEnd);
//! @brief count the number of char in the specified range
int32_t countRawSize(int64_t _pos, int64_t _posEnd);
int64_t generateBrace(int64_t _start, int64_t _stop, bool _needBrace, bool _checkElse = false);
int64_t generateCondition(int64_t _pos);
int64_t generateFunction(int64_t _pos);
bool previousIs(int64_t _pos, enum estyle::lexer::tocken _previousType);
bool nextIs(int64_t _pos, enum estyle::lexer::tocken _nextType);
int32_t countCurrentLevelComa(int64_t _pos);
int32_t getWhileCondition(int64_t _pos);
etk::String generateType(int64_t _pos);
};
}