ERMParser.cpp 24 KB

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