element.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 _ELEMENT_H
  17. #define _ELEMENT_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 "errno.h"
  27. #include "util/tc_autoptr.h"
  28. using namespace std;
  29. void yyerror(char const *msg);
  30. /**
  31. * 语法元素基类
  32. */
  33. class GrammarBase : virtual public tars::TC_HandleBase
  34. {
  35. public:
  36. virtual ~GrammarBase(){};
  37. };
  38. typedef tars::TC_AutoPtr<GrammarBase> GrammarBasePtr;
  39. /**
  40. * 解析过程中的字符串
  41. */
  42. class StringGrammar : public GrammarBase
  43. {
  44. public:
  45. StringGrammar() { }
  46. string v;
  47. };
  48. typedef tars::TC_AutoPtr<StringGrammar> StringGrammarPtr;
  49. /**
  50. * 解析过程中的整形数
  51. */
  52. class IntergerGrammar : public GrammarBase
  53. {
  54. public:
  55. IntergerGrammar():v(0) { }
  56. long long v;
  57. };
  58. typedef tars::TC_AutoPtr<IntergerGrammar> IntergerGrammarPtr;
  59. /**
  60. * 解析过程中的浮点数
  61. */
  62. class FloatGrammar : public GrammarBase
  63. {
  64. public:
  65. FloatGrammar():v(0.0f) { }
  66. double v;
  67. };
  68. typedef tars::TC_AutoPtr<FloatGrammar> FloatGrammarPtr;
  69. /**
  70. * 解析过程中的bool类型
  71. */
  72. class BoolGrammar : public GrammarBase
  73. {
  74. public:
  75. BoolGrammar():v(false) { }
  76. bool v;
  77. };
  78. typedef tars::TC_AutoPtr<BoolGrammar> BoolGrammarPtr;
  79. /**
  80. * 解析过程中的常类型
  81. */
  82. class ConstGrammar : public GrammarBase
  83. {
  84. public:
  85. ConstGrammar():t(VALUE) { }
  86. enum
  87. {
  88. VALUE,
  89. STRING,
  90. BOOL,
  91. ENUM
  92. };
  93. int t;
  94. string v;
  95. };
  96. typedef tars::TC_AutoPtr<ConstGrammar> ConstGrammarPtr;
  97. ///////////////////////////////////////////////
  98. /**
  99. * 类型基类
  100. */
  101. class Type : virtual public GrammarBase
  102. {
  103. public:
  104. /**
  105. * 构造函数
  106. * @param Type
  107. */
  108. Type():_size(0),_array(false),_pointer(false){};
  109. //是否简单类型
  110. virtual bool isSimple() const { return false;}
  111. //是否数组类型
  112. virtual bool isArray() const { return _array;}
  113. virtual void setArray(int size) {_array = true;_size = size;}
  114. virtual int getSize() const {return _size;};
  115. //是否指针类型
  116. virtual bool isPointer() const { return _pointer;}
  117. virtual void setPointer(bool b) {_pointer = b;}
  118. public:
  119. int _size;
  120. bool _array;
  121. bool _pointer;
  122. };
  123. typedef tars::TC_AutoPtr<Type> TypePtr;
  124. /**
  125. * 内建类型
  126. */
  127. class Builtin : public Type
  128. {
  129. public:
  130. enum Kind
  131. {
  132. KindVoid,
  133. KindBool,
  134. KindByte,
  135. KindShort,
  136. KindInt,
  137. KindLong,
  138. KindFloat,
  139. KindDouble,
  140. KindString,
  141. KindVector,
  142. KindMap
  143. };
  144. /**
  145. * 构造函数
  146. * @param kind
  147. */
  148. Builtin(Kind kind,bool isUnsigned);
  149. /**
  150. * 类型
  151. *
  152. * @return Kind
  153. */
  154. Kind kind() const;
  155. /**
  156. * 是否是简单类型
  157. *
  158. * @return bool
  159. */
  160. bool isSimple() const;
  161. /**
  162. * 是否是unsined类型
  163. *
  164. * @return bool
  165. */
  166. bool isUnsigned() const {return _isUnsigned;}
  167. /**
  168. * 设置是unsined类型
  169. *
  170. * @return bool
  171. */
  172. void setUnsigned(bool isUnsigned = false) {_isUnsigned = isUnsigned;}
  173. /**
  174. * 缺省值
  175. *
  176. * @return string
  177. */
  178. string def() const;
  179. /**
  180. * 字符串标示
  181. */
  182. static const char* builtinTable[];
  183. protected:
  184. Kind _kind;
  185. bool _isUnsigned;
  186. };
  187. typedef tars::TC_AutoPtr<Builtin> BuiltinPtr;
  188. /**
  189. * Vector类型
  190. */
  191. class Vector : public Type
  192. {
  193. public:
  194. /**
  195. * 构造函数
  196. * @param ptr
  197. */
  198. Vector(const TypePtr& ptr);
  199. /**
  200. * 获取类型
  201. *
  202. * @return TypePtr&
  203. */
  204. TypePtr& getTypePtr() {return _ptr;}
  205. protected:
  206. TypePtr _ptr;
  207. };
  208. typedef tars::TC_AutoPtr<Vector> VectorPtr;
  209. /**
  210. * Map类型
  211. */
  212. class Map : public Type
  213. {
  214. public:
  215. /**
  216. * 构造函数
  217. * @param pleft
  218. * @param pright
  219. */
  220. Map(const TypePtr& pleft, const TypePtr& pright);
  221. /**
  222. * 获取左类型
  223. *
  224. * @return TypePtr&
  225. */
  226. TypePtr& getLeftTypePtr() {return _pleft;}
  227. /**
  228. * 获取右类型
  229. *
  230. * @return TypePtr&
  231. */
  232. TypePtr& getRightTypePtr() {return _pright;}
  233. protected:
  234. TypePtr _pleft;
  235. TypePtr _pright;
  236. };
  237. typedef tars::TC_AutoPtr<Map> MapPtr;
  238. /**
  239. * 变量定义
  240. */
  241. class TypeId : public GrammarBase
  242. {
  243. public:
  244. /**
  245. * 构造函数
  246. * @param ptr
  247. * @param id
  248. */
  249. TypeId(const TypePtr& ptr, const string& id);
  250. /**
  251. * 变量名称
  252. *
  253. * @return string
  254. */
  255. string getId() const { return _id; }
  256. /**
  257. * 变量类型
  258. *
  259. * @return Type*
  260. */
  261. TypePtr& getTypePtr() { return _ptr;}
  262. /**
  263. * 是否需要该参数
  264. *
  265. * @return bool
  266. */
  267. bool isRequire() const { return _bRequire; }
  268. /**
  269. * 设置必选字段
  270. * @param tag
  271. */
  272. void setRequire(int tag);
  273. /**
  274. * 设置可选字段
  275. * 只有基本类型才有缺省值
  276. */
  277. void setDefault(const string &def);
  278. /**
  279. *
  280. * 只有基本类型才有缺省值
  281. */
  282. void disableDefault();
  283. /**
  284. * 设置可选字段
  285. * @param tag
  286. */
  287. void setOptional(int tag);
  288. /**
  289. * tag标识
  290. */
  291. int getTag() const { return _tag; }
  292. /**
  293. * 缺省值
  294. *
  295. * @return string
  296. */
  297. string def() const { return _default; }
  298. /**
  299. * 是否有缺省值
  300. *
  301. * @return bool
  302. */
  303. bool hasDefault() const { return _bHasDefault; }
  304. protected:
  305. TypePtr _ptr;
  306. string _id;
  307. bool _bRequire;
  308. int _tag;
  309. bool _bHasDefault;
  310. string _default;
  311. public:
  312. int _size;
  313. bool _array;
  314. };
  315. typedef tars::TC_AutoPtr<TypeId> TypeIdPtr;
  316. ////////////////////////////////////////////////////
  317. //
  318. class Namespace;
  319. typedef tars::TC_AutoPtr<Namespace> NamespacePtr;
  320. /**
  321. * 容器基类, 所有可以包含其他元素的元素都从该基类继承
  322. */
  323. class Container : virtual public GrammarBase
  324. {
  325. public:
  326. /**
  327. * 构造函数
  328. * @param id
  329. */
  330. Container(const string &id) : _id(id)
  331. {
  332. }
  333. /**
  334. * 生成名字空间
  335. * @param id
  336. *
  337. * @return NamespacePtr
  338. */
  339. NamespacePtr createNamespace(const string &id);
  340. /**
  341. * 获取ID
  342. *
  343. * @return string
  344. */
  345. string getId() const { return _id;}
  346. /**
  347. * 获取所有的名字空间
  348. *
  349. * @return vector<NamespacePtr>&
  350. */
  351. vector<NamespacePtr> &getAllNamespacePtr() { return _ns; }
  352. protected:
  353. string _id;
  354. vector<NamespacePtr> _ns;
  355. };
  356. typedef tars::TC_AutoPtr<Container> ContainerPtr;
  357. ////////////////////////////////////////////////////
  358. //
  359. class Const : public GrammarBase
  360. {
  361. public:
  362. /**
  363. *
  364. * @param tPtr
  365. * @param cPtr
  366. */
  367. Const(TypeIdPtr &tPtr, ConstGrammarPtr &cPtr) : _tPtr(tPtr), _cPtr(cPtr)
  368. {
  369. }
  370. /**
  371. *
  372. *
  373. * @return TypePtr&
  374. */
  375. TypeIdPtr &getTypeIdPtr() { return _tPtr; }
  376. /**
  377. *
  378. *
  379. * @return ConstGrammarPtr
  380. */
  381. ConstGrammarPtr getConstGrammarPtr() { return _cPtr; }
  382. protected:
  383. TypeIdPtr _tPtr;
  384. ConstGrammarPtr _cPtr;
  385. };
  386. typedef tars::TC_AutoPtr<Const> ConstPtr;
  387. /////////////////////////////////////////////////////////
  388. //
  389. class Enum : virtual public Container, virtual public Type
  390. {
  391. public:
  392. Enum(const string &id, const string &sid) : Container(id), _sid(sid)
  393. {
  394. }
  395. /**
  396. * 添加成员变量
  397. * @param ptid
  398. */
  399. void addMember(const TypeIdPtr &ptr);
  400. /**
  401. * 获取所有的成员变量
  402. *
  403. * @return vector<TypeIdPtr>&
  404. */
  405. vector<TypeIdPtr>& getAllMemberPtr() {return _members;}
  406. /**
  407. * 获取结构的名称
  408. *
  409. * @return string
  410. */
  411. string getSid() const { return _sid; }
  412. /**
  413. * 是否是简单类型
  414. *
  415. * @return bool
  416. */
  417. virtual bool isSimple() const { return true; }
  418. protected:
  419. /**
  420. * 每个变量名称
  421. */
  422. vector<TypeIdPtr> _members;
  423. /**
  424. * 包含名字的空间的名称
  425. */
  426. string _sid;
  427. };
  428. typedef tars::TC_AutoPtr<Enum> EnumPtr;
  429. ////////////////////////////////////////////////////
  430. //
  431. /**
  432. * 结构
  433. */
  434. class Struct : virtual public Container, virtual public Type
  435. {
  436. public:
  437. /**
  438. * 构造函数
  439. * @param id
  440. */
  441. Struct(const string& id, const string &sid) : Container(id), _sid(sid)
  442. {
  443. }
  444. /**
  445. * 添加成员变量
  446. * @param ptid
  447. */
  448. void addTypeId(const TypeIdPtr &ptr);
  449. /**
  450. * 获取所有的成员变量
  451. *
  452. * @return vector<TypeIdPtr>&
  453. */
  454. vector<TypeIdPtr>& getAllMemberPtr() {return _members;}
  455. /**
  456. * 获取结构的名称
  457. *
  458. * @return string
  459. */
  460. string getSid() const { return _sid; }
  461. /**
  462. * 增加小于memeber
  463. * @param member
  464. */
  465. void addKey(const string &member);
  466. vector<string> getKey() { return _key; }
  467. protected:
  468. vector<TypeIdPtr> _members;
  469. vector<string> _key;
  470. string _sid;
  471. };
  472. typedef tars::TC_AutoPtr<Struct> StructPtr;
  473. ////////////////////////////////////////////////////
  474. //
  475. /**
  476. * 参数描述
  477. */
  478. class ParamDecl : public GrammarBase
  479. {
  480. public:
  481. /**
  482. * 构造
  483. * @param typeIdPtr
  484. * @param v
  485. */
  486. ParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k)
  487. : _typeIdPtr(typeIdPtr), _v(v), _k(k)
  488. {
  489. }
  490. /**
  491. * 变量声明
  492. *
  493. * @return TypeIdPtr&
  494. */
  495. TypeIdPtr& getTypeIdPtr() { return _typeIdPtr; }
  496. /**
  497. * 是否是输出参数
  498. *
  499. * @return bool
  500. */
  501. bool isOut() const { return _v; }
  502. /**
  503. * 是否是需要路由的字段
  504. *
  505. * @return bool
  506. */
  507. bool isRouteKey() const { return _k; }
  508. protected:
  509. TypeIdPtr _typeIdPtr;
  510. bool _v;
  511. bool _k;
  512. };
  513. typedef tars::TC_AutoPtr<ParamDecl> ParamDeclPtr;
  514. ///////////////////////////////////////////////////////
  515. //
  516. /**
  517. * 操作类
  518. */
  519. class Operation : public Container
  520. {
  521. public:
  522. /**
  523. * 构造函数
  524. * @param id
  525. * @param typePtr
  526. */
  527. Operation(const string &id, const TypePtr &typePtr) : Container(id), _itag(0)
  528. {
  529. _retPtr = new TypeId(typePtr, "_ret");
  530. _retPtr->setRequire(_itag);
  531. }
  532. /**
  533. * 生成一个参数
  534. * @param typeIdPtr
  535. * @param v
  536. * @param k
  537. *
  538. * @return ParamDeclPtr
  539. */
  540. ParamDeclPtr createParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k);
  541. /**
  542. * 获取返回值类型
  543. *
  544. * @return TypePtr&
  545. */
  546. TypeIdPtr &getReturnPtr() { return _retPtr; }
  547. /**
  548. * 获取所有参数
  549. *
  550. * @return vector<ParamDeclPtr>&
  551. */
  552. vector<ParamDeclPtr> &getAllParamDeclPtr() { return _ps; }
  553. protected:
  554. int _itag;
  555. TypeIdPtr _retPtr;
  556. vector<ParamDeclPtr> _ps;
  557. };
  558. typedef tars::TC_AutoPtr<Operation> OperationPtr;
  559. ///////////////////////////////////////////////////////
  560. //
  561. /**
  562. * 接口描述
  563. */
  564. class Interface : public Container
  565. {
  566. public:
  567. /**
  568. * 构造
  569. * @param id
  570. */
  571. Interface(const string &id) : Container(id)
  572. {
  573. }
  574. /**
  575. * 生成一个操作
  576. * @param id
  577. * @param typePtr
  578. *
  579. * @return OperationPtr
  580. */
  581. OperationPtr createOperation(const string &id, const TypePtr &typePtr);
  582. /**
  583. * 获取所有操作
  584. *
  585. * @return vector<OperationPtr>&
  586. */
  587. vector<OperationPtr> &getAllOperationPtr() { return _ops; }
  588. protected:
  589. vector<OperationPtr> _ops;
  590. };
  591. typedef tars::TC_AutoPtr<Interface> InterfacePtr;
  592. /////////////////////////////////////////////////////////
  593. //
  594. /**
  595. * 名字空间
  596. */
  597. class Namespace : public Container
  598. {
  599. public:
  600. /**
  601. * 构造函数
  602. * @param id
  603. */
  604. Namespace(const string &id) : Container(id)
  605. {
  606. }
  607. /**
  608. * 生成接口
  609. * @param id
  610. *
  611. * @return InterfacePtr
  612. */
  613. InterfacePtr createInterface(const string &id);
  614. /**
  615. * 生成结构
  616. * @param id
  617. *
  618. * @return StructPtr
  619. */
  620. StructPtr createStruct(const string& id);
  621. /**
  622. * 生成枚举类型
  623. * @param id
  624. *
  625. * @return EnumPtr
  626. */
  627. EnumPtr createEnum(const string &id);
  628. /**
  629. *
  630. * @param pPtr
  631. * @param cPtr
  632. *
  633. * @return ConstPtr
  634. */
  635. ConstPtr createConst(TypeIdPtr &pPtr, ConstGrammarPtr &cPtr);
  636. /**
  637. * 是否有接口
  638. *
  639. * @return bool
  640. */
  641. bool hasInterface() const { return !_is.empty(); }
  642. /**
  643. * 获取所有的接口
  644. *
  645. * @return vector<InterfacePtr>&
  646. */
  647. vector<InterfacePtr> &getAllInterfacePtr() { return _is; }
  648. /**
  649. * 获取所有的结构
  650. *
  651. * @return vector<StructPtr>&
  652. */
  653. vector<StructPtr> &getAllStructPtr() { return _ss; }
  654. /**
  655. * 生成枚举类型
  656. *
  657. * @return vector<EnumPtr>&
  658. */
  659. vector<EnumPtr> &getAllEnumPtr() { return _es; }
  660. /**
  661. * 常量类型
  662. *
  663. * @return vector<ConstPtr>&
  664. */
  665. vector<ConstPtr> &getAllConstPtr() { return _cs; }
  666. protected:
  667. vector<InterfacePtr> _is;
  668. vector<StructPtr> _ss;
  669. vector<EnumPtr> _es;
  670. vector<ConstPtr> _cs;
  671. };
  672. /////////////////////////////////////////////////////////////////
  673. /**
  674. * 上下文
  675. */
  676. class Context : public tars::TC_HandleBase
  677. {
  678. public:
  679. /**
  680. * 构造函数
  681. */
  682. Context(const string &file) : _currline(1), _filename(file)
  683. {
  684. }
  685. /**
  686. * 下一行
  687. */
  688. void nextLine() { _currline++; }
  689. /**
  690. * 目前的行
  691. *
  692. * @return size_t
  693. */
  694. size_t getCurrLine() { return _currline; }
  695. /**
  696. * 当前文件名
  697. *
  698. * @return string
  699. */
  700. string getFileName() { return _filename; }
  701. /**
  702. * 添加include的文件
  703. * @param incl
  704. */
  705. void addInclude(const string &incl);
  706. /**
  707. * 添加属于这个文件的名字空间
  708. * @param c
  709. */
  710. void addNamespacePtr(const NamespacePtr &c)
  711. {
  712. _namespaces.push_back(c);
  713. }
  714. /**
  715. * 获取includes的文件
  716. *
  717. * @return vector<string>
  718. */
  719. vector<string> getIncludes() { return _includes; }
  720. /**
  721. * 获取名字空间
  722. *
  723. * @return vector<NamespacePtr>
  724. */
  725. vector<NamespacePtr> getNamespaces() { return _namespaces; }
  726. protected:
  727. size_t _currline;
  728. string _filename;
  729. vector<string> _includes;
  730. vector<NamespacePtr> _namespaces;
  731. };
  732. typedef tars::TC_AutoPtr<Context> ContextPtr;
  733. #endif