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:
  69. {
  70. if(!inString)
  71. wholeLine = " ";
  72. inString = true;
  73. wholeLine += lineBuf;
  74. }
  75. break;
  76. case ERMParser::END_OF:
  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. typedef std::string TVModifier; //'`', ',', ',@', '#''
  239. struct TSymbol
  240. {
  241. std::vector<TVModifier> symModifier;
  242. std::string sym;
  243. };
  244. //for #'symbol expression
  245. struct TVExp;
  246. typedef boost::variant<boost::recursive_wrapper<TVExp>, TSymbol, char, double, int, Tcommand, TStringConstant > TVOption; //options in v-expression
  247. //v-expression
  248. struct TVExp
  249. {
  250. std::vector<TVModifier> modifier;
  251. std::vector<TVOption> children;
  252. };
  253. //script line
  254. typedef boost::variant<TVExp, TERMline> TLine;
  255. //console printer
  256. struct VarPrinterVisitor : boost::static_visitor<>
  257. {
  258. void operator()(TVarExpNotMacro const& val) const
  259. {
  260. tlog2 << val.varsym;
  261. if(val.val.is_initialized())
  262. {
  263. tlog2 << val.val.get();
  264. }
  265. }
  266. void operator()(TMacroUsage const& val) const
  267. {
  268. tlog2 << "$" << val.macro << "&";
  269. }
  270. };
  271. void varPrinter(const TVarExp & var)
  272. {
  273. boost::apply_visitor(VarPrinterVisitor(), var);
  274. }
  275. struct IExpPrinterVisitor : boost::static_visitor<>
  276. {
  277. void operator()(int const & constant) const
  278. {
  279. tlog2 << constant;
  280. }
  281. void operator()(TVarExp const & var) const
  282. {
  283. varPrinter(var);
  284. }
  285. };
  286. void iexpPrinter(const TIexp & exp)
  287. {
  288. boost::apply_visitor(IExpPrinterVisitor(), exp);
  289. }
  290. struct IdentifierPrinterVisitor : boost::static_visitor<>
  291. {
  292. void operator()(TIexp const& iexp) const
  293. {
  294. iexpPrinter(iexp);
  295. }
  296. void operator()(TArithmeticOp const& arop) const
  297. {
  298. iexpPrinter(arop.lhs);
  299. tlog2 << " " << arop.opcode << " ";
  300. iexpPrinter(arop.rhs);
  301. }
  302. };
  303. void identifierPrinter(const boost::optional<Tidentifier> & id)
  304. {
  305. if(id.is_initialized())
  306. {
  307. tlog2 << "identifier: ";
  308. BOOST_FOREACH(TIdentifierInternal x, id.get())
  309. {
  310. tlog2 << "#";
  311. boost::apply_visitor(IdentifierPrinterVisitor(), x);
  312. }
  313. }
  314. }
  315. struct ConditionCondPrinterVisitor : boost::static_visitor<>
  316. {
  317. void operator()(TComparison const& cmp) const
  318. {
  319. iexpPrinter(cmp.lhs);
  320. tlog2 << " " << cmp.compSign << " ";
  321. iexpPrinter(cmp.rhs);
  322. }
  323. void operator()(int const& flag) const
  324. {
  325. tlog2 << "condflag " << flag;
  326. }
  327. };
  328. void conditionPrinter(const boost::optional<Tcondition> & cond)
  329. {
  330. if(cond.is_initialized())
  331. {
  332. Tcondition condp = cond.get();
  333. tlog2 << " condition: ";
  334. boost::apply_visitor(ConditionCondPrinterVisitor(), condp.cond);
  335. tlog2 << " cond type: " << condp.ctype;
  336. //recursive call
  337. if(condp.rhs.is_initialized())
  338. {
  339. tlog2 << "rhs: ";
  340. boost::optional<Tcondition> rhsc = condp.rhs.get().get();
  341. conditionPrinter(rhsc);
  342. }
  343. else
  344. {
  345. tlog2 << "no rhs; ";
  346. }
  347. }
  348. }
  349. struct BodyVarpPrinterVisitor : boost::static_visitor<>
  350. {
  351. void operator()(TVarExpNotMacro const& cmp) const
  352. {
  353. if(cmp.questionMark.is_initialized())
  354. {
  355. tlog2 << cmp.questionMark.get();
  356. }
  357. if(cmp.val.is_initialized())
  358. {
  359. tlog2 << "val:" << cmp.val.get();
  360. }
  361. tlog2 << "varsym: |" << cmp.varsym << "|";
  362. }
  363. void operator()(TQMacroUsage const& cmp) const
  364. {
  365. tlog2 << "???$$" << cmp.qmacro << "$$";
  366. }
  367. };
  368. struct BodyOptionItemPrinterVisitor : boost::static_visitor<>
  369. {
  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. BOOST_FOREACH(TVModifier mod, cmd.symModifier)
  504. {
  505. tlog2 << mod << " ";
  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. BOOST_FOREACH(TVModifier mod, exp.modifier)
  533. {
  534. tlog2 << mod << " ";
  535. }
  536. tlog2 << "[ ";
  537. BOOST_FOREACH(TVOption opt, exp.children)
  538. {
  539. boost::apply_visitor(VOptionPrinterVisitor(), opt);
  540. tlog2 << " ";
  541. }
  542. tlog2 << "]";
  543. }
  544. struct TLPrinterVisitor : boost::static_visitor<>
  545. {
  546. void operator()(TVExp const& cmd) const
  547. {
  548. printTVExp(cmd);
  549. }
  550. void operator()(TERMline const& cmd) const
  551. {
  552. printERM(cmd);
  553. }
  554. };
  555. void printAST(const TLine & ast)
  556. {
  557. boost::apply_visitor(TLPrinterVisitor(), ast);
  558. tlog2 << std::endl;
  559. }
  560. }
  561. BOOST_FUSION_ADAPT_STRUCT(
  562. ERM::TStringConstant,
  563. (std::string, str)
  564. )
  565. BOOST_FUSION_ADAPT_STRUCT(
  566. ERM::TMacroUsage,
  567. (std::string, macro)
  568. )
  569. BOOST_FUSION_ADAPT_STRUCT(
  570. ERM::TQMacroUsage,
  571. (std::string, qmacro)
  572. )
  573. BOOST_FUSION_ADAPT_STRUCT(
  574. ERM::TMacroDef,
  575. (std::string, macro)
  576. )
  577. BOOST_FUSION_ADAPT_STRUCT(
  578. ERM::TVarExpNotMacro,
  579. (boost::optional<char>, questionMark)
  580. (std::string, varsym)
  581. (ERM::TVarExpNotMacro::Tval, val)
  582. )
  583. BOOST_FUSION_ADAPT_STRUCT(
  584. ERM::TArithmeticOp,
  585. (ERM::TIexp, lhs)
  586. (char, opcode)
  587. (ERM::TIexp, rhs)
  588. )
  589. BOOST_FUSION_ADAPT_STRUCT(
  590. ERM::TVarpExp,
  591. (ERM::TVarpExp::Tvartype, var)
  592. )
  593. BOOST_FUSION_ADAPT_STRUCT(
  594. ERM::TVRLogic,
  595. (char, opcode)
  596. (ERM::TIexp, var)
  597. )
  598. BOOST_FUSION_ADAPT_STRUCT(
  599. ERM::TVRArithmetic,
  600. (char, opcode)
  601. (ERM::TIexp, rhs)
  602. )
  603. BOOST_FUSION_ADAPT_STRUCT(
  604. ERM::TNormalBodyOption,
  605. (char, optionCode)
  606. (ERM::TNormalBodyOptionList, params)
  607. )
  608. BOOST_FUSION_ADAPT_STRUCT(
  609. ERM::Ttrigger,
  610. (ERM::TCmdName, name)
  611. (boost::optional<ERM::Tidentifier>, identifier)
  612. (boost::optional<ERM::Tcondition>, condition)
  613. )
  614. BOOST_FUSION_ADAPT_STRUCT(
  615. ERM::TComparison,
  616. (ERM::TIexp, lhs)
  617. (std::string, compSign)
  618. (ERM::TIexp, rhs)
  619. )
  620. BOOST_FUSION_ADAPT_STRUCT(
  621. ERM::TSemiCompare,
  622. (std::string, compSign)
  623. (ERM::TIexp, rhs)
  624. )
  625. BOOST_FUSION_ADAPT_STRUCT(
  626. ERM::TCurriedString,
  627. (ERM::TIexp, iexp)
  628. (ERM::TStringConstant, string)
  629. )
  630. BOOST_FUSION_ADAPT_STRUCT(
  631. ERM::TVarConcatString,
  632. (ERM::TVarExp, var)
  633. (ERM::TStringConstant, string)
  634. )
  635. BOOST_FUSION_ADAPT_STRUCT(
  636. ERM::Tcondition,
  637. (char, ctype)
  638. (ERM::Tcondition::Tcond, cond)
  639. (ERM::TconditionNode, rhs)
  640. )
  641. BOOST_FUSION_ADAPT_STRUCT(
  642. ERM::Tinstruction,
  643. (ERM::TCmdName, name)
  644. (boost::optional<ERM::Tidentifier>, identifier)
  645. (boost::optional<ERM::Tcondition>, condition)
  646. (ERM::Tbody, body)
  647. )
  648. BOOST_FUSION_ADAPT_STRUCT(
  649. ERM::Treceiver,
  650. (ERM::TCmdName, name)
  651. (boost::optional<ERM::Tidentifier>, identifier)
  652. (boost::optional<ERM::Tcondition>, condition)
  653. (boost::optional<ERM::Tbody>, body)
  654. )
  655. BOOST_FUSION_ADAPT_STRUCT(
  656. ERM::TPostTrigger,
  657. (ERM::TCmdName, name)
  658. (boost::optional<ERM::Tidentifier>, identifier)
  659. (boost::optional<ERM::Tcondition>, condition)
  660. )
  661. BOOST_FUSION_ADAPT_STRUCT(
  662. ERM::Tcommand,
  663. (ERM::Tcommand::Tcmd, cmd)
  664. (std::string, comment)
  665. )
  666. BOOST_FUSION_ADAPT_STRUCT(
  667. ERM::TVExp,
  668. (std::vector<ERM::TVModifier>, modifier)
  669. (std::vector<ERM::TVOption>, children)
  670. )
  671. BOOST_FUSION_ADAPT_STRUCT(
  672. ERM::TSymbol,
  673. (std::vector<ERM::TVModifier>, symModifier)
  674. (std::string, sym)
  675. )
  676. namespace ERM
  677. {
  678. template<typename Iterator>
  679. struct ERM_grammar : qi::grammar<Iterator, TLine(), ascii::space_type>
  680. {
  681. ERM_grammar() : ERM_grammar::base_type(vline, "VERM script line")
  682. {
  683. //do not build too complicated expressions, e.g. (a >> b) | c, qi has problems with them
  684. macroUsage %= qi::lexeme[qi::lit('$') >> *(qi::char_ - '$') >> qi::lit('$')];
  685. macroDef %= qi::lexeme[qi::lit('@') >> *(qi::char_ - '@') >> qi::lit('@')];
  686. varExpNotMacro %= -qi::char_("?") >> (+(qi::char_("a-z") - 'u')) >> -qi::int_;
  687. qMacroUsage %= qi::lexeme[qi::lit("?$") >> *(qi::char_ - '$') >> qi::lit('$')];
  688. varExp %= varExpNotMacro | macroUsage;
  689. iexp %= varExp | qi::int_;
  690. varp %=/* qi::lit("?") >> */(varExpNotMacro | qMacroUsage);
  691. comment %= *qi::char_;
  692. commentLine %= (~qi::char_("!") >> comment | (qi::char_('!') >> (~qi::char_("?!$#[")) >> comment ));
  693. cmdName %= qi::lexeme[qi::repeat(2)[qi::char_]];
  694. arithmeticOp %= iexp >> qi::char_ >> iexp;
  695. //identifier is usually a vector of i-expressions but VR receiver performs arithmetic operations on it
  696. identifier %= (iexp | arithmeticOp) % qi::lit('/');
  697. comparison %= iexp >> (*qi::char_("<=>")) >> iexp;
  698. condition %= qi::char_("&|X/") >> (comparison | qi::int_) >> -condition;
  699. trigger %= cmdName >> -identifier >> -condition > qi::lit(";"); /////
  700. string %= qi::lexeme['^' >> *(qi::char_ - '^') >> '^'];
  701. VRLogic %= qi::char_("&|X") >> iexp;
  702. VRarithmetic %= qi::char_("+*:/%-") >> iexp;
  703. semiCompare %= *qi::char_("<=>") >> iexp;
  704. curStr %= iexp >> string;
  705. varConcatString %= varExp >> qi::lit("+") >> string;
  706. bodyOptionItem %= varConcatString | curStr | string | semiCompare | macroUsage | macroDef | varp | iexp | qi::eps;
  707. exactBodyOptionList %= (bodyOptionItem % qi::lit("/"));
  708. normalBodyOption = qi::char_("A-Z+") > exactBodyOptionList;
  709. bodyOption %= VRLogic | VRarithmetic | normalBodyOption;
  710. body %= qi::lit(":") >> +(bodyOption) > qi::lit(";");
  711. instruction %= cmdName >> -identifier >> -condition >> body;
  712. receiver %= cmdName >> -identifier >> -condition >> -body; //receiver without body exists... change needed
  713. postTrigger %= cmdName >> -identifier >> -condition > qi::lit(";");
  714. command %= (qi::lit("!") >>
  715. (
  716. (qi::lit("?") >> trigger) |
  717. (qi::lit("!") >> receiver) |
  718. (qi::lit("#") >> instruction) |
  719. (qi::lit("$") >> postTrigger)
  720. ) >> comment
  721. );
  722. rline %=
  723. (
  724. command | commentLine | spirit::eps
  725. );
  726. vmod %= qi::string("`") | qi::string(",!") | qi::string(",") | qi::string("#'");
  727. vsym %= *vmod >> qi::lexeme[+qi::char_("+*/$%&_=<>~a-zA-Z0-9-")];
  728. qi::real_parser<double, qi::strict_real_policies<double> > strict_double;
  729. vopt %= qi::lexeme[(qi::lit("!") >> qi::char_ >> qi::lit("!"))] | qi::lexeme[strict_double] | qi::lexeme[qi::int_] | command | vexp | string | vsym;
  730. vexp %= *vmod >> qi::lit("[") >> *(vopt) >> qi::lit("]");
  731. vline %= (( qi::lit("!") >>vexp) | rline ) > spirit::eoi;
  732. //error handling
  733. string.name("string constant");
  734. iexp.name("i-expression");
  735. comment.name("comment");
  736. commentLine.name("comment line");
  737. cmdName.name("name of a command");
  738. identifier.name("identifier");
  739. condition.name("condition");
  740. trigger.name("trigger");
  741. body.name("body");
  742. instruction.name("instruction");
  743. receiver.name("receiver");
  744. postTrigger.name("post trigger");
  745. command.name("command");
  746. rline.name("ERM script line");
  747. vsym.name("V symbol");
  748. vopt.name("V option");
  749. vexp.name("V expression");
  750. vline.name("VERM line");
  751. qi::on_error<qi::fail>
  752. (
  753. vline
  754. , std::cout //or phoenix::ref(std::count), is there any difference?
  755. << phoenix::val("Error! Expecting ")
  756. << qi::_4 // what failed?
  757. << phoenix::val(" here: \"")
  758. << phoenix::construct<std::string>(qi::_3, qi::_2) // iterators to error-pos, end
  759. << phoenix::val("\"")
  760. << std::endl
  761. );
  762. }
  763. qi::rule<Iterator, TStringConstant(), ascii::space_type> string;
  764. qi::rule<Iterator, TMacroUsage(), ascii::space_type> macroUsage;
  765. qi::rule<Iterator, TQMacroUsage(), ascii::space_type> qMacroUsage;
  766. qi::rule<Iterator, TMacroDef(), ascii::space_type> macroDef;
  767. qi::rule<Iterator, TVarExpNotMacro(), ascii::space_type> varExpNotMacro;
  768. qi::rule<Iterator, TVarExp(), ascii::space_type> varExp;
  769. qi::rule<Iterator, TIexp(), ascii::space_type> iexp;
  770. qi::rule<Iterator, TVarpExp(), ascii::space_type> varp;
  771. qi::rule<Iterator, TArithmeticOp(), ascii::space_type> arithmeticOp;
  772. qi::rule<Iterator, std::string(), ascii::space_type> comment;
  773. qi::rule<Iterator, std::string(), ascii::space_type> commentLine;
  774. qi::rule<Iterator, TCmdName(), ascii::space_type> cmdName;
  775. qi::rule<Iterator, Tidentifier(), ascii::space_type> identifier;
  776. qi::rule<Iterator, TComparison(), ascii::space_type> comparison;
  777. qi::rule<Iterator, Tcondition(), ascii::space_type> condition;
  778. qi::rule<Iterator, TVRLogic(), ascii::space_type> VRLogic;
  779. qi::rule<Iterator, TVRArithmetic(), ascii::space_type> VRarithmetic;
  780. qi::rule<Iterator, TSemiCompare(), ascii::space_type> semiCompare;
  781. qi::rule<Iterator, TCurriedString(), ascii::space_type> curStr;
  782. qi::rule<Iterator, TVarConcatString(), ascii::space_type> varConcatString;
  783. qi::rule<Iterator, TBodyOptionItem(), ascii::space_type> bodyOptionItem;
  784. qi::rule<Iterator, TNormalBodyOptionList(), ascii::space_type> exactBodyOptionList;
  785. qi::rule<Iterator, TNormalBodyOption(), ascii::space_type> normalBodyOption;
  786. qi::rule<Iterator, TBodyOption(), ascii::space_type> bodyOption;
  787. qi::rule<Iterator, Ttrigger(), ascii::space_type> trigger;
  788. qi::rule<Iterator, Tbody(), ascii::space_type> body;
  789. qi::rule<Iterator, Tinstruction(), ascii::space_type> instruction;
  790. qi::rule<Iterator, Treceiver(), ascii::space_type> receiver;
  791. qi::rule<Iterator, TPostTrigger(), ascii::space_type> postTrigger;
  792. qi::rule<Iterator, Tcommand(), ascii::space_type> command;
  793. qi::rule<Iterator, TERMline(), ascii::space_type> rline;
  794. qi::rule<Iterator, TSymbol(), ascii::space_type> vsym;
  795. qi::rule<Iterator, TVModifier(), ascii::space_type> vmod;
  796. qi::rule<Iterator, TVOption(), ascii::space_type> vopt;
  797. qi::rule<Iterator, TVExp(), ascii::space_type> vexp;
  798. qi::rule<Iterator, TLine(), ascii::space_type> vline;
  799. };
  800. };
  801. void ERMParser::parseLine( const std::string & line )
  802. {
  803. std::string::const_iterator beg = line.begin(),
  804. end = line.end();
  805. ERM::ERM_grammar<std::string::const_iterator> ERMgrammar;
  806. ERM::TLine AST;
  807. // bool r = qi::phrase_parse(beg, end, ERMgrammar, ascii::space, AST);
  808. // if(!r || beg != end)
  809. // {
  810. // tlog1 << "Parse error for line (" << parsedLine << ") : " << line << std::endl;
  811. // tlog1 << "\tCannot parse: " << std::string(beg, end) << std::endl;
  812. // }
  813. // else
  814. // {
  815. // //parsing succeeded
  816. // tlog2 << line << std::endl;
  817. // ERM::printAST(AST);
  818. // }
  819. }
  820. ERMParser::ELineType ERMParser::classifyLine( const std::string & line, bool inString ) const
  821. {
  822. ERMParser::ELineType ret;
  823. if(line[0] == '!')
  824. {
  825. if(countHatsBeforeSemicolon(line) % 2 == 1)
  826. ret = ERMParser::UNFINISHED;
  827. else
  828. ret = ERMParser::COMMAND_FULL;
  829. }
  830. else
  831. {
  832. if(inString)
  833. {
  834. if(countHatsBeforeSemicolon(line) % 2 == 1)
  835. ret = ERMParser::END_OF;
  836. else
  837. ret = ERMParser::UNFINISHED;
  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