#include #include #include #include #include #include #include #include #include namespace karma = boost::spirit::karma; namespace phoenix = boost::phoenix; class astIntDouble { public: astIntDouble(); astIntDouble(int x, bool force = false); astIntDouble(double x, bool force = false); int getInt() const {return 3;} double getDouble() const { return 10; } bool isInt() const {return true;} bool isDouble() const {return false;} }; enum kommentarModus { kommentarStrichpunkt, kommentarKlammer }; BOOST_STRONG_TYPEDEF(std::string, astKommentarKlammer); // Kommentar BOOST_STRONG_TYPEDEF(std::string, astKommentarStrichpunkt); // Kommentar struct astShortWord { // g0 oder x4.5 astShortWord(); // addr='?' explicit astShortWord(const char addr_, const astIntDouble &val_); bool operator== (const astShortWord &other) const; char addr; astIntDouble val; }; struct astShortFunctionWord { // B=DC(50) Q=SIN(10) astShortFunctionWord(); // addr='?' explicit astShortFunctionWord(const char addr_, const std::string &func_, const astIntDouble &val_); bool operator== (const astShortWord &other) const; char addr; std::string func; astIntDouble val; }; typedef boost::variant astZyklusParameter; struct astZyklusCallWord { bool operator== (const astZyklusCallWord &other) const; bool operator!= (const astZyklusCallWord &other) const; std::string name; std::list param; }; BOOST_STRONG_TYPEDEF(std::string, astKeyWord); // mcall struct astShortArrayWord { // r4=23 oder r8=3.4 q7=6 aber nicht ccc3=2.0 astShortArrayWord(); // addr='?' explicit astShortArrayWord(const char addr_, const int index_, const astIntDouble &val_); char addr; int index; astIntDouble val; }; struct astReturn {}; struct astProjectMemberIdentifier { astProjectMemberIdentifier() : siemensAltNumber(0), fanucNumber(0) {} bool operator== (const astProjectMemberIdentifier &other) const; bool operator< (const astProjectMemberIdentifier &other) const; int siemensAltNumber; // 0 == undefined (507 == L507) int fanucNumber; // 0 == undefined (12 == <0012>) std::string name; // empty == undefined (POS1) }; // gemaess Definition CYCLE82 CWK2 struct astErikNcBohrPlanSenk { astErikNcBohrPlanSenk(); double RTP, RFP, SDIS, DP, DPR, DTB; }; typedef boost::variant astSubCall; typedef boost::optional astSetModal; struct astTool { astTool(); // toolNumber -1, mode=NURVORBEREITEN enum { NURVORBEREITEN, UNBEKANNTEINWECHSELN, VORBEREITENUNDEINWECHSELN } mode; int toolNumber; // undefiniert bei mode == UNBEKANNTEINWECHSELN }; typedef boost::variant astWord; struct astKnownLine { astKnownLine() : isModalTrigger(false) {} bool isModalTrigger; std::list words; }; BOOST_STRONG_TYPEDEF(std::string, astUnknownLine); // ~GAGA BOOST_STRONG_TYPEDEF(std::string, astPasteLine); // %_MPF33 - paste without asking typedef boost::variant astLine; // einzelne Zeile typedef std::list astProgram; // vollstaendiges Programm struct astProjectMember { astProjectMember(); // isMainProgram(false), modalActiveOnCall(false) astProjectMemberIdentifier ident; bool isMainProgram; // else sub bool modalActiveOnCall; astProgram myAstProgram; }; struct astProject { std::string name; std::list members; }; BOOST_FUSION_ADAPT_STRUCT(astShortWord,(char,addr)(astIntDouble,val)) BOOST_FUSION_ADAPT_STRUCT(astShortFunctionWord,(char,addr)(std::string,func)(astIntDouble,val)) BOOST_FUSION_ADAPT_STRUCT(astZyklusCallWord,(std::string,name)(std::list,param)) BOOST_FUSION_ADAPT_STRUCT(astShortArrayWord,(char,addr)(int,index)(astIntDouble,val)) BOOST_FUSION_ADAPT_STRUCT(astProjectMemberIdentifier,(int,siemensAltNumber)(int,fanucNumber)(std::string,name)) struct directToStringImpl { template struct result { typedef std::string type; }; template std::string operator()(Arg arg1) const { return arg1; } }; phoenix::function directToString; template struct ncRealPolicy : karma::real_policies { static int floatfield(Num n) { return karma::real_policies::fmtflags::fixed; } static unsigned precision(Num n) { return 6; } static bool trailing_zeros(Num n) { return false; } template static bool dot(OutputIterator& sink, Num n, unsigned precision) { if (n==0) return true; return karma::real_policies::dot(sink, n, precision); } template static bool fraction_part(OutputIterator& sink, Num n, unsigned adjprec, unsigned precision) { if (n==0) return true; return karma::real_policies::fraction_part(sink, n, adjprec, precision); } }; struct intDoubleToStringImpl { template struct result { typedef std::string type; }; template std::string operator()(const Arg &arg1) const { std::string generated; std::back_insert_iterator outiter(generated); karma::real_generator > const ncDouble; if (arg1.isDouble()) karma::generate(outiter, ncDouble, arg1.getDouble()); else if (arg1.isInt()) karma::generate(outiter, ncDouble, arg1.getInt()); return generated; } }; phoenix::function intDoubleToString; struct knownLineToListWordImpl { template struct result { typedef std::list type; }; template std::list operator()(const Arg &arg1) const { return arg1.words; } }; phoenix::function knownLineToListWord; struct returnToIntImpl { template struct result { typedef int type; }; template int operator()(const Arg &arg1) const { return 0; } }; phoenix::function returnToInt; struct anyToDumpStringImpl { template struct result { typedef std::string type; }; template std::string operator()(const Arg &arg1) const { return "bla"; } }; phoenix::function anyToDumpString; template struct astGenerate : karma::grammar { astGenerate() : astGenerate::base_type(ruleProgram) { using karma::char_; using karma::int_; using karma::double_; using karma::omit; using karma::eol; using karma::lit; using karma::_val; using karma::_1; ruleIntDouble = karma::string [ _1 = intDoubleToString(_val) ]; ruleKommentarKlammer = lit('(') << karma::string [ _1 = directToString(_val) ] << lit(')'); ruleKommentarStrichpunkt = lit(';') << karma::string [ _1 = directToString(_val) ]; ruleShortWord = char_ << ruleIntDouble; ruleShortFunctionWord = char_ << lit('=') << karma::string << lit('(') << ruleIntDouble << lit(')'); ruleShortArrayWord = char_ << int_ << lit('=') << ruleIntDouble; ruleZyklusParameter = karma::string | ruleIntDouble; ruleZyklusCallWord = karma::string << lit('(') << ruleZyklusParameter % lit(',') << lit(')'); ruleKeyWord = karma::string [ _1 = directToString(_val) ]; ruleReturn = "astReturn" << omit[int_] [ _1 = returnToInt(_val) ]; ruleErikNcBohrPlanSenk = karma::string [ _1 = anyToDumpString(_val) ]; ruleProjectMemberIdentifier = "projectMemberIdentifier(siemensAltNumber=" << int_ << ", fanucNumber=" << int_ << ", name=" << karma::string << ')'; ruleSubCall = "subCall(" << ruleProjectMemberIdentifier | ruleErikNcBohrPlanSenk << ')'; ruleSetModal = "ruleSetModal(" << -ruleSubCall << ')'; ruleTool = karma::string [ _1 = anyToDumpString(_val) ]; ruleWord = ( ruleKeyWord | ruleShortWord | ruleShortFunctionWord | ruleShortArrayWord | ruleKommentarKlammer | ruleKommentarStrichpunkt | ruleZyklusCallWord | ruleReturn | ruleSubCall | ruleSetModal | ruleTool ); ruleKnownLineWords = ruleWord % lit(' '); ruleKnownLine = ruleKnownLineWords [ _1 = knownLineToListWord(_val) ] << eol; ruleUnknownLine = "??? " << karma::string [ _1 = directToString(_val) ] << eol; rulePasteLine = karma::string [ _1 = directToString(_val) ] << eol; ruleLine = ruleKnownLine | ruleUnknownLine | rulePasteLine; ruleProgram = *ruleLine; } karma::rule ruleIntDouble; karma::rule ruleKommentarKlammer; karma::rule ruleKommentarStrichpunkt; karma::rule ruleShortWord; karma::rule ruleShortFunctionWord; karma::rule ruleShortArrayWord; karma::rule ruleZyklusParameter; karma::rule ruleZyklusCallWord; karma::rule ruleKeyWord; karma::rule ruleReturn; karma::rule ruleErikNcBohrPlanSenk; karma::rule ruleProjectMemberIdentifier; karma::rule ruleSubCall; karma::rule ruleSetModal; karma::rule ruleTool; karma::rule ruleWord; karma::rule()> ruleKnownLineWords; karma::rule ruleKnownLine; karma::rule ruleUnknownLine; karma::rule rulePasteLine; karma::rule ruleLine; karma::rule ruleProgram; }; void astGenerateString(const astProgram &input, std::string &output) { output.clear(); std::back_insert_iterator sink(output); astGenerate > myAstGenerate; if ( ! karma::generate(sink, myAstGenerate, input)) throw "NC Programmgenerierung fehlgeschlagen. Interner Fehler, darf nicht passieren"; } int main(int argc, char *argv[]) { astProjectMemberIdentifier myAstProjectMemberIdentifier; myAstProjectMemberIdentifier.siemensAltNumber=100; astSetModal myAstSetModal; myAstSetModal = myAstProjectMemberIdentifier; astKnownLine myAstKnownLine; myAstKnownLine.words.push_back(myAstSetModal); astProgram myAstProgram; myAstProgram.push_back(myAstKnownLine); std::string out; astGenerateString(myAstProgram, out); std::cout << out; return 0; }