ERMParser.h 5.4 KB

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