ERMParser.h 5.6 KB

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