ERMParser.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. #define VCMI_DLL
  2. #include "ERMParser.h"
  3. #include <boost/version.hpp>
  4. //To make compilation with older boost versions possible
  5. //Don't know exact version - 1.46 works while 1.42 not
  6. #if BOOST_VERSION >= 104600
  7. #include <boost/spirit/include/qi.hpp>
  8. #include <boost/bind.hpp>
  9. #include <boost/spirit/include/phoenix_core.hpp>
  10. #include <boost/spirit/include/phoenix_operator.hpp>
  11. #include <boost/spirit/include/phoenix_fusion.hpp>
  12. #include <boost/spirit/include/phoenix_stl.hpp>
  13. #include <boost/spirit/include/phoenix_object.hpp>
  14. #include <boost/fusion/include/adapt_struct.hpp>
  15. #include <fstream>
  16. #include <boost/algorithm/string/trim.hpp>
  17. namespace spirit = boost::spirit;
  18. namespace qi = boost::spirit::qi;
  19. namespace ascii = spirit::ascii;
  20. namespace phoenix = boost::phoenix;
  21. //Greenspun's Tenth Rule of Programming:
  22. //Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified,
  23. //bug-ridden, slow implementation of half of Common Lisp.
  24. //actually these macros help in dealing with boost::variant
  25. #define BEGIN_TYPE_CASE(LinePrinterVisitor) struct LinePrinterVisitor : boost::static_visitor<> \
  26. {
  27. #define FOR_TYPE(TYPE, VAR) void operator()(TYPE const& VAR) const
  28. #define DO_TYPE_CASE(LinePrinterVisitor, VAR) } ___UN; boost::apply_visitor(___UN, VAR);
  29. CERMPreprocessor::CERMPreprocessor(const std::string &Fname) : fname(Fname), file(Fname), lineNo(0), version(INVALID)
  30. {
  31. if(!file.is_open())
  32. {
  33. tlog1 << "File " << Fname << " not found or unable to open\n";
  34. return;
  35. }
  36. //check header
  37. std::string header;
  38. getline(header);
  39. if(header == "ZVSE")
  40. version = ERM;
  41. else if(header == "VERM")
  42. version = VERM;
  43. else
  44. {
  45. tlog1 << "File " << fname << " has wrong header\n";
  46. return;
  47. }
  48. }
  49. std::string CERMPreprocessor::retreiveCommandLine()
  50. {
  51. std::string wholeCommand;
  52. //parse file
  53. bool verm = false;
  54. bool openedString = false;
  55. int openedBraces = 0;
  56. while(file.good())
  57. {
  58. std::string line ;
  59. getline(line); //reading line
  60. int dash = line.find_first_of('^');
  61. bool inTheMiddle = openedBraces || openedString;
  62. if(!inTheMiddle)
  63. {
  64. if(line.size() < 2)
  65. continue;
  66. if(line[0] != '!' ) //command lines must begin with ! -> otherwise treat as comment
  67. continue;
  68. verm = line[1] == '[';
  69. }
  70. if(openedString)
  71. {
  72. wholeCommand += "\\n";
  73. if(dash != std::string::npos)
  74. {
  75. wholeCommand += line.substr(0, dash);
  76. line.erase(0,dash);
  77. }
  78. else //no closing marker -> the whole line is further part of string
  79. {
  80. wholeCommand += line;
  81. continue;
  82. }
  83. }
  84. int i = 0;
  85. for(; i < line.length(); i++)
  86. {
  87. char c = line[i];
  88. if(!openedString)
  89. {
  90. if(c == '[')
  91. openedBraces++;
  92. else if(c == ']')
  93. {
  94. openedBraces--;
  95. if(!openedBraces) //the last brace has been matched -> stop "parsing", everything else in the line is comment
  96. {
  97. i++;
  98. break;
  99. }
  100. }
  101. else if(c == '^')
  102. openedString = true;
  103. else if(c == ';') // a ';' that is in command line (and not in string) ends the command -> throw away rest
  104. {
  105. line.erase(i+!verm, line.length() - i - !verm); //leave ';' at the end only at ERM commands
  106. break;
  107. }
  108. }
  109. else if(c == '^')
  110. openedString = false;
  111. }
  112. if(verm && !openedBraces && i < line.length())
  113. {
  114. line.erase(i, line.length() - i);
  115. }
  116. wholeCommand += line;
  117. if(!openedBraces && !openedString)
  118. return wholeCommand;
  119. //loop end
  120. }
  121. if(openedBraces || openedString)
  122. tlog1 << "Ill-formed file: " << fname << std::endl;
  123. return "";
  124. }
  125. void CERMPreprocessor::getline(std::string &ret)
  126. {
  127. lineNo++;
  128. std::getline(file, ret);
  129. boost::trim(ret); //get rid of wspace
  130. }
  131. ERMParser::ERMParser(std::string file)
  132. :srcFile(file)
  133. {}
  134. void ERMParser::parseFile()
  135. {
  136. CERMPreprocessor preproc(srcFile);
  137. while(1)
  138. {
  139. std::string command = preproc.retreiveCommandLine();
  140. if(command.length() == 0)
  141. break;
  142. repairEncoding(command);
  143. parseLine(command);
  144. }
  145. }
  146. void callme(char const& i)
  147. {
  148. std::cout << "fd";
  149. }
  150. namespace ERM
  151. {
  152. struct TStringConstant
  153. {
  154. std::string str;
  155. };
  156. struct TMacroUsage
  157. {
  158. std::string macro;
  159. };
  160. //macro with '?', for write only
  161. struct TQMacroUsage
  162. {
  163. std::string qmacro;
  164. };
  165. //definition of a macro
  166. struct TMacroDef
  167. {
  168. std::string macro;
  169. };
  170. typedef std::string TCmdName;
  171. struct TVarExpNotMacro
  172. {
  173. typedef boost::optional<int> Tval;
  174. boost::optional<char> questionMark;
  175. std::string varsym;
  176. Tval val;
  177. };
  178. typedef boost::variant<TVarExpNotMacro, TMacroUsage> TVarExp;
  179. //write-only variable expression
  180. struct TVarpExp
  181. {
  182. typedef boost::variant<TVarExpNotMacro, TQMacroUsage> Tvartype;
  183. Tvartype var;
  184. };
  185. //i-expression (identifier expression) - an integral constant, variable symbol or array symbol
  186. typedef boost::variant<TVarExp, int> TIexp;
  187. struct TArithmeticOp
  188. {
  189. TIexp lhs, rhs;
  190. char opcode;
  191. };
  192. struct TVRLogic
  193. {
  194. char opcode;
  195. TIexp var;
  196. };
  197. struct TVRArithmetic
  198. {
  199. char opcode;
  200. TIexp rhs;
  201. };
  202. struct TSemiCompare
  203. {
  204. std::string compSign;
  205. TIexp rhs;
  206. };
  207. struct TCurriedString
  208. {
  209. TIexp iexp;
  210. TStringConstant string;
  211. };
  212. struct TVarConcatString
  213. {
  214. TVarExp var;
  215. TStringConstant string;
  216. };
  217. typedef boost::variant<TVarConcatString, TStringConstant, TCurriedString, TSemiCompare, TMacroUsage, TMacroDef, TIexp, TVarpExp, qi::unused_type> TBodyOptionItem;
  218. typedef std::vector<TBodyOptionItem> TNormalBodyOptionList;
  219. struct TNormalBodyOption
  220. {
  221. char optionCode;
  222. TNormalBodyOptionList params;
  223. };
  224. typedef boost::variant<TVRLogic, TVRArithmetic, TNormalBodyOption> TBodyOption;
  225. typedef boost::variant<TIexp, TArithmeticOp > TIdentifierInternal;
  226. typedef std::vector< TIdentifierInternal > Tidentifier;
  227. struct TComparison
  228. {
  229. std::string compSign;
  230. TIexp lhs, rhs;
  231. };
  232. struct Tcondition;
  233. typedef
  234. boost::optional<
  235. boost::recursive_wrapper<Tcondition>
  236. >
  237. TconditionNode;
  238. struct Tcondition
  239. {
  240. typedef boost::variant<
  241. TComparison,
  242. int>
  243. Tcond; //comparison or condition flag
  244. char ctype;
  245. Tcond cond;
  246. TconditionNode rhs;
  247. };
  248. struct Ttrigger
  249. {
  250. TCmdName name;
  251. boost::optional<Tidentifier> identifier;
  252. boost::optional<Tcondition> condition;
  253. };
  254. //a dirty workaround for preprocessor magic that prevents the use types with comma in it in BOOST_FUSION_ADAPT_STRUCT
  255. //see http://comments.gmane.org/gmane.comp.lib.boost.user/62501 for some info
  256. //
  257. //moreover, I encountered a quite serious bug in boost: http://boost.2283326.n4.nabble.com/container-hpp-111-error-C2039-value-type-is-not-a-member-of-td3352328.html
  258. //not sure how serious it is...
  259. //typedef boost::variant<char, TStringConstant, TMacroUsage, TMacroDef> bodyItem;
  260. typedef std::vector<TBodyOption> Tbody;
  261. struct Tinstruction
  262. {
  263. TCmdName name;
  264. boost::optional<Tidentifier> identifier;
  265. boost::optional<Tcondition> condition;
  266. Tbody body;
  267. };
  268. struct Treceiver
  269. {
  270. TCmdName name;
  271. boost::optional<Tidentifier> identifier;
  272. boost::optional<Tcondition> condition;
  273. boost::optional<Tbody> body;
  274. };
  275. struct TPostTrigger
  276. {
  277. TCmdName name;
  278. boost::optional<Tidentifier> identifier;
  279. boost::optional<Tcondition> condition;
  280. };
  281. struct Tcommand
  282. {
  283. typedef boost::variant<
  284. Ttrigger,
  285. Tinstruction,
  286. Treceiver,
  287. TPostTrigger
  288. >
  289. Tcmd;
  290. Tcmd cmd;
  291. std::string comment;
  292. };
  293. //vector expression
  294. typedef boost::variant<Tcommand, std::string, qi::unused_type> TERMline;
  295. typedef std::string TVModifier; //'`', ',', ',@', '#''
  296. struct TSymbol
  297. {
  298. std::vector<TVModifier> symModifier;
  299. std::string sym;
  300. };
  301. //for #'symbol expression
  302. struct TVExp;
  303. typedef boost::variant<boost::recursive_wrapper<TVExp>, TSymbol, char, double, int, Tcommand, TStringConstant > TVOption; //options in v-expression
  304. //v-expression
  305. struct TVExp
  306. {
  307. std::vector<TVModifier> modifier;
  308. std::vector<TVOption> children;
  309. };
  310. //script line
  311. typedef boost::variant<TVExp, TERMline> TLine;
  312. //console printer
  313. struct VarPrinterVisitor : boost::static_visitor<>
  314. {
  315. void operator()(TVarExpNotMacro const& val) const
  316. {
  317. tlog2 << val.varsym;
  318. if(val.val.is_initialized())
  319. {
  320. tlog2 << val.val.get();
  321. }
  322. }
  323. void operator()(TMacroUsage const& val) const
  324. {
  325. tlog2 << "$" << val.macro << "&";
  326. }
  327. };
  328. void varPrinter(const TVarExp & var)
  329. {
  330. boost::apply_visitor(VarPrinterVisitor(), var);
  331. }
  332. struct IExpPrinterVisitor : boost::static_visitor<>
  333. {
  334. void operator()(int const & constant) const
  335. {
  336. tlog2 << constant;
  337. }
  338. void operator()(TVarExp const & var) const
  339. {
  340. varPrinter(var);
  341. }
  342. };
  343. void iexpPrinter(const TIexp & exp)
  344. {
  345. boost::apply_visitor(IExpPrinterVisitor(), exp);
  346. }
  347. struct IdentifierPrinterVisitor : boost::static_visitor<>
  348. {
  349. void operator()(TIexp const& iexp) const
  350. {
  351. iexpPrinter(iexp);
  352. }
  353. void operator()(TArithmeticOp const& arop) const
  354. {
  355. iexpPrinter(arop.lhs);
  356. tlog2 << " " << arop.opcode << " ";
  357. iexpPrinter(arop.rhs);
  358. }
  359. };
  360. void identifierPrinter(const boost::optional<Tidentifier> & id)
  361. {
  362. if(id.is_initialized())
  363. {
  364. tlog2 << "identifier: ";
  365. BOOST_FOREACH(TIdentifierInternal x, id.get())
  366. {
  367. tlog2 << "#";
  368. boost::apply_visitor(IdentifierPrinterVisitor(), x);
  369. }
  370. }
  371. }
  372. struct ConditionCondPrinterVisitor : boost::static_visitor<>
  373. {
  374. void operator()(TComparison const& cmp) const
  375. {
  376. iexpPrinter(cmp.lhs);
  377. tlog2 << " " << cmp.compSign << " ";
  378. iexpPrinter(cmp.rhs);
  379. }
  380. void operator()(int const& flag) const
  381. {
  382. tlog2 << "condflag " << flag;
  383. }
  384. };
  385. void conditionPrinter(const boost::optional<Tcondition> & cond)
  386. {
  387. if(cond.is_initialized())
  388. {
  389. Tcondition condp = cond.get();
  390. tlog2 << " condition: ";
  391. boost::apply_visitor(ConditionCondPrinterVisitor(), condp.cond);
  392. tlog2 << " cond type: " << condp.ctype;
  393. //recursive call
  394. if(condp.rhs.is_initialized())
  395. {
  396. tlog2 << "rhs: ";
  397. boost::optional<Tcondition> rhsc = condp.rhs.get().get();
  398. conditionPrinter(rhsc);
  399. }
  400. else
  401. {
  402. tlog2 << "no rhs; ";
  403. }
  404. }
  405. }
  406. struct BodyVarpPrinterVisitor : boost::static_visitor<>
  407. {
  408. void operator()(TVarExpNotMacro const& cmp) const
  409. {
  410. if(cmp.questionMark.is_initialized())
  411. {
  412. tlog2 << cmp.questionMark.get();
  413. }
  414. if(cmp.val.is_initialized())
  415. {
  416. tlog2 << "val:" << cmp.val.get();
  417. }
  418. tlog2 << "varsym: |" << cmp.varsym << "|";
  419. }
  420. void operator()(TQMacroUsage const& cmp) const
  421. {
  422. tlog2 << "???$$" << cmp.qmacro << "$$";
  423. }
  424. };
  425. struct BodyOptionItemPrinterVisitor : boost::static_visitor<>
  426. {
  427. void operator()(TVarConcatString const& cmp) const
  428. {
  429. tlog2 << "+concat\"";
  430. varPrinter(cmp.var);
  431. tlog2 << " with " << cmp.string.str;
  432. }
  433. void operator()(TStringConstant const& cmp) const
  434. {
  435. tlog2 << " \"" << cmp.str << "\" ";
  436. }
  437. void operator()(TCurriedString const& cmp) const
  438. {
  439. tlog2 << "cs: ";
  440. iexpPrinter(cmp.iexp);
  441. tlog2 << " '" << cmp.string.str << "' ";
  442. }
  443. void operator()(TSemiCompare const& cmp) const
  444. {
  445. tlog2 << cmp.compSign << "; rhs: ";
  446. iexpPrinter(cmp.rhs);
  447. }
  448. void operator()(TMacroUsage const& cmp) const
  449. {
  450. tlog2 << "$$" << cmp.macro << "$$";
  451. }
  452. void operator()(TMacroDef const& cmp) const
  453. {
  454. tlog2 << "@@" << cmp.macro << "@@";
  455. }
  456. void operator()(TIexp const& cmp) const
  457. {
  458. iexpPrinter(cmp);
  459. }
  460. void operator()(TVarpExp const& cmp) const
  461. {
  462. tlog2 << "varp";
  463. boost::apply_visitor(BodyVarpPrinterVisitor(), cmp.var);
  464. }
  465. void operator()(qi::unused_type const& cmp) const
  466. {
  467. tlog2 << "nothing";
  468. }
  469. };
  470. struct BodyOptionVisitor : boost::static_visitor<>
  471. {
  472. void operator()(TVRLogic const& cmp) const
  473. {
  474. tlog2 << cmp.opcode << " ";
  475. iexpPrinter(cmp.var);
  476. }
  477. void operator()(TVRArithmetic const& cmp) const
  478. {
  479. tlog2 << cmp.opcode << " ";
  480. iexpPrinter(cmp.rhs);
  481. }
  482. void operator()(TNormalBodyOption const& cmp) const
  483. {
  484. tlog2 << cmp.optionCode << "~";
  485. BOOST_FOREACH(TBodyOptionItem optList, cmp.params)
  486. {
  487. boost::apply_visitor(BodyOptionItemPrinterVisitor(), optList);
  488. }
  489. }
  490. };
  491. void bodyPrinter(const Tbody & body)
  492. {
  493. tlog2 << " body items: ";
  494. BOOST_FOREACH(TBodyOption bi, body)
  495. {
  496. tlog2 << " (";
  497. apply_visitor(BodyOptionVisitor(), bi);
  498. tlog2 << ") ";
  499. }
  500. }
  501. struct CommandPrinterVisitor : boost::static_visitor<>
  502. {
  503. void operator()(Ttrigger const& trig) const
  504. {
  505. tlog2 << "trigger: " << trig.name << " ";
  506. identifierPrinter(trig.identifier);
  507. conditionPrinter(trig.condition);
  508. }
  509. void operator()(Tinstruction const& trig) const
  510. {
  511. tlog2 << "instruction: " << trig.name << " ";
  512. identifierPrinter(trig.identifier);
  513. conditionPrinter(trig.condition);
  514. bodyPrinter(trig.body);
  515. }
  516. void operator()(Treceiver const& trig) const
  517. {
  518. tlog2 << "receiver: " << trig.name << " ";
  519. identifierPrinter(trig.identifier);
  520. conditionPrinter(trig.condition);
  521. if(trig.body.is_initialized())
  522. bodyPrinter(trig.body.get());
  523. }
  524. void operator()(TPostTrigger const& trig) const
  525. {
  526. tlog2 << "post trigger: " << trig.name << " ";
  527. identifierPrinter(trig.identifier);
  528. conditionPrinter(trig.condition);
  529. }
  530. };
  531. struct LinePrinterVisitor : boost::static_visitor<>
  532. {
  533. void operator()(Tcommand const& cmd) const
  534. {
  535. CommandPrinterVisitor un;
  536. boost::apply_visitor(un, cmd.cmd);
  537. std::cout << "Line comment: " << cmd.comment << std::endl;
  538. }
  539. void operator()(std::string const& comment) const
  540. {
  541. }
  542. void operator()(qi::unused_type const& nothing) const
  543. {
  544. }
  545. };
  546. void printERM(const TERMline & ast)
  547. {
  548. tlog2 << "";
  549. boost::apply_visitor(LinePrinterVisitor(), ast);
  550. }
  551. void printTVExp(const TVExp & exp);
  552. struct VOptionPrinterVisitor : boost::static_visitor<>
  553. {
  554. void operator()(TVExp const& cmd) const
  555. {
  556. printTVExp(cmd);
  557. }
  558. void operator()(TSymbol const& cmd) const
  559. {
  560. BOOST_FOREACH(TVModifier mod, cmd.symModifier)
  561. {
  562. tlog2 << mod << " ";
  563. }
  564. tlog2 << cmd.sym;
  565. }
  566. void operator()(char const& cmd) const
  567. {
  568. tlog2 << "'" << cmd << "'";
  569. }
  570. void operator()(int const& cmd) const
  571. {
  572. tlog2 << cmd;
  573. }
  574. void operator()(double const& cmd) const
  575. {
  576. tlog2 << cmd;
  577. }
  578. void operator()(TERMline const& cmd) const
  579. {
  580. printERM(cmd);
  581. }
  582. void operator()(TStringConstant const& cmd) const
  583. {
  584. tlog2 << "^" << cmd.str << "^";
  585. }
  586. };
  587. void printTVExp(const TVExp & exp)
  588. {
  589. BOOST_FOREACH(TVModifier mod, exp.modifier)
  590. {
  591. tlog2 << mod << " ";
  592. }
  593. tlog2 << "[ ";
  594. BOOST_FOREACH(TVOption opt, exp.children)
  595. {
  596. boost::apply_visitor(VOptionPrinterVisitor(), opt);
  597. tlog2 << " ";
  598. }
  599. tlog2 << "]";
  600. }
  601. struct TLPrinterVisitor : boost::static_visitor<>
  602. {
  603. void operator()(TVExp const& cmd) const
  604. {
  605. printTVExp(cmd);
  606. }
  607. void operator()(TERMline const& cmd) const
  608. {
  609. printERM(cmd);
  610. }
  611. };
  612. void printAST(const TLine & ast)
  613. {
  614. boost::apply_visitor(TLPrinterVisitor(), ast);
  615. tlog2 << std::endl;
  616. }
  617. }
  618. BOOST_FUSION_ADAPT_STRUCT(
  619. ERM::TStringConstant,
  620. (std::string, str)
  621. )
  622. BOOST_FUSION_ADAPT_STRUCT(
  623. ERM::TMacroUsage,
  624. (std::string, macro)
  625. )
  626. BOOST_FUSION_ADAPT_STRUCT(
  627. ERM::TQMacroUsage,
  628. (std::string, qmacro)
  629. )
  630. BOOST_FUSION_ADAPT_STRUCT(
  631. ERM::TMacroDef,
  632. (std::string, macro)
  633. )
  634. BOOST_FUSION_ADAPT_STRUCT(
  635. ERM::TVarExpNotMacro,
  636. (boost::optional<char>, questionMark)
  637. (std::string, varsym)
  638. (ERM::TVarExpNotMacro::Tval, val)
  639. )
  640. BOOST_FUSION_ADAPT_STRUCT(
  641. ERM::TArithmeticOp,
  642. (ERM::TIexp, lhs)
  643. (char, opcode)
  644. (ERM::TIexp, rhs)
  645. )
  646. BOOST_FUSION_ADAPT_STRUCT(
  647. ERM::TVarpExp,
  648. (ERM::TVarpExp::Tvartype, var)
  649. )
  650. BOOST_FUSION_ADAPT_STRUCT(
  651. ERM::TVRLogic,
  652. (char, opcode)
  653. (ERM::TIexp, var)
  654. )
  655. BOOST_FUSION_ADAPT_STRUCT(
  656. ERM::TVRArithmetic,
  657. (char, opcode)
  658. (ERM::TIexp, rhs)
  659. )
  660. BOOST_FUSION_ADAPT_STRUCT(
  661. ERM::TNormalBodyOption,
  662. (char, optionCode)
  663. (ERM::TNormalBodyOptionList, params)
  664. )
  665. BOOST_FUSION_ADAPT_STRUCT(
  666. ERM::Ttrigger,
  667. (ERM::TCmdName, name)
  668. (boost::optional<ERM::Tidentifier>, identifier)
  669. (boost::optional<ERM::Tcondition>, condition)
  670. )
  671. BOOST_FUSION_ADAPT_STRUCT(
  672. ERM::TComparison,
  673. (ERM::TIexp, lhs)
  674. (std::string, compSign)
  675. (ERM::TIexp, rhs)
  676. )
  677. BOOST_FUSION_ADAPT_STRUCT(
  678. ERM::TSemiCompare,
  679. (std::string, compSign)
  680. (ERM::TIexp, rhs)
  681. )
  682. BOOST_FUSION_ADAPT_STRUCT(
  683. ERM::TCurriedString,
  684. (ERM::TIexp, iexp)
  685. (ERM::TStringConstant, string)
  686. )
  687. BOOST_FUSION_ADAPT_STRUCT(
  688. ERM::TVarConcatString,
  689. (ERM::TVarExp, var)
  690. (ERM::TStringConstant, string)
  691. )
  692. BOOST_FUSION_ADAPT_STRUCT(
  693. ERM::Tcondition,
  694. (char, ctype)
  695. (ERM::Tcondition::Tcond, cond)
  696. (ERM::TconditionNode, rhs)
  697. )
  698. BOOST_FUSION_ADAPT_STRUCT(
  699. ERM::Tinstruction,
  700. (ERM::TCmdName, name)
  701. (boost::optional<ERM::Tidentifier>, identifier)
  702. (boost::optional<ERM::Tcondition>, condition)
  703. (ERM::Tbody, body)
  704. )
  705. BOOST_FUSION_ADAPT_STRUCT(
  706. ERM::Treceiver,
  707. (ERM::TCmdName, name)
  708. (boost::optional<ERM::Tidentifier>, identifier)
  709. (boost::optional<ERM::Tcondition>, condition)
  710. (boost::optional<ERM::Tbody>, body)
  711. )
  712. BOOST_FUSION_ADAPT_STRUCT(
  713. ERM::TPostTrigger,
  714. (ERM::TCmdName, name)
  715. (boost::optional<ERM::Tidentifier>, identifier)
  716. (boost::optional<ERM::Tcondition>, condition)
  717. )
  718. BOOST_FUSION_ADAPT_STRUCT(
  719. ERM::Tcommand,
  720. (ERM::Tcommand::Tcmd, cmd)
  721. (std::string, comment)
  722. )
  723. BOOST_FUSION_ADAPT_STRUCT(
  724. ERM::TVExp,
  725. (std::vector<ERM::TVModifier>, modifier)
  726. (std::vector<ERM::TVOption>, children)
  727. )
  728. BOOST_FUSION_ADAPT_STRUCT(
  729. ERM::TSymbol,
  730. (std::vector<ERM::TVModifier>, symModifier)
  731. (std::string, sym)
  732. )
  733. namespace ERM
  734. {
  735. template<typename Iterator>
  736. struct ERM_grammar : qi::grammar<Iterator, TLine(), ascii::space_type>
  737. {
  738. ERM_grammar() : ERM_grammar::base_type(vline, "VERM script line")
  739. {
  740. //do not build too complicated expressions, e.g. (a >> b) | c, qi has problems with them
  741. macroUsage %= qi::lexeme[qi::lit('$') >> *(qi::char_ - '$') >> qi::lit('$')];
  742. macroDef %= qi::lexeme[qi::lit('@') >> *(qi::char_ - '@') >> qi::lit('@')];
  743. varExpNotMacro %= -qi::char_("?") >> (+(qi::char_("a-z") - 'u')) >> -qi::int_;
  744. qMacroUsage %= qi::lexeme[qi::lit("?$") >> *(qi::char_ - '$') >> qi::lit('$')];
  745. varExp %= varExpNotMacro | macroUsage;
  746. iexp %= varExp | qi::int_;
  747. varp %=/* qi::lit("?") >> */(varExpNotMacro | qMacroUsage);
  748. comment %= *qi::char_;
  749. commentLine %= (~qi::char_("!") >> comment | (qi::char_('!') >> (~qi::char_("?!$#[")) >> comment ));
  750. cmdName %= qi::lexeme[qi::repeat(2)[qi::char_]];
  751. arithmeticOp %= iexp >> qi::char_ >> iexp;
  752. //identifier is usually a vector of i-expressions but VR receiver performs arithmetic operations on it
  753. identifier %= (iexp | arithmeticOp) % qi::lit('/');
  754. comparison %= iexp >> (*qi::char_("<=>")) >> iexp;
  755. condition %= qi::char_("&|X/") >> (comparison | qi::int_) >> -condition;
  756. trigger %= cmdName >> -identifier >> -condition > qi::lit(";"); /////
  757. string %= qi::lexeme['^' >> *(qi::char_ - '^') >> '^'];
  758. VRLogic %= qi::char_("&|X") >> iexp;
  759. VRarithmetic %= qi::char_("+*:/%-") >> iexp;
  760. semiCompare %= *qi::char_("<=>") >> iexp;
  761. curStr %= iexp >> string;
  762. varConcatString %= varExp >> qi::lit("+") >> string;
  763. bodyOptionItem %= varConcatString | curStr | string | semiCompare | macroUsage | macroDef | varp | iexp | qi::eps;
  764. exactBodyOptionList %= (bodyOptionItem % qi::lit("/"));
  765. normalBodyOption = qi::char_("A-Z+") > exactBodyOptionList;
  766. bodyOption %= VRLogic | VRarithmetic | normalBodyOption;
  767. body %= qi::lit(":") >> +(bodyOption) > qi::lit(";");
  768. instruction %= cmdName >> -identifier >> -condition >> body;
  769. receiver %= cmdName >> -identifier >> -condition >> -body; //receiver without body exists... change needed
  770. postTrigger %= cmdName >> -identifier >> -condition > qi::lit(";");
  771. command %= (qi::lit("!") >>
  772. (
  773. (qi::lit("?") >> trigger) |
  774. (qi::lit("!") >> receiver) |
  775. (qi::lit("#") >> instruction) |
  776. (qi::lit("$") >> postTrigger)
  777. ) >> comment
  778. );
  779. rline %=
  780. (
  781. command | commentLine | spirit::eps
  782. );
  783. vmod %= qi::string("`") | qi::string(",!") | qi::string(",") | qi::string("#'");
  784. vsym %= *vmod >> qi::lexeme[+qi::char_("+*/$%&_=<>~a-zA-Z0-9-")];
  785. qi::real_parser<double, qi::strict_real_policies<double> > strict_double;
  786. vopt %= qi::lexeme[(qi::lit("!") >> qi::char_ >> qi::lit("!"))] | qi::lexeme[strict_double] | qi::lexeme[qi::int_] | command | vexp | string | vsym;
  787. vexp %= *vmod >> qi::lit("[") >> *(vopt) >> qi::lit("]");
  788. vline %= (( qi::lit("!") >>vexp) | rline ) > spirit::eoi;
  789. //error handling
  790. string.name("string constant");
  791. iexp.name("i-expression");
  792. comment.name("comment");
  793. commentLine.name("comment line");
  794. cmdName.name("name of a command");
  795. identifier.name("identifier");
  796. condition.name("condition");
  797. trigger.name("trigger");
  798. body.name("body");
  799. instruction.name("instruction");
  800. receiver.name("receiver");
  801. postTrigger.name("post trigger");
  802. command.name("command");
  803. rline.name("ERM script line");
  804. vsym.name("V symbol");
  805. vopt.name("V option");
  806. vexp.name("V expression");
  807. vline.name("VERM line");
  808. qi::on_error<qi::fail>
  809. (
  810. vline
  811. , std::cout //or phoenix::ref(std::count), is there any difference?
  812. << phoenix::val("Error! Expecting ")
  813. << qi::_4 // what failed?
  814. << phoenix::val(" here: \"")
  815. << phoenix::construct<std::string>(qi::_3, qi::_2) // iterators to error-pos, end
  816. << phoenix::val("\"")
  817. << std::endl
  818. );
  819. }
  820. qi::rule<Iterator, TStringConstant(), ascii::space_type> string;
  821. qi::rule<Iterator, TMacroUsage(), ascii::space_type> macroUsage;
  822. qi::rule<Iterator, TQMacroUsage(), ascii::space_type> qMacroUsage;
  823. qi::rule<Iterator, TMacroDef(), ascii::space_type> macroDef;
  824. qi::rule<Iterator, TVarExpNotMacro(), ascii::space_type> varExpNotMacro;
  825. qi::rule<Iterator, TVarExp(), ascii::space_type> varExp;
  826. qi::rule<Iterator, TIexp(), ascii::space_type> iexp;
  827. qi::rule<Iterator, TVarpExp(), ascii::space_type> varp;
  828. qi::rule<Iterator, TArithmeticOp(), ascii::space_type> arithmeticOp;
  829. qi::rule<Iterator, std::string(), ascii::space_type> comment;
  830. qi::rule<Iterator, std::string(), ascii::space_type> commentLine;
  831. qi::rule<Iterator, TCmdName(), ascii::space_type> cmdName;
  832. qi::rule<Iterator, Tidentifier(), ascii::space_type> identifier;
  833. qi::rule<Iterator, TComparison(), ascii::space_type> comparison;
  834. qi::rule<Iterator, Tcondition(), ascii::space_type> condition;
  835. qi::rule<Iterator, TVRLogic(), ascii::space_type> VRLogic;
  836. qi::rule<Iterator, TVRArithmetic(), ascii::space_type> VRarithmetic;
  837. qi::rule<Iterator, TSemiCompare(), ascii::space_type> semiCompare;
  838. qi::rule<Iterator, TCurriedString(), ascii::space_type> curStr;
  839. qi::rule<Iterator, TVarConcatString(), ascii::space_type> varConcatString;
  840. qi::rule<Iterator, TBodyOptionItem(), ascii::space_type> bodyOptionItem;
  841. qi::rule<Iterator, TNormalBodyOptionList(), ascii::space_type> exactBodyOptionList;
  842. qi::rule<Iterator, TNormalBodyOption(), ascii::space_type> normalBodyOption;
  843. qi::rule<Iterator, TBodyOption(), ascii::space_type> bodyOption;
  844. qi::rule<Iterator, Ttrigger(), ascii::space_type> trigger;
  845. qi::rule<Iterator, Tbody(), ascii::space_type> body;
  846. qi::rule<Iterator, Tinstruction(), ascii::space_type> instruction;
  847. qi::rule<Iterator, Treceiver(), ascii::space_type> receiver;
  848. qi::rule<Iterator, TPostTrigger(), ascii::space_type> postTrigger;
  849. qi::rule<Iterator, Tcommand(), ascii::space_type> command;
  850. qi::rule<Iterator, TERMline(), ascii::space_type> rline;
  851. qi::rule<Iterator, TSymbol(), ascii::space_type> vsym;
  852. qi::rule<Iterator, TVModifier(), ascii::space_type> vmod;
  853. qi::rule<Iterator, TVOption(), ascii::space_type> vopt;
  854. qi::rule<Iterator, TVExp(), ascii::space_type> vexp;
  855. qi::rule<Iterator, TLine(), ascii::space_type> vline;
  856. };
  857. };
  858. void ERMParser::parseLine( const std::string & line )
  859. {
  860. std::string::const_iterator beg = line.begin(),
  861. end = line.end();
  862. ERM::ERM_grammar<std::string::const_iterator> ERMgrammar;
  863. ERM::TLine AST;
  864. // bool r = qi::phrase_parse(beg, end, ERMgrammar, ascii::space, AST);
  865. // if(!r || beg != end)
  866. // {
  867. // tlog1 << "Parse error for line (" << parsedLine << ") : " << line << std::endl;
  868. // tlog1 << "\tCannot parse: " << std::string(beg, end) << std::endl;
  869. // }
  870. // else
  871. // {
  872. // //parsing succeeded
  873. // tlog2 << line << std::endl;
  874. // ERM::printAST(AST);
  875. // }
  876. }
  877. ERMParser::ELineType ERMParser::classifyLine( const std::string & line, bool inString ) const
  878. {
  879. ERMParser::ELineType ret;
  880. if(line[0] == '!')
  881. {
  882. if(countHatsBeforeSemicolon(line) % 2 == 1)
  883. ret = ERMParser::UNFINISHED;
  884. else
  885. ret = ERMParser::COMMAND_FULL;
  886. }
  887. else
  888. {
  889. if(inString)
  890. {
  891. if(countHatsBeforeSemicolon(line) % 2 == 1)
  892. ret = ERMParser::END_OF;
  893. else
  894. ret = ERMParser::UNFINISHED;
  895. }
  896. else
  897. {
  898. ret = ERMParser::COMMENT;
  899. }
  900. }
  901. return ret;
  902. }
  903. int ERMParser::countHatsBeforeSemicolon( const std::string & line ) const
  904. {
  905. //CHECK: omit macros? or anything else?
  906. int numOfHats = 0; //num of '^' before ';'
  907. //check for unmatched ^
  908. BOOST_FOREACH(char c, line)
  909. {
  910. if(c == ';')
  911. break;
  912. if(c == '^')
  913. ++numOfHats;
  914. }
  915. return numOfHats;
  916. }
  917. void ERMParser::repairEncoding( std::string & str ) const
  918. {
  919. for(int g=0; g<str.size(); ++g)
  920. if(str[g] & 0x80)
  921. str[g] = '|';
  922. }
  923. void ERMParser::repairEncoding( char * str, int len ) const
  924. {
  925. for(int g=0; g<len; ++g)
  926. if(str[g] & 0x80)
  927. str[g] = '|';
  928. }
  929. #else
  930. ERMParser::ERMParser(std::string file){}
  931. void ERMParser::parseFile(){}
  932. #endif