ERMParser.h 5.6 KB

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