ERMParser.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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;
  81. TIexp rhs;
  82. char opcode;
  83. };
  84. struct TVRLogic
  85. {
  86. char opcode;
  87. TIexp var;
  88. };
  89. struct TVRArithmetic
  90. {
  91. char opcode;
  92. TIexp rhs;
  93. };
  94. struct TSemiCompare
  95. {
  96. std::string compSign;
  97. TIexp rhs;
  98. };
  99. struct TCurriedString
  100. {
  101. TIexp iexp;
  102. TStringConstant string;
  103. };
  104. struct TVarConcatString
  105. {
  106. TVarExp var;
  107. TStringConstant string;
  108. };
  109. typedef std::variant<TVarConcatString, TStringConstant, TCurriedString, TSemiCompare, TMacroDef, TIexp, TVarpExp> TBodyOptionItem;
  110. typedef std::vector<TBodyOptionItem> TNormalBodyOptionList;
  111. struct TNormalBodyOption
  112. {
  113. char optionCode;
  114. std::optional<TNormalBodyOptionList> params;
  115. };
  116. typedef std::variant<TVRLogic, TVRArithmetic, TNormalBodyOption> TBodyOption;
  117. // typedef std::variant<TIexp, TArithmeticOp > TIdentifierInternal;
  118. typedef std::vector< TIexp > Tidentifier;
  119. struct TComparison
  120. {
  121. std::string compSign;
  122. TIexp lhs;
  123. TIexp rhs;
  124. };
  125. struct Tcondition;
  126. typedef std::optional<boost::recursive_wrapper<Tcondition>> TconditionNode;
  127. struct Tcondition
  128. {
  129. typedef std::variant<
  130. TComparison,
  131. int>
  132. Tcond; //comparison or condition flag
  133. char ctype;
  134. Tcond cond;
  135. TconditionNode rhs;
  136. };
  137. struct TTriggerBase
  138. {
  139. bool pre; //if false it's !$ post-trigger, elsewise it's !# (pre)trigger
  140. TCmdName name;
  141. std::optional<Tidentifier> identifier;
  142. std::optional<Tcondition> condition;
  143. };
  144. struct Ttrigger : TTriggerBase
  145. {
  146. Ttrigger()
  147. {
  148. pre = true;
  149. }
  150. };
  151. struct TPostTrigger : TTriggerBase
  152. {
  153. TPostTrigger()
  154. {
  155. pre = false;
  156. }
  157. };
  158. //a dirty workaround for preprocessor magic that prevents the use types with comma in it in BOOST_FUSION_ADAPT_STRUCT
  159. //see http://comments.gmane.org/gmane.comp.lib.boost.user/62501 for some info
  160. //
  161. //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
  162. //not sure how serious it is...
  163. //typedef std::variant<char, TStringConstant, TMacroUsage, TMacroDef> bodyItem;
  164. typedef std::vector<TBodyOption> Tbody;
  165. struct Tinstruction
  166. {
  167. TCmdName name;
  168. std::optional<Tidentifier> identifier;
  169. std::optional<Tcondition> condition;
  170. Tbody body;
  171. };
  172. struct Treceiver
  173. {
  174. TCmdName name;
  175. std::optional<Tidentifier> identifier;
  176. std::optional<Tcondition> condition;
  177. std::optional<Tbody> body;
  178. };
  179. struct Tcommand
  180. {
  181. typedef std::variant<
  182. Ttrigger,
  183. Tinstruction,
  184. Treceiver,
  185. TPostTrigger
  186. >
  187. Tcmd;
  188. Tcmd cmd;
  189. //std::string comment;
  190. };
  191. //vector expression
  192. typedef std::variant<Tcommand, std::string, boost::spirit::unused_type> TERMline;
  193. typedef std::string TVModifier; //'`', ',', ',@', '#''
  194. struct TSymbol
  195. {
  196. std::vector<TVModifier> symModifier;
  197. std::string sym;
  198. };
  199. //for #'symbol expression
  200. enum EVOptions{VEXP, SYMBOL, CHAR, DOUBLE, INT, TCMD, STRINGC};
  201. struct TVExp;
  202. typedef std::variant<boost::recursive_wrapper<TVExp>, TSymbol, char, double, int, Tcommand, TStringConstant > TVOption; //options in v-expression
  203. //v-expression
  204. struct TVExp
  205. {
  206. std::vector<TVModifier> modifier;
  207. std::vector<TVOption> children;
  208. };
  209. //script line
  210. typedef std::variant<TVExp, TERMline> TLine;
  211. template <typename T> struct ERM_grammar;
  212. }
  213. struct LineInfo
  214. {
  215. ERM::TLine tl;
  216. int realLineNum;
  217. };
  218. class ERMParser
  219. {
  220. public:
  221. std::shared_ptr<ERM::ERM_grammar<std::string::const_iterator>> ERMgrammar;
  222. ERMParser();
  223. virtual ~ERMParser();
  224. std::vector<LineInfo> parseFile(CERMPreprocessor & preproc);
  225. private:
  226. void repairEncoding(char * str, int len) const; //removes nonstandard ascii characters from string
  227. void repairEncoding(std::string & str) const; //removes nonstandard ascii characters from string
  228. ERM::TLine parseLine(const std::string & line, int realLineNo);
  229. ERM::TLine parseLine(const std::string & line);
  230. };