ERMParser.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #pragma once
  2. #include "../../global.h"
  3. #include <fstream>
  4. #include <boost/variant.hpp>
  5. #include <boost/optional.hpp>
  6. #include <boost/spirit/home/support/unused.hpp>
  7. /*
  8. * ERMParser.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CERMPreprocessor
  17. {
  18. std::string fname;
  19. std::ifstream file;
  20. int lineNo;
  21. enum {INVALID, ERM, VERM} version;
  22. void getline(std::string &ret);
  23. public:
  24. CERMPreprocessor(const std::string &Fname);
  25. std::string retreiveCommandLine();
  26. int getCurLineNo() const
  27. {
  28. return lineNo;
  29. }
  30. };
  31. //various classes that represent ERM/VERM AST
  32. namespace ERM
  33. {
  34. struct TStringConstant
  35. {
  36. std::string str;
  37. };
  38. struct TMacroUsage
  39. {
  40. std::string macro;
  41. };
  42. // //macro with '?', for write only
  43. // struct TQMacroUsage
  44. // {
  45. // std::string qmacro;
  46. // };
  47. //definition of a macro
  48. struct TMacroDef
  49. {
  50. std::string macro;
  51. };
  52. typedef std::string TCmdName;
  53. struct TVarExpNotMacro
  54. {
  55. typedef boost::optional<int> Tval;
  56. boost::optional<char> questionMark;
  57. std::string varsym;
  58. Tval val;
  59. };
  60. typedef boost::variant<TVarExpNotMacro, TMacroUsage> TVarExp;
  61. //write-only variable expression
  62. struct TVarpExp
  63. {
  64. TVarExp var;
  65. };
  66. //i-expression (identifier expression) - an integral constant, variable symbol or array symbol
  67. typedef boost::variant<TVarExp, int> TIexp;
  68. struct TArithmeticOp
  69. {
  70. TIexp lhs, rhs;
  71. char opcode;
  72. };
  73. struct TVRLogic
  74. {
  75. char opcode;
  76. TIexp var;
  77. };
  78. struct TVRArithmetic
  79. {
  80. char opcode;
  81. TIexp rhs;
  82. };
  83. struct TSemiCompare
  84. {
  85. std::string compSign;
  86. TIexp rhs;
  87. };
  88. struct TCurriedString
  89. {
  90. TIexp iexp;
  91. TStringConstant string;
  92. };
  93. struct TVarConcatString
  94. {
  95. TVarExp var;
  96. TStringConstant string;
  97. };
  98. typedef boost::variant<TVarConcatString, TStringConstant, TCurriedString, TSemiCompare, TMacroDef, TIexp, TVarpExp, boost::spirit::unused_type> TBodyOptionItem;
  99. typedef std::vector<TBodyOptionItem> TNormalBodyOptionList;
  100. struct TNormalBodyOption
  101. {
  102. char optionCode;
  103. TNormalBodyOptionList params;
  104. };
  105. typedef boost::variant<TVRLogic, TVRArithmetic, TNormalBodyOption> TBodyOption;
  106. typedef boost::variant<TIexp, TArithmeticOp > TIdentifierInternal;
  107. typedef std::vector< TIdentifierInternal > Tidentifier;
  108. struct TComparison
  109. {
  110. std::string compSign;
  111. TIexp lhs, rhs;
  112. };
  113. struct Tcondition;
  114. typedef
  115. boost::optional<
  116. boost::recursive_wrapper<Tcondition>
  117. >
  118. TconditionNode;
  119. struct Tcondition
  120. {
  121. typedef boost::variant<
  122. TComparison,
  123. int>
  124. Tcond; //comparison or condition flag
  125. char ctype;
  126. Tcond cond;
  127. TconditionNode rhs;
  128. };
  129. struct TTriggerBase
  130. {
  131. bool pre; //if false it's !$ post-trigger, elsewise it's !# (pre)trigger
  132. TCmdName name;
  133. boost::optional<Tidentifier> identifier;
  134. boost::optional<Tcondition> condition;
  135. };
  136. struct Ttrigger : TTriggerBase
  137. {
  138. Ttrigger()
  139. {
  140. pre = true;
  141. }
  142. };
  143. struct TPostTrigger : TTriggerBase
  144. {
  145. TPostTrigger()
  146. {
  147. pre = false;
  148. }
  149. };
  150. //a dirty workaround for preprocessor magic that prevents the use types with comma in it in BOOST_FUSION_ADAPT_STRUCT
  151. //see http://comments.gmane.org/gmane.comp.lib.boost.user/62501 for some info
  152. //
  153. //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
  154. //not sure how serious it is...
  155. //typedef boost::variant<char, TStringConstant, TMacroUsage, TMacroDef> bodyItem;
  156. typedef std::vector<TBodyOption> Tbody;
  157. struct Tinstruction
  158. {
  159. TCmdName name;
  160. boost::optional<Tidentifier> identifier;
  161. boost::optional<Tcondition> condition;
  162. Tbody body;
  163. };
  164. struct Treceiver
  165. {
  166. TCmdName name;
  167. boost::optional<Tidentifier> identifier;
  168. boost::optional<Tcondition> condition;
  169. boost::optional<Tbody> body;
  170. };
  171. struct Tcommand
  172. {
  173. typedef boost::variant<
  174. Ttrigger,
  175. Tinstruction,
  176. Treceiver,
  177. TPostTrigger
  178. >
  179. Tcmd;
  180. Tcmd cmd;
  181. std::string comment;
  182. };
  183. //vector expression
  184. typedef boost::variant<Tcommand, std::string, boost::spirit::unused_type> TERMline;
  185. typedef std::string TVModifier; //'`', ',', ',@', '#''
  186. struct TSymbol
  187. {
  188. std::vector<TVModifier> symModifier;
  189. std::string sym;
  190. };
  191. //for #'symbol expression
  192. enum EVOtions{VEXP, SYMBOL, CHAR, DOUBLE, INT, TCMD, STRINGC};
  193. struct TVExp;
  194. typedef boost::variant<boost::recursive_wrapper<TVExp>, TSymbol, char, double, int, Tcommand, TStringConstant > TVOption; //options in v-expression
  195. //v-expression
  196. struct TVExp
  197. {
  198. std::vector<TVModifier> modifier;
  199. std::vector<TVOption> children;
  200. };
  201. //script line
  202. typedef boost::variant<TVExp, TERMline> TLine;
  203. }
  204. struct LineInfo
  205. {
  206. ERM::TLine tl;
  207. int realLineNum;
  208. };
  209. class ERMParser
  210. {
  211. private:
  212. std::string srcFile;
  213. void repairEncoding(char * str, int len) const; //removes nonstandard ascii characters from string
  214. void repairEncoding(std::string & str) const; //removes nonstandard ascii characters from string
  215. enum ELineType{COMMAND_FULL, COMMENT, UNFINISHED, END_OF};
  216. int countHatsBeforeSemicolon(const std::string & line) const;
  217. ELineType classifyLine(const std::string & line, bool inString) const;
  218. ERM::TLine parseLine(const std::string & line, int realLineNo);
  219. public:
  220. ERMParser(std::string file);
  221. std::vector<LineInfo> parseFile();
  222. };