ERMParser.h 6.0 KB

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