Files
ScriptPreprocessor/CPreprocessor.hpp
T

99 lines
3.9 KiB
C++
Raw Normal View History

2015-05-07 18:10:22 -07:00
#ifndef CPREPROCESSOR_HPP
#define CPREPROCESSOR_HPP
#include <map>
#include <string>
#include <functional>
#include "CLexer.hpp"
#include "CLineTranslator.hpp"
class CPreprocessor
{
public:
2015-05-09 16:45:41 -07:00
struct Macro
{
std::string name;
std::vector<CLexer::Token> args;
std::vector<CLexer::Token> code;
};
struct PreprocessorState
{
std::string currentFile;
std::string rootFile;
unsigned int currentLine;
unsigned int globalLine;
};
2015-05-07 18:10:22 -07:00
struct PragmaInstance
{
std::string name;
std::string text;
2015-05-09 16:45:41 -07:00
PreprocessorState state;
2015-05-07 18:10:22 -07:00
};
CPreprocessor();
typedef std::map<std::string, int> ArgSet;
struct DefineEntry
{
CLexer::TokenList tokens;
ArgSet arguments;
};
typedef std::map<std::string, DefineEntry> DefineTable;
typedef DefineTable::iterator DefineIterator;
typedef std::map<std::string, std::function<void(PragmaInstance)> > PragmaMap;
typedef PragmaMap::iterator PragmaIterator;
2015-05-09 16:45:41 -07:00
typedef std::map<std::string, std::function<void(CLexer::TokenList&, DefineTable&, PreprocessorState)> > HookMap;
2015-05-07 19:20:06 -07:00
typedef HookMap::iterator HookIterator;
2015-05-09 16:45:41 -07:00
typedef std::vector<Macro> MacroList;
typedef MacroList::iterator MacroIterator;
2015-05-07 18:10:22 -07:00
void define(const std::string& def);
void undefine(const std::string& def);
void registerPragma(const std::string& name, std::function<void(PragmaInstance)> cb);
2015-05-09 16:45:41 -07:00
void registerHook(const std::string& name, std::function<void(CLexer::TokenList&, DefineTable&, PreprocessorState)> cb);
2015-05-07 18:10:22 -07:00
std::string finalizedSource();
2015-05-09 16:45:41 -07:00
bool preprocessFile(const std::string& filename);
bool preprocessCode(const std::string& filename, const std::string& code);
2015-05-07 19:20:06 -07:00
static void advanceList(CLexer::TokenList& tokens);
2015-05-07 18:10:22 -07:00
private:
std::string _loadSource(const std::string& filename);
void printErrorMessage(const std::string& errMsg);
void printWarningMessage(const std::string& warnMesg);
2015-05-09 16:45:41 -07:00
bool preprocessRecursive(const std::string& filename, const std::string& code, CLexer::TokenList& tokens, DefineTable& defineTable);
2015-05-07 18:10:22 -07:00
void callPragma(const std::string& name, const PragmaInstance& parms);
CLexer::TokenIterator _findToken(CLexer::TokenIterator begin, CLexer::TokenIterator end, CLexer::TokenType type);
CLexer::TokenIterator _parseStatement(CLexer::TokenIterator begin, CLexer::TokenIterator end, CLexer::TokenList& dest);
CLexer::TokenIterator _parseDefineArguments(CLexer::TokenIterator begin, CLexer::TokenIterator end, CLexer::TokenList& tokens, std::vector<CLexer::TokenList>& args);
CLexer::TokenIterator _expandDefine(CLexer::TokenIterator begin, CLexer::TokenIterator end, CLexer::TokenList& tokens, DefineTable& defineTable);
2015-05-09 16:45:41 -07:00
void _expandMacro(CLexer::TokenIterator begin, CLexer::TokenIterator end, CLexer::TokenList& tokens, const Macro& macro);
2015-05-07 18:10:22 -07:00
void _parseDefine(DefineTable& defineTable, CLexer::TokenList& tokens);
CLexer::TokenIterator _parseIfDef(CLexer::TokenIterator begin, CLexer::TokenIterator end);
void _parseIf(CLexer::TokenList& directive, std::string& nameOut);
void _parsePragma(CLexer::TokenList& args);
std::string _expandMessage(DefineTable& defineTable, CLexer::TokenList& args);
void _parseWarning(CLexer::TokenList& args, DefineTable& defineTable);
void _parseError(CLexer::TokenList& args, DefineTable& defineTable);
2015-05-09 16:45:41 -07:00
CLexer::TokenIterator _parseIdentifier(CLexer::TokenIterator begin, CLexer::TokenIterator end, CLexer::TokenList& tokens, DefineTable& defineTable);
2015-05-07 18:10:22 -07:00
DefineTable m_applicationDefined;
PragmaMap m_registeredPragmas;
2015-05-07 19:20:06 -07:00
HookMap m_registeredHooks;
2015-05-07 18:10:22 -07:00
CLineTranslator m_lineTranslator;
CLexer::TokenList m_tokens;
std::string m_rootFile;
std::string m_currentFile;
unsigned int m_currentLine;
unsigned int m_currentFileLines;
unsigned int m_errorCount;
2015-05-09 16:45:41 -07:00
MacroList m_macros;
2015-05-07 18:10:22 -07:00
};
#endif // CPREPROCESSOR_HPP