ERMParser.h 5.5 KB

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