ERMParser.h 5.6 KB

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