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. };
  34. //various classes that represent ERM/VERM AST
  35. namespace ERM
  36. {
  37. using ValType = int; //todo: set to int64_t
  38. using IType = int; //todo: set to int32_t
  39. struct TStringConstant
  40. {
  41. std::string str;
  42. };
  43. struct TMacroUsage
  44. {
  45. std::string macro;
  46. };
  47. // //macro with '?', for write only
  48. // struct TQMacroUsage
  49. // {
  50. // std::string qmacro;
  51. // };
  52. //definition of a macro
  53. struct TMacroDef
  54. {
  55. std::string macro;
  56. };
  57. typedef std::string TCmdName;
  58. struct TVarExpNotMacro
  59. {
  60. typedef boost::optional<int> Tval;
  61. boost::optional<char> questionMark;
  62. std::string varsym;
  63. Tval val;
  64. };
  65. typedef boost::variant<TVarExpNotMacro, TMacroUsage> TVarExp;
  66. //write-only variable expression
  67. struct TVarpExp
  68. {
  69. TVarExp var;
  70. };
  71. //i-expression (identifier expression) - an integral constant, variable symbol or array symbol
  72. typedef boost::variant<TVarExp, int> TIexp;
  73. struct TArithmeticOp
  74. {
  75. TIexp lhs, rhs;
  76. char opcode;
  77. };
  78. struct TVRLogic
  79. {
  80. char opcode;
  81. TIexp var;
  82. };
  83. struct TVRArithmetic
  84. {
  85. char opcode;
  86. TIexp rhs;
  87. };
  88. struct TSemiCompare
  89. {
  90. std::string compSign;
  91. TIexp rhs;
  92. };
  93. struct TCurriedString
  94. {
  95. TIexp iexp;
  96. TStringConstant string;
  97. };
  98. struct TVarConcatString
  99. {
  100. TVarExp var;
  101. TStringConstant string;
  102. };
  103. typedef boost::variant<TVarConcatString, TStringConstant, TCurriedString, TSemiCompare, TMacroDef, TIexp, TVarpExp> TBodyOptionItem;
  104. typedef std::vector<TBodyOptionItem> TNormalBodyOptionList;
  105. struct TNormalBodyOption
  106. {
  107. char optionCode;
  108. boost::optional<TNormalBodyOptionList> params;
  109. };
  110. typedef boost::variant<TVRLogic, TVRArithmetic, TNormalBodyOption> TBodyOption;
  111. // typedef boost::variant<TIexp, TArithmeticOp > TIdentifierInternal;
  112. typedef std::vector< TIexp > Tidentifier;
  113. struct TComparison
  114. {
  115. std::string compSign;
  116. TIexp lhs, rhs;
  117. };
  118. struct Tcondition;
  119. typedef
  120. boost::optional<
  121. boost::recursive_wrapper<Tcondition>
  122. >
  123. TconditionNode;
  124. struct Tcondition
  125. {
  126. typedef boost::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. boost::optional<Tidentifier> identifier;
  139. boost::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 boost::variant<char, TStringConstant, TMacroUsage, TMacroDef> bodyItem;
  161. typedef std::vector<TBodyOption> Tbody;
  162. struct Tinstruction
  163. {
  164. TCmdName name;
  165. boost::optional<Tidentifier> identifier;
  166. boost::optional<Tcondition> condition;
  167. Tbody body;
  168. };
  169. struct Treceiver
  170. {
  171. TCmdName name;
  172. boost::optional<Tidentifier> identifier;
  173. boost::optional<Tcondition> condition;
  174. boost::optional<Tbody> body;
  175. };
  176. struct Tcommand
  177. {
  178. typedef boost::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 boost::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 boost::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 boost::variant<TVExp, TERMline> TLine;
  208. template <typename T> class 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. };