ERMParser.h 5.4 KB

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