parse.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TARS_PARSE_H_
  17. #define __TARS_PARSE_H_
  18. #include <iostream>
  19. #include <string>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <iterator>
  23. #include <map>
  24. #include <stack>
  25. #include <sstream>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include "element.h"
  29. #include "util/tc_common.h"
  30. #include "util/tc_autoptr.h"
  31. using namespace std;
  32. /**
  33. * Tars文件解析类
  34. *
  35. */
  36. class TarsParse : public tars::TC_HandleBase
  37. {
  38. public:
  39. /**
  40. * 构造函数
  41. */
  42. TarsParse();
  43. /**
  44. * 是否支持tars开头的标识
  45. * @param bWithTars
  46. */
  47. void setTars(bool bWithTars);
  48. /**
  49. * 头文件路径
  50. * @param sHeader
  51. */
  52. void setHeader(const string &sHeader);
  53. /**
  54. * 是否支持优先使用当前tars文件
  55. * @param bWithTars
  56. */
  57. void setCurrentPriority(bool bFlag);
  58. /**
  59. * 获取头文件路径
  60. */
  61. string getHeader();
  62. /**
  63. * 解析某一个文件
  64. * @param sFileName
  65. */
  66. void parse(const string &sFileName);
  67. /**
  68. * 错误提示
  69. * @param msg
  70. */
  71. void error(const string &msg);
  72. /**
  73. * 检查关键字
  74. * @param s
  75. *
  76. * @return int
  77. */
  78. int checkKeyword(const string &s);
  79. /**
  80. * 下一行
  81. */
  82. void nextLine();
  83. /**
  84. * 目前解析的文件名称
  85. *
  86. * @return string
  87. */
  88. string getCurrFileName();
  89. /**
  90. * tab
  91. *
  92. * @return string
  93. */
  94. string getTab();
  95. /**
  96. * 增加tab数
  97. */
  98. void incTab() { _itab++; }
  99. /**
  100. * 减少tab数
  101. */
  102. void delTab() { _itab--; }
  103. /**
  104. * 解析文件
  105. * @param file
  106. */
  107. void pushFile(const string &file);
  108. /**
  109. * 弹出解析文件
  110. */
  111. ContextPtr popFile();
  112. /**
  113. * 获取所有的上下文
  114. *
  115. * @return std::vector<ContextPtr>
  116. */
  117. std::vector<ContextPtr> getContexts() { return _vcontexts; }
  118. /**
  119. * 获取目前的容器
  120. *
  121. * @return ContainerPtr
  122. */
  123. ContainerPtr currentContainer();
  124. /**
  125. * push容器
  126. * @param c
  127. */
  128. void pushContainer(const ContainerPtr &c);
  129. /**
  130. * 目前的上下文
  131. *
  132. * @return ContextPtr
  133. */
  134. ContextPtr currentContextPtr();
  135. /**
  136. * 弹出容器
  137. *
  138. * @return ContainerPtr
  139. */
  140. ContainerPtr popContainer();
  141. /**
  142. * 生成Builtin元素
  143. * @param kind
  144. *
  145. * @return BuiltinPtr
  146. */
  147. BuiltinPtr createBuiltin(Builtin::Kind kind,bool isUnsigned = false);
  148. /**
  149. * 生成Vector元素
  150. * @param ptr
  151. *
  152. * @return VectorPtr
  153. */
  154. VectorPtr createVector(const TypePtr &ptr);
  155. /**
  156. * 生成Map元素
  157. * @param pleft
  158. * @param pright
  159. *
  160. * @return MapPtr
  161. */
  162. MapPtr createMap(const TypePtr &pleft, const TypePtr &pright);
  163. /**
  164. * 添加结构元素
  165. * @param sPtr
  166. */
  167. void addStructPtr(const StructPtr &sPtr);
  168. /**
  169. * 查找结构
  170. * @param id
  171. *
  172. * @return StructPtr
  173. */
  174. StructPtr findStruct(const string &sid);
  175. /**
  176. * 添加枚举元素
  177. * @param ePtr
  178. */
  179. void addEnumPtr(const EnumPtr &ePtr);
  180. /**
  181. * 查找结构
  182. * @param id
  183. *
  184. * @return EnumPtr
  185. */
  186. EnumPtr findEnum(const string &sid);
  187. /**
  188. * 检查冲突
  189. * @param id
  190. */
  191. void checkConflict(const string &sid);
  192. /**
  193. * 查找自定义类型
  194. * @param sid
  195. *
  196. * @return TypePtr
  197. */
  198. TypePtr findUserType(const string &sid);
  199. /**
  200. * 查找名字空间
  201. * @param id
  202. *
  203. * @return NamespacePtr
  204. */
  205. NamespacePtr findNamespace(const string &id);
  206. /**
  207. * 目前的名字空间
  208. *
  209. * @return NamespacePtr
  210. */
  211. NamespacePtr currentNamespace();
  212. /**
  213. * 检查tag的合法性
  214. * @param i
  215. */
  216. void checkTag(int i);
  217. /**
  218. * 检查szie的合法性
  219. * @param i
  220. */
  221. void checkSize(int i);
  222. /**
  223. * 检查array的合法性
  224. * @param i
  225. */
  226. void checkArrayVaid(TypePtr &tPtr,int size);
  227. /**
  228. * 检查pointer的合法性
  229. * @param i
  230. */
  231. void checkPointerVaid(TypePtr &tPtr);
  232. /**
  233. * 检查常量类型和值是否一致
  234. * @param c
  235. * @param b
  236. */
  237. void checkConstValue(TypeIdPtr &tPtr, int b);
  238. /**
  239. * 获取文件名
  240. * @param s
  241. *
  242. * @return string
  243. */
  244. bool getFilePath(const string &s, string &file);
  245. void setKeyStruct(const StructPtr& key)
  246. {
  247. _key = key;
  248. }
  249. StructPtr getKeyStruct()
  250. {
  251. return _key;
  252. }
  253. /**
  254. * 打印文件开头注释
  255. */
  256. string printHeaderRemark();
  257. /**
  258. * 判断当前的标识符是否是枚举变量
  259. */
  260. bool checkEnum(const string &idName);
  261. /**
  262. * 设置include的tars文件全部从当前文件搜寻
  263. */
  264. void setUseCurrentPath(bool bEnable) { _bUseCurrentPath = bEnable; }
  265. /**
  266. * 查找tars文件时,设置include路径
  267. */
  268. void addIncludePath(const string &include)
  269. {
  270. vector<string> v = tars::TC_Common::sepstr<string>(include, "; ,", false);
  271. _vIncludePath.insert(_vIncludePath.end(), v.begin(), v.end());
  272. }
  273. /**
  274. *
  275. * @param fileName
  276. * @return
  277. */
  278. string getFileName(const string &fileName);
  279. /**
  280. * replace ext
  281. * @param fileName
  282. * @param ext
  283. * @return
  284. */
  285. string replaceFileName(const string &fileName, const string &ext);
  286. /**
  287. * get absolute filename
  288. * @param baseDir
  289. * @param fileName
  290. * @return
  291. */
  292. string getAbsoluteFileName(const string &baseDir, const string &fileName);
  293. protected:
  294. /**
  295. * 添加名字空间
  296. * @param nPtr
  297. */
  298. void addNamespacePtr(const NamespacePtr &nPtr);
  299. /**
  300. * 初始化
  301. */
  302. void initScanner();
  303. /**
  304. * 清除
  305. */
  306. void clear();
  307. protected:
  308. bool _bWithTars;
  309. std::map<std::string, int> _keywordMap;
  310. int _itab;
  311. StructPtr _key;
  312. std::stack<ContextPtr> _contexts;
  313. std::stack<ContainerPtr> _contains;
  314. std::vector<ContextPtr> _vcontexts;
  315. std::vector<StructPtr> _structs;
  316. std::vector<EnumPtr> _enums;
  317. std::vector<NamespacePtr> _namespaces;
  318. string _sHeader;
  319. bool _bUseCurrentPath;
  320. bool _bUseCurrentPathFirst;
  321. std::vector<string> _vIncludePath;
  322. };
  323. extern int yyparse();
  324. extern int yylex();
  325. extern FILE *yyin, *yyout;
  326. typedef tars::TC_AutoPtr<TarsParse> TarsParsePtr;
  327. extern TarsParsePtr g_parse;
  328. #endif