ERMParser.h 5.4 KB

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