Skip to content

Instantly share code, notes, and snippets.

@quarnster
Created August 2, 2012 16:32
Show Gist options
  • Save quarnster/3238415 to your computer and use it in GitHub Desktop.
Save quarnster/3238415 to your computer and use it in GitHub Desktop.
AngelScript preprocessor transforming some c++ syntax into AngelScript code
// Preprocessor.h ----------------------------
#ifndef __INCLUDED_SCRIPTING_ANGELSCRIPT_PREPROCESSING_H
#define __INCLUDED_SCRIPTING_ANGELSCRIPT_PREPROCESSING_H
#include <string>
#include <vector>
class Preprocessor
{
public:
std::string Preprocess(const std::string &data) const;
std::vector<std::pair<std::string, std::string> > typedefs;
private:
template<typename Archive> friend void serialize(Archive & ar, Preprocessor& t, const unsigned int);
};
#endif
// Preprocessor.cpp ----------------------------
#include "Preprocessor.h"
#include <boost/regex.hpp>
#include <boost/foreach.hpp>
std::string Preprocessor::Preprocess(const std::string &data) const
{
std::string ret;
boost::regex exp("(\\w+\\s*)\\*(\\s*\\w+\\s*)(;|=|,|\\))");
ret = boost::regex_replace(data, exp, "\\1@\\2\\3", boost::match_posix);
exp="->";
ret = boost::regex_replace(ret, exp, ".");
exp="^\\s*#include[^\\n]+\\n";
ret = boost::regex_replace(ret, exp, "");
std::string be("(\\(|\\s|\\$|,|\\=|\\+|\\-|\\*|/|;|\\))");
exp = be + "&(\\w+)" + be;
ret = boost::regex_replace(ret, exp, "\\1\\2\\3");
typedef std::pair<std::string, std::string> pairtype;
BOOST_FOREACH(const pairtype & s, typedefs)
{
exp = be + s.first + be;
ret = boost::regex_replace(ret, exp, std::string("\\1") + s.second + "\\2");
}
printf("%s\n", ret.c_str());
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment